From 610ca77fc979bc151403f476536f76a1f358f8c7 Mon Sep 17 00:00:00 2001 From: Ignoramuss Date: Mon, 8 Dec 2025 07:55:06 -0800 Subject: [PATCH] api: enable optionalorrequired linter for scheduling API Enable the optionalorrequired linter rule and add exception patterns for all other API groups except scheduling. This ensures the scheduling API group is validated for proper +optional/+required markers. Part of #134671 --- hack/golangci-hints.yaml | 18 +++++++++++++----- hack/golangci.yaml | 18 +++++++++++++----- hack/kube-api-linter/exceptions.yaml | 10 +++++++++- hack/kube-api-linter/kube-api-linter.yaml | 2 +- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/hack/golangci-hints.yaml b/hack/golangci-hints.yaml index 5a823fa884b..d68e6ef1420 100644 --- a/hack/golangci-hints.yaml +++ b/hack/golangci-hints.yaml @@ -177,7 +177,7 @@ linters: # The following exceptions are for fields in stable or deprecated APIs that cannot be # changed due to backward compatibility constraints. Each rule is scoped to the # specific field to avoid hiding new violations. - + ## For the "Extra" field, which is common across several auth-related APIs. - text: "field UserInfo.Extra should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/authentication/(v1|v1beta1)/types.go" @@ -185,7 +185,7 @@ linters: path: "staging/src/k8s.io/api/authorization/(v1|v1beta1)/types.go" - text: "field CertificateSigningRequestSpec.Extra should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/certificates/(v1|v1beta1)/types.go" - + ## For ResourceList fields in the core API. - text: "field (PersistentVolumeSpec.Capacity|ContainerStatus.AllocatedResources|PodSpec.Overhead|ResourceQuotaSpec.Hard) type ResourceList should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/core/v1/types.go" @@ -197,11 +197,11 @@ linters: path: "staging/src/k8s.io/api/core/v1/types.go" - text: "type ResourceList should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/core/v1/types.go" - + ## For the Secret.Data and ConfigMap.BinaryData fields in the core API. - text: "field (Secret.Data|ConfigMap.BinaryData) should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/core/v1/types.go" - + ## For map fields in the resource API (across all versions). - text: "field (CounterSet|DeviceCounterConsumption).Counters should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/resource/(v1|v1beta1|v1beta2)/types.go" @@ -212,6 +212,14 @@ linters: - text: "field (CapacityRequirements.Requests|DeviceRequestAllocationResult.ConsumedCapacity) should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/resource/(v1|v1beta1|v1beta2)/types.go" + # OptionalOrRequired is being enabled over time. For now, each API group should be added to this list until we comb through each group and fix the missing tags. + - text: "must be marked as optional or required" + path: "staging/src/k8s.io/api/(admission|apidiscovery|apiserverinternal|apps|authentication|authorization|autoscaling|batch|certificates|coordination|core|discovery|events|extensions|flowcontrol|imagepolicy|networking|node|policy|rbac|resource|storage|storagemigration)" + + # OptionalOrRequired - Existing fields that are marked as both optional and required (based on standard optional vs kubebuilder:validation:Required) and should not be fixed. + - text: "field (PortStatus|IngressPortStatus)\\.Error must not be marked as both optional and required" + path: "staging/src/k8s.io/api/(core/v1|extensions/v1beta1|networking/(v1|v1beta1))" + default: standard enable: # please keep this alphabetized - depguard @@ -372,7 +380,7 @@ linters: # - "nophase" # Ensure field names do not have the word "phase" in them. - "notimestamp" # Ensure fields are not named "timestamp", prefer "time". # - "optionalfields" # Ensure fields marked optional have omitempty and pointers. - # - "optionalorrequired" # Every field should be marked as `+optional` xor `+required`. + - "optionalorrequired" # Every field should be marked as `+optional` xor `+required`. # - "requiredfields" # Required fields should only be pointers when required based on the validity of the zero value, they should always have `omitempty`. - "ssatags" # Ensure lists have a listType tag. # - "uniquemarkers" # Ensure markers are not duplicated across field and type definitions. diff --git a/hack/golangci.yaml b/hack/golangci.yaml index d9fca382ba0..f3557df3dc8 100644 --- a/hack/golangci.yaml +++ b/hack/golangci.yaml @@ -188,7 +188,7 @@ linters: # The following exceptions are for fields in stable or deprecated APIs that cannot be # changed due to backward compatibility constraints. Each rule is scoped to the # specific field to avoid hiding new violations. - + ## For the "Extra" field, which is common across several auth-related APIs. - text: "field UserInfo.Extra should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/authentication/(v1|v1beta1)/types.go" @@ -196,7 +196,7 @@ linters: path: "staging/src/k8s.io/api/authorization/(v1|v1beta1)/types.go" - text: "field CertificateSigningRequestSpec.Extra should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/certificates/(v1|v1beta1)/types.go" - + ## For ResourceList fields in the core API. - text: "field (PersistentVolumeSpec.Capacity|ContainerStatus.AllocatedResources|PodSpec.Overhead|ResourceQuotaSpec.Hard) type ResourceList should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/core/v1/types.go" @@ -208,11 +208,11 @@ linters: path: "staging/src/k8s.io/api/core/v1/types.go" - text: "type ResourceList should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/core/v1/types.go" - + ## For the Secret.Data and ConfigMap.BinaryData fields in the core API. - text: "field (Secret.Data|ConfigMap.BinaryData) should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/core/v1/types.go" - + ## For map fields in the resource API (across all versions). - text: "field (CounterSet|DeviceCounterConsumption).Counters should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/resource/(v1|v1beta1|v1beta2)/types.go" @@ -223,6 +223,14 @@ linters: - text: "field (CapacityRequirements.Requests|DeviceRequestAllocationResult.ConsumedCapacity) should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/resource/(v1|v1beta1|v1beta2)/types.go" + # OptionalOrRequired is being enabled over time. For now, each API group should be added to this list until we comb through each group and fix the missing tags. + - text: "must be marked as optional or required" + path: "staging/src/k8s.io/api/(admission|apidiscovery|apiserverinternal|apps|authentication|authorization|autoscaling|batch|certificates|coordination|core|discovery|events|extensions|flowcontrol|imagepolicy|networking|node|policy|rbac|resource|storage|storagemigration)" + + # OptionalOrRequired - Existing fields that are marked as both optional and required (based on standard optional vs kubebuilder:validation:Required) and should not be fixed. + - text: "field (PortStatus|IngressPortStatus)\\.Error must not be marked as both optional and required" + path: "staging/src/k8s.io/api/(core/v1|extensions/v1beta1|networking/(v1|v1beta1))" + default: none enable: # please keep this alphabetized - depguard @@ -381,7 +389,7 @@ linters: # - "nophase" # Ensure field names do not have the word "phase" in them. - "notimestamp" # Ensure fields are not named "timestamp", prefer "time". # - "optionalfields" # Ensure fields marked optional have omitempty and pointers. - # - "optionalorrequired" # Every field should be marked as `+optional` xor `+required`. + - "optionalorrequired" # Every field should be marked as `+optional` xor `+required`. # - "requiredfields" # Required fields should only be pointers when required based on the validity of the zero value, they should always have `omitempty`. - "ssatags" # Ensure lists have a listType tag. # - "uniquemarkers" # Ensure markers are not duplicated across field and type definitions. diff --git a/hack/kube-api-linter/exceptions.yaml b/hack/kube-api-linter/exceptions.yaml index 5ccf3c9da57..037269b40d1 100644 --- a/hack/kube-api-linter/exceptions.yaml +++ b/hack/kube-api-linter/exceptions.yaml @@ -86,4 +86,12 @@ - text: "field Device.(Attributes|Capacity) should not use a map type, use a list type with a unique name/identifier instead" path: "staging/src/k8s.io/api/resource/(v1|v1beta2)/types.go" - text: "field (CapacityRequirements.Requests|DeviceRequestAllocationResult.ConsumedCapacity) should not use a map type, use a list type with a unique name/identifier instead" - path: "staging/src/k8s.io/api/resource/(v1|v1beta1|v1beta2)/types.go" \ No newline at end of file + path: "staging/src/k8s.io/api/resource/(v1|v1beta1|v1beta2)/types.go" + +# OptionalOrRequired is being enabled over time. For now, each API group should be added to this list until we comb through each group and fix the missing tags. +- text: "must be marked as optional or required" + path: "staging/src/k8s.io/api/(admission|apidiscovery|apiserverinternal|apps|authentication|authorization|autoscaling|batch|certificates|coordination|core|discovery|events|extensions|flowcontrol|imagepolicy|networking|node|policy|rbac|resource|storage|storagemigration)" + +# OptionalOrRequired - Existing fields that are marked as both optional and required (based on standard optional vs kubebuilder:validation:Required) and should not be fixed. +- text: "field (PortStatus|IngressPortStatus)\\.Error must not be marked as both optional and required" + path: "staging/src/k8s.io/api/(core/v1|extensions/v1beta1|networking/(v1|v1beta1))" diff --git a/hack/kube-api-linter/kube-api-linter.yaml b/hack/kube-api-linter/kube-api-linter.yaml index cd47d6e7039..1a221c5e617 100644 --- a/hack/kube-api-linter/kube-api-linter.yaml +++ b/hack/kube-api-linter/kube-api-linter.yaml @@ -15,7 +15,7 @@ linters: # - "nophase" # Ensure field names do not have the word "phase" in them. - "notimestamp" # Ensure fields are not named "timestamp", prefer "time". # - "optionalfields" # Ensure fields marked optional have omitempty and pointers. - # - "optionalorrequired" # Every field should be marked as `+optional` xor `+required`. + - "optionalorrequired" # Every field should be marked as `+optional` xor `+required`. # - "requiredfields" # Required fields should only be pointers when required based on the validity of the zero value, they should always have `omitempty`. - "ssatags" # Ensure lists have a listType tag. # - "uniquemarkers" # Ensure markers are not duplicated across field and type definitions.