Skip to content

Commit f058d9c

Browse files
authored
Merge pull request #5723 from Jefftree/default
Add default tag to api conventions
2 parents 8d80f03 + 3a97f34 commit f058d9c

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`.
@@ -696,10 +696,10 @@ have a built-in `nil` value (e.g. maps and slices).
696696
- The API server should allow POSTing and PUTing a resource with this field
697697
unset.
698698

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

705705
Note that for backward compatibility, any field that has the `omitempty` struct
@@ -714,7 +714,7 @@ Required fields have the opposite properties, namely:
714714
- The API server should not allow POSTing or PUTing a resource with this field
715715
unset.
716716

717-
Using the `+optional` or the `omitempty` tag causes OpenAPI documentation to
717+
Using the `+optional` or the `omitempty` tag causes OpenAPI documentation to
718718
reflect that the field is optional.
719719

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

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

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

893900
Built-in types and ownerReferences do not support cross namespaces references.
894-
If a non-built-in types chooses to have cross-namespace references the semantics of the edge cases above should be
901+
If a non-built-in types chooses to have cross-namespace references the semantics of the edge cases above should be
895902
clearly described and the permissions issues should be resolved.
896903
This could be done with a double opt-in (an opt-in from both the referrer and the refer-ee) or with secondary permissions
897-
checks performed in admission.
904+
checks performed in admission.
898905

899906
### Naming of the reference field
900907

@@ -979,9 +986,9 @@ A single kind object reference is straightforward in that the controller can har
979986
```yaml
980987
# for a single resource, the suffix should be Ref, with the field name
981988
# providing an indication as to the resource type referenced.
982-
secretRef:
989+
secretRef:
983990
name: foo
984-
# namespace would generally not be needed and is discouraged,
991+
# namespace would generally not be needed and is discouraged,
985992
# as explained above.
986993
namespace: foo-namespace
987994
```
@@ -1077,7 +1084,7 @@ fooObjectRef:
10771084
group: operator.openshift.io
10781085
resource: openshiftapiservers
10791086
name: cluster
1080-
# namespace is unset if the resource is cluster-scoped, or lives in the
1087+
# namespace is unset if the resource is cluster-scoped, or lives in the
10811088
# same namespace as the referrer.
10821089
```
10831090

@@ -1448,7 +1455,7 @@ reduce data volume, load on the system, and noise exposed to users.
14481455
* Go field names must be PascalCase. JSON field names must be camelCase. Other
14491456
than capitalization of the initial letter, the two should almost always match.
14501457
No underscores or dashes in either.
1451-
* Field and resource names should be declarative, not imperative (SomethingDoer,
1458+
* Field and resource names should be declarative, not imperative (SomethingDoer,
14521459
DoneBy, DoneAt).
14531460
* Use `Node` where referring to
14541461
the node resource in the context of the cluster. Use `Host` where referring to
@@ -1582,7 +1589,7 @@ Kubernetes components and tools:
15821589
- Key prefixes under "kubernetes.io" and "k8s.io" are reserved for the Kubernetes
15831590
project.
15841591
- Such keys are effectively part of the kubernetes API and may be subject
1585-
to deprecation and compatibility policies.
1592+
to deprecation and compatibility policies.
15861593
- Key names, including prefixes, should be precise enough that a user could
15871594
plausibly understand where it came from and what it is for.
15881595
- Key prefixes should carry as much context as possible.
@@ -1671,19 +1678,19 @@ API objects often are [union](#Unions) object containing the following:
16711678
1. One or more fields identifying the `Type` specific to API object (aka the `discriminator`).
16721679
2. A set of N fields, only one of which should be set at any given time - effectively a union.
16731680

1674-
Controllers operating on the API type often allocate resources based on
1675-
the `Type` and/or some additional data provided by user. A canonical example
1676-
of this is the `Service` API object where resources such as IPs and network ports
1677-
will be set in the API object based on `Type`. When the user does not specify
1678-
resources, they will be allocated, and when the user specifies exact value, they will
1681+
Controllers operating on the API type often allocate resources based on
1682+
the `Type` and/or some additional data provided by user. A canonical example
1683+
of this is the `Service` API object where resources such as IPs and network ports
1684+
will be set in the API object based on `Type`. When the user does not specify
1685+
resources, they will be allocated, and when the user specifies exact value, they will
16791686
be reserved or rejected.
16801687

1681-
When the user chooses to change the `discriminator` value (e.g., from `Type X` to `Type Y`) without
1682-
changing any other fields then the system should clear the fields that were used to represent `Type X`
1683-
in the union along with releasing resources that were attached to `Type X`. This should automatically
1684-
happen irrespective of how these values and resources were allocated (i.e., reserved by the user or
1685-
automatically allocated by the system. A concrete example of this is again `Service` API. The system
1686-
allocates resources such as `NodePorts` and `ClusterIPs` and automatically fill in the fields that
1687-
represent them in case of the service is of type `NodePort` or `ClusterIP` (`discriminator` values).
1688-
These resources and the fields representing them are automatically cleared when the users changes
1689-
service type to `ExternalName` where these resources and field values no longer apply.
1688+
When the user chooses to change the `discriminator` value (e.g., from `Type X` to `Type Y`) without
1689+
changing any other fields then the system should clear the fields that were used to represent `Type X`
1690+
in the union along with releasing resources that were attached to `Type X`. This should automatically
1691+
happen irrespective of how these values and resources were allocated (i.e., reserved by the user or
1692+
automatically allocated by the system. A concrete example of this is again `Service` API. The system
1693+
allocates resources such as `NodePorts` and `ClusterIPs` and automatically fill in the fields that
1694+
represent them in case of the service is of type `NodePort` or `ClusterIP` (`discriminator` values).
1695+
These resources and the fields representing them are automatically cleared when the users changes
1696+
service type to `ExternalName` where these resources and field values no longer apply.

0 commit comments

Comments
 (0)