Skip to content

golangci plugin for sorting feature gates #132438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dims
Copy link
Member

@dims dims commented Jun 21, 2025

We have a bunch of files where we want the var or const block with a bunch of variables/constants that need to be in the correct order for longer term maintainability.

In this PR, we are adding a golangci based linter in the following files:

  • cmd/kubeadm/app/features/features.go
  • pkg/features/kube_features.go
  • staging/src/k8s.io/apiserver/pkg/features/kube_features.go
  • staging/src/k8s.io/client-go/features/known_features.go
  • staging/src/k8s.io/controller-manager/pkg/features/kube_features.go
  • staging/src/k8s.io/apiextensions-apiserver/pkg/features/kube_features.go
  • test/e2e/feature/feature.go
  • test/e2e/environment/environment.go

There is an additional commit that sorts all the above files as well to ensure a clean run of the linter for posterity.

What type of PR is this?

xref: #130779

/kind cleanup

What this PR does / why we need it:

Which issue(s) this PR is related to:

Special notes for your reviewer:

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/apiserver area/dependency Issues or PRs related to dependency changes area/test sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/testing Categorizes an issue or PR as relevant to SIG Testing. approved Indicates a PR has been approved by an approver from all required OWNERS files. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jun 21, 2025
@dims dims force-pushed the golangci-plugin-for-sorting-feature-gates branch from b16f761 to 2c847d2 Compare June 21, 2025 11:45
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jun 21, 2025
@dims dims changed the title [WIP] golangci plugin for sorting feature gates golangci plugin for sorting feature gates + sort feature gates in various files Jun 21, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 21, 2025
@dims
Copy link
Member Author

dims commented Jun 21, 2025

/area code-organization

@k8s-ci-robot k8s-ci-robot added the area/code-organization Issues or PRs related to kubernetes code organization label Jun 21, 2025
@dims
Copy link
Member Author

dims commented Jun 21, 2025

/assign @aojea @pohly

@dims
Copy link
Member Author

dims commented Jun 21, 2025

/priority important-soon

This is a good time to add this as we just had the enhancement freeze and we have about 30+ days before code freeze.

@k8s-ci-robot k8s-ci-robot added priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 21, 2025
@dims dims force-pushed the golangci-plugin-for-sorting-feature-gates branch 2 times, most recently from 1bd4fde to 6dfdc9d Compare June 25, 2025 12:21
@dims dims changed the title [WIP] golangci plugin for sorting feature gates golangci plugin for sorting feature gates Jun 25, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 25, 2025
@dims
Copy link
Member Author

dims commented Jun 25, 2025

/retest

@dims dims force-pushed the golangci-plugin-for-sorting-feature-gates branch from 6dfdc9d to dc1015e Compare June 25, 2025 14:16
@dims
Copy link
Member Author

dims commented Jun 25, 2025

@pohly here's the output after i unsorted a few things in pkg/features/features.go

