forked from banzaicloud/koperator
-
Notifications
You must be signed in to change notification settings - Fork 15
Comply with mutating webhook #204
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
Open
dobrerazvan
wants to merge
7
commits into
master
Choose a base branch
from
mutation_hooks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ebd59b
Comply with mutating webhook
dobrerazvan f6b0f59
lint
dobrerazvan aa73ccb
mod tidy
dobrerazvan aa1fb42
Unittest
dobrerazvan de7798a
Patch
dobrerazvan 2248b58
Remove unused code
dobrerazvan 70884fa
Fix linting
dobrerazvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| // Copyright © 2019 Cisco Systems, Inc. and/or its affiliates | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package k8sutil | ||
|
|
||
| import ( | ||
| "emperror.dev/errors" | ||
| json "github.com/json-iterator/go" | ||
| corev1 "k8s.io/api/core/v1" | ||
|
|
||
| "github.com/banzaicloud/k8s-objectmatcher/patch" | ||
| ) | ||
|
|
||
| // IgnoreMutationWebhookFields creates a CalculateOption that ignores fields commonly | ||
| // modified by mutation webhooks like Gatekeeper, OPA, and Pod Security Policies | ||
| func IgnoreMutationWebhookFields() patch.CalculateOption { | ||
| return func(current, modified []byte) ([]byte, []byte, error) { | ||
| currentPod := &corev1.Pod{} | ||
| if err := json.Unmarshal(current, currentPod); err != nil { | ||
| // Not a pod, return unchanged | ||
| return current, modified, nil | ||
| } | ||
|
|
||
| modifiedPod := &corev1.Pod{} | ||
| if err := json.Unmarshal(modified, modifiedPod); err != nil { | ||
| return current, modified, nil | ||
| } | ||
|
|
||
| // Remove fields that mutation webhooks commonly modify | ||
| currentPod = cleanMutationWebhookFields(currentPod) | ||
| modifiedPod = cleanMutationWebhookFields(modifiedPod) | ||
|
|
||
| currentBytes, err := json.Marshal(currentPod) | ||
| if err != nil { | ||
| return []byte{}, []byte{}, errors.Wrap(err, "could not marshal cleaned current pod") | ||
| } | ||
|
|
||
| modifiedBytes, err := json.Marshal(modifiedPod) | ||
| if err != nil { | ||
| return []byte{}, []byte{}, errors.Wrap(err, "could not marshal cleaned modified pod") | ||
| } | ||
|
|
||
| return currentBytes, modifiedBytes, nil | ||
| } | ||
| } | ||
|
|
||
| func cleanMutationWebhookFields(pod *corev1.Pod) *corev1.Pod { | ||
| // Create a copy to avoid modifying the original | ||
| cleaned := pod.DeepCopy() | ||
|
|
||
| // Remove mutation webhook annotations that should not trigger reconciliation | ||
| if cleaned.Annotations != nil { | ||
| delete(cleaned.Annotations, "gatekeeper.sh/mutation-id") | ||
| delete(cleaned.Annotations, "gatekeeper.sh/mutations") | ||
| } | ||
|
|
||
| // Clean security context fields commonly set by PSPs/Gatekeeper | ||
| for i := range cleaned.Spec.InitContainers { | ||
| cleanSecurityContext(&cleaned.Spec.InitContainers[i]) | ||
| } | ||
| for i := range cleaned.Spec.Containers { | ||
| cleanSecurityContext(&cleaned.Spec.Containers[i]) | ||
| } | ||
|
|
||
| return cleaned | ||
| } | ||
|
|
||
| func cleanSecurityContext(container *corev1.Container) { | ||
| if container.SecurityContext == nil { | ||
| return | ||
| } | ||
|
|
||
| // Note: We intentionally do NOT clean security context fields here by default | ||
| // because those are typically important security controls that should be reconciled. | ||
| // If you need to ignore specific security context fields, uncomment the relevant lines below: | ||
|
|
||
| // AllowPrivilegeEscalation is often set by PSPs | ||
| // container.SecurityContext.AllowPrivilegeEscalation = nil | ||
|
|
||
| // ReadOnlyRootFilesystem is often set by PSPs | ||
| // container.SecurityContext.ReadOnlyRootFilesystem = nil | ||
|
|
||
| // Capabilities are often modified by PSPs | ||
| // container.SecurityContext.Capabilities = nil | ||
| } | ||
|
|
||
| // IgnorePodResourcesIfAnnotated creates a CalculateOption that ignores pod resource | ||
| // requests/limits if the pod has specific annotations indicating it's managed by | ||
| // an external system (e.g., ScaleOps, VPA) | ||
| func IgnorePodResourcesIfAnnotated() patch.CalculateOption { | ||
| return func(current, modified []byte) ([]byte, []byte, error) { | ||
| currentMap := map[string]interface{}{} | ||
| if err := json.Unmarshal(current, ¤tMap); err != nil { | ||
| return current, modified, nil | ||
| } | ||
|
|
||
| // Check if this pod should ignore resource diffs (e.g., via annotation) | ||
| if shouldIgnoreResources(currentMap) { | ||
| // Remove resources from comparison | ||
| current = removeResourcesFromPod(current) | ||
| modified = removeResourcesFromPod(modified) | ||
| } | ||
|
|
||
| return current, modified, nil | ||
| } | ||
| } | ||
|
|
||
| func shouldIgnoreResources(podMap map[string]interface{}) bool { | ||
| metadata, ok := podMap["metadata"].(map[string]interface{}) | ||
| if !ok { | ||
| return false | ||
| } | ||
|
|
||
| annotations, ok := metadata["annotations"].(map[string]interface{}) | ||
| if !ok { | ||
| return false | ||
| } | ||
|
|
||
| // Check for annotations that indicate external resource management | ||
| annotationsToCheck := []string{ | ||
| "scaleops.sh/pod-owner-grouping", | ||
| "vpa.k8s.io/updateMode", | ||
| "cluster-autoscaler.kubernetes.io/safe-to-evict-local-volumes", | ||
| } | ||
|
|
||
| for _, ann := range annotationsToCheck { | ||
| if _, exists := annotations[ann]; exists { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| func removeResourcesFromPod(podBytes []byte) []byte { | ||
| podMap := map[string]interface{}{} | ||
| if err := json.Unmarshal(podBytes, &podMap); err != nil { | ||
| return podBytes | ||
| } | ||
|
|
||
| if spec, ok := podMap["spec"].(map[string]interface{}); ok { | ||
| // Remove resources from all containers | ||
| if containers, ok := spec["containers"].([]interface{}); ok { | ||
| for _, c := range containers { | ||
| if container, ok := c.(map[string]interface{}); ok { | ||
| delete(container, "resources") | ||
| } | ||
| } | ||
| } | ||
| if initContainers, ok := spec["initContainers"].([]interface{}); ok { | ||
| for _, c := range initContainers { | ||
| if container, ok := c.(map[string]interface{}); ok { | ||
| delete(container, "resources") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| result, err := json.Marshal(podMap) | ||
| if err != nil { | ||
| return podBytes | ||
| } | ||
| return result | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.