Skip to content

chore: modernize using slices, min builtin, and %w error wrapping#2916

Open
mrueg wants to merge 1 commit intokubernetes:mainfrom
mrueg:modernize-go-primitives
Open

chore: modernize using slices, min builtin, and %w error wrapping#2916
mrueg wants to merge 1 commit intokubernetes:mainfrom
mrueg:modernize-go-primitives

Conversation

@mrueg
Copy link
Copy Markdown
Member

@mrueg mrueg commented Apr 7, 2026

What this PR does / why we need it:

  • Replace sort.StringSlice + .Sort() with []string + slices.Sort()
  • Replace sort.Slice with slices.SortFunc (typed comparator)
  • Replace sort.Strings with slices.Sort across multiple packages
  • Replace math.Min(float64(len(...))) with min() builtin (Go 1.21)
  • Replace fmt.Errorf("%v", err) with "%w" to preserve error chains
  • Update test to compare error messages rather than concrete error types

How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)

Which issue(s) this PR fixes: (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged)
Fixes #

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mrueg

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

The pull request process is described here

Details 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

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Apr 7, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: mrueg / name: Manuel Rüger (90d1ad7)

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 7, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Apr 7, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in SIG Instrumentation Apr 7, 2026
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 7, 2026
@mrueg mrueg changed the title modernize: use slices, min builtin, and %w error wrapping chore: modernize using slices, min builtin, and %w error wrapping Apr 7, 2026
@mrueg mrueg force-pushed the modernize-go-primitives branch from 5baa5f1 to b01a72c Compare April 7, 2026 23:05
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 7, 2026
@mrueg mrueg requested a review from Copilot April 7, 2026 23:07
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Modernizes the Go codebase by adopting newer standard-library helpers for sorting, min selection, and error wrapping to improve determinism and error-chain preservation.

Changes:

  • Replace sort.* usage with slices.Sort / slices.SortFunc for deterministic ordering.
  • Replace math.Min(float64(len(...))) with the min() builtin.
  • Wrap returned errors with %w in several paths to preserve errors.Is/As behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/options/types.go Switches set stringification sorting to slices.Sort.
pkg/optin/optin.go Uses slices.Sort to keep opt-in status output deterministic.
pkg/metrics_store/metrics_writer.go Wraps write errors with %w for proper error chaining.
pkg/customresourcestate/registry_factory.go Modernizes sorting and min usage; wraps certain errors with %w.
pkg/customresourcestate/registry_factory_test.go Updates tests to compare error messages rather than concrete error types.
pkg/app/server.go Converts multiple error returns to %w wrapping.
internal/store/utils.go Updates label-key sorting to slices.Sort.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/customresourcestate/registry_factory.go Outdated
Comment on lines 220 to 223
func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error) {
onError := func(err error) {
errs = append(errs, fmt.Errorf("%s: %v", c.Path(), err))
errs = append(errs, fmt.Errorf("%s: %w", c.Path(), err))
}
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

PR description mentions switching fmt.Errorf("%v", err) to "%w" to preserve error chains. This file updates compiledGauge.Values to wrap with %w, but compiledInfo.Values still formats aggregated errors with %v (and therefore doesn’t preserve unwrap-able causes). Either update compiledInfo.Values similarly (e.g., by joining/wrapping underlying errors) or adjust the PR description/scope so it doesn’t imply full conversion.

Copilot uses AI. Check for mistakes.
Comment thread pkg/customresourcestate/registry_factory_test.go Outdated
@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. and removed cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 7, 2026
@mrueg mrueg requested a review from Copilot April 7, 2026 23:27
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Replace sort.StringSlice + .Sort() with []string + slices.Sort()
- Replace sort.Slice with slices.SortFunc (typed comparator)
- Replace sort.Strings with slices.Sort across multiple packages
- Replace math.Min(float64(len(...))) with min() builtin (Go 1.21)
- Replace fmt.Errorf("%v", err) with "%w" to preserve error chains
- Update test to compare error messages rather than concrete error types
@mrueg mrueg force-pushed the modernize-go-primitives branch from a47febd to 90d1ad7 Compare April 9, 2026 08:38
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 9, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

3 participants