Skip to content

Commit 2df23d8

Browse files
gparvinopenshift-merge-robot
authored andcommitted
Fix template sync duplicate detection
We don't want duplicate config policy templates, but the logic prevented me from having a Constraint and a ConstraintTemplate that had the same name. Of course a config policy couldn't have that same name either so adding gvk in the name check. Refs: - https://issues.redhat.com/browse/ACM-7265 Signed-off-by: Gus Parvin <[email protected]>
1 parent 747b933 commit 2df23d8

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

controllers/templatesync/template_sync.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,9 @@ func hasDupName(pol *policiesv1.Policy) bool {
15511551
}
15521552

15531553
name := unstructured.GetName()
1554+
apiv := unstructured.GetAPIVersion()
1555+
kind := unstructured.GetKind()
1556+
name = fmt.Sprintf("%s/%s/%s", name, apiv, kind)
15541557

15551558
if _, has := foundNames[name]; has {
15561559
return true

controllers/templatesync/template_sync_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"testing"
88

9+
gktemplatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1112
"k8s.io/apimachinery/pkg/runtime"
@@ -91,3 +92,124 @@ func TestHandleSyncSuccessNoDoubleRemoveStatus(t *testing.T) {
9192
t.Fatalf("handleSyncSuccess failed unexpectedly: %s", err)
9293
}
9394
}
95+
96+
func TestHasDuplicateNames(t *testing.T) {
97+
policy := policiesv1.Policy{
98+
TypeMeta: metav1.TypeMeta{
99+
Kind: "Policy",
100+
APIVersion: "policy.open-cluster-management.io/v1",
101+
},
102+
ObjectMeta: metav1.ObjectMeta{
103+
Name: "test-policy",
104+
Namespace: "managed",
105+
},
106+
}
107+
108+
configPolicy := configpoliciesv1.ConfigurationPolicy{
109+
TypeMeta: metav1.TypeMeta{
110+
Kind: "ConfigurationPolicy",
111+
APIVersion: "policy.open-cluster-management.io/v1",
112+
},
113+
ObjectMeta: metav1.ObjectMeta{
114+
Name: "test-configpolicy",
115+
Namespace: "managed",
116+
},
117+
}
118+
119+
outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, &configPolicy)
120+
if err != nil {
121+
t.Fatalf("Could not serialize the config policy: %s", err)
122+
}
123+
124+
raw := runtime.RawExtension{
125+
Raw: outBytes,
126+
}
127+
128+
x := policiesv1.PolicyTemplate{
129+
ObjectDefinition: raw,
130+
}
131+
132+
policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &x)
133+
134+
has := hasDupName(&policy)
135+
if has {
136+
t.Fatal("Duplicate names found in templates but not expected")
137+
}
138+
139+
// add a gatekeeper constraint template with a duplicate name
140+
gkt := gktemplatesv1.ConstraintTemplate{
141+
TypeMeta: metav1.TypeMeta{
142+
Kind: "ConstraintTemplate",
143+
APIVersion: "templates.gatekeeper.sh/v1beta1",
144+
},
145+
ObjectMeta: metav1.ObjectMeta{
146+
Name: "test-configpolicy",
147+
},
148+
}
149+
150+
outBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, &gkt)
151+
if err != nil {
152+
t.Fatalf("Could not serialize the constraint template: %s", err)
153+
}
154+
155+
y := policiesv1.PolicyTemplate{
156+
ObjectDefinition: runtime.RawExtension{
157+
Raw: outBytes,
158+
},
159+
}
160+
161+
policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &y)
162+
163+
has = hasDupName(&policy)
164+
if has {
165+
t.Fatal("Duplicate names found in templates but not expected")
166+
}
167+
168+
// add a gatekeeper constraint with a duplicate name
169+
gkc := gktemplatesv1.ConstraintTemplate{
170+
TypeMeta: metav1.TypeMeta{
171+
Kind: "ContainerEnvMaxMemory",
172+
APIVersion: "constraints.gatekeeper.sh/v1beta1",
173+
},
174+
ObjectMeta: metav1.ObjectMeta{
175+
Name: "test-configpolicy",
176+
},
177+
}
178+
179+
outBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, &gkc)
180+
if err != nil {
181+
t.Fatalf("Could not serialize the constraint template: %s", err)
182+
}
183+
184+
z := policiesv1.PolicyTemplate{
185+
ObjectDefinition: runtime.RawExtension{
186+
Raw: outBytes,
187+
},
188+
}
189+
190+
policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &z)
191+
192+
has = hasDupName(&policy)
193+
if has {
194+
t.Fatal("Duplicate names found in templates but not expected")
195+
}
196+
197+
// add a config policy with a duplicate name
198+
outBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, &configPolicy)
199+
if err != nil {
200+
t.Fatalf("Could not serialize the config policy: %s", err)
201+
}
202+
203+
x2 := policiesv1.PolicyTemplate{
204+
ObjectDefinition: runtime.RawExtension{
205+
Raw: outBytes,
206+
},
207+
}
208+
209+
policy.Spec.PolicyTemplates = append(policy.Spec.PolicyTemplates, &x2)
210+
211+
has = hasDupName(&policy)
212+
if !has { // expect duplicate detection to return true
213+
t.Fatal("Duplicate name not detected")
214+
}
215+
}

0 commit comments

Comments
 (0)