|
| 1 | +# Markers for Config/Code Generation |
| 2 | + |
| 3 | +KubeBuilder makes use of a tool called `controller-gen` (from |
| 4 | +[controller-tools](https://godoc.org/sigs.k8s.io/controller-tools)) for |
| 5 | +generating utility code and Kubernetes YAML. This code and config |
| 6 | +generation is controlled by the presenence of special "marker comments" in |
| 7 | +Go code. |
| 8 | + |
| 9 | +Markers are single-line comments that start with a plus, followed by |
| 10 | +a marker name, optionally followed by some marker specific configuration: |
| 11 | + |
| 12 | +```go |
| 13 | +// +kubebuilder:validation:Optional |
| 14 | +// +kubebuilder:validation:MaxItems=2 |
| 15 | +// +kubebuilder:printcolumn:JSONPath=".status.replicas",name=Replicas,type=string |
| 16 | +``` |
| 17 | + |
| 18 | +See each subsection for information about different types of code and YAML |
| 19 | +generation. |
| 20 | + |
| 21 | +## Generating Code & Artifacts in KubeBuilder |
| 22 | + |
| 23 | +KubeBuilder projects have two `make` targets that make use of |
| 24 | +controller-gen: |
| 25 | + |
| 26 | +- `make manifests` generates Kubernetes object YAML, like |
| 27 | + [CustomResourceDefinitions](./markers/crd.md), |
| 28 | + [WebhookConfigurations](./markers/webhook.md), and [RBAC |
| 29 | + roles](./markers/rbac.md). |
| 30 | + |
| 31 | +- `make generate` generates code, like [runtime.Object/DeepCopy |
| 32 | + implementations](./markers/object.md). |
| 33 | + |
| 34 | +See [Generating CRDs](./generating-crd.md) for a comprehensive overview. |
| 35 | + |
| 36 | +## Marker Syntax |
| 37 | + |
| 38 | +Exact syntax is described in the [godocs for |
| 39 | +controller-tools](https://godoc.org/sigs.k8s.io/controller-tools/pkg/markers). |
| 40 | + |
| 41 | +In general, markers may either be: |
| 42 | + |
| 43 | +- **Empty** (`+kubebuilder:validation:Optional`): empty markers are like boolean flags on the command line |
| 44 | + -- just specifying them enables some behavior. |
| 45 | + |
| 46 | +- **Anonymous** (`+kubebuilder:validation:MaxItems=2`): anonymous markers take |
| 47 | + a single value as their argument. |
| 48 | + |
| 49 | +- **Multi-option** |
| 50 | + (`+kubebuilder:printcolumn:JSONPath=".status.replicas",name=Replicas,type=string`): multi-option |
| 51 | + markers take one or more named arguments. The first argument is |
| 52 | + separated from the name by a colon, and latter arguments are |
| 53 | + comma-separated. Order of arguments doesn't matter. Some arguments may |
| 54 | + be optional. |
| 55 | + |
| 56 | +Marker arguments may be strings, ints, bools, or slices thereof. Strings, |
| 57 | +ints, and bools follow their Go syntax: |
| 58 | + |
| 59 | +```go |
| 60 | +// +kubebuilder:validation:ExclusiveMaximum=false |
| 61 | +// +kubebulder:validation:Format="date-time" |
| 62 | +// +kubebuilder:validation:Maxium=42 |
| 63 | +``` |
| 64 | + |
| 65 | +For convinience, in simple cases the quotes may be omitted from strings, |
| 66 | +although this is not encouraged for anything other than single-word |
| 67 | +strings: |
| 68 | + |
| 69 | +```go |
| 70 | +// +kubebuilder:validation:Type=string |
| 71 | +``` |
| 72 | + |
| 73 | +Slices may be specified either by surrounding them with curly braces and |
| 74 | +separating with commas: |
| 75 | + |
| 76 | +```go |
| 77 | +// +kubebuilder:webhooks:Enum={"crackers, Gromit, we forgot the crackers!","not even wensleydale?"} |
| 78 | +``` |
| 79 | + |
| 80 | +or, in simple cases, by separating with semicolons: |
| 81 | + |
| 82 | +```go |
| 83 | +// +kubebuilder:validation:Enum=Wallace;Gromit;Chicken |
| 84 | +``` |
0 commit comments