What’s in a basic project?

When scaffolding out a new project, Kubebuilder provides us with a fewbasic pieces of boilerplate.

Build Infrastructure

First up, basic infrastructure for building your project:

go.mod: A new Go module matching our project, with basic dependencies
  1. module tutorial.kubebuilder.io/project
  2. go 1.12
  3. require (
  4. github.com/go-logr/logr v0.1.0
  5. github.com/gogo/protobuf v1.2.1 // indirect
  6. github.com/json-iterator/go v1.1.6 // indirect
  7. github.com/modern-go/reflect2 v1.0.1 // indirect
  8. github.com/onsi/ginkgo v1.8.0 // indirect
  9. github.com/onsi/gomega v1.5.0 // indirect
  10. github.com/robfig/cron v1.1.0
  11. github.com/spf13/pflag v1.0.3 // indirect
  12. golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 // indirect
  13. golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872 // indirect
  14. golang.org/x/text v0.3.2 // indirect
  15. gopkg.in/yaml.v2 v2.2.2 // indirect
  16. k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
  17. k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
  18. k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
  19. sigs.k8s.io/controller-runtime v0.2.0-beta.4
  20. )
Makefile: Make targets for building and deploying your controller
  1. # Image URL to use all building/pushing image targets
  2. IMG ?= controller:latest
  3. # Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
  4. CRD_OPTIONS ?= "crd:trivialVersions=true"
  5. all: manager
  6. # Run tests
  7. test: generate fmt vet manifests
  8. go test ./api/… ./controllers/… -coverprofile cover.out
  9. # Build manager binary
  10. manager: generate fmt vet
  11. go build -o bin/manager main.go
  12. # Run against the configured Kubernetes cluster in ~/.kube/config
  13. run: generate fmt vet
  14. go run ./main.go
  15. # Install CRDs into a cluster
  16. install: manifests
  17. kubectl apply -f config/crd/bases
  18. # Deploy controller in the configured Kubernetes cluster in ~/.kube/config
  19. deploy: manifests
  20. kubectl apply -f config/crd/bases
  21. kustomize build config/default | kubectl apply -f -
  22. # Generate manifests e.g. CRD, RBAC etc.
  23. manifests: controller-gen
  24. $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./api/…;./controllers/…" output:crd:artifacts:config=config/crd/bases
  25. # Run go fmt against code
  26. fmt:
  27. go fmt ./…
  28. # Run go vet against code
  29. vet:
  30. go vet ./…
  31. # Generate code
  32. generate: controller-gen
  33. $(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./api/…
  34. # Build the docker image
  35. docker-build: test
  36. docker build . -t ${IMG}
  37. @echo "updating kustomize image patch file for manager resource"
  38. sed -i'' -e 's@image: .*@image: '"${IMG}"'@' ./config/default/manager_image_patch.yaml
  39. # Push the docker image
  40. docker-push:
  41. docker push ${IMG}
  42. # find or download controller-gen
  43. # download controller-gen if necessary
  44. controller-gen:
  45. ifeq (, $(shell which controller-gen))
  46. go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.0-beta.3
  47. CONTROLLER_GEN=$(shell go env GOPATH)/bin/controller-gen
  48. else
  49. CONTROLLER_GEN=$(shell which controller-gen)
  50. endif
PROJECT: Kubebuilder metadata for scaffolding new components
  1. version: "2"
  2. domain: tutorial.kubebuilder.io
  3. repo: tutorial.kubebuilder.io/project

Launch Configuration

We also get launch configurations under theconfig/directory. Right now, it just containsKustomize YAML definitions required tolaunch our controller on a cluster, but once we get started writing ourcontroller, it’ll also hold our CustomResourceDefinitions, RBACconfiguration, and WebhookConfigurations.

config/default contains a Kustomize base for launchingthe controller in a standard configuration.

Each other directory contains a different piece of configuration,refactored out into its own base:

  • config/manager: launch your controllers as pods in thecluster

  • config/rbac: permissions required to run yourcontrollers under their own service account

The Entrypoint

Last, but certainly not least, Kubebuilder scaffolds out the basicentrypoint of our project: main.go. Let’s take a look at that next…