github.com/giantswarm/apiextensions/v6@v6.6.0/README.md (about) 1 [](https://circleci.com/gh/giantswarm/apiextensions) 2 3 # apiextensions 4 5 This library provides generated Kubernetes clients for the Giant Swarm infrastructure. 6 7 ## Usage 8 9 - [`pkg/apis`](https://pkg.go.dev/github.com/giantswarm/apiextensions/pkg/apis?tab=doc): Contains data structures for 10 custom resources in `*.giantswarm.io` API groups. See full documentation 11 [here](https://pkg.go.dev/github.com/giantswarm/apiextensions/pkg/apis?tab=doc). 12 13 ## Contributing 14 15 ### Setup 16 17 While the generation scripts can run without a GitHub token defined, you may encounter rate limit errors without one. The 18 token will be loaded from the `GITHUB_TOKEN` environment variable if it exists. Giant Swarm engineers can generally use 19 `export GITHUB_TOKEN=$OPSCTL_GITHUB_TOKEN` to configure this before running `make`. 20 21 ### Changing Existing Custom Resources 22 23 - Make the desired changes in `pkg/apis/<group>/<version>`. 24 - Update generated files by calling `make generate`. 25 - Review and commit all changes including generated code. 26 27 #### Naming Convention 28 29 Custom resource structs are placed in packages corresponding to the endpoints in 30 Kubernetes API. For example, structs in package 31 `github.com/giantswarm/apiextensions/pkg/apis/infrastructure/v1alpha2` are created 32 from objects under `/apis/infrastructure.giantswarm.io/v1alpha2/` endpoint. 33 34 As this is common to have name collisions between field type names in different 35 custom objects sharing the same group and version (e.g. `Spec` or `Status`) we prefix all type names 36 referenced inside custom object with the name of the parent object. 37 38 Example: 39 40 ```go 41 package v1alpha1 42 43 import ( 44 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 45 ) 46 47 type NewObj struct { 48 metav1.TypeMeta `json:",inline"` 49 metav1.ObjectMeta `json:"metadata"` 50 Spec NewObjSpec `json:"spec"` 51 } 52 53 type NewObjSpec struct { 54 Field string `json:"field"` 55 } 56 ``` 57 58 ### Adding a New Custom Resource 59 60 This is example skeleton for adding new object. 61 62 - Make sure group and version of the object to add exists (described in 63 [previous paragraph](#adding-a-new-group-andor-version)). 64 - Replace `NewObj` with your object name. 65 - Put struct definitions inside a proper package denoted by group and version 66 in a file named `new_obj_types.go`. Replace `new_obj` with snake_case-formatted object name. 67 - Add `NewObj` and `NewObjList` to `knownTypes` slice in `register.go` 68 - Generate code for the resource by calling `make`. 69 - Commit changes and create a release. 70 71 ```go 72 package v1alpha1 73 74 import ( 75 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 76 ) 77 78 // +genclient 79 // +genclient:noStatus 80 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 81 82 // NewObj godoc. 83 type NewObj struct { 84 metav1.TypeMeta `json:",inline"` 85 metav1.ObjectMeta `json:"metadata"` 86 Spec NewObjSpec `json:"spec"` 87 } 88 89 // NewObjSpec godoc. 90 type NewObjSpec struct { 91 FieldName string `json:"fieldName"` 92 } 93 94 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 95 96 // NewObjList godoc. 97 type NewObjList struct { 98 metav1.TypeMeta `json:",inline"` 99 metav1.ListMeta `json:"metadata"` 100 Items []NewObj `json:"items"` 101 } 102 ``` 103 104 ### Adding a New Group and/or Version 105 106 This is example skeleton for adding new group and/or version. 107 108 - Replace `GROUP` with new group name and `VERSION` with new version name. 109 - Create a new package `/pkg/apis/GROUP/VERSION/`. 110 - Inside the package create a file `doc.go` (content below). 111 - Inside the package create a file `register.go` (content below). 112 - Add a new object (described in [next paragraph](#adding-a-new-custom-object)). 113 114 Example `doc.go` content. 115 116 ```go 117 // +k8s:openapi-gen=true 118 // +k8s:deepcopy-gen=package,register 119 120 // +groupName=GROUP.giantswarm.io 121 package VERSION 122 ``` 123 124 Example `register.go` content. 125 126 ```go 127 package VERSION 128 129 import ( 130 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 131 "k8s.io/apimachinery/pkg/runtime" 132 "k8s.io/apimachinery/pkg/runtime/schema" 133 ) 134 135 const ( 136 group = "GROUP.giantswarm.io" 137 version = "VERSION" 138 ) 139 140 // knownTypes is the full list of objects to register with the scheme. It 141 // should contain pointers of zero values of all custom objects and custom 142 // object lists in the group version. 143 var knownTypes = []runtime.Object{ 144 //&Object{}, 145 //&ObjectList{}, 146 } 147 148 // SchemeGroupVersion is group version used to register these objects 149 var SchemeGroupVersion = schema.GroupVersion{ 150 Group: group, 151 Version: version, 152 } 153 154 var ( 155 schemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) 156 157 // AddToScheme is used by the generated client. 158 AddToScheme = schemeBuilder.AddToScheme 159 ) 160 161 // Adds the list of known types to api.Scheme. 162 func addKnownTypes(scheme *runtime.Scheme) error { 163 scheme.AddKnownTypes(SchemeGroupVersion, knownTypes...) 164 metav1.AddToGroupVersion(scheme, SchemeGroupVersion) 165 return nil 166 } 167 ``` 168 169 ### Updating dependencies 170 171 #### Upstream CRDs 172 173 `apiextensions` generates Giant Swarm CRDs based on code in the `pkg/apis` directory and also aggregates CRDs from 174 various sources. Any repository that publishes CRDs as a YAML-formatted manifest of CRDs attached as an asset to a 175 GitHub release ("upstream" CRDs) or in a source tree ("remote" CRDs) can be used. The current set of CRDs 176 including target version is defined in `hack/assets.go` in the `upstreamReleaseAssets` and `remoteRepositories` variables 177 for upstream and remote CRDs respectively. Update the values and re-run `make` to update the aggregated CRDs. 178 179 #### Code Generation Tools 180 181 To change the version of a tool, edit the version manually in `hack/tools/<tool>/go.mod` and run `go mod tidy` in 182 that directory so that `go.sum` is updated. 183 184 ### Versioning 185 186 This library uses standard semantic versioning. Versioning of CRDs is a separate issue covered in the [Kubernetes 187 deprecation policy](https://kubernetes.io/docs/reference/using-api/deprecation-policy/). 188 In short, if an API field needs to be removed, a new version must be created, and any versions served concurrently 189 must be convertible in both directions without data loss. 190 191 ## Code generation 192 193 This library uses code generation to generate several components so that they do not need to be maintained manually 194 and to reduce the chance of mistakes such as when, for example, defining an OpenAPIV3 schema for a CRD. 195 196 ### Makefile 197 198 The `Makefile` at the root of the repository ensures that required tools (defined below) are installed in 199 `hack/tools/bin` and then runs each step of the code generation pipeline sequentially. 200 201 The main code generation steps are as follows: 202 - `generate-deepcopy`: Generates `zz_generated.deepcopy.go` in each package in `pkg/apis` with deep copy functions. 203 - `generate-manifests`: Generates CRDs in `config/crd` from CRs found in `pkg/apis`. 204 - `imports`: Sorts imports in all source files under `./pkg`. 205 - `patch`: Applies the git patch `hack/generated.patch` to work around limitations in code generators. 206 207 These can all be run with `make generate` or simply `make` as `generate` is the default rule. 208 209 Extra commands are provided including: 210 - `clean-tools`: Deletes all tools from the tools binary directory. 211 - `clean-generated`: Deletes all generated files. 212 - `verify`: Regenerates files and exits with a non-zero exit code if generated files don't match `HEAD` in source control. 213 214 ### Tools 215 216 Tools are third-party executables which perform a particular action as part of the code generation pipeline. They are 217 defined in `hack/tools` in separate directories. Versions for the tools are defined in the `go.mod` file in their 218 respective directories. A common `go.mod` isn't used so that their dependencies don't interfere. 219 220 #### [`controller-gen`](https://book.kubebuilder.io/reference/controller-gen.html) 221 222 Generates a custom resource definition (CRD) for each custom resource using special comments such as 223 `// +kubebuilder:validation:Optional`. 224 225 #### [`goimports`](https://pkg.go.dev/golang.org/x/tools/cmd/goimports) 226 227 Updates Go import lines by adding missing ones and removing unreferenced ones. This is required because CI checks for 228 imports ordered in three sections (standard library, third-party, local) but certain code generators only generate 229 source files with two sections. 230 231 ### Annotation Documentation 232 233 Docs generator has support for documenting annotations. This is achieved by creating code annotations in YAML on the definition of the variables like: 234 235 ``` 236 // support: 237 // - crd: awsclusters.infrastructure.giantswarm.io 238 // apiversion: v1alpha2 239 // release: Since 14.0.0 240 // documentation: 241 // Here is the documentation related to this annotation 242 ``` 243 244 Check [AWS Annotations](pkg/annotation/aws.go) for examples. 245 246 The `documentation` is later on parsed as Markdown syntax. 247 Annotations that don't have this YAML format are ignored and not published to the public docs. 248 Annotations belong to specific API Version of the CRDs but they can be linked to multiple CRDs and multiple API Versions.