-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Enforce static tier default action #11468
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
base: master
Are you sure you want to change the base?
Enforce static tier default action #11468
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds validation to enforce that static Calico tiers (default, kube-admin, and kube-baseline) have the correct default action values in addition to their existing order validation. The default tier must use Deny, while kube-admin and kube-baseline tiers must use Pass.
Key Changes:
- Refactored tier validation to use a helper function
validateTierSpecthat validates both order and default action - Added validation logic to enforce specific default action values for static tiers
- Expanded test coverage to include default action validation scenarios for all three static tiers
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| libcalico-go/lib/validator/v3/validator.go | Refactored tier validation logic to validate both order and default action for static tiers using a new helper function; added redundant import |
| libcalico-go/lib/validator/v3/validator_test.go | Added comprehensive test cases for default action validation on all static tiers; reorganized variable declarations |
Comments suppressed due to low confidence (1)
libcalico-go/lib/validator/v3/validator.go:26
- This import is redundant. The package
github.com/projectcalico/api/pkg/apis/projectcalico/v3is already imported on line 25 with the aliasapi. You can useapi.DefaultTierOrder,api.Deny, etc. instead of importing it again asv3. Remove this line and replacev3.references in the code below withapi..
"github.com/projectcalico/api/pkg/lib/numorstring"
| ) | ||
| } | ||
| } | ||
|
|
||
| if tier.Name == names.KubeAdminTierName { | ||
| if tier.Spec.Order == nil || *tier.Spec.Order != api.KubeAdminTierOrder { | ||
| tierDefaultActionMatched := false | ||
| tierDefaultActionStr := "nil" | ||
| if tier.Spec.DefaultAction != nil { | ||
| tierDefaultActionStr = fmt.Sprintf("%v", *tier.Spec.DefaultAction) |
Copilot
AI
Dec 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first argument to ReportError should be the actual field value (wrapped in reflect.ValueOf() if needed), not its string representation. Currently, tierOrderStr is being passed, which is a string representation of the order value or "nil". This should be:
structLevel.ReportError(
reflect.ValueOf(tier.Spec.Order),
"TierSpec.Order",
"",
reason(fmt.Sprintf("%v tier order must be %v", tier.Name, expectedOrder)),
"",
)This matches the pattern used in the original code (lines 1684-1690) and ensures proper error reporting.
| "", | ||
| reason(fmt.Sprintf("kube-admin tier order must be %v", api.KubeAdminTierOrder)), | ||
| reason(fmt.Sprintf("%v tier default action must be %v", tier.Name, expectedDefaultAction)), | ||
| "", | ||
| ) | ||
| } | ||
| } | ||
|
|
Copilot
AI
Dec 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first argument to ReportError should be the actual field value (wrapped in reflect.ValueOf() if needed), not its string representation. Currently, tierDefaultActionStr is being passed, which is a string representation of the default action value or "nil". This should be:
structLevel.ReportError(
reflect.ValueOf(tier.Spec.DefaultAction),
"TierSpec.DefaultAction",
"",
reason(fmt.Sprintf("%v tier default action must be %v", tier.Name, expectedDefaultAction)),
"",
)This ensures consistency with how other validation errors are reported in this file.
| DefaultAction: &defaultActionDeny, | ||
| }}, false), | ||
| Entry("Tier: allow default tier with the predefined order", &api.Tier{ | ||
| Entry("Tier: disallow default tier with an invalid default action", &api.Tier{ |
Copilot
AI
Dec 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test coverage for static tiers with nil DefaultAction. The validator code would reject static tiers (default, kube-admin, kube-baseline) when DefaultAction is nil, but there are no explicit test cases for this scenario. Consider adding test cases like:
Entry("Tier: disallow default tier with nil default action", &api.Tier{
ObjectMeta: v1.ObjectMeta{Name: names.DefaultTierName},
Spec: api.TierSpec{
Order: &defaultTierOrder,
}}, false),Similar test cases should be added for kube-admin and kube-baseline tiers to explicitly verify this validation requirement.
Description
Related issues/PRs
Todos
Release Note
Reminder for the reviewer
Make sure that this PR has the correct labels and milestone set.
Every PR needs one
docs-*label.docs-pr-required: This change requires a change to the documentation that has not been completed yet.docs-completed: This change has all necessary documentation completed.docs-not-required: This change has no user-facing impact and requires no docs.Every PR needs one
release-note-*label.release-note-required: This PR has user-facing changes. Most PRs should have this label.release-note-not-required: This PR has no user-facing changes.Other optional labels:
cherry-pick-candidate: This PR should be cherry-picked to an earlier release. For bug fixes only.needs-operator-pr: This PR is related to install and requires a corresponding change to the operator.