Skip to content

Commit

Permalink
The ConfigurationPolicy error message for a missing namespace is inco…
Browse files Browse the repository at this point in the history
…mplete

Signed-off-by: Chunxi Luo <chuluo@redhat.com>
  • Loading branch information
ChunxiAlexLuo committed Nov 18, 2022
1 parent 18e33b7 commit 9e249de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 11 additions & 2 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ func (r *ConfigurationPolicyReconciler) getObjectTemplateDetails(
templateObjs := make([]objectTemplateDetails, len(plc.Spec.ObjectTemplates))
selectedNamespaces := []string{}
queryNamespaces := false
kindWithoutNS := ""

for idx, objectT := range plc.Spec.ObjectTemplates {
unstruct, err := unmarshalFromJSON(objectT.ObjectDefinition.Raw)
Expand All @@ -437,22 +438,30 @@ func (r *ConfigurationPolicyReconciler) getObjectTemplateDetails(

if templateObjs[idx].isNamespaced && templateObjs[idx].namespace == "" {
queryNamespaces = true
kindWithoutNS = kindWithoutNS + "/" + templateObjs[idx].kind
}
}

// If required, query for namespaces specified in NamespaceSelector for objects to use
if queryNamespaces {
// Retrieve the namespaces based on filters in NamespaceSelector
selector := plc.Spec.NamespaceSelector
plcName := plc.GetName()
// If MatchLabels/MatchExpressions/Include were not provided, return no namespaces
if selector.MatchLabels == nil && selector.MatchExpressions == nil && len(selector.Include) == 0 {
log.Info("namespaceSelector is empty. Skipping namespace retrieval.")
log.Info(
"Policy name:%s & Kind:%s - namespaceSelector is empty. Skipping namespace retrieval.",
plcName,
kindWithoutNS,
)
} else {
// If an error occurred in the NamespaceSelector, update the policy status and abort
var err error
selectedNamespaces, err = common.GetSelectedNamespaces(r.TargetK8sClient, selector)
if err != nil {
errMsg := "Error filtering namespaces with provided namespaceSelector"
errMsg := fmt.Sprintf(
"Policy name:%s/Kind:%s - Error filtering namespaces with provided namespaceSelector",
plcName, kindWithoutNS)
log.Error(
err, errMsg,
"namespaceSelector", fmt.Sprintf("%+v", selector))
Expand Down
9 changes: 6 additions & 3 deletions test/e2e/case19_ns_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@ var _ = Describe("Test object namespace selection", Ordered, func() {
},
"Malformed filepath in include": {
"{\"include\":[\"*-[a-z-*\"]}",
"Error filtering namespaces with provided namespaceSelector: " +
"Policy name:" + case19PolicyName +
" & Kind:/ConfigMap - Error filtering namespaces with provided namespaceSelector: " +
"error parsing 'include' pattern '*-[a-z-*': syntax error in pattern",
},
"MatchExpressions with incorrect operator": {
"{\"matchExpressions\":[{\"key\":\"name\",\"operator\":\"Seriously\"}]}",
"Error filtering namespaces with provided namespaceSelector: " +
"Policy name:" + case19PolicyName +
" & Kind:/ConfigMap - Error filtering namespaces with provided namespaceSelector: " +
"error parsing namespace LabelSelector: \"Seriously\" is not a valid pod selector operator",
},
"MatchExpressions with missing values": {
"{\"matchExpressions\":[{\"key\":\"name\",\"operator\":\"In\",\"values\":[]}]}",
"Error filtering namespaces with provided namespaceSelector: " +
"Policy name:" + case19PolicyName +
" & Kind:/ConfigMap - Error filtering namespaces with provided namespaceSelector: " +
"error parsing namespace LabelSelector: " +
"values: Invalid value: []string(nil): for 'in', 'notin' operators, values set can't be empty",
},
Expand Down

0 comments on commit 9e249de

Please sign in to comment.