-
Notifications
You must be signed in to change notification settings - Fork 834
feat: add operations field to mutation ApplyTo spec #4135
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?
feat: add operations field to mutation ApplyTo spec #4135
Conversation
Add granular operation control to Gatekeeper mutations by introducing an 'operations' field in the ApplyTo specification. This allows users to specify which Kubernetes admission operations (CREATE, UPDATE, DELETE) should trigger mutations. Key Changes: - Add Operations []string field to ApplyTo struct - Update mutation matching logic to check operation compatibility - Add Operation field to Mutable type for context propagation - Maintain backward compatibility (defaults to CREATE+UPDATE) - Add comprehensive unit tests for operation matching - Update documentation with usage examples and best practices - Include practical examples demonstrating various operation patterns Fixes: Resolves issues with mutations attempting to modify immutable fields during UPDATE operations, such as Pod environment variables in CronJob scenarios. Backward Compatibility: Existing mutations without operations field continue working unchanged with default CREATE+UPDATE behavior. Testing: Added unit tests, live cluster validation, and comprehensive examples covering all supported operation combinations. Signed-off-by: sai kiran pikili <saikiranpikili@localhost.localdomain>
6dc29a7 to
3babba5
Compare
|
@JaydipGabani Any chance we can continue with this PR? It's solving a real pain point in our production environments and I would love to see this land in one of the next releases. |
|
I am planning to discuss this in today's community meeting. I will take a look today to see if anything is missing. Thanks for the ping and thr PR. |
|
@pikilisaikiran ptal - #4121 (comment) |
chewong
left a comment
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.
Overall looks good!
| # No operations field - defaults to CREATE and UPDATE for backward compatibility | ||
| match: | ||
| scope: Namespaced | ||
| location: "metadata.labels.legacy-mutator" |
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.
I don't think this is a good example because Assign is not supposed to mutate metadata field per https://open-policy-agent.github.io/gatekeeper/website/docs/mutation#mutation-crds
| Versions []string `json:"versions,omitempty"` | ||
| // Operations specifies which admission operations (CREATE, UPDATE, DELETE) should trigger | ||
| // this mutation. If empty, defaults to ["CREATE", "UPDATE"] for backward compatibility. | ||
| Operations []string `json:"operations,omitempty"` |
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.
Let's create type ApplyToOperation string instead of using raw string. We could also add a kubebuilder marker for API validation purposes.
| Operations []string `json:"operations,omitempty"` | |
| // +kubebuilder:validation:Enum=CREATE;UPDATE;DELETE | |
| Operations []ApplyToOperation `json:"operations,omitempty"` |
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.
Could you also run make manifests so the updated CRDs can get copied to the staging helm chart?
| func (a ApplyTo) MatchesOperation(operation string) bool { | ||
| // If no operations specified, default to CREATE and UPDATE for backward compatibility | ||
| if len(a.Operations) == 0 { | ||
| return operation == "CREATE" || operation == "UPDATE" |
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.
nit: let's rename the constants from admissionv1 package, or create new constants in the match package
| return operation == "CREATE" || operation == "UPDATE" | |
| return operation == admissionv1.Create || operation == admissionv1.Update |
Add operations field to mutation ApplyTo specification
Summary
This PR introduces an
operationsfield to theApplyTospecification in Gatekeeper mutations, enabling granular control over which Kubernetes admission operations (CREATE, UPDATE, DELETE) trigger mutations.Problem Solved
Fixes #4121: CronJob Pods getting stuck due to mutations attempting to modify immutable fields during UPDATE operations.
Before: Mutations always applied on both CREATE and UPDATE operations, causing failures when trying to mutate immutable Pod fields like environment variables.
After: Users can specify
operations: ["CREATE"]to only mutate during resource creation, avoiding immutable field issues.Key Changes
Core Implementation
Operations []stringfield toApplyTostructMatchWithApplyTofunctionOperationfield toMutabletype for context propagationTesting & Validation
Documentation & Examples
Usage Examples
Solving Immutable Field Issues (Main Use Case)
Other Supported Patterns
operations: ["CREATE"]- Creation-only mutationsoperations: ["UPDATE"]- Update-only mutationsoperations: ["CREATE", "UPDATE"]- Explicit default behavioroperations: ["DELETE"]- Advanced cleanup scenariosBackward Compatibility
✅ Zero Breaking Changes
operationsfield work unchangedTesting Performed
Related Issue
Closes #4121
Checklist