Skip to content

Commit 739f979

Browse files
authored
Merge pull request #12671 from k8s-infra-cherrypick-robot/cherry-pick-12581-to-release-1.11
[release-1.11] 📖 Update API conventions
2 parents 51210c3 + cb44fa2 commit 739f979

File tree

1 file changed

+8
-49
lines changed

1 file changed

+8
-49
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
- [Breaking Changes](#breaking-changes)
2525
- [Dependency Licence Management](#dependency-licence-management)
2626
- [API conventions](#api-conventions)
27-
- [Optional vs. Required](#optional-vs-required)
28-
- [Example](#example)
29-
- [Exceptions](#exceptions)
3027
- [CRD additionalPrinterColumns](#crd-additionalprintercolumns)
3128
- [Google Doc Viewing Permissions](#google-doc-viewing-permissions)
3229
- [Issue and Pull Request Management](#issue-and-pull-request-management)
@@ -380,63 +377,25 @@ licenses dependencies and other artifacts use. For go dependencies only dependen
380377

381378
## API conventions
382379

383-
This project follows the [Kubernetes API conventions](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md). Minor modifications or additions to the conventions are listed below.
380+
This project follows the [Kubernetes API conventions](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md).
381+
We enforce the API conventions via [kube-api-linter](https://github.com/kubernetes-sigs/kube-api-linter).
382+
The corresponding configuration field can be found [here](https://github.com/kubernetes-sigs/cluster-api/blob/main/.golangci-kal.yml).
384383

385-
### Optional vs. Required
386-
387-
* Status fields MUST be optional. Our controllers are patching selected fields instead of updating the entire status in every reconciliation.
388-
389-
* If a field is required (for our controllers to work) and has a default value specified via OpenAPI schema, but we don't want to force users to set the field, we have to mark the field as optional. Otherwise, the client-side kubectl OpenAPI schema validation will force the user to set it even though it would be defaulted on the server-side.
390-
391-
Optional fields have the following properties:
392-
* An optional field MUST be marked with `+optional` and include an `omitempty` JSON tag.
393-
* Fields SHOULD be pointers if there is a good reason for it, for example:
394-
* the nil and the zero values (by Go standards) have semantic differences.
395-
* Note: This doesn't apply to map or slice types as they are assignable to `nil`.
396-
* the field is of a struct type, contains only fields with `omitempty` and you want
397-
to prevent that it shows up as an empty object after marshalling (e.g. `kubectl get`)
398-
399-
#### Example
400-
401-
When using ClusterClass, the semantic difference is important when you have a field in a template which will
402-
have instance-specific different values in derived objects. Because in this case it's possible to set the field to `nil`
403-
in the template and then the value can be set in derived objects without being overwritten by the cluster topology controller.
404-
405-
#### Exceptions
406-
407-
* Fields in root objects should be kept as scaffolded by kubebuilder, e.g.:
408-
```golang
409-
type Machine struct {
410-
metav1.TypeMeta `json:",inline"`
411-
metav1.ObjectMeta `json:"metadata,omitempty"`
412-
413-
Spec MachineSpec `json:"spec,omitempty"`
414-
Status MachineStatus `json:"status,omitempty"`
415-
}
416-
type MachineList struct {
417-
metav1.TypeMeta `json:",inline"`
418-
metav1.ListMeta `json:"metadata,omitempty"`
419-
Items []Machine `json:"items"`
420-
}
421-
```
422-
423-
* Top-level fields in `status` must always have the `+optional` annotation. If we want the field to be always visible even if it
424-
has the zero value, it must **not** have the `omitempty` JSON tag, e.g.:
425-
* Replica counters like `availableReplicas` in the `MachineDeployment`
426-
* Flags expressing progress in the object lifecycle like `infrastructureReady` in `Machine`
384+
Minor additions to the conventions are listed below.
427385

428386
### CRD additionalPrinterColumns
429387

430388
All our CRD objects should have the following `additionalPrinterColumns` order (if the respective field exists in the CRD):
431389
* Namespace (added automatically)
432390
* Name (added automatically)
433-
* Cluster
434-
* Other fields
391+
* ClusterClass and/or Cluster owning this resource
392+
* Available or Ready condition
435393
* Replica-related fields
394+
* Other fields for -o wide (fields with priority `1` are only shown with `-o wide` and not per default)
395+
* Paused (only shows with -o wide)
436396
* Phase
437397
* Age (mandatory field for all CRDs)
438398
* Version
439-
* Other fields for -o wide (fields with priority `1` are only shown with `-o wide` and not per default)
440399

441400
***NOTE***: The columns can be configured via the `kubebuilder:printcolumn` annotation on root objects. For examples, please see the `./api` package.
442401

0 commit comments

Comments
 (0)