Skip to content

Instantly share code, notes, and snippets.

@yokawasa
Last active February 10, 2022 20:55
Show Gist options
  • Save yokawasa/98f80e827b72cad0b78e7e768f0f53c0 to your computer and use it in GitHub Desktop.
Save yokawasa/98f80e827b72cad0b78e7e768f0f53c0 to your computer and use it in GitHub Desktop.
Kubebuilder Tips

Kubebuilder Tips

Bootstrapping

Kuberbuilder init & create api

# kubebuilder init
kubebuilder init --domain my.domain.com --repo github.com/my-org/my-operator

# define resources
kubebuilder create api --group my-operator --version v1alpha1 --kind MyCustomResource

Create Resource [y/n]
y
Create Controller [y/n]
y

# build the operator
make

CRD & RBAC

Edit types.go to define API objects

# Generate CRD, RBAC, etc by controller-gen (Or it's ok to run make simply)
make manifests

# Run "make" to regenerate code after modifying types.go
make

TIPS

  • +kubebuilder:validation:Required
    • controller-gen analyzes the maker and auto generate CRD, RBAC, etc.
  • +optional
    • Manifest for CRD is auto generated in make manifest or make

Developing controller

  1. Implement Reconcile in controller.go

  2. Edit main func in main.go

  3. Run the operator

# Regist the CRDs to Kubernetes cluster 
make install

# Run Go process in local environment
make run
  1. Install Instances of Custom Resources
kubectl apply -f config/samples/
  1. Run it on the cluster
# Build and push your image to the location specified by IMG:
make docker-build docker-push IMG=<some-registry>/<project-name>:tag

# Deploy the controller to the cluster with image specified by IMG:
make deploy IMG=<some-registry>/<project-name>:tag

# undeploy
make undeploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment