Skip to content

Commit 92984ef

Browse files
Override the namespace set in the template
Previously, if the user set a namespace in the template, then when the template-sync tried to create the template, it would fail. Now the template-sync always uses the cluster namespace, overriding what the user might have provided. Refs: - https://issues.redhat.com/browse/ACM-17666 Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent bad4c95 commit 92984ef

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

controllers/templatesync/template_sync.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
530530

531531
overrideRemediationAction(instance, tObjectUnstructured)
532532

533+
tObjectUnstructured.SetNamespace(resourceNs)
534+
533535
eObject, err = res.Create(ctx, tObjectUnstructured, metav1.CreateOptions{})
534536
if err != nil {
535537
multiTemplateRegExp := regexp.MustCompile(

test/e2e/case9_template_sync_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ import (
1111
"open-cluster-management.io/governance-policy-propagator/test/utils"
1212
)
1313

14+
var _ = Describe("Test template sync overrides namespace", func() {
15+
const (
16+
namespaceSetPolicyName string = "case9-test-policy-with-ns"
17+
namespaceSetPolicyYaml string = "../resources/case9_template_sync/case9-test-policy-with-ns.yaml"
18+
namespaceSetConfigPolicyName string = "case9-config-policy-with-ns"
19+
)
20+
21+
BeforeEach(func() {
22+
hubApplyPolicy(namespaceSetPolicyName, namespaceSetPolicyYaml)
23+
})
24+
AfterEach(func() {
25+
By("Deleting a policy on the hub in ns:" + clusterNamespaceOnHub)
26+
_, err := kubectlHub("delete", "-f", namespaceSetPolicyYaml, "-n", clusterNamespaceOnHub, "--ignore-not-found")
27+
Expect(err).ShouldNot(HaveOccurred())
28+
opt := metav1.ListOptions{}
29+
utils.ListWithTimeout(clientManagedDynamic, gvrPolicy, opt, 0, true, defaultTimeoutSeconds)
30+
})
31+
It("should override the namespace in the template", func() {
32+
By("Checking that the ConfigurationPolicy was created in the cluster namespace")
33+
foundPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigurationPolicy, namespaceSetConfigPolicyName,
34+
clusterNamespace, true, defaultTimeoutSeconds)
35+
Expect(foundPlc).NotTo(BeNil())
36+
})
37+
})
38+
1439
var _ = Describe("Test template sync", func() {
1540
const (
1641
case9PolicyName string = "case9-test-policy"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: case9-test-policy-with-ns
5+
labels:
6+
policy.open-cluster-management.io/cluster-name: managed
7+
policy.open-cluster-management.io/cluster-namespace: managed
8+
policy.open-cluster-management.io/root-policy: case9-test-policy-with-ns
9+
spec:
10+
remediationAction: inform
11+
disabled: false
12+
policy-templates:
13+
- objectDefinition:
14+
apiVersion: policy.open-cluster-management.io/v1
15+
kind: ConfigurationPolicy
16+
metadata:
17+
name: case9-config-policy-with-ns
18+
namespace: bad
19+
spec:
20+
remediationAction: inform
21+
object-templates:
22+
- complianceType: musthave
23+
objectDefinition:
24+
apiVersion: v1
25+
kind: Pod
26+
metadata:
27+
name: nginx-pod-e2e
28+
namespace: default
29+
spec:
30+
containers:
31+
- name: nginx
32+

0 commit comments

Comments
 (0)