github.com/giantswarm/apiextensions/v2@v2.6.2/README.md (about)

     1  [![CircleCI](https://circleci.com/gh/giantswarm/apiextensions.svg?&style=shield)](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  - [`pkg/clientset/versioned`](https://pkg.go.dev/github.com/giantswarm/apiextensions/pkg/clientset/versioned?tab=doc): 
    13      Contains a clientset, a client for each custom resource, and a fake client for unit testing. See full documentation
    14      [here](https://pkg.go.dev/github.com/giantswarm/apiextensions/pkg/clientset/versioned?tab=doc).
    15  - [`pkg/crd`](https://pkg.go.dev/github.com/giantswarm/apiextensions/pkg/crd?tab=doc): Contains an interface for 
    16      accessing individual CRDs or listing all CRDs. See full documentation 
    17      [here](https://pkg.go.dev/github.com/giantswarm/apiextensions/pkg/crd?tab=doc).
    18  
    19  ## Contributing
    20  
    21  ### Changing Existing Custom Resources
    22  
    23  - Make the desired changes in `pkg/apis/<group>/<version>`
    24  - Update generated files by calling `make`.
    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:deepcopy-gen=package,register
   118  
   119  // +groupName=GROUP.giantswarm.io
   120  package VERSION
   121  ```
   122  
   123  Example `register.go` content.
   124  
   125  ```go
   126  package VERSION
   127  
   128  import (
   129  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
   130  	"k8s.io/apimachinery/pkg/runtime"
   131  	"k8s.io/apimachinery/pkg/runtime/schema"
   132  )
   133  
   134  const (
   135  	group   = "GROUP.giantswarm.io"
   136  	version = "VERSION"
   137  )
   138  
   139  // knownTypes is the full list of objects to register with the scheme. It
   140  // should contain pointers of zero values of all custom objects and custom
   141  // object lists in the group version.
   142  var knownTypes = []runtime.Object{
   143  		//&Object{},
   144  		//&ObjectList{},
   145  }
   146  
   147  // SchemeGroupVersion is group version used to register these objects
   148  var SchemeGroupVersion = schema.GroupVersion{
   149  	Group:   group,
   150  	Version: version,
   151  }
   152  
   153  var (
   154  	schemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
   155  
   156  	// AddToScheme is used by the generated client.
   157  	AddToScheme = schemeBuilder.AddToScheme
   158  )
   159  
   160  // Adds the list of known types to api.Scheme.
   161  func addKnownTypes(scheme *runtime.Scheme) error {
   162  	scheme.AddKnownTypes(SchemeGroupVersion, knownTypes...)
   163  	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
   164  	return nil
   165  }
   166  ```
   167  
   168  ### Updating dependencies
   169  
   170  #### Cluster API
   171  
   172  Cluster API CRDs are also exported by this library using `controller-gen`. The version used is determined by the value
   173  of `sigs.k8s.io/cluster-api` in `hack/go.mod`.
   174  
   175  #### Code Generation Tools
   176  
   177  To change the version of a tool, edit the version manually in `hack/tools/<tool>/go.mod` and run `go mod tidy` in
   178  that directory so that `go.sum` is updated.
   179  
   180  ### Versioning
   181  
   182  This library uses standard semantic versioning. Versioning of CRDs is a separate issue covered in the [Kubernetes
   183      deprecation policy](https://kubernetes.io/docs/reference/using-api/deprecation-policy/).
   184      In short, if an API field needs to be removed, a new version must be created, and any versions served concurrently
   185      must be convertible in both directions without data loss.
   186  
   187  ## Code generation
   188  
   189  This library uses code generation to generate several components so that they do not need to be maintained manually
   190  and to reduce the chance of mistakes such as when, for example, defining an OpenAPIV3 schema for a CRD.
   191  
   192  ### Makefile
   193  
   194  The `Makefile` at the root of the repository ensures that required tools (defined below) are installed in 
   195  `hack/tools/bin` and then runs each step of the code generation pipeline sequentially.
   196  
   197  The main code generation steps are as follows:
   198  - `generate-clientset`: Generates the clientset for accessing custom resources in a Kubernetes cluster.
   199  - `generate-deepcopy`: Generates `zz_generated.deepcopy.go` in each package in `pkg/apis` with deep copy functions.
   200  - `generate-manifests`: Generates CRDs in `config/crd/v1` and `config/crd/v1beta1` from CRs found in `pkg/apis`. 
   201  - `generate-fs`: Generates `pkg/crd/internal` package containing a filesystem holding all files in `config/crd`.
   202  - `imports`: Sorts imports in all source files under `./pkg`.
   203  - `patch`: Applies the git patch `hack/generated.patch` to work around limitations in code generators.
   204  
   205  These can all be run with `make generate` or simply `make` as `generate` is the default rule.
   206  
   207  Extra commands are provided including:
   208  - `clean-tools`: Deletes all tools from the tools binary directory.
   209  - `clean-generated`: Deletes all generated files.
   210  - `verify`: Regenerates files and exits with a non-zero exit code if generated files don't match `HEAD` in source control.
   211  
   212  ### Tools
   213  
   214  Tools are third-party executables which perform a particular action as part of the code generation pipeline. They are 
   215  defined in `hack/tools` in separate directories. Versions for the tools are defined in the `go.mod` file in their
   216  respective directories. A common `go.mod` isn't used so that their dependencies don't interfere.
   217  
   218  #### [`deepcopy-gen`](https://godoc.org/k8s.io/code-generator/cmd/deepcopy-gen)
   219  
   220  Generates `DeepCopy` and `DeepCopyInto` functions for all custom resources to satisfy the `runtime.Object` interface.
   221  
   222  #### [`client-gen`](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/generating-clientset.md)
   223  
   224  Generates a "client set" which provides CRUD interfaces for each custom resource.
   225  
   226  #### [`esc`](https://github.com/mjibson/esc)
   227  
   228  Encodes local filesystem trees into a Go source file containing an `http.FileSystem` which provides access to the
   229  files at runtime. This allows these files to be accessed from a binary outside of the source tree containing those files.
   230  
   231  #### [`controller-gen`](https://book.kubebuilder.io/reference/controller-gen.html)
   232  
   233  Generates a custom resource definition (CRD) for each custom resource using special comments such as 
   234  `// +kubebuilder:validation:Optional`.
   235  
   236  #### [`kustomize`](https://github.com/kubernetes-sigs/kustomize)
   237  
   238  Provides an extra patch step for generated CRD YAML files because certain CRD fields can't be modified with
   239  `controller-gen` directly.
   240  
   241  #### [`goimports`](https://pkg.go.dev/golang.org/x/tools/cmd/goimports)
   242  
   243  Updates Go import lines by adding missing ones and removing unreferenced ones. This is required because CI checks for
   244  imports ordered in three sections (standard library, third-party, local) but certain code generators only generate
   245  source files with two sections.