Skip to content

Commit 58c5e5c

Browse files
Correct details list when templates are removed
Refs: - https://issues.redhat.com/browse/ACM-14550 Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 9172504 commit 58c5e5c

File tree

3 files changed

+83
-30
lines changed

3 files changed

+83
-30
lines changed

controllers/configurationpolicy_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,14 @@ func (r *ConfigurationPolicyReconciler) handleTemplatization(
12281228
plc.Spec.ObjectTemplates[i].ObjectDefinition.Raw = resolvedTemplate.ResolvedJSON
12291229
}
12301230

1231+
// Set the CompliancyDetails array length accordingly in case the number of
1232+
// object-templates was reduced (the status update will handle if it's longer).
1233+
// Note that this still works when using `object-templates-raw` because the
1234+
// ObjectTemplates are manually set above to match what was resolved
1235+
if len(plc.Spec.ObjectTemplates) < len(plc.Status.CompliancyDetails) {
1236+
plc.Status.CompliancyDetails = plc.Status.CompliancyDetails[:len(plc.Spec.ObjectTemplates)]
1237+
}
1238+
12311239
return parentStatusUpdateNeeded, nil
12321240
}
12331241

test/e2e/case5_multi_test.go

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,88 +6,110 @@ package e2e
66
import (
77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
9+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
910

1011
"open-cluster-management.io/config-policy-controller/test/utils"
1112
)
1213

13-
const (
14-
case5ConfigPolicyNameInform string = "policy-pod-multi-mh"
15-
case5ConfigPolicyNameEnforce string = "policy-pod-multi-create"
16-
case5ConfigPolicyNameCombo string = "policy-pod-multi-combo"
17-
case5PodName1 string = "case5-nginx-pod-1"
18-
case5PodName2 string = "case5-nginx-pod-2"
19-
case5InformYaml string = "../resources/case5_multi/case5_multi_mh.yaml"
20-
case5EnforceYaml string = "../resources/case5_multi/case5_multi_enforce.yaml"
21-
case5ComboYaml string = "../resources/case5_multi/case5_multi_combo.yaml"
22-
)
23-
2414
var _ = Describe("Test multiple obj template handling", Ordered, func() {
15+
const (
16+
configPolicyNameInform string = "policy-pod-multi-mh"
17+
configPolicyNameEnforce string = "policy-pod-multi-create"
18+
configPolicyNameCombo string = "policy-pod-multi-combo"
19+
podName1 string = "case5-nginx-pod-1"
20+
podName2 string = "case5-nginx-pod-2"
21+
informYaml string = "../resources/case5_multi/case5_multi_mh.yaml"
22+
enforceYaml string = "../resources/case5_multi/case5_multi_enforce.yaml"
23+
comboYaml string = "../resources/case5_multi/case5_multi_combo.yaml"
24+
singleYaml string = "../resources/case5_multi/case5_single_mh.yaml"
25+
)
26+
2527
Describe("Create a policy on managed cluster in ns:"+testNamespace, Ordered, func() {
2628
It("should be created properly on the managed cluster", func() {
27-
By("Creating " + case5ConfigPolicyNameInform + " and " + case5ConfigPolicyNameCombo + " on managed")
28-
utils.Kubectl("apply", "-f", case5InformYaml, "-n", testNamespace)
29+
By("Creating " + configPolicyNameInform + " and " + configPolicyNameCombo + " on managed")
30+
utils.Kubectl("apply", "-f", informYaml, "-n", testNamespace)
2931
plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
30-
case5ConfigPolicyNameInform, testNamespace, true, defaultTimeoutSeconds)
32+
configPolicyNameInform, testNamespace, true, defaultTimeoutSeconds)
3133
Expect(plc).NotTo(BeNil())
3234
Eventually(func(g Gomega) {
3335
managedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
34-
case5ConfigPolicyNameInform, testNamespace, true, defaultTimeoutSeconds)
36+
configPolicyNameInform, testNamespace, true, defaultTimeoutSeconds)
3537

3638
utils.CheckComplianceStatus(g, managedPlc, "NonCompliant")
3739
}, defaultTimeoutSeconds, 1).Should(Succeed())
38-
utils.Kubectl("apply", "-f", case5ComboYaml, "-n", testNamespace)
40+
utils.Kubectl("apply", "-f", comboYaml, "-n", testNamespace)
3941
plc = utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
40-
case5ConfigPolicyNameCombo, testNamespace, true, defaultTimeoutSeconds)
42+
configPolicyNameCombo, testNamespace, true, defaultTimeoutSeconds)
4143
Expect(plc).NotTo(BeNil())
4244
Eventually(func(g Gomega) {
4345
managedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
44-
case5ConfigPolicyNameCombo, testNamespace, true, defaultTimeoutSeconds)
46+
configPolicyNameCombo, testNamespace, true, defaultTimeoutSeconds)
4547

4648
utils.CheckComplianceStatus(g, managedPlc, "NonCompliant")
4749
}, defaultTimeoutSeconds, 1).Should(Succeed())
4850
})
4951
It("should create pods on managed cluster", func() {
50-
By("creating " + case5ConfigPolicyNameEnforce + " on hub with spec.remediationAction = enforce")
51-
utils.Kubectl("apply", "-f", case5EnforceYaml, "-n", testNamespace)
52+
By("creating " + configPolicyNameEnforce + " on hub with spec.remediationAction = enforce")
53+
utils.Kubectl("apply", "-f", enforceYaml, "-n", testNamespace)
5254
plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
53-
case5ConfigPolicyNameEnforce, testNamespace, true, defaultTimeoutSeconds)
55+
configPolicyNameEnforce, testNamespace, true, defaultTimeoutSeconds)
5456
Expect(plc).NotTo(BeNil())
5557
Eventually(func(g Gomega) {
5658
managedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
57-
case5ConfigPolicyNameEnforce, testNamespace, true, defaultTimeoutSeconds)
59+
configPolicyNameEnforce, testNamespace, true, defaultTimeoutSeconds)
5860

5961
utils.CheckComplianceStatus(g, managedPlc, "Compliant")
6062
}, defaultTimeoutSeconds, 1).Should(Succeed())
6163
Eventually(func(g Gomega) {
6264
informPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
63-
case5ConfigPolicyNameInform, testNamespace, true, defaultTimeoutSeconds)
65+
configPolicyNameInform, testNamespace, true, defaultTimeoutSeconds)
6466

6567
utils.CheckComplianceStatus(g, informPlc, "Compliant")
6668
}, defaultTimeoutSeconds, 1).Should(Succeed())
6769
Eventually(func(g Gomega) {
6870
comboPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
69-
case5ConfigPolicyNameCombo, testNamespace, true, defaultTimeoutSeconds)
71+
configPolicyNameCombo, testNamespace, true, defaultTimeoutSeconds)
7072

7173
utils.CheckComplianceStatus(g, comboPlc, "NonCompliant")
7274
}, defaultTimeoutSeconds, 1).Should(Succeed())
7375
pod1 := utils.GetWithTimeout(clientManagedDynamic, gvrPod,
74-
case5PodName1, "default", true, defaultTimeoutSeconds)
76+
podName1, "default", true, defaultTimeoutSeconds)
7577
Expect(pod1).NotTo(BeNil())
7678
pod2 := utils.GetWithTimeout(clientManagedDynamic, gvrPod,
77-
case5PodName2, "default", true, defaultTimeoutSeconds)
79+
podName2, "default", true, defaultTimeoutSeconds)
7880
Expect(pod2).NotTo(BeNil())
7981
})
82+
It("should only have compliancy details on the current objects when templates are removed", func() {
83+
By("confirming the current details on the policy")
84+
Eventually(func(g Gomega) {
85+
managedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
86+
configPolicyNameInform, testNamespace, true, defaultTimeoutSeconds)
87+
details, _, _ := unstructured.NestedSlice(managedPlc.Object, "status", "compliancyDetails")
88+
g.Expect(details).To(HaveLen(2))
89+
}, defaultTimeoutSeconds, 1).Should(Succeed())
90+
91+
By("removing the second template on the policy")
92+
utils.Kubectl("apply", "-f", singleYaml, "-n", testNamespace)
93+
94+
By("checking the new details on the policy")
95+
Eventually(func(g Gomega) {
96+
managedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
97+
configPolicyNameInform, testNamespace, true, defaultTimeoutSeconds)
98+
details, _, _ := unstructured.NestedSlice(managedPlc.Object, "status", "compliancyDetails")
99+
g.Expect(details).To(HaveLen(1))
100+
}, defaultTimeoutSeconds, 1).Should(Succeed())
101+
})
80102
AfterAll(func() {
81103
policies := []string{
82-
case5ConfigPolicyNameInform,
83-
case5ConfigPolicyNameEnforce,
84-
case5ConfigPolicyNameCombo,
104+
configPolicyNameInform,
105+
configPolicyNameEnforce,
106+
configPolicyNameCombo,
85107
}
86108

87109
deleteConfigPolicies(policies)
88110

89111
By("Delete pods")
90-
pods := []string{case5PodName1, case5PodName2}
112+
pods := []string{podName1, podName2}
91113
namespaces := []string{"default"}
92114
deletePods(pods, namespaces)
93115
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: ConfigurationPolicy
3+
metadata:
4+
name: policy-pod-multi-mh
5+
namespace: managed
6+
spec:
7+
remediationAction: inform
8+
namespaceSelector:
9+
exclude: ["kube-*"]
10+
include: ["default"]
11+
object-templates:
12+
- complianceType: musthave
13+
objectDefinition:
14+
apiVersion: v1
15+
kind: Pod
16+
metadata:
17+
name: case5-nginx-pod-1
18+
spec:
19+
containers:
20+
- image: nginx:1.7.9
21+
name: nginx
22+
ports:
23+
- containerPort: 80

0 commit comments

Comments
 (0)