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

Fix behavior when the kube api might omit some more fields #128

Conversation

JustinKuli
Copy link
Member

@JustinKuli JustinKuli commented Apr 25, 2023

These are two more situations where the kube api server may omit "empty" values in some of the default kubernetes objects.

https://issues.redhat.com/browse/ACM-5132

This is another situation where the kube api server may omit "empty"
values in some of the default kubernetes objects. This commit also
refactors the test code for re-use.

Refs:
 - https://issues.redhat.com/browse/ACM-5132

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
Creating a configuration policy that "belongs" to a parent policy is
slightly tricky, because it needs an owner reference with a UID. A
common function makes this a bit easier.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
Similar to some other fixes we had for empty bools, strings, and ints.

Refs:
 - https://issues.redhat.com/browse/ACM-5132

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
@JustinKuli
Copy link
Member Author

/hold

Looking for any maps to see if omitempty is handled nicely there

@@ -115,6 +115,10 @@ func equalObjWithSort(mergedObj interface{}, oldObj interface{}) (areEqual bool)
// this includes the case where oldObj is nil
return false
case []interface{}:
if len(mergedObj) == 0 && oldObj == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we need this line 170, 176

Copy link
Member Author

Choose a reason for hiding this comment

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

On what was line 170, case []map[string]interface{}, the condition here is not necessary, because the length would never be 0. If it was length zero, it would just be [], and that doesn't have the required type information to get into that case.

I think the rest of the logic in checkFieldsWithSort should already cover these cases. At least, that was the intention of a663462, and what I was thinking of adding seems equivalent.

_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(case15NeverCompliantParentYaml, case15NeverCompliantParentName,
Copy link
Contributor

Choose a reason for hiding this comment

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

great! createConfigPolicyWithParent

@JustinKuli JustinKuli changed the title Fix behavior when the kube api might omit some empty lists Fix behavior when the kube api might omit some more fields Apr 27, 2023
@JustinKuli
Copy link
Member Author

JustinKuli commented Apr 27, 2023

Interesting, the kube validation of events must have changed between kubernetes versions. On 1.19, the test runs into this:

2023-04-27T17:39:42.426Z	error	configuration-policy-controller	controllers/configurationpolicy_controller.go:1655	Could not handle missing musthave object	{"object": "configpol-test-event", "policy": "config-policy-event-emptystruct", "index": 0, "error": "Event \"configpol-test-event\" is invalid: involvedObject.namespace: Invalid value: \"\": does not match event.namespace"}

For anyone curious, the change was in kube 1.20, compare:

So creating the event in namespace "default" only sometimes works (1.20+). But "kube-system" will work on older versions and newer versions.

It was tricky to find an example of this. Most optional structs I found
would get populated with some default values, meaning they wouldn't be
omitted by the kube api.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
@JustinKuli
Copy link
Member Author

/unhold

I'm happy with this now

@openshift-ci
Copy link

openshift-ci bot commented May 1, 2023

[APPROVALNOTIFIER] This PR is APPROVED

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

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,yiraeChristineKim]

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

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.

3 participants