-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify with the API server if an empty map is equal to nil
If a policy specified an empty map but the object didn't return a value for the map, it was assumed that API server was just not returning an empty value. This is true in most cases, however, if the underlying Go type of the map is a pointer to a struct, an empty map may have a different meaning than nil. One example is the `emptyDir` key in the "configs.imageregistry.operator.openshift.io" resource. This commit changes the local comparison logic from considering empty maps being the same as a nil value. The controller then performs a dry run update request to see if the API server returns an empty map or omits the value entirely (i.e. seen as nil). The result of the object comparison is now cached to not continuously making dry run update requests on every policy evaluation. Relates: https://issues.redhat.com/browse/ACM-7810 Signed-off-by: mprahl <mprahl@users.noreply.github.com>
- Loading branch information
1 parent
bc358da
commit ffc115c
Showing
9 changed files
with
444 additions
and
67 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright Contributors to the Open Cluster Management project | ||
|
||
package e2e | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"open-cluster-management.io/config-policy-controller/test/utils" | ||
) | ||
|
||
var _ = Describe("Test resource creation when there are empty labels in configurationPolicy", Ordered, func() { | ||
const ( | ||
case36AddEmptyMap = "../resources/case36_empty_map/policy-should-add-empty-map.yaml" | ||
case36AddEmptyMapName = "case36-check-selinux-options" | ||
case36NoEmptyMap = "../resources/case36_empty_map/policy-should-not-add-empty-map.yaml" | ||
case36NoEmptyMapName = "case36-check-node-selector" | ||
case36Deployment = "../resources/case36_empty_map/deployment.yaml" | ||
case36DeploymentName = "case36-deployment" | ||
) | ||
|
||
BeforeEach(func() { | ||
By("creating the deployment " + case36DeploymentName) | ||
utils.Kubectl("apply", "-f", case36Deployment) | ||
}) | ||
|
||
AfterEach(func() { | ||
By("deleting the deployment " + case36DeploymentName) | ||
utils.Kubectl("delete", "-f", case36Deployment, "--ignore-not-found") | ||
deleteConfigPolicies([]string{case36AddEmptyMapName, case36NoEmptyMapName}) | ||
}) | ||
|
||
It("verifies the policy "+case36AddEmptyMapName+"is NonCompliant", func() { | ||
By("creating the policy " + case36AddEmptyMapName) | ||
utils.Kubectl("apply", "-f", case36AddEmptyMap, "-n", testNamespace) | ||
|
||
By("checking if the policy " + case36AddEmptyMapName + " is NonCompliant") | ||
Eventually(func() interface{} { | ||
managedPlc := utils.GetWithTimeout( | ||
clientManagedDynamic, | ||
gvrConfigPolicy, | ||
case36AddEmptyMapName, | ||
testNamespace, | ||
true, | ||
defaultTimeoutSeconds, | ||
) | ||
|
||
return utils.GetComplianceState(managedPlc) | ||
}, defaultTimeoutSeconds, 1).Should(Equal("NonCompliant")) | ||
}) | ||
|
||
It("verifies the policy "+case36NoEmptyMapName+"is Compliant", func() { | ||
By("creating the policy " + case36NoEmptyMapName) | ||
utils.Kubectl("apply", "-f", case36NoEmptyMap, "-n", testNamespace) | ||
|
||
By("checking if the policy " + case36NoEmptyMapName + " is Compliant") | ||
Eventually(func() interface{} { | ||
managedPlc := utils.GetWithTimeout( | ||
clientManagedDynamic, | ||
gvrConfigPolicy, | ||
case36NoEmptyMapName, | ||
testNamespace, | ||
true, | ||
defaultTimeoutSeconds, | ||
) | ||
|
||
return utils.GetComplianceState(managedPlc) | ||
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant")) | ||
}) | ||
}) |
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,22 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: case36-deployment | ||
namespace: default | ||
labels: | ||
test: case36-deployment | ||
spec: | ||
replicas: 0 | ||
selector: | ||
matchLabels: | ||
test: case36-deployment | ||
template: | ||
metadata: | ||
labels: | ||
test: case36-deployment | ||
spec: | ||
securityContext: {} | ||
containers: | ||
- image: nginx:1.7.9 | ||
imagePullPolicy: Never | ||
name: nginx |
21 changes: 21 additions & 0 deletions
21
test/resources/case36_empty_map/policy-should-add-empty-map.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,21 @@ | ||
apiVersion: policy.open-cluster-management.io/v1 | ||
kind: ConfigurationPolicy | ||
metadata: | ||
name: case36-check-selinux-options | ||
spec: | ||
remediationAction: inform | ||
object-templates: | ||
- complianceType: musthave | ||
objectDefinition: | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: case36-deployment | ||
namespace: default | ||
spec: | ||
template: | ||
spec: | ||
# securityContext defaults to `{}` and seLinuxOptions has omitempty on the API server but is a pointer, | ||
# so setting this to an empty object will cause the empty object to be returned by the API. | ||
securityContext: | ||
seLinuxOptions: {} |
Oops, something went wrong.