You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is intended to track the various linter rules, based on API conventions and API reviewer experience, that we wish to implement within KAL
commentstart - Godoc for fields within structs should start with the serialised JSON tag name
jsontags - Each field should have a json tag, and the field name should be camelCase
optionalorrequired - Each field should have either an optional, or a required marker
[add statussubresource lint check #2] statussubresource - (CRD - not default) If a struct is marked as // +kubebuilder:object:root=true and contains a status field, it should also have // +kubebuilder:subresource:status
@JoelSpeed disagrees with some of the conventions in upstream here, will need to discuss with upstream, below are what I think we should have. Pattern that lead to exceptions around optional fields seems discouraged now.
Should have omitempty (will need to toggle this, or somehow allow exceptions)
If field is a struct, and has omitempty, field must a pointer (json can't omit refs to structs)
Should not use pointer for maps and slices (difference between 0 length and omitted is likely an anti-pattern?)
For strings, if not a pointer, must have a minLength (0 length would be omitted on a round trip)
Require all optional fields be points (would like to toggle this, we don't enforce in OpenShift)
Should have 1 required field with +uniondiscriminator
Other fields should be optional and marked with +unionMember or +unionMember,optional
For CRDs, should have a CEL validation rule has(self.<discriminator> && self.<discriminator> == "<member>" ? has(self.member) : !has(self.member) or equivalent if the member is optional
Should not have non-member fields present (strong opinion, so, configurable)
If you have an array of a struct type, the struct must have at least one required field.
Prevents the problem seen in NetworkPolicy where adding or removing a single “-” from the YAML representation wildly changes the meaning of an object… (“Match all packets to 10.0.0.1, port 80” vs “Match all packets to 10.0.0.1 on any port, and also match all packets to port 80 on any IP”) I don’t know if other people have run into this and consider it a problem…)
Look for top level runtime objects (ie Kinds) and ensure that they have inline typemeta, objectmeta is optional/omitempty, spec is required, status is optional, none of the fields are pointers
Thinking also about the idea of presets (golangci-lint has these) where you could select a preset config for Kubernetes, CRD, OpenShift or others in the future if they have variations. The presets would configure different defaults for the configuration, e.g. setting which linters are on by default, and where there are variations in individual rule config, setting those defaults as well
This issue is intended to track the various linter rules, based on API conventions and API reviewer experience, that we wish to implement within KAL
// +kubebuilder:object:root=trueand contains a status field, it should also have// +kubebuilder:subresource:status+listType=map,+listMapKey=type,+patchStrategy=merge,+patchMergeKey=typeand+optionaljson:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"phasefields are deprecated and conditions should be preferred, avoid phase like enum fieldsint, useint32orint64, preferint32unless explicitly needed (check bounds and suggest int32)omitempty, field must a pointer (json can't omit refs to structs)stampin field names, eg fooTimestamp, use fooTime insteadlistMapKeymarker+enum.int64must be-(2^53) < x < (2^53)or should be represented as a string (javascript float overflows otherwise)+union+uniondiscriminator+unionMemberor+unionMember,optionalhas(self.<discriminator> && self.<discriminator> == "<member>" ? has(self.member) : !has(self.member)or equivalent if the member is optional+kubebuilder:defaultand prefer upstream marker=and some require:=(more research required)+ kubebuilderfooSeconds, avoids having to implement go duration parsingThinking also about the idea of presets (golangci-lint has these) where you could select a preset config for
Kubernetes,CRD,OpenShiftor others in the future if they have variations. The presets would configure different defaults for the configuration, e.g. setting which linters are on by default, and where there are variations in individual rule config, setting those defaults as well