From e9595173c08341660f9aef03dfa17e333ca65544 Mon Sep 17 00:00:00 2001 From: Chunxi Luo Date: Fri, 18 Nov 2022 15:08:28 -0500 Subject: [PATCH] ConfigurationPolicy error message for a missing namespace is incomplete Signed-off-by: Chunxi Luo --- controllers/configurationpolicy_controller.go | 16 ++++++++++++---- test/e2e/case19_ns_selector_test.go | 18 +++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/controllers/configurationpolicy_controller.go b/controllers/configurationpolicy_controller.go index 793abbba..34d3af0b 100644 --- a/controllers/configurationpolicy_controller.go +++ b/controllers/configurationpolicy_controller.go @@ -1180,12 +1180,20 @@ func (r *ConfigurationPolicyReconciler) handleObjects( dclient, rsrc := r.getResourceAndDynamicClient(mapping) if objDetails.isNamespaced && namespace == "" { - log.Info("The object template is namespaced but no namespace is specified. Cannot process.") + objName := objDetails.name + kindWithoutNS := objDetails.kind + log.Info( + "The object template is namespaced but no namespace is specified. Cannot process.", + "name", objName, + "kind", kindWithoutNS, + ) // namespaced but none specified, generate violation - statusUpdateNeeded = addConditionToStatus(policy, index, false, "K8s missing namespace", - "namespaced object has no namespace specified "+ - "from the policy namespaceSelector nor the object metadata", + msg := fmt.Sprintf("namespaced object %s of kind %s has no namespace specified "+ + "from the policy namespaceSelector nor the object metadata", + objName, kindWithoutNS, ) + statusUpdateNeeded = addConditionToStatus(policy, index, false, "K8s missing namespace", msg) + if statusUpdateNeeded { eventType := eventNormal if index < len(policy.Status.CompliancyDetails) && diff --git a/test/e2e/case19_ns_selector_test.go b/test/e2e/case19_ns_selector_test.go index 4574964c..e582d3d5 100644 --- a/test/e2e/case19_ns_selector_test.go +++ b/test/e2e/case19_ns_selector_test.go @@ -11,11 +11,13 @@ import ( ) const ( - case19PolicyName string = "policy-configmap-selector-e2e" - case19PolicyYaml string = "../resources/case19_ns_selector/case19_cm_policy.yaml" - case19PrereqYaml string = "../resources/case19_ns_selector/case19_cm_manifest.yaml" - case19PatchPrefix string = "[{\"op\":\"replace\",\"path\":\"/spec/namespaceSelector\",\"value\":" - case19PatchSuffix string = "}]" + case19PolicyName string = "policy-configmap-selector-e2e" + case19PolicyYaml string = "../resources/case19_ns_selector/case19_cm_policy.yaml" + case19TemplatesName string = "configmap-selector-e2e" + case19TemplatesKind string = "ConfigMap" + case19PrereqYaml string = "../resources/case19_ns_selector/case19_cm_manifest.yaml" + case19PatchPrefix string = "[{\"op\":\"replace\",\"path\":\"/spec/namespaceSelector\",\"value\":" + case19PatchSuffix string = "}]" ) // Test setup for namespace selection policy tests: @@ -33,12 +35,14 @@ var _ = Describe("Test object namespace selection", Ordered, func() { }{ "no namespaceSelector specified": { "{}", - "namespaced object has no namespace specified" + + "namespaced object " + case19TemplatesName + " of kind " + case19TemplatesKind + + " has no namespace specified" + " from the policy namespaceSelector nor the object metadata", }, "a non-matching LabelSelector": { "{\"matchLabels\":{\"name\":\"not-a-namespace\"}}", - "namespaced object has no namespace specified" + + "namespaced object " + case19TemplatesName + " of kind " + case19TemplatesKind + + " has no namespace specified" + " from the policy namespaceSelector nor the object metadata", }, "LabelSelector and exclude": {