Skip to content

Commit 3a97f34

Browse files
committed
Add default tag to api conventions
1 parent f026ec2 commit 3a97f34

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

contributors/devel/sig-architecture/api-conventions.md

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ defaults) and may not have lists.
123123

124124
In addition, all lists that return objects with labels should support label
125125
filtering (see [the labels documentation](https://kubernetes.io/docs/user-guide/labels/)), and most
126-
lists should support filtering by fields (see
126+
lists should support filtering by fields (see
127127
[the fields documentation](https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/)).
128128

129129
Examples: `PodList`, `ServiceList`, `NodeList`.
@@ -691,10 +691,10 @@ have a built-in `nil` value (e.g. maps and slices).
691691
- The API server should allow POSTing and PUTing a resource with this field
692692
unset.
693693

694-
In most cases, optional fields should also have the `omitempty` struct tag (the
694+
In most cases, optional fields should also have the `omitempty` struct tag (the
695695
`omitempty` option specifies that the field should be omitted from the json
696-
encoding if the field has an empty value). However, If you want to have
697-
different logic for an optional field which is not provided vs. provided with
696+
encoding if the field has an empty value). However, If you want to have
697+
different logic for an optional field which is not provided vs. provided with
698698
empty values, do not use `omitempty` (e.g. https://github.com/kubernetes/kubernetes/issues/34641).
699699

700700
Note that for backward compatibility, any field that has the `omitempty` struct
@@ -709,7 +709,7 @@ Required fields have the opposite properties, namely:
709709
- The API server should not allow POSTing or PUTing a resource with this field
710710
unset.
711711

712-
Using the `+optional` or the `omitempty` tag causes OpenAPI documentation to
712+
Using the `+optional` or the `omitempty` tag causes OpenAPI documentation to
713713
reflect that the field is optional.
714714

715715
Using a pointer allows distinguishing unset from the zero value for that type.
@@ -739,6 +739,13 @@ Incorporating the default values into the `Spec` ensures that `Spec` depicts the
739739
full desired state so that it is easier for the system to determine how to
740740
achieve the state, and for the user to know what to anticipate.
741741

742+
Default values can be specified on a field using the `+default=` tag. Primitives
743+
will have their values directly assigned while structs will go through the
744+
JSON unmarshalling process. Fields that do not have an `omitempty` json tag will
745+
default to the zero value of their corresponding type if no default is assigned.
746+
747+
Refer to [defaulting docs](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#defaulting) for more information.
748+
742749
API version-specific default values are set by the API server.
743750

744751
## Late Initialization
@@ -880,16 +887,16 @@ references can have unexpected impacts, including:
880887
or opt-in's from both involved namespaces.
881888
3. referential integrity problems that one party cannot solve. Referencing namespace/B from namespace/A doesn't imply the
882889
power to control the other namespace. This means that you can refer to a thing you cannot create or update.
883-
4. unclear semantics on deletion. If a namespaced resource is referenced by other namespaces, should a delete of the
890+
4. unclear semantics on deletion. If a namespaced resource is referenced by other namespaces, should a delete of the
884891
referenced resource result in removal or should the referenced resource be force to remain.
885892
5. unclear semantics on creation. If a referenced resource is created after its reference, there is no way to know if it
886893
is the one that is expected or if it is a different one created with the same name.
887894

888895
Built-in types and ownerReferences do not support cross namespaces references.
889-
If a non-built-in types chooses to have cross-namespace references the semantics of the edge cases above should be
896+
If a non-built-in types chooses to have cross-namespace references the semantics of the edge cases above should be
890897
clearly described and the permissions issues should be resolved.
891898
This could be done with a double opt-in (an opt-in from both the referrer and the refer-ee) or with secondary permissions
892-
checks performed in admission.
899+
checks performed in admission.
893900

894901
### Naming of the reference field
895902

@@ -974,9 +981,9 @@ A single kind object reference is straightforward in that the controller can har
974981
```yaml
975982
# for a single resource, the suffix should be Ref, with the field name
976983
# providing an indication as to the resource type referenced.
977-
secretRef:
984+
secretRef:
978985
name: foo
979-
# namespace would generally not be needed and is discouraged,
986+
# namespace would generally not be needed and is discouraged,
980987
# as explained above.
981988
namespace: foo-namespace
982989
```
@@ -1023,7 +1030,7 @@ fooObjectRef:
10231030
group: operator.openshift.io
10241031
resource: openshiftapiservers
10251032
name: cluster
1026-
# namespace is unset if the resource is cluster-scoped, or lives in the
1033+
# namespace is unset if the resource is cluster-scoped, or lives in the
10271034
# same namespace as the referrer.
10281035
```
10291036

@@ -1394,7 +1401,7 @@ reduce data volume, load on the system, and noise exposed to users.
13941401
* Go field names must be PascalCase. JSON field names must be camelCase. Other
13951402
than capitalization of the initial letter, the two should almost always match.
13961403
No underscores or dashes in either.
1397-
* Field and resource names should be declarative, not imperative (SomethingDoer,
1404+
* Field and resource names should be declarative, not imperative (SomethingDoer,
13981405
DoneBy, DoneAt).
13991406
* Use `Node` where referring to
14001407
the node resource in the context of the cluster. Use `Host` where referring to
@@ -1528,7 +1535,7 @@ Kubernetes components and tools:
15281535
- Key prefixes under "kubernetes.io" and "k8s.io" are reserved for the Kubernetes
15291536
project.
15301537
- Such keys are effectively part of the kubernetes API and may be subject
1531-
to deprecation and compatibility policies.
1538+
to deprecation and compatibility policies.
15321539
- Key names, including prefixes, should be precise enough that a user could
15331540
plausibly understand where it came from and what it is for.
15341541
- Key prefixes should carry as much context as possible.
@@ -1617,19 +1624,19 @@ API objects often are [union](#Unions) object containing the following:
16171624
1. One or more fields identifying the `Type` specific to API object (aka the `discriminator`).
16181625
2. A set of N fields, only one of which should be set at any given time - effectively a union.
16191626

1620-
Controllers operating on the API type often allocate resources based on
1621-
the `Type` and/or some additional data provided by user. A canonical example
1622-
of this is the `Service` API object where resources such as IPs and network ports
1623-
will be set in the API object based on `Type`. When the user does not specify
1624-
resources, they will be allocated, and when the user specifies exact value, they will
1627+
Controllers operating on the API type often allocate resources based on
1628+
the `Type` and/or some additional data provided by user. A canonical example
1629+
of this is the `Service` API object where resources such as IPs and network ports
1630+
will be set in the API object based on `Type`. When the user does not specify
1631+
resources, they will be allocated, and when the user specifies exact value, they will
16251632
be reserved or rejected.
16261633

1627-
When the user chooses to change the `discriminator` value (e.g., from `Type X` to `Type Y`) without
1628-
changing any other fields then the system should clear the fields that were used to represent `Type X`
1629-
in the union along with releasing resources that were attached to `Type X`. This should automatically
1630-
happen irrespective of how these values and resources were allocated (i.e., reserved by the user or
1631-
automatically allocated by the system. A concrete example of this is again `Service` API. The system
1632-
allocates resources such as `NodePorts` and `ClusterIPs` and automatically fill in the fields that
1633-
represent them in case of the service is of type `NodePort` or `ClusterIP` (`discriminator` values).
1634-
These resources and the fields representing them are automatically cleared when the users changes
1635-
service type to `ExternalName` where these resources and field values no longer apply.
1634+
When the user chooses to change the `discriminator` value (e.g., from `Type X` to `Type Y`) without
1635+
changing any other fields then the system should clear the fields that were used to represent `Type X`
1636+
in the union along with releasing resources that were attached to `Type X`. This should automatically
1637+
happen irrespective of how these values and resources were allocated (i.e., reserved by the user or
1638+
automatically allocated by the system. A concrete example of this is again `Service` API. The system
1639+
allocates resources such as `NodePorts` and `ClusterIPs` and automatically fill in the fields that
1640+
represent them in case of the service is of type `NodePort` or `ClusterIP` (`discriminator` values).
1641+
These resources and the fields representing them are automatically cleared when the users changes
1642+
service type to `ExternalName` where these resources and field values no longer apply.

0 commit comments

Comments
 (0)