forked from kyverno/kyverno
-
Notifications
You must be signed in to change notification settings - Fork 2
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
fix: ur not cleaned up when deleting namespaced policy #17
Open
sandeshlmore
wants to merge
4
commits into
develop
Choose a base branch
from
issue_5101
base: develop
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6091af6
fix: wrong logger used (#5311)
eddycharly 001db94
Fix issue where CLI test command ignores failures (#5189)
sosheskaz 97a2b9a
test: add kuttl tests for jmespath special chars (#5310)
somebadcode 5c8e78e
fix: ur not cleaned up when deleting namespaced policy
sandeshlmore 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -99,7 +99,7 @@ func NewController( | |
}) | ||
polInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
UpdateFunc: c.updatePolicy, | ||
DeleteFunc: c.deletePolicy, | ||
DeleteFunc: c.deleteNSPolicy, | ||
}) | ||
|
||
c.informersSynced = []cache.InformerSynced{cpolInformer.Informer().HasSynced, polInformer.Informer().HasSynced, urInformer.Informer().HasSynced, namespaceInformer.Informer().HasSynced, podInformer.Informer().HasSynced} | ||
|
@@ -370,6 +370,50 @@ func (c *controller) deletePolicy(obj interface{}) { | |
} | ||
} | ||
|
||
func (c *controller) deleteNSPolicy(obj interface{}) { | ||
p, ok := kubeutils.GetObjectWithTombstone(obj).(*kyvernov1.Policy) | ||
if !ok { | ||
logger.Info("Failed to get deleted object", "obj", obj) | ||
return | ||
} | ||
|
||
logger.V(4).Info("deleting policy", "name", p.Name) | ||
key, err := cache.MetaNamespaceKeyFunc(kubeutils.GetObjectWithTombstone(obj)) | ||
if err != nil { | ||
logger.Error(err, "failed to load policy key") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we put a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
return | ||
} | ||
logger.V(4).Info("updating policy", "key", key) | ||
|
||
// check if deleted policy is clone generate policy | ||
generatePolicyWithClone := pkgCommon.ProcessDeletePolicyForCloneGenerateRule(p, c.client, c.kyvernoClient, c.urLister, p.GetName(), logger) | ||
|
||
// get the generated resource name from update request | ||
selector := labels.SelectorFromSet(labels.Set(map[string]string{ | ||
kyvernov1beta1.URGeneratePolicyLabel: p.Name, | ||
})) | ||
|
||
urList, err := c.urLister.List(selector) | ||
if err != nil { | ||
logger.Error(err, "failed to get UR for resource", "label", kyvernov1beta1.URGeneratePolicyLabel) | ||
return | ||
} | ||
|
||
if !generatePolicyWithClone { | ||
// re-evaluate the UR as the policy was updated | ||
for _, ur := range urList { | ||
logger.V(4).Info("enqueue the UR for cleanup", "UR", ur.Name) | ||
c.enqueueUpdateRequest(ur) | ||
} | ||
} else { | ||
for _, ur := range urList { | ||
for _, generatedResource := range ur.Status.GeneratedResources { | ||
logger.V(4).Info("retaining resource for cloned policy", "apiVersion", generatedResource.APIVersion, "kind", generatedResource.Kind, "name", generatedResource.Name, "namespace", generatedResource.Namespace) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *controller) addUR(obj interface{}) { | ||
ur := obj.(*kyvernov1beta1.UpdateRequest) | ||
c.enqueueUpdateRequest(ur) | ||
|
This file contains 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 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
6 changes: 6 additions & 0 deletions
6
...ormance/kuttl/mutate/clusterpolicy/cornercases/jmespath-with-special-chars/00-policy.yaml
This file contains 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,6 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestStep | ||
apply: | ||
- policy.yaml | ||
assert: | ||
- policy-assert.yaml |
6 changes: 6 additions & 0 deletions
6
...nce/kuttl/mutate/clusterpolicy/cornercases/jmespath-with-special-chars/01-deployment.yaml
This file contains 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,6 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestStep | ||
apply: | ||
- resources.yaml | ||
assert: | ||
- resources-assert.yaml |
12 changes: 12 additions & 0 deletions
12
...ce/kuttl/mutate/clusterpolicy/cornercases/jmespath-with-special-chars/README.md
This file contains 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,12 @@ | ||
## Description | ||
|
||
This test checks that document references with special characters in their names are supported. | ||
|
||
## Expected Behavior | ||
|
||
JMESPath references generated when documents are traversed are escaped properly according to the JMESPath standard. | ||
|
||
## Reference Issue(s) | ||
|
||
3578 | ||
3616 |
9 changes: 9 additions & 0 deletions
9
...nce/kuttl/mutate/clusterpolicy/cornercases/jmespath-with-special-chars/policy-assert.yaml
This file contains 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,9 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: jmespath-with-special-chars-demo | ||
status: | ||
conditions: | ||
- reason: Succeeded | ||
status: "True" | ||
type: Ready |
28 changes: 28 additions & 0 deletions
28
...onformance/kuttl/mutate/clusterpolicy/cornercases/jmespath-with-special-chars/policy.yaml
This file contains 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,28 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: jmespath-with-special-chars-demo | ||
spec: | ||
rules: | ||
- name: format-deploy-zone | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- Pod | ||
mutate: | ||
patchStrategicMerge: | ||
metadata: | ||
labels: | ||
deploy-zone: "{{ to_upper('{{@}}') }}" | ||
- name: retention-adjust | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- Pod | ||
mutate: | ||
patchStrategicMerge: | ||
metadata: | ||
labels: | ||
corp.com/retention: "{{ regex_replace_all('([0-9])([0-9])', '{{ @ }}', '${1}0') }}" |
7 changes: 7 additions & 0 deletions
7
.../kuttl/mutate/clusterpolicy/cornercases/jmespath-with-special-chars/resources-assert.yaml
This file contains 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,7 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: busybox | ||
labels: | ||
deploy-zone: FRANKFURT | ||
corp.com/retention: days_30 |
12 changes: 12 additions & 0 deletions
12
...ormance/kuttl/mutate/clusterpolicy/cornercases/jmespath-with-special-chars/resources.yaml
This file contains 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,12 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: busybox | ||
labels: | ||
deploy-zone: frankfurt | ||
corp.com/retention: days_37 | ||
spec: | ||
containers: | ||
- name: busybox | ||
image: busybox:stable | ||
command: ["sleep", "600"] |
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.
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.
It will be good if we can write unit tests for deleteNSPolicy function?
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.
Unit test cant be added for this function. i tried with fake client as well.