Skip to content

Commit

Permalink
kubebuilder create api
Browse files Browse the repository at this point in the history
Signed-off-by: g0xu <[email protected]>
  • Loading branch information
kechigon committed Aug 21, 2024
1 parent 0c55b86 commit 86ad971
Show file tree
Hide file tree
Showing 16 changed files with 489 additions and 3 deletions.
14 changes: 12 additions & 2 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: pona.cybozu.com
domain: cybozu.com
layout:
- go.kubebuilder.io/v4
projectName: tmp
projectName: pona
repo: github.com/cybozu-go/pona
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: cybozu.com
group: pona
kind: Egress
path: github.com/cybozu-go/pona/api/v1beta1
version: v1beta1
version: "3"
48 changes: 48 additions & 0 deletions api/v1beta1/egress_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package v1beta1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// EgressSpec defines the desired state of Egress
type EgressSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Egress. Edit egress_types.go to remove/update
Foo string `json:"foo,omitempty"`
}

// EgressStatus defines the observed state of Egress
type EgressStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// Egress is the Schema for the egresses API
type Egress struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec EgressSpec `json:"spec,omitempty"`
Status EgressStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// EgressList contains a list of Egress
type EgressList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Egress `json:"items"`
}

func init() {
SchemeBuilder.Register(&Egress{}, &EgressList{})
}
20 changes: 20 additions & 0 deletions api/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Package v1beta1 contains API Schema definitions for the pona v1beta1 API group
// +kubebuilder:object:generate=true
// +groupName=pona.cybozu.com
package v1beta1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "pona.cybozu.com", Version: "v1beta1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
98 changes: 98 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions cmd/egress-controller/main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

ponav1beta1 "github.com/cybozu-go/pona/api/v1beta1"
"github.com/cybozu-go/pona/internal/controller"
// +kubebuilder:scaffold:imports
)

Expand All @@ -29,6 +32,7 @@ var (
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(ponav1beta1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

Expand Down Expand Up @@ -124,6 +128,13 @@ func main() {
os.Exit(1)
}

if err = (&controller.EgressReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Egress")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This kustomization.yaml is not intended to be run by itself,
# since it depends on service name and namespace that are out of this kustomize package.
# It should be run by config/default
resources:
- bases/pona.cybozu.com_egresses.yaml
# +kubebuilder:scaffold:crdkustomizeresource

patches:
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
# patches here are for enabling the conversion webhook for each CRD
# +kubebuilder:scaffold:crdkustomizewebhookpatch

# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
# patches here are for enabling the CA injection for each CRD
#- path: patches/cainjection_in_egresses.yaml
# +kubebuilder:scaffold:crdkustomizecainjectionpatch

# [WEBHOOK] To enable webhook, uncomment the following section
# the following config is for teaching kustomize how to do kustomization for CRDs.

#configurations:
#- kustomizeconfig.yaml
19 changes: 19 additions & 0 deletions config/crd/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file is for teaching kustomize how to substitute name and namespace reference in CRD
nameReference:
- kind: Service
version: v1
fieldSpecs:
- kind: CustomResourceDefinition
version: v1
group: apiextensions.k8s.io
path: spec/conversion/webhook/clientConfig/service/name

namespace:
- kind: CustomResourceDefinition
version: v1
group: apiextensions.k8s.io
path: spec/conversion/webhook/clientConfig/service/namespace
create: false

varReference:
- path: metadata/annotations
2 changes: 1 addition & 1 deletion config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namePrefix: tmp-
# someName: someValue

resources:
#- ../crd
- ../crd
- ../rbac
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
Expand Down
27 changes: 27 additions & 0 deletions config/rbac/egress_editor_role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# permissions for end users to edit egresses.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: pona
app.kubernetes.io/managed-by: kustomize
name: egress-editor-role
rules:
- apiGroups:
- pona.cybozu.com
resources:
- egresses
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- pona.cybozu.com
resources:
- egresses/status
verbs:
- get
23 changes: 23 additions & 0 deletions config/rbac/egress_viewer_role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# permissions for end users to view egresses.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: pona
app.kubernetes.io/managed-by: kustomize
name: egress-viewer-role
rules:
- apiGroups:
- pona.cybozu.com
resources:
- egresses
verbs:
- get
- list
- watch
- apiGroups:
- pona.cybozu.com
resources:
- egresses/status
verbs:
- get
7 changes: 7 additions & 0 deletions config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ resources:
- metrics_auth_role.yaml
- metrics_auth_role_binding.yaml
- metrics_reader_role.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
# if you do not want those helpers be installed with your Project.
- egress_editor_role.yaml
- egress_viewer_role.yaml

4 changes: 4 additions & 0 deletions config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Append samples of your project ##
resources:
- pona_v1beta1_egress.yaml
# +kubebuilder:scaffold:manifestskustomizesamples
9 changes: 9 additions & 0 deletions config/samples/pona_v1beta1_egress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: pona.cybozu.com/v1beta1
kind: Egress
metadata:
labels:
app.kubernetes.io/name: pona
app.kubernetes.io/managed-by: kustomize
name: egress-sample
spec:
# TODO(user): Add fields here
46 changes: 46 additions & 0 deletions internal/controller/egress_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package controller

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

ponav1beta1 "github.com/cybozu-go/pona/api/v1beta1"
)

// EgressReconciler reconciles a Egress object
type EgressReconciler struct {
client.Client
Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups=pona.cybozu.com,resources=egresses,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=pona.cybozu.com,resources=egresses/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=pona.cybozu.com,resources=egresses/finalizers,verbs=update

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Egress object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *EgressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)

// TODO(user): your logic here

return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *EgressReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&ponav1beta1.Egress{}).
Complete(r)
}
Loading

0 comments on commit 86ad971

Please sign in to comment.