Skip to content

Commit a093b06

Browse files
authored
Merge pull request #11909 from sivchari/enable-optionalorrequired
🌱 Enable optionalorrequired linter
2 parents e48409b + c2e43bb commit a093b06

31 files changed

+320
-38
lines changed

.golangci-kal.yml

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ linters-settings:
2323
- "maxlength" # Ensure all strings and arrays have maximum lengths/maximum items.
2424
- "nobools" # Bools do not evolve over time, should use enums instead.
2525
- "nofloats" # Ensure floats are not used.
26+
- "optionalorrequired" # Every field should be marked as `+optional` or `+required`.
2627
- "requiredfields" # Required fields should not be pointers, and should not have `omitempty`.
2728
- "statussubresource" # All root objects that have a `status` field should have a status subresource.
2829

@@ -32,7 +33,6 @@ linters-settings:
3233
# - "nophase" # Phase fields are discouraged by the Kube API conventions, use conditions instead.
3334

3435
# Linters below this line are disabled, pending conversation on how and when to enable them.
35-
# - "optionalorrequired" # Every field should be marked as `+optional` or `+required`.
3636
disable:
3737
- "*" # We will manually enable new linters after understanding the impact. Disable all by default.
3838
lintersConfig:
@@ -102,3 +102,16 @@ issues:
102102
text: "field (XPreserveUnknownFields|XPreserveUnknownFields|XValidations|XMetadata|XIntOrString) json tag does not match pattern"
103103
linters:
104104
- kal
105+
# The following rules are disabled until we migrate to the new API.
106+
- path: "bootstrap/kubeadm/api/v1beta1/kubeadm_types.go"
107+
text: "field Token is marked as required, should not be a pointer"
108+
linters:
109+
- kal
110+
- path: "api/v1beta1/clusterclass_types.go"
111+
text: "field Ref is marked as required, should not be a pointer"
112+
linters:
113+
- kal
114+
- path: "api/v1alpha1/*|api/v1beta1/*|api/v1alpha3/*"
115+
text: "field Items must be marked as optional or required"
116+
linters:
117+
- kal

api/v1beta1/cluster_types.go

+15
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ type ClusterAvailabilityGate struct {
541541
// Topology encapsulates the information of the managed resources.
542542
type Topology struct {
543543
// class is the name of the ClusterClass object to create the topology.
544+
// +required
544545
Class string `json:"class"`
545546

546547
// classNamespace is the namespace of the ClusterClass object to create the topology.
@@ -553,6 +554,7 @@ type Topology struct {
553554
ClassNamespace string `json:"classNamespace,omitempty"`
554555

555556
// version is the Kubernetes version of the cluster.
557+
// +required
556558
Version string `json:"version"`
557559

558560
// rolloutAfter performs a rollout of the entire cluster one component at a time,
@@ -669,12 +671,14 @@ type MachineDeploymentTopology struct {
669671
// class is the name of the MachineDeploymentClass used to create the set of worker nodes.
670672
// This should match one of the deployment classes defined in the ClusterClass object
671673
// mentioned in the `Cluster.Spec.Class` field.
674+
// +required
672675
Class string `json:"class"`
673676

674677
// name is the unique identifier for this MachineDeploymentTopology.
675678
// The value is used with other unique identifiers to create a MachineDeployment's Name
676679
// (e.g. cluster's name, etc). In case the name is greater than the allowed maximum length,
677680
// the values are hashed together.
681+
// +required
678682
Name string `json:"name"`
679683

680684
// failureDomain is the failure domain the machines will be created in.
@@ -772,12 +776,14 @@ type MachinePoolTopology struct {
772776
// class is the name of the MachinePoolClass used to create the pool of worker nodes.
773777
// This should match one of the deployment classes defined in the ClusterClass object
774778
// mentioned in the `Cluster.Spec.Class` field.
779+
// +required
775780
Class string `json:"class"`
776781

777782
// name is the unique identifier for this MachinePoolTopology.
778783
// The value is used with other unique identifiers to create a MachinePool's Name
779784
// (e.g. cluster's name, etc). In case the name is greater than the allowed maximum length,
780785
// the values are hashed together.
786+
// +required
781787
Name string `json:"name"`
782788

783789
// failureDomains is the list of failure domains the machine pool will be created in.
@@ -826,6 +832,7 @@ type MachinePoolTopology struct {
826832
// Variable definition in the ClusterClass `status` variables.
827833
type ClusterVariable struct {
828834
// name of the variable.
835+
// +required
829836
Name string `json:"name"`
830837

831838
// definitionFrom specifies where the definition of this Variable is from.
@@ -842,6 +849,7 @@ type ClusterVariable struct {
842849
// hard-coded schema for apiextensionsv1.JSON which cannot be produced by another type via controller-tools,
843850
// i.e. it is not possible to have no type field.
844851
// Ref: https://github.com/kubernetes-sigs/controller-tools/blob/d0e03a142d0ecdd5491593e941ee1d6b5d91dba6/pkg/crd/known_types.go#L106-L111
852+
// +required
845853
Value apiextensionsv1.JSON `json:"value"`
846854
}
847855

@@ -908,6 +916,7 @@ type ClusterNetwork struct {
908916
type NetworkRanges struct {
909917
// cidrBlocks is a list of CIDR blocks.
910918
// +kubebuilder:validation:MaxItems=100
919+
// +required
911920
CIDRBlocks []string `json:"cidrBlocks"`
912921
}
913922

@@ -1073,9 +1082,11 @@ func (c *ClusterStatus) GetTypedPhase() ClusterPhase {
10731082
// APIEndpoint represents a reachable Kubernetes API endpoint.
10741083
type APIEndpoint struct {
10751084
// host is the hostname on which the API server is serving.
1085+
// +required
10761086
Host string `json:"host"`
10771087

10781088
// port is the port on which the API server is serving.
1089+
// +required
10791090
Port int32 `json:"port"`
10801091
}
10811092

@@ -1110,11 +1121,14 @@ type Cluster struct {
11101121
metav1.TypeMeta `json:",inline"`
11111122
// metadata is the standard object's metadata.
11121123
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
1124+
// +optional
11131125
metav1.ObjectMeta `json:"metadata,omitempty"`
11141126

11151127
// spec is the desired state of Cluster.
1128+
// +optional
11161129
Spec ClusterSpec `json:"spec,omitempty"`
11171130
// status is the observed state of Cluster.
1131+
// +optional
11181132
Status ClusterStatus `json:"status,omitempty"`
11191133
}
11201134

@@ -1248,6 +1262,7 @@ type ClusterList struct {
12481262
metav1.TypeMeta `json:",inline"`
12491263
// metadata is the standard list's metadata.
12501264
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#lists-and-simple-kinds
1265+
// +optional
12511266
metav1.ListMeta `json:"metadata,omitempty"`
12521267
// items is the list of Clusters.
12531268
Items []Cluster `json:"items"`

api/v1beta1/clusterclass_types.go

+36
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ type ClusterClass struct {
7575
metav1.TypeMeta `json:",inline"`
7676
// metadata is the standard object's metadata.
7777
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
78+
// +optional
7879
metav1.ObjectMeta `json:"metadata,omitempty"`
7980

8081
// spec is the desired state of ClusterClass.
82+
// +optional
8183
Spec ClusterClassSpec `json:"spec,omitempty"`
8284
// status is the observed state of ClusterClass.
85+
// +optional
8386
Status ClusterClassStatus `json:"status,omitempty"`
8487
}
8588

@@ -255,10 +258,12 @@ type MachineDeploymentClass struct {
255258
// class denotes a type of worker node present in the cluster,
256259
// this name MUST be unique within a ClusterClass and can be referenced
257260
// in the Cluster to create a managed MachineDeployment.
261+
// +required
258262
Class string `json:"class"`
259263

260264
// template is a local struct containing a collection of templates for creation of
261265
// MachineDeployment objects representing a set of worker nodes.
266+
// +required
262267
Template MachineDeploymentClassTemplate `json:"template"`
263268

264269
// machineHealthCheck defines a MachineHealthCheck for this MachineDeploymentClass.
@@ -300,6 +305,7 @@ type MachineDeploymentClass struct {
300305
// Defaults to 0 (machine will be considered available as soon as it
301306
// is ready)
302307
// NOTE: This value can be overridden while defining a Cluster.Topology using this MachineDeploymentClass.
308+
// +optional
303309
MinReadySeconds *int32 `json:"minReadySeconds,omitempty"`
304310

305311
// readinessGates specifies additional conditions to include when evaluating Machine Ready condition.
@@ -319,6 +325,7 @@ type MachineDeploymentClass struct {
319325
// strategy is the deployment strategy to use to replace existing machines with
320326
// new ones.
321327
// NOTE: This value can be overridden while defining a Cluster.Topology using this MachineDeploymentClass.
328+
// +optional
322329
Strategy *MachineDeploymentStrategy `json:"strategy,omitempty"`
323330
}
324331

@@ -332,10 +339,12 @@ type MachineDeploymentClassTemplate struct {
332339

333340
// bootstrap contains the bootstrap template reference to be used
334341
// for the creation of worker Machines.
342+
// +required
335343
Bootstrap LocalObjectTemplate `json:"bootstrap"`
336344

337345
// infrastructure contains the infrastructure template reference to be used
338346
// for the creation of worker Machines.
347+
// +required
339348
Infrastructure LocalObjectTemplate `json:"infrastructure"`
340349
}
341350

@@ -410,10 +419,12 @@ type MachinePoolClass struct {
410419
// class denotes a type of machine pool present in the cluster,
411420
// this name MUST be unique within a ClusterClass and can be referenced
412421
// in the Cluster to create a managed MachinePool.
422+
// +required
413423
Class string `json:"class"`
414424

415425
// template is a local struct containing a collection of templates for creation of
416426
// MachinePools objects representing a pool of worker nodes.
427+
// +required
417428
Template MachinePoolClassTemplate `json:"template"`
418429

419430
// failureDomains is the list of failure domains the MachinePool should be attached to.
@@ -452,6 +463,7 @@ type MachinePoolClass struct {
452463
// Defaults to 0 (machine will be considered available as soon as it
453464
// is ready)
454465
// NOTE: This value can be overridden while defining a Cluster.Topology using this MachinePoolClass.
466+
// +optional
455467
MinReadySeconds *int32 `json:"minReadySeconds,omitempty"`
456468
}
457469

@@ -465,10 +477,12 @@ type MachinePoolClassTemplate struct {
465477

466478
// bootstrap contains the bootstrap template reference to be used
467479
// for the creation of the Machines in the MachinePool.
480+
// +required
468481
Bootstrap LocalObjectTemplate `json:"bootstrap"`
469482

470483
// infrastructure contains the infrastructure template reference to be used
471484
// for the creation of the MachinePool.
485+
// +required
472486
Infrastructure LocalObjectTemplate `json:"infrastructure"`
473487
}
474488

@@ -495,12 +509,14 @@ func (m MachineHealthCheckClass) IsZero() bool {
495509
// be configured in the Cluster topology and used in patches.
496510
type ClusterClassVariable struct {
497511
// name of the variable.
512+
// +required
498513
Name string `json:"name"`
499514

500515
// required specifies if the variable is required.
501516
// Note: this applies to the variable as a whole and thus the
502517
// top-level object defined in the schema. If nested fields are
503518
// required, this will be specified inside the schema.
519+
// +required
504520
Required bool `json:"required"`
505521

506522
// metadata is the metadata of a variable.
@@ -513,6 +529,7 @@ type ClusterClassVariable struct {
513529
Metadata ClusterClassVariableMetadata `json:"metadata,omitempty"`
514530

515531
// schema defines the schema of the variable.
532+
// +required
516533
Schema VariableSchema `json:"schema"`
517534
}
518535

@@ -539,6 +556,7 @@ type VariableSchema struct {
539556
// openAPIV3Schema defines the schema of a variable via OpenAPI v3
540557
// schema. The schema is a subset of the schema used in
541558
// Kubernetes CRDs.
559+
// +required
542560
OpenAPIV3Schema JSONSchemaProps `json:"openAPIV3Schema"`
543561
}
544562

@@ -549,9 +567,11 @@ type VariableSchema struct {
549567
// which are not supported in CAPI have been removed.
550568
type JSONSchemaProps struct {
551569
// description is a human-readable description of this variable.
570+
// +optional
552571
Description string `json:"description,omitempty"`
553572

554573
// example is an example for this variable.
574+
// +optional
555575
Example *apiextensionsv1.JSON `json:"example,omitempty"`
556576

557577
// type is the type of the variable.
@@ -871,9 +891,11 @@ const (
871891
// ClusterClassPatch defines a patch which is applied to customize the referenced templates.
872892
type ClusterClassPatch struct {
873893
// name of the patch.
894+
// +required
874895
Name string `json:"name"`
875896

876897
// description is a human-readable description of this patch.
898+
// +optional
877899
Description string `json:"description,omitempty"`
878900

879901
// enabledIf is a Go template to be used to calculate if a patch should be enabled.
@@ -900,12 +922,14 @@ type ClusterClassPatch struct {
900922
// PatchDefinition defines a patch which is applied to customize the referenced templates.
901923
type PatchDefinition struct {
902924
// selector defines on which templates the patch should be applied.
925+
// +required
903926
Selector PatchSelector `json:"selector"`
904927

905928
// jsonPatches defines the patches which should be applied on the templates
906929
// matching the selector.
907930
// Note: Patches will be applied in the order of the array.
908931
// +kubebuilder:validation:MaxItems=100
932+
// +required
909933
JSONPatches []JSONPatch `json:"jsonPatches"`
910934
}
911935

@@ -916,12 +940,15 @@ type PatchDefinition struct {
916940
// Note: The results of selection based on the individual fields are ANDed.
917941
type PatchSelector struct {
918942
// apiVersion filters templates by apiVersion.
943+
// +required
919944
APIVersion string `json:"apiVersion"`
920945

921946
// kind filters templates by kind.
947+
// +required
922948
Kind string `json:"kind"`
923949

924950
// matchResources selects templates based on where they are referenced.
951+
// +required
925952
MatchResources PatchSelectorMatch `json:"matchResources"`
926953
}
927954

@@ -972,13 +999,15 @@ type PatchSelectorMatchMachinePoolClass struct {
972999
type JSONPatch struct {
9731000
// op defines the operation of the patch.
9741001
// Note: Only `add`, `replace` and `remove` are supported.
1002+
// +required
9751003
Op string `json:"op"`
9761004

9771005
// path defines the path of the patch.
9781006
// Note: Only the spec of a template can be patched, thus the path has to start with /spec/.
9791007
// Note: For now the only allowed array modifications are `append` and `prepend`, i.e.:
9801008
// * for op: `add`: only index 0 (prepend) and - (append) are allowed
9811009
// * for op: `replace` or `remove`: no indexes are allowed
1010+
// +required
9821011
Path string `json:"path"`
9831012

9841013
// value defines the value of the patch.
@@ -1039,6 +1068,7 @@ type ExternalPatchDefinition struct {
10391068
type LocalObjectTemplate struct {
10401069
// ref is a required reference to a custom resource
10411070
// offered by a provider.
1071+
// +required
10421072
Ref *corev1.ObjectReference `json:"ref"`
10431073
}
10441074

@@ -1079,6 +1109,7 @@ type ClusterClassV1Beta2Status struct {
10791109
// ClusterClassStatusVariable defines a variable which appears in the status of a ClusterClass.
10801110
type ClusterClassStatusVariable struct {
10811111
// name is the name of the variable.
1112+
// +required
10821113
Name string `json:"name"`
10831114

10841115
// definitionsConflict specifies whether or not there are conflicting definitions for a single variable name.
@@ -1087,6 +1118,7 @@ type ClusterClassStatusVariable struct {
10871118

10881119
// definitions is a list of definitions for a variable.
10891120
// +kubebuilder:validation:MaxItems=100
1121+
// +required
10901122
Definitions []ClusterClassStatusVariableDefinition `json:"definitions"`
10911123
}
10921124

@@ -1095,12 +1127,14 @@ type ClusterClassStatusVariableDefinition struct {
10951127
// from specifies the origin of the variable definition.
10961128
// This will be `inline` for variables defined in the ClusterClass or the name of a patch defined in the ClusterClass
10971129
// for variables discovered from a DiscoverVariables runtime extensions.
1130+
// +required
10981131
From string `json:"from"`
10991132

11001133
// required specifies if the variable is required.
11011134
// Note: this applies to the variable as a whole and thus the
11021135
// top-level object defined in the schema. If nested fields are
11031136
// required, this will be specified inside the schema.
1137+
// +required
11041138
Required bool `json:"required"`
11051139

11061140
// metadata is the metadata of a variable.
@@ -1113,6 +1147,7 @@ type ClusterClassStatusVariableDefinition struct {
11131147
Metadata ClusterClassVariableMetadata `json:"metadata,omitempty"`
11141148

11151149
// schema defines the schema of the variable.
1150+
// +required
11161151
Schema VariableSchema `json:"schema"`
11171152
}
11181153

@@ -1151,6 +1186,7 @@ type ClusterClassList struct {
11511186
metav1.TypeMeta `json:",inline"`
11521187
// metadata is the standard list's metadata.
11531188
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#lists-and-simple-kinds
1189+
// +optional
11541190
metav1.ListMeta `json:"metadata,omitempty"`
11551191
// items is the list of ClusterClasses.
11561192
Items []ClusterClass `json:"items"`

api/v1beta1/common_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,11 @@ const (
292292
// MachineAddress contains information for the node's address.
293293
type MachineAddress struct {
294294
// type is the machine address type, one of Hostname, ExternalIP, InternalIP, ExternalDNS or InternalDNS.
295+
// +required
295296
Type MachineAddressType `json:"type"`
296297

297298
// address is the machine address.
299+
// +required
298300
Address string `json:"address"`
299301
}
300302

0 commit comments

Comments
 (0)