fix(options): validate flags that are currently accepted at any value - #3062
fix(options): validate flags that are currently accepted at any value#3062mrueg wants to merge 4 commits into
Conversation
Validate returned early when --node was empty, so the two checks below that guard only ever ran for node-scoped deployments. In the ordinary cluster-wide setup --auto-gomemlimit-ratio and --object-limit were accepted at any value, despite their flag help documenting the bounds: a ratio above 1 reached memlimit.SetGoMemLimitWithOpts and a negative object limit reached the list options. Scope the resource check to the node case and let the rest run always.
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
This issue is currently awaiting triage. If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the The DetailsInstructions 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. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
ChangesValidation scope and shard constraints
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/options/options.go`:
- Around line 209-216: Update Validate for AutoGoMemlimitRatio to reject NaN by
replacing the current comparisons with an inverted range check that treats
values outside the valid bounds, including NaN, as invalid. Add a math.NaN()
table-driven validation case alongside the existing ratio cases.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 6f0ea12e-405e-4b1b-854d-c2319fb9b25f
📒 Files selected for processing (2)
pkg/options/options.gopkg/options/options_test.go
The shard an object belongs to is jump.Hash(fnv64a(uid), totalShards) compared against the configured shard index. jump.Hash returns -1 for a non-positive bucket count, and no shard index can equal an out-of-range one, so --total-shards=0 or --shard past the end filters out every object. Neither is rejected today: the process starts, lists and watches every object at full cost, and serves an empty /metrics with HTTP 200 and nothing logged. Validate them at startup instead. Autosharding is unaffected -- it sets these through ConfigureSharding at runtime rather than through the flags this validates.
NaN parses as a valid float64, and every comparison against it is false, so it passed both bounds of the range check and reached memlimit.WithRatio. --auto-gomemlimit-ratio=NaN was accepted. Reject it explicitly.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/options/options_test.go (1)
133-140: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd an explicit non-node resource case.
The table rejects
deploymentsonly whenNodeis set. Add a case withNodeunset and an unshardable resource. This directly verifies that the resource restriction remains node-scoped.Suggested test case
{ name: "node scoped run with an unshardable resource", mutate: func(o *Options) { o.Node = "node-1"; o.Resources = ResourceSet{"deployments": struct{}{}} }, wantErr: true, }, + { + name: "non-node run with an unshardable resource", + mutate: func(o *Options) { o.Resources = ResourceSet{"deployments": struct{}{}} }, + },🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/options/options_test.go` around lines 133 - 140, Add a table entry in the Options validation tests with Node unset and an unshardable resource such as deployments, asserting the expected validation result. Keep the existing node-scoped deployments case and pods case unchanged, so the test explicitly covers the non-node-scoped behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@pkg/options/options_test.go`:
- Around line 133-140: Add a table entry in the Options validation tests with
Node unset and an unshardable resource such as deployments, asserting the
expected validation result. Keep the existing node-scoped deployments case and
pods case unchanged, so the test explicitly covers the non-node-scoped behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: e1b19ada-5d18-4bc9-be36-624eba8bea48
📒 Files selected for processing (2)
pkg/options/options.gopkg/options/options_test.go
Scoping the shardable-resource check to node-mode runs is the behaviour this PR introduces, so assert it directly rather than only asserting the node-mode rejection.
What this PR does / why we need it:
Three related gaps in
Options.Validate(), each of which lets a misconfiguration through silently.1. The general validations never ran without
--nodeValidate()returned early when--nodewas empty, leaving every check below that guard unreachable for the ordinary cluster-wide deployment:So
--auto-gomemlimit-ratioand--object-limitwere accepted at any value unless--nodehappened to be set, even though their flag help documents the bounds. The values are then used: the ratio reachesmemlimit.SetGoMemLimitWithOpts(memlimit.WithRatio(...)), where anything above 1 setsGOMEMLIMITabove the detected container limit and defeats the flag; the object limit reaches the list options asLimit. Demonstrated onmain: without--node,AutoGoMemlimitRatio = 5.0andObjectLimit = -1both pass; setting--nodemakes the identical values fail.The resource check is now scoped to the node case and the rest run on every path.
2. NaN passed the ratio range check
--auto-gomemlimit-ratio=NaNparses as a valid float64, and every comparison against NaN is false, so it satisfied both<= 0.0and> 1.0and reachedmemlimit.WithRatio. Rejected explicitly now. (Thanks to the review comment for catching this.)3. A shard configuration matching no objects was accepted
The shard an object belongs to is
jump.Hash(fnv64a(uid), totalShards)compared against the configured index.jump.Hashreturns-1for a non-positive bucket count, and no index can equal an out-of-range one, so--total-shards=0(or negative) and--shard >= --total-shardsboth filter out every object. KSM starts normally, lists and watches everything at full cost, and serves an empty/metricswith HTTP 200 and nothing logged anywhere.Autosharding is unaffected — it configures shards through
ConfigureShardingat runtime rather than through the flags this validates.Testing
TestValidatecovers all three, node and non-node paths, and the valid boundary cases. The new cases fail onmainand pass here.Behaviour change worth noting: a deployment currently passing an out-of-range
--auto-gomemlimit-ratio, a negative--object-limit, or an unsatisfiable shard configuration starts today and will now fail fast with the documented error. That is what the existing validation intends, and the defaults are unaffected.How does this change affect the cardinality of KSM: does not change cardinality
Which issue(s) this PR fixes: N/A
Summary by CodeRabbit
Bug Fixes
Tests