Skip to content

Commit

Permalink
Fix dependency logic considering dots in names
Browse files Browse the repository at this point in the history
There was an assumption a dot in a name meant it
contained the namespace. But this doesn't make
sense if the namespace is also provided.

ref: https://issues.redhat.com/browse/ACM-14241
Signed-off-by: Dale Haiducek <19750917+dhaiducek@users.noreply.github.com>
(cherry picked from commit e5ef033822692b72e5c3d50033d92626ae28b5cf)
  • Loading branch information
dhaiducek authored and openshift-merge-bot[bot] committed Sep 17, 2024
1 parent 1fa4a43 commit 3ca0f04
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
23 changes: 12 additions & 11 deletions controllers/propagator/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,21 @@ func (r *Propagator) canonicalizeDependencies(
})
}
} else if depIsPolicy(dep) {
split := strings.Split(dep.Name, ".")
if len(split) == 2 { // assume it's already in the correct <namespace>.<name> format
deps = append(deps, dep)
} else {
if dep.Namespace == "" {
// use the namespace from the dependent policy when otherwise not provided
dep.Namespace = defaultNamespace
if dep.Namespace == "" {
split := strings.Split(dep.Name, ".")
if len(split) >= 2 { // assume the name is already in the correct <namespace>.<name> format
deps = append(deps, dep)

continue
}
// use the namespace from the dependent policy when otherwise not provided
dep.Namespace = defaultNamespace
}

dep.Name = dep.Namespace + "." + dep.Name
dep.Namespace = ""
dep.Name = dep.Namespace + "." + dep.Name
dep.Namespace = ""

deps = append(deps, dep)
}
deps = append(deps, dep)
} else {
deps = append(deps, dep)
}
Expand Down
6 changes: 5 additions & 1 deletion controllers/propagator/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ func TestCanonicalizeDependencies(t *testing.T) {
input: []policiesv1.PolicyDependency{
depPol("red", "colors", "Compliant"),
depPol("blue", "colors", "NonCompliant"),
depPol("1.2", "colors", "NonCompliant"),
depPol("colors.1.2", "", "NonCompliant"),
},
want: []policiesv1.PolicyDependency{
depPol("colors.red", "", "Compliant"),
depPol("colors.blue", "", "NonCompliant"),
depPol("colors.1.2", "", "NonCompliant"),
depPol("colors.1.2", "", "NonCompliant"),
},
},
"policies without namespaces": {
Expand Down Expand Up @@ -225,7 +229,7 @@ func TestCanonicalizeDependencies(t *testing.T) {
}

if !reflect.DeepEqual(test.want, got) {
t.Fatalf("expected: %v, got: %v", test.want, got)
t.Fatalf("%s\nexpected:\n%v\ngot:\n%v", name, test.want, got)
}
})
}
Expand Down

0 comments on commit 3ca0f04

Please sign in to comment.