Skip to content
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

Empty label fields in policies are ignored #150

Merged

Conversation

zyjjay
Copy link
Contributor

@zyjjay zyjjay commented Jul 6, 2023

Fix parsing issue when policy label fields are set to empty strings

ref: https://issues.redhat.com/browse/ACM-5398

@zyjjay zyjjay changed the title Bug: ACM-5398, ACM ignores Policies about empty label ACM ignores Policies about empty label Aug 15, 2023
Comment on lines 51 to 58
found := false

_, ok := labelMap[case33StorageLabel]
if ok {
found = true
}

return found
Copy link
Contributor

@yiraeChristineKim yiraeChristineKim Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
found := false
_, ok := labelMap[case33StorageLabel]
if ok {
found = true
}
return found
_, ok := labelMap[case33StorageLabel]
if ok {
return true
}
return false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, this would probably be better handled by the HaveKey() Gomega assertion rather than returning a vague boolean.

You could even use HaveKeyWithValue() to assert both the key and the value to combine both of this and the next test.

https://onsi.github.io/gomega/#havekeykey-interface

Eventually(func() interface{} {
node, _ := clientManaged.CoreV1().Nodes().Get(context.TODO(), case33NodeName, metav1.GetOptions{})
labelMap := node.GetLabels()
merged := labelMap[case33StorageLabel]
Copy link
Contributor

@yiraeChristineKim yiraeChristineKim Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
merged := labelMap[case33StorageLabel]
_, ok := labelMap[case33StorageLabel]
return ok

We wanted to verify whether the label exist or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified it in the Eventually block above this. This check is to make sure the value of the field is an empty string.

@@ -0,0 +1,17 @@
apiVersion: v1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where this file used? cannot find

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I have no idea what this is either, I will fix up my PR to remove this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a kubeconfig file. This looks to me to be the result of naming the managed cluster test-managed instead of just managed and apparently we don't have that in our .gitignore. 🙂

Comment on lines 16 to 25
const (
case33ConfigPolicy = "../resources/case33_empty_label_handling/empty-labels-config.yaml"
case33ConfigPolicyName = "case33-empty-labels"
case33NodeName = "test-managed-control-plane"
case33StorageLabel = "cluster.ocs.openshift.io/openshift-storage"
)

var _ = Describe("Test resource creation when there are empty labels in configurationPolicy", Ordered, func() {
Describe("Appending empty labels to a Node", func() {
Context("When the Node already exists", func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be using global variables in tests unless they truly need to be global (it was an inadvisable practice that was used previously and hadn't been cleaned up yet).

Also, it seems to me the Describe() and Context() nodes aren't necessary with a single test suite.

Suggested change
const (
case33ConfigPolicy = "../resources/case33_empty_label_handling/empty-labels-config.yaml"
case33ConfigPolicyName = "case33-empty-labels"
case33NodeName = "test-managed-control-plane"
case33StorageLabel = "cluster.ocs.openshift.io/openshift-storage"
)
var _ = Describe("Test resource creation when there are empty labels in configurationPolicy", Ordered, func() {
Describe("Appending empty labels to a Node", func() {
Context("When the Node already exists", func() {
var _ = Describe("Test resource creation when there are empty labels in configurationPolicy", Ordered, func() {
const (
configPolicy = "../resources/case33_empty_label_handling/empty-labels-config.yaml"
configPolicyName = "case33-empty-labels"
nodeName = "test-managed-control-plane"
storageLabel = "cluster.ocs.openshift.io/openshift-storage"
)

@@ -0,0 +1,17 @@
apiVersion: v1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a kubeconfig file. This looks to me to be the result of naming the managed cluster test-managed instead of just managed and apparently we don't have that in our .gitignore. 🙂

metadata:
name: test-managed-control-plane
labels:
cluster.ocs.openshift.io/openshift-storage: ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a sanity check, could we also check using a policy that sets cluster.ocs.openshift.io/openshift-storage: null to make sure the policy doesn't remove the field? (Though I'm also uncertain what the behavior would be there in terms of compliance...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a test just to check what this currently does, and understand if that behavior has changed (or if we change it accidentally in the future)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, having an unset label/setting it to nil results in the deletion of all the labels attached to the object. I'll probably make a separate ticket to address this issue as it's a bit more involved and possibly affect annotations as well (need to confirm this but they both use the same helper function to populate the fields).

const (
case33ConfigPolicy = "../resources/case33_empty_label_handling/empty-labels-config.yaml"
case33ConfigPolicyName = "case33-empty-labels"
case33NodeName = "test-managed-control-plane"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to use a different resource for the test. This relies on using a kind cluster of a specific name. If the test creates a configmap or something, the policy could just label that.

JustinKuli
JustinKuli previously approved these changes Aug 21, 2023
Copy link
Member

@JustinKuli JustinKuli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/hold

Looks good to me! Thanks for the changes to the tests, it looks great now. Just holding for any other reviewer comments.

@mprahl
Copy link
Member

mprahl commented Aug 22, 2023

@zyjjay could you please format your commit message title and description to match the contribution guidelines?
https://github.com/open-cluster-management-io/community/blob/main/sig-policy/contribution-guidelines.md#commit-messages

Also, ACM shouldn't be mentioned in the commit message since this is in the upstream Open Cluster Management community.

@zyjjay zyjjay changed the title ACM ignores Policies about empty label Empty label fields in policies are ignored Aug 22, 2023
@yiraeChristineKim
Copy link
Contributor

/lgtm

@openshift-ci openshift-ci bot added the lgtm label Aug 22, 2023
@yiraeChristineKim
Copy link
Contributor

/hold

@yiraeChristineKim
Copy link
Contributor

yiraeChristineKim commented Aug 22, 2023

/hold
Until the commit message is changed

@mprahl
Copy link
Member

mprahl commented Aug 23, 2023

@zyjjay do you need help with changing the commit message?

Fix parsing issue when policy label fields are set to empty strings

ref: https://issues.redhat.com/browse/ACM-5398

Signed-off-by: Jason Zhang <jaszhang@redhat.com>
@openshift-ci openshift-ci bot added the lgtm label Aug 23, 2023
@mprahl
Copy link
Member

mprahl commented Aug 23, 2023

@zyjjay the commit title should start with a verb in the imperative mood (i.e. Fix ....). Please do this next time.

Copy link
Member

@mprahl mprahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/unhold

@openshift-ci
Copy link

openshift-ci bot commented Aug 23, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JustinKuli, mprahl, yiraeChristineKim, zyjjay

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [JustinKuli,mprahl,yiraeChristineKim]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@yiraeChristineKim
Copy link
Contributor

/cherry-pick release-2.7

@mprahl
Copy link
Member

mprahl commented Aug 30, 2023

/cherry-pick release-2.7

You have to do that on the Stolostron PR:
stolostron/config-policy-controller#568

@openshift-cherrypick-robot

@yiraeChristineKim: only open-cluster-management-io org members may request cherry picks. You can still do the cherry-pick manually.

In response to this:

/cherry-pick release-2.7

Instructions 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/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants