|
| 1 | +{% panel style="danger", title="Staging" %} |
| 2 | +Staging documentation under review. |
| 3 | +{% endpanel %} |
| 4 | + |
| 5 | +# Project Creation and Structure {#project-creation-and-structure} |
| 6 | + |
| 7 | +## Go package Structure |
| 8 | + |
| 9 | +Kubebuilder projects contain 4 important packages. |
| 10 | + |
| 11 | +##### cmd/... |
| 12 | + |
| 13 | +The `cmd` package contains the controller-manager main program which runs controllers. Users typically |
| 14 | +will not need to modify this package unless they are doing something special. |
| 15 | + |
| 16 | +##### pkg/apis/... |
| 17 | + |
| 18 | +The `pkg/apis/...` packages contains the *go* structs that define the resource schemas. |
| 19 | +Users edit `*_types.go` files under this director to implement their API definitions. |
| 20 | + |
| 21 | +Each resource lives in a `pkg/apis/<api-group-name>/<api-version-name>/<api-kind-name>_types.go` |
| 22 | +file. |
| 23 | + |
| 24 | +For more information on API Group, Version and Kinds, see the *What is a Resource* chapter. |
| 25 | + |
| 26 | +{% panel style="info", title="Generated code" %} |
| 27 | +Kubebuilder generates boilerplate code to add required types and register APIs in this package. |
| 28 | +Boilerplate code is written to `zz_generated.*` files, and should only be written |
| 29 | +by kubebuilder. |
| 30 | +{% endpanel %} |
| 31 | + |
| 32 | +##### pkg/controller/... |
| 33 | + |
| 34 | +The `pkg/controller/...` packages contain the *go* types and functions that implement the |
| 35 | +business logic for APIs in *controllers*. |
| 36 | + |
| 37 | +More information on Controllers in the *What is a Controller* chapter. |
| 38 | + |
| 39 | +##### pkg/inject/... |
| 40 | + |
| 41 | +The `pkg/inject/...` packages contain the generated code that registers annotated |
| 42 | +Controllers and Resources. |
| 43 | + |
| 44 | +*Note*: This package is unique to kubebuilder. |
| 45 | + |
| 46 | +## Additional directories |
| 47 | + |
| 48 | +In addition to the packages above, a Kubebuilder project has several other directories. |
| 49 | + |
| 50 | +##### hack/... |
| 51 | + |
| 52 | +Kubebuilder puts misc files into the hack directory, such as API installation yaml files |
| 53 | +and samples resource configs. |
| 54 | + |
| 55 | +##### docs/... |
| 56 | + |
| 57 | +API reference documentation, user defined API samples and API conceptual documentation go here. |
| 58 | + |
| 59 | +{% method %} |
| 60 | +## Create a new project |
| 61 | + |
| 62 | +Create a new kubebuilder project. This will automatically initialize the vendored go libraries |
| 63 | +that will be required to build your project. |
| 64 | + |
| 65 | +{% sample lang="bash" %} |
| 66 | +```bash |
| 67 | +$ kubebuilder init --domain k8s.io |
| 68 | +``` |
| 69 | +{% endmethod %} |
| 70 | + |
| 71 | +{% method %} |
| 72 | +## Create a new API (Resource) |
| 73 | + |
| 74 | +Create the *_types.go file and controller.go files. |
| 75 | + |
| 76 | +{% sample lang="bash" %} |
| 77 | +```bash |
| 78 | +$ kubebuilder create resource --group mygroup --version v1beta1 --kind MyKind |
| 79 | +``` |
| 80 | +{% endmethod %} |
| 81 | + |
| 82 | +{% method %} |
| 83 | +## Run your controller-manager locally against a Kubernetes cluster |
| 84 | + |
| 85 | +Users may run the controller-manager binary locally against a Kubernetes cluster. This will |
| 86 | +install the APIs into the cluster and begin watching and reconciling the resources. |
| 87 | + |
| 88 | +{% sample lang="bash" %} |
| 89 | +```bash |
| 90 | +# Create a minikube cluster |
| 91 | +$ minikube start |
| 92 | + |
| 93 | +# Install the APIs into the minikube cluster and begin watching it |
| 94 | +$ GOBIN=${PWD}/bin go install ${PWD#$GOPATH/src/}/cmd/controller-manager |
| 95 | +$ bin/controller-manager --kubeconfig ~/.kube/config |
| 96 | +``` |
| 97 | +{% endmethod %} |
| 98 | + |
| 99 | +{% method %} |
| 100 | +## Create an object |
| 101 | + |
| 102 | +Create a new instance of your Resource. Observe the controller-manager logs after creating the object. |
| 103 | + |
| 104 | +{% sample lang="bash" %} |
| 105 | +```bash |
| 106 | +$ kubectl apply -f hack/sample/<resource>.yaml |
| 107 | +``` |
| 108 | +{% endmethod %} |
0 commit comments