From 3ca0f04a90de987571e25446f4a5477695602b1b Mon Sep 17 00:00:00 2001 From: Dale Haiducek <19750917+dhaiducek@users.noreply.github.com> Date: Wed, 11 Sep 2024 12:11:12 -0400 Subject: [PATCH] Fix dependency logic considering dots in names 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) --- controllers/propagator/replication.go | 23 +++++++++++----------- controllers/propagator/replication_test.go | 6 +++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/controllers/propagator/replication.go b/controllers/propagator/replication.go index 36504f91..3e381cf6 100644 --- a/controllers/propagator/replication.go +++ b/controllers/propagator/replication.go @@ -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 . 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 . 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) } diff --git a/controllers/propagator/replication_test.go b/controllers/propagator/replication_test.go index 6904b9d0..80f78231 100644 --- a/controllers/propagator/replication_test.go +++ b/controllers/propagator/replication_test.go @@ -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": { @@ -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) } }) }