ERROR: pkg/features/kube_features.go:41:1: not sorted alphabetically:
ERROR:          // Allow kubelet to request a certificate without any Node IP available, only
ERROR:          // with DNS names.
ERROR:          AllowDNSOnlyNodeCSR = value
ERROR: -
ERROR: -        // owner: @krmayankk
ERROR: -        //
ERROR: -        // Enables maxUnavailable for StatefulSet
ERROR: -        MaxUnavailableStatefulSet = value
ERROR:
ERROR:          // owner: @micahhausler
ERROR:          //
ERROR: @@ -94,6 +89,12 @@
ERROR:          //
ERROR:          // Enable ClusterTrustBundle Kubelet projected volumes.  Depends on ClusterTrustBundle.
ERROR:          ClusterTrustBundleProjection = value
ERROR: +
ERROR: +        // owner: @adrianreber
ERROR: +        // kep: https://kep.k8s.io/2008
ERROR: +        //
ERROR: +        // Enables container Checkpoint support in the kubelet
ERROR: +        ContainerCheckpoint = value
ERROR:
ERROR:          // owner: @sreeram-venkitesh
ERROR:          //
ERROR: @@ -397,12 +398,6 @@
ERROR:          // Enable POD resources API to return resources allocated by Dynamic Resource Allocation
ERROR:          KubeletPodResourcesDynamicResources = value
ERROR:
ERROR: -        // owner: @adrianreber
ERROR: -        // kep: https://kep.k8s.io/2008
ERROR: -        //
ERROR: -        // Enables container Checkpoint support in the kubelet
ERROR: -        ContainerCheckpoint = value
ERROR: -
ERROR:          // owner: @moshe010
ERROR:          //
ERROR:          // Enable POD resources API with Get method
ERROR: @@ -476,6 +471,11 @@
ERROR:          //
ERROR:          // Enable merging key-value labels into LabelSelector corresponding to MatchLabelKeys in PodTopologySpread.
ERROR:          MatchLabelKeysInPodTopologySpreadSelectorMerge = value
ERROR: +
ERROR: +        // owner: @krmayankk
ERROR: +        //
ERROR: +        // Enables maxUnavailable for StatefulSet
ERROR: +        MaxUnavailableStatefulSet = value
ERROR:
ERROR:          // owner: @cynepco3hahue(alukiano) @cezaryzukowski @k-wiatrzyk, @Tal-or (only for GA graduation)
ERROR:          //
ERROR: Run hack/update-sortfeatures.sh to fix (sortedfeatures)
ERROR: const (
ERROR: ^
ERROR: pkg/features/kube_features.go:984:1: map 'defaultVersionedKubernetesFeatureGates' keys not sorted alphabetically:
ERROR:          ClusterTrustBundle: ...,
ERROR:          ClusterTrustBundleProjection: ...,
ERROR:          ContainerCheckpoint: ...,
ERROR: -        NodeLogQuery: ...,
ERROR:          ContainerStopSignals: ...,
ERROR:          CronJobsScheduledAnnotation: ...,
ERROR:          CrossNamespaceVolumeDataSource: ...,
ERROR: @@ -53,6 +52,7 @@
ERROR:          KubeletCgroupDriverFromCRI: ...,
ERROR:          KubeletCrashLoopBackOffMax: ...,
ERROR:          KubeletEnsureSecretPulledImages: ...,
ERROR: +        KubeletFineGrainedAuthz: ...,
ERROR:          KubeletInUserNamespace: ...,
ERROR:          KubeletPSI: ...,
ERROR:          KubeletPodResourcesDynamicResources: ...,
ERROR: @@ -70,12 +70,12 @@
ERROR:          MatchLabelKeysInPodTopologySpreadSelectorMerge: ...,
ERROR:          MaxUnavailableStatefulSet: ...,
ERROR:          MemoryManager: ...,
ERROR: -        KubeletFineGrainedAuthz: ...,
ERROR:          MemoryQoS: ...,
ERROR:          MultiCIDRServiceAllocator: ...,
ERROR:          MutableCSINodeAllocatableCount: ...,
ERROR:          NFTablesProxyMode: ...,
ERROR:          NodeInclusionPolicyInPodTopologySpread: ...,
ERROR: +        NodeLogQuery: ...,
ERROR:          NodeSwap: ...,
ERROR:          OrderedNamespaceDeletion: ...,
ERROR:          PodAndContainerStatsFromCRI: ...,
ERROR: Run hack/update-sortfeatures.sh to fix (sortedfeatures)

@dims
Copy link
Member Author

dims commented Jun 25, 2025

@pohly please see output above. I think it is good enough to land now so i have removed the WIP. thanks!

@yongruilin
Copy link
Contributor

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 26, 2025
@dims dims force-pushed the golangci-plugin-for-sorting-feature-gates branch from dc1015e to fd9b598 Compare June 27, 2025 00:34
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 27, 2025
@dims
Copy link
Member Author

dims commented Jun 27, 2025

/retest

@dims dims force-pushed the golangci-plugin-for-sorting-feature-gates branch from 7211621 to 4e47e2c Compare June 27, 2025 15:07
@dims dims force-pushed the golangci-plugin-for-sorting-feature-gates branch from 4e47e2c to d50e1a6 Compare June 27, 2025 15:14
Copy link
Contributor

@pohly pohly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm


## How It Works

The linter analyzes const and var blocks in specified files, extracts feature declarations, and checks if they are
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO (later): document support for maps.

@@ -0,0 +1,137 @@
# SortedFeatures Linter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO (later): remove the "feature" part of the linter and turn it into a generic "sorted" linter:

  • rename to "sorted"
  • remove all references to "feature" from the doc and code

}

// Report the issue with the diff
pass.Reportf(decl.Pos(), "not sorted alphabetically:\n%s\n", stripHeader(diffText, 3))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO (later): "not sorted alphabetically (-got, +want):"

Example:

ERROR: test/e2e/feature/feature.go:27:1: not sorted alphabetically:
ERROR:  var (
ERROR: +	// TODO: document the feature (owning SIG, when to use this feature for a test)
ERROR: +	APIServerIdentity = value
ERROR: +
ERROR:  	// Owner: sig-lifecycle
ERROR:  	// This label is used for tests which need the following controllers to be enabled:
ERROR:  	// - bootstrap-signer-controller
ERROR: @@ -6,62 +9,11 @@
ERROR:  	BootstrapTokens = value
ERROR:  
ERROR:  	// TODO: document the feature (owning SIG, when to use this feature for a test)
ERROR: -	APIServerIdentity = value
ERROR: -
ERROR: -	// TODO: document the feature (owning SIG, when to use this feature for a test)
ERROR:  	BoundServiceAccountTokenVolume = value
ERROR:  
ERROR:  	// Owner: sig-api-machinery
ERROR:  	// Marks tests that exercise the CBOR data format for serving or storage.
ERROR:  	CBOR = value

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 27, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: ef7b67b9237f9df0bff5032cfdc409799174fbcb

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dims, pohly

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@dims
Copy link
Member Author

dims commented Jun 27, 2025

/retest

1 similar comment
@dims
Copy link
Member Author

dims commented Jun 27, 2025

/retest

@k8s-ci-robot k8s-ci-robot merged commit 2e02a00 into kubernetes:master Jun 27, 2025
16 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.34 milestone Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/apiserver area/code-organization Issues or PRs related to kubernetes code organization area/dependency Issues or PRs related to dependency changes area/kubeadm area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/cloud-provider Categorizes an issue or PR as relevant to SIG Cloud Provider. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants