Skip to content

Commit 5e7b12d

Browse files
authored
chore: kube-api-linter make maxlength validation more strict (#2742)
1 parent 669fccd commit 5e7b12d

32 files changed

+521
-21
lines changed

.golangci-kube-api.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,10 @@ linters:
125125
- linters:
126126
- kubeapilinter
127127
path: api/(konnect/v1alpha(1|2)|gateway-operator/v2beta1)/.*
128-
text: 'maxlength: .*'
128+
text: 'maxlength: field .* must have a maximum items, add kubebuilder:validation:MaxItems marker'
129+
130+
# TODO: remove this.
131+
- linters:
132+
- kubeapilinter
133+
path: api/(common/v1alpha1|konnect/v1alpha1|konnect/v1alpha2)/.*
134+
text: 'maxlength: field .* must have a maximum length, add kubebuilder:validation:(items:)?MaxLength marker'

api/gateway-operator/v1beta1/controlplane_conversion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (c *ControlPlaneOptions) convertTo(dst *operatorv2beta1.ControlPlaneOptions
216216
return err
217217
}
218218

219-
storageState, err := parseEnvForToggle[operatorv2beta1.ControlPlaneKonnectLicensingState](envControllerEnableKonnectLicensingStorage, containerEnvVars)
219+
storageState, err := parseEnvForToggle[operatorv2beta1.ControlPlaneKonnectLicenseStorageState](envControllerEnableKonnectLicensingStorage, containerEnvVars)
220220
if err != nil {
221221
return err
222222
}

api/gateway-operator/v2beta1/controlplane_types.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type ControlPlaneOptions struct {
101101
// If omitted, Ingress resources will not be supported by the ControlPlane.
102102
//
103103
// +optional
104+
// +kubebuilder:validation:MaxLength=63
104105
IngressClass *string `json:"ingressClass,omitempty"`
105106

106107
// WatchNamespaces indicates the namespaces to watch for resources.
@@ -253,6 +254,8 @@ type ControlPlaneDataPlaneSync struct {
253254
}
254255

255256
// ControlPlaneReverseSyncState defines the state of the reverse sync feature.
257+
//
258+
// +kubebuilder:validation:Enum=enabled;disabled
256259
type ControlPlaneReverseSyncState string
257260

258261
const (
@@ -304,6 +307,7 @@ type ControlPlaneDataPlaneTargetRef struct {
304307
//
305308
// +required
306309
// +kubebuilder:validation:MinLength=1
310+
// +kubebuilder:validation:MaxLength=63
307311
Name string `json:"name"`
308312
}
309313

@@ -415,6 +419,7 @@ type ControlPlaneController struct {
415419
//
416420
// +required
417421
// +kubebuilder:validation:MinLength=1
422+
// +kubebuilder:validation:MaxLength=63
418423
Name string `json:"name"`
419424

420425
// State indicates whether the feature gate is enabled or disabled.
@@ -425,6 +430,8 @@ type ControlPlaneController struct {
425430
}
426431

427432
// FeatureGateState defines the state of a feature gate.
433+
//
434+
// +kubebuilder:validation:Enum=enabled;disabled
428435
type FeatureGateState string
429436

430437
const (
@@ -443,12 +450,12 @@ type ControlPlaneFeatureGate struct {
443450
//
444451
// +required
445452
// +kubebuilder:validation:MinLength=1
453+
// +kubebuilder:validation:MaxLength=63
446454
Name string `json:"name"`
447455

448456
// State indicates whether the feature gate is enabled or disabled.
449457
//
450458
// +required
451-
// +kubebuilder:validation:Enum=enabled;disabled
452459
State FeatureGateState `json:"state"`
453460
}
454461

@@ -497,6 +504,7 @@ type ControlPlaneDataPlaneStatus struct {
497504
//
498505
// +required
499506
// +kubebuilder:validation:MinLength=1
507+
// +kubebuilder:validation:MaxLength=63
500508
Name string `json:"name"`
501509
}
502510

@@ -523,7 +531,6 @@ type ControlPlaneKonnectOptions struct {
523531
//
524532
// +optional
525533
// +kubebuilder:default=enabled
526-
// +kubebuilder:validation:Enum=enabled;disabled
527534
ConsumersSync *ControlPlaneKonnectConsumersSyncState `json:"consumersSync,omitempty"`
528535

529536
// Licensing defines the configuration for Konnect licensing.
@@ -543,6 +550,8 @@ type ControlPlaneKonnectOptions struct {
543550
}
544551

545552
// ControlPlaneKonnectConsumersSyncState defines the state of consumer synchronization with Konnect.
553+
//
554+
// +kubebuilder:validation:Enum=enabled;disabled
546555
type ControlPlaneKonnectConsumersSyncState string
547556

548557
const (
@@ -563,7 +572,6 @@ type ControlPlaneKonnectLicensing struct {
563572
//
564573
// +optional
565574
// +kubebuilder:default=disabled
566-
// +kubebuilder:validation:Enum=enabled;disabled
567575
State *ControlPlaneKonnectLicensingState `json:"state,omitempty"`
568576

569577
// InitialPollingPeriod is the initial polling period for license checks.
@@ -582,11 +590,24 @@ type ControlPlaneKonnectLicensing struct {
582590
//
583591
// +optional
584592
// +kubebuilder:default=enabled
585-
// +kubebuilder:validation:Enum=enabled;disabled
586-
StorageState *ControlPlaneKonnectLicensingState `json:"storageState,omitempty"`
593+
StorageState *ControlPlaneKonnectLicenseStorageState `json:"storageState,omitempty"`
587594
}
588595

596+
// ControlPlaneKonnectLicenseStorageState defines the state of Konnect licensing.
597+
//
598+
// +kubebuilder:validation:Enum=enabled;disabled
599+
type ControlPlaneKonnectLicenseStorageState string
600+
601+
const (
602+
// ControlPlaneKonnectLicenseStorageStateEnabled indicates that Konnect license storage is enabled.
603+
ControlPlaneKonnectLicenseStorageStateEnabled ControlPlaneKonnectLicenseStorageState = "enabled"
604+
// ControlPlaneKonnectLicenseStorageStateDisabled indicates that Konnect license storage is disabled.
605+
ControlPlaneKonnectLicenseStorageStateDisabled ControlPlaneKonnectLicenseStorageState = "disabled"
606+
)
607+
589608
// ControlPlaneKonnectLicensingState defines the state of Konnect licensing.
609+
//
610+
// +kubebuilder:validation:Enum=enabled;disabled
590611
type ControlPlaneKonnectLicensingState string
591612

592613
const (

api/gateway-operator/v2beta1/gatewayconfiguration_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ type GatewayConfigDataPlaneOptions struct {
155155
// use this GatewayConfig.
156156
//
157157
// +optional
158+
// +kubebuilder:validation:MaxItems=32
158159
PluginsToInstall []NamespacedName `json:"pluginsToInstall,omitempty"`
159160
}
160161

api/gateway-operator/v2beta1/shared_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ type HorizontalScaling struct {
7171
// increased, and vice-versa. See the individual metric source types for
7272
// more information about how each type of metric must respond.
7373
// If not set, the default metric will be set to 80% average CPU utilization.
74+
//
7475
// +listType=atomic
7576
// +optional
77+
// +kubebuilder:validation:MaxItems=8
7678
Metrics []autoscalingv2.MetricSpec `json:"metrics,omitempty" protobuf:"bytes,4,rep,name=metrics"`
7779

7880
// behavior configures the scaling behavior of the target
@@ -250,11 +252,13 @@ type NamespacedName struct {
250252
//
251253
// +required
252254
// +kubebuilder:validation:MinLength=1
255+
// +kubebuilder:validation:MaxLength=63
253256
Name string `json:"name"`
254257

255258
// Namespace is the namespace of the resource.
256259
//
257260
// +optional
261+
// +kubebuilder:validation:MaxLength=63
258262
Namespace string `json:"namespace"`
259263
}
260264

@@ -288,6 +292,7 @@ type ServiceOptions struct {
288292
// If Name is empty, the controller will generate a service name from the owning object.
289293
//
290294
// +optional
295+
// +kubebuilder:validation:MaxLength=63
291296
Name *string `json:"name,omitempty"`
292297

293298
// Annotations is an unstructured key value map stored with a resource that may be

api/gateway-operator/v2beta1/watch_namespaces_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type WatchNamespaces struct {
1616
// Only used when Type is set to List.
1717
//
1818
// +optional
19+
// +kubebuilder:validation:MaxItems=64
20+
// +kubebuilder:validation:items:MaxLength=64
1921
List []string `json:"list,omitempty"`
2022
}
2123

api/gateway-operator/v2beta1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/test/conversion/gateway-operator.konghq.com/v1beta1/controlplane_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ func TestControlPlane_RoundTrip(t *testing.T) {
482482
State: lo.ToPtr(operatorv2beta1.ControlPlaneKonnectLicensingStateEnabled),
483483
InitialPollingPeriod: &metav1.Duration{Duration: 10 * time.Second},
484484
PollingPeriod: &metav1.Duration{Duration: 60 * time.Second},
485-
StorageState: lo.ToPtr(operatorv2beta1.ControlPlaneKonnectLicensingStateDisabled),
485+
StorageState: lo.ToPtr(operatorv2beta1.ControlPlaneKonnectLicenseStorageStateDisabled),
486486
},
487487
NodeRefreshPeriod: &metav1.Duration{Duration: 30 * time.Second},
488488
ConfigUploadPeriod: &metav1.Duration{Duration: 10 * time.Second},

api/test/conversion/gateway-operator.konghq.com/v1beta1/gatewayconfiguration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func TestGatewayConfiguration_RoundTrip(t *testing.T) {
502502
State: lo.ToPtr(operatorv2beta1.ControlPlaneKonnectLicensingStateEnabled),
503503
InitialPollingPeriod: &metav1.Duration{Duration: 10 * time.Second},
504504
PollingPeriod: &metav1.Duration{Duration: 60 * time.Second},
505-
StorageState: lo.ToPtr(operatorv2beta1.ControlPlaneKonnectLicensingStateDisabled),
505+
StorageState: lo.ToPtr(operatorv2beta1.ControlPlaneKonnectLicenseStorageStateDisabled),
506506
},
507507
NodeRefreshPeriod: &metav1.Duration{Duration: 30 * time.Second},
508508
ConfigUploadPeriod: &metav1.Duration{Duration: 10 * time.Second},

charts/kong-operator/charts/ko-crds/templates/ko-crds.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9456,6 +9456,7 @@ spec:
94569456
properties:
94579457
name:
94589458
description: Name is the name of the controller.
9459+
maxLength: 63
94599460
minLength: 1
94609461
type: string
94619462
state:
@@ -9487,6 +9488,7 @@ spec:
94879488
properties:
94889489
name:
94899490
description: Ref is the name of the DataPlane to configure.
9491+
maxLength: 63
94909492
minLength: 1
94919493
type: string
94929494
required:
@@ -9519,6 +9521,9 @@ spec:
95199521
description: |-
95209522
ReverseSync sends configuration to DataPlane (Kong Gateway) even if
95219523
the configuration checksum has not changed since previous update.
9524+
enum:
9525+
- enabled
9526+
- disabled
95229527
type: string
95239528
timeout:
95249529
description: Timeout is the timeout of a single run of syncing
@@ -9580,6 +9585,7 @@ spec:
95809585
properties:
95819586
name:
95829587
description: Name is the name of the feature gate.
9588+
maxLength: 63
95839589
minLength: 1
95849590
type: string
95859591
state:
@@ -9620,6 +9626,7 @@ spec:
96209626
which Ingress resources this ControlPlane should be responsible for.
96219627

96229628
If omitted, Ingress resources will not be supported by the ControlPlane.
9629+
maxLength: 63
96239630
type: string
96249631
konnect:
96259632
description: Konnect defines the Konnect-related configuration options
@@ -9775,7 +9782,9 @@ spec:
97759782
List is a list of namespaces to watch for resources.
97769783
Only used when Type is set to List.
97779784
items:
9785+
maxLength: 64
97789786
type: string
9787+
maxItems: 64
97799788
type: array
97809789
type:
97819790
description: |-
@@ -9881,6 +9890,7 @@ spec:
98819890
properties:
98829891
name:
98839892
description: Name is the name of the controller.
9893+
maxLength: 63
98849894
minLength: 1
98859895
type: string
98869896
state:
@@ -9906,6 +9916,7 @@ spec:
99069916
properties:
99079917
name:
99089918
description: Name is the name of the DataPlane.
9919+
maxLength: 63
99099920
minLength: 1
99109921
type: string
99119922
required:
@@ -9921,6 +9932,7 @@ spec:
99219932
properties:
99229933
name:
99239934
description: Name is the name of the feature gate.
9935+
maxLength: 63
99249936
minLength: 1
99259937
type: string
99269938
state:
@@ -38679,6 +38691,7 @@ spec:
3867938691
properties:
3868038692
name:
3868138693
description: Name is the name of the controller.
38694+
maxLength: 63
3868238695
minLength: 1
3868338696
type: string
3868438697
state:
@@ -38710,6 +38723,9 @@ spec:
3871038723
description: |-
3871138724
ReverseSync sends configuration to DataPlane (Kong Gateway) even if
3871238725
the configuration checksum has not changed since previous update.
38726+
enum:
38727+
- enabled
38728+
- disabled
3871338729
type: string
3871438730
timeout:
3871538731
description: Timeout is the timeout of a single run of syncing
@@ -38726,6 +38742,7 @@ spec:
3872638742
properties:
3872738743
name:
3872838744
description: Name is the name of the feature gate.
38745+
maxLength: 63
3872938746
minLength: 1
3873038747
type: string
3873138748
state:
@@ -38766,6 +38783,7 @@ spec:
3876638783
which Ingress resources this ControlPlane should be responsible for.
3876738784

3876838785
If omitted, Ingress resources will not be supported by the ControlPlane.
38786+
maxLength: 63
3876938787
type: string
3877038788
konnect:
3877138789
description: Konnect defines the Konnect-related configuration
@@ -38924,7 +38942,9 @@ spec:
3892438942
List is a list of namespaces to watch for resources.
3892538943
Only used when Type is set to List.
3892638944
items:
38945+
maxLength: 64
3892738946
type: string
38947+
maxItems: 64
3892838948
type: array
3892938949
type:
3893038950
description: |-
@@ -48280,6 +48300,7 @@ spec:
4828048300
required:
4828148301
- type
4828248302
type: object
48303+
maxItems: 8
4828348304
type: array
4828448305
x-kubernetes-list-type: atomic
4828548306
minReplicas:
@@ -48353,6 +48374,7 @@ spec:
4835348374
description: |-
4835448375
Name defines the name of the service.
4835548376
If Name is empty, the controller will generate a service name from the owning object.
48377+
maxLength: 63
4835648378
type: string
4835748379
type:
4835848380
default: LoadBalancer
@@ -48396,14 +48418,17 @@ spec:
4839648418
properties:
4839748419
name:
4839848420
description: Name is the name of the resource.
48421+
maxLength: 63
4839948422
minLength: 1
4840048423
type: string
4840148424
namespace:
4840248425
description: Namespace is the namespace of the resource.
48426+
maxLength: 63
4840348427
type: string
4840448428
required:
4840548429
- name
4840648430
type: object
48431+
maxItems: 32
4840748432
type: array
4840848433
resources:
4840948434
description: |-

0 commit comments

Comments
 (0)