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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions controllers/configurationpolicy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,30 @@ 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.

return true
}

if oldObjList, ok := oldObj.([]interface{}); ok {
return checkListsMatch(mergedObj, oldObjList)
}

return false
default:
// NOTE: when type is string, int, bool
var oVal interface{}

default: // when mergedObj's type is string, int, bool, or nil
if oldObj == nil && mergedObj != nil {
// compare the zero value of mergedObj's type to mergedObj
ref := reflect.ValueOf(mergedObj)
oVal = reflect.Zero(ref.Type()).Interface()
zero := reflect.Zero(ref.Type()).Interface()

return fmt.Sprint(zero) == fmt.Sprint(mergedObj)
}

if mergedObj == nil && oldObj != nil {
// compare the zero value of oldObj's type to oldObj
ref := reflect.ValueOf(oldObj)
zero := reflect.Zero(ref.Type()).Interface()

return fmt.Sprint(oVal) == fmt.Sprint(mergedObj)
return fmt.Sprint(zero) == fmt.Sprint(oldObj)
}

if !reflect.DeepEqual(fmt.Sprint(mergedObj), fmt.Sprint(oldObj)) {
Expand Down
68 changes: 8 additions & 60 deletions test/e2e/case15_event_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,8 @@ const (

var _ = Describe("Testing compliance event formatting", func() {
It("Records the right events for a policy that is always compliant", func() {
By("Creating parent policy " + case15AlwaysCompliantParentName + " on " + testNamespace)
utils.Kubectl("apply", "-f", case15AlwaysCompliantParentYaml, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
case15AlwaysCompliantParentName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating compliant policy " + case15AlwaysCompliantName + " on " + testNamespace + " with parent " +
case15AlwaysCompliantParentName)
plcDef := utils.ParseYaml(case15AlwaysCompliantYaml)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(case15AlwaysCompliantParentYaml, case15AlwaysCompliantParentName,
case15AlwaysCompliantYaml)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15AlwaysCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -81,21 +68,8 @@ var _ = Describe("Testing compliance event formatting", func() {
Expect(nonCompParentEvents).To(BeEmpty())
})
It("Records the right events for a policy that is never compliant", func() {
By("Creating parent policy " + case15NeverCompliantParentName + " on " + testNamespace)
utils.Kubectl("apply", "-f", case15NeverCompliantParentYaml, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
case15NeverCompliantParentName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating noncompliant policy " + case15NeverCompliantName + " on " + testNamespace + " with parent " +
case15NeverCompliantParentName)
plcDef := utils.ParseYaml(case15NeverCompliantYaml)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, 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

case15NeverCompliantYaml)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15NeverCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -126,21 +100,8 @@ var _ = Describe("Testing compliance event formatting", func() {
Expect(nonCompParentEvents).NotTo(BeEmpty())
})
It("Records events for a policy that becomes compliant", func() {
By("Creating parent policy " + case15BecomesCompliantParentName + " on " + testNamespace)
utils.Kubectl("apply", "-f", case15BecomesCompliantParentYaml, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
case15BecomesCompliantParentName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating noncompliant policy " + case15BecomesCompliantName + " on " + testNamespace + " with parent " +
case15BecomesCompliantParentName)
plcDef := utils.ParseYaml(case15BecomesCompliantYaml)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(case15BecomesCompliantParentYaml, case15BecomesCompliantParentName,
case15BecomesCompliantYaml)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15BecomesCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -176,21 +137,8 @@ var _ = Describe("Testing compliance event formatting", func() {
Expect(compParentEvents).NotTo(BeEmpty())
})
It("Records events for a policy that becomes noncompliant", func() {
By("Creating parent policy " + case15BecomesNonCompliantParentName + " on " + testNamespace)
utils.Kubectl("apply", "-f", case15BecomesNonCompliantParentYaml, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
case15BecomesNonCompliantParentName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating compliant policy " + case15BecomesNonCompliantName + " on " + testNamespace + " with parent " +
case15BecomesNonCompliantParentName)
plcDef := utils.ParseYaml(case15BecomesNonCompliantYaml)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(case15BecomesNonCompliantParentYaml, case15BecomesNonCompliantParentName,
case15BecomesNonCompliantYaml)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15BecomesNonCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down
21 changes: 1 addition & 20 deletions test/e2e/case17_evaluation_interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,7 @@ const (

var _ = Describe("Test evaluation interval", func() {
It("Verifies that status.lastEvaluated is properly set", func() {
By("Creating the parent policy " + case17ParentPolicyName + " on the managed cluster")
utils.Kubectl("apply", "-f", case17ParentPolicy, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic,
gvrPolicy,
case17ParentPolicyName,
testNamespace,
true,
defaultTimeoutSeconds,
)
Expect(parent).NotTo(BeNil())

By("Creating " + case17PolicyName + " on the managed cluster")
plcDef := utils.ParseYaml(case17Policy)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).Create(
context.TODO(), plcDef, v1.CreateOptions{},
)
Expect(err).To(BeNil())
createConfigPolicyWithParent(case17ParentPolicy, case17ParentPolicyName, case17Policy)

By("Getting status.lastEvaluated")
var managedPlc *unstructured.Unstructured
Expand Down
17 changes: 2 additions & 15 deletions test/e2e/case21_alternative_kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,7 @@ var _ = Describe("Test an alternative kubeconfig for policy evaluation", Ordered
})

It("should create the namespace using the alternative kubeconfig", func() {
By("Creating parent policy " + parentPolicyName + " on " + testNamespace)
utils.Kubectl("apply", "-f", parentPolicyYAML, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
parentPolicyName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating the " + policyName + " policy")
plcDef := utils.ParseYaml(policyYAML)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(parentPolicyYAML, parentPolicyName, policyYAML)

By("Verifying that the " + policyName + " policy is compliant")
Eventually(func() interface{} {
Expand All @@ -83,7 +70,7 @@ var _ = Describe("Test an alternative kubeconfig for policy evaluation", Ordered
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))

By("Verifying that the " + policyName + " was created using the alternative kubeconfig")
_, err = targetK8sClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
_, err := targetK8sClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
Expect(err).To(BeNil())

By("Verifying that a compliance event was created on the parent policy")
Expand Down
Loading