Skip to content

Commit

Permalink
ConfigurationPolicy error message for a missing namespace is incomplete
Browse files Browse the repository at this point in the history
Signed-off-by: Chunxi Luo <chuluo@redhat.com>
  • Loading branch information
ChunxiAlexLuo authored and openshift-merge-robot committed Nov 23, 2022
1 parent 18e33b7 commit 36aed58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
16 changes: 12 additions & 4 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
18 changes: 11 additions & 7 deletions test/e2e/case19_ns_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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": {
Expand Down

0 comments on commit 36aed58

Please sign in to comment.