Skip to content

Commit 1139cec

Browse files
mprahlopenshift-merge-bot[bot]
authored andcommitted
Set the parent policy to NonCompliant when there is a duplicate name
The noncompliant event was sent in this case but the status-sync did not pick it up because it specified the policy template name using the array index. Relates: https://issues.redhat.com/browse/ACM-11806 Signed-off-by: mprahl <[email protected]>
1 parent d780b0e commit 1139cec

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

controllers/templatesync/template_sync.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
177177
}
178178

179179
// Check duplicate names in configuration-policies
180-
has := hasDupName(instance)
181-
if has {
182-
msg := "There are duplicate names in configurationpolicies, please check the policy"
180+
dupName := getDupName(instance)
181+
if dupName != "" {
182+
msg := "All policy-template names must be unique within a policy"
183183
reqLogger.Info(msg)
184184

185185
for tIndex := range instance.Spec.PolicyTemplates {
186-
_ = r.emitTemplateError(ctx, instance, tIndex, fmt.Sprintf("template-%v", tIndex), false, msg)
186+
_ = r.emitTemplateError(ctx, instance, tIndex, dupName, false, msg)
187187
}
188188

189189
return reconcile.Result{}, nil
@@ -1693,27 +1693,27 @@ func (r *PolicyReconciler) setCreatedGkConstraint(b bool) {
16931693
r.createdGkConstraint = &b
16941694
}
16951695

1696-
// Check duplicate names in policy-templates(configurationPolicies)
1697-
func hasDupName(pol *policiesv1.Policy) bool {
1696+
// Check duplicate names in policy-templates and return the first duplicate name found, if any.
1697+
func getDupName(pol *policiesv1.Policy) string {
16981698
templates := pol.Spec.PolicyTemplates
16991699

1700-
foundNames := make(map[string]struct{})
1700+
foundNames := make(map[string]bool, len(templates))
17011701

17021702
for _, v := range templates {
17031703
unstructured, err := unmarshalFromJSON(v.ObjectDefinition.Raw)
17041704
if err != nil {
17051705
// Skip unmarshal error here, template error should appear later
1706-
return false
1706+
return ""
17071707
}
17081708

17091709
name := unstructured.GetName()
17101710

1711-
if _, has := foundNames[name]; has {
1712-
return true
1711+
if foundNames[name] {
1712+
return name
17131713
}
17141714

1715-
foundNames[name] = struct{}{}
1715+
foundNames[name] = true
17161716
}
17171717

1718-
return false
1718+
return ""
17191719
}

controllers/templatesync/template_sync_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestHandleSyncSuccessNoDoubleRemoveStatus(t *testing.T) {
9393
}
9494
}
9595

96-
func TestHasDuplicateNames(t *testing.T) {
96+
func TestGetDupName(t *testing.T) {
9797
policy := policiesv1.Policy{
9898
TypeMeta: metav1.TypeMeta{
9999
Kind: "Policy",
@@ -131,8 +131,8 @@ func TestHasDuplicateNames(t *testing.T) {
131131

132132
policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &x)
133133

134-
has := hasDupName(&policy)
135-
if has {
134+
dupName := getDupName(&policy)
135+
if dupName != "" {
136136
t.Fatal("Unexpected duplicate policy template names")
137137
}
138138

@@ -160,8 +160,8 @@ func TestHasDuplicateNames(t *testing.T) {
160160

161161
policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &y)
162162

163-
has = hasDupName(&policy)
164-
if !has {
163+
dupName = getDupName(&policy)
164+
if dupName != "test-configpolicy" {
165165
t.Fatal("Duplicate names for templates not detected")
166166
}
167167

@@ -189,8 +189,8 @@ func TestHasDuplicateNames(t *testing.T) {
189189

190190
policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &z)
191191

192-
has = hasDupName(&policy)
193-
if !has {
192+
dupName = getDupName(&policy)
193+
if dupName != "test-configpolicy" {
194194
t.Fatal("Duplicate names for templates not detected")
195195
}
196196

@@ -208,8 +208,8 @@ func TestHasDuplicateNames(t *testing.T) {
208208

209209
policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &x2)
210210

211-
has = hasDupName(&policy)
212-
if !has { // expect duplicate detection to return true
211+
dupName = getDupName(&policy)
212+
if dupName != "test-configpolicy" { // expect duplicate detection to return true
213213
t.Fatal("Duplicate name not detected")
214214
}
215215
}

test/e2e/case10_error_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,27 @@ var _ = Describe("Test error handling", func() {
4242

4343
By("Should generate warning events")
4444
Eventually(
45-
checkForEvent(dupNamePolicyName,
46-
"There are duplicate names in configurationpolicies, please check the policy"),
45+
checkForEvent(dupNamePolicyName, "All policy-template names must be unique within a policy"),
4746
defaultTimeoutSeconds,
4847
1,
4948
).Should(BeTrue())
5049

50+
By("Checking if the policy status is NonCompliant")
51+
Eventually(func(g Gomega) string {
52+
hubPlc := utils.GetWithTimeout(
53+
clientHubDynamic,
54+
gvrPolicy,
55+
dupNamePolicyName,
56+
clusterNamespaceOnHub,
57+
true,
58+
defaultTimeoutSeconds,
59+
)
60+
61+
compliant, _, _ := unstructured.NestedString(hubPlc.Object, "status", "compliant")
62+
63+
return compliant
64+
}, defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))
65+
5166
By("Should not create any configuration policy")
5267
Consistently(func() interface{} {
5368
return utils.GetWithTimeout(clientManagedDynamic, gvrConfigurationPolicy, dupConfigPolicyName,

0 commit comments

Comments
 (0)