diff --git a/pkg/utils/namer.go b/pkg/utils/namer.go index 11ec6814cd..4803a096c0 100644 --- a/pkg/utils/namer.go +++ b/pkg/utils/namer.go @@ -224,28 +224,21 @@ func (n *Namer) ParseName(name string) *NameComponents { } } -// negBelongsToCluster checks that the UID is present and a substring of the -// cluster uid, since the NEG naming schema truncates it to 8 characters. -// This is only valid for NEGs, BackendServices and Healthchecks for NEG. -func (n *Namer) negBelongsToCluster(name string) bool { - fields := strings.Split(name, "-") - var uid string - if len(fields) > 1 { - uid = fields[1] - } - - return len(uid) > 0 && strings.Contains(n.UID(), uid) -} - // NameBelongsToCluster checks if a given name is tagged with this // cluster's UID. func (n *Namer) NameBelongsToCluster(name string) bool { - if !strings.HasPrefix(name, n.prefix) { + // Name follows the NEG naming scheme + if n.IsNEG(name) { + return true + } + + // Name follows the naming scheme where clusterid is the suffix. + if !strings.HasPrefix(name, n.prefix+"-") { return false } clusterName := n.UID() components := n.ParseName(name) - return components.ClusterName == clusterName || n.negBelongsToCluster(name) + return components.ClusterName == clusterName } // IGBackend constructs the name for a backend service targeting instance groups. @@ -389,6 +382,9 @@ func (n *Namer) NEG(namespace, name string, port int32) string { } // IsNEG returns true if the name is a NEG owned by this cluster. +// It checks that the UID is present and a substring of the +// cluster uid, since the NEG naming schema truncates it to 8 characters. +// This is only valid for NEGs, BackendServices and Healthchecks for NEG. func (n *Namer) IsNEG(name string) bool { return strings.HasPrefix(name, n.negPrefix()) } diff --git a/pkg/utils/namer_test.go b/pkg/utils/namer_test.go index 8cd938bc2c..337feb6471 100644 --- a/pkg/utils/namer_test.go +++ b/pkg/utils/namer_test.go @@ -463,6 +463,12 @@ func TestIsNEG(t *testing.T) { {defaultPrefix, "k8s1-uid1-namespace-name-80-1e047e33", true}, {"mci", "mci1-uid1-ns-svc-port-16c06497", true}, {defaultPrefix, "k8s1-uid1234567890123-namespace-name-80-2d8100t5", true}, + {defaultPrefix, "k8s1-uid12345-namespace-name-80-1e047e33", true}, + {defaultPrefix, "k8s1-uid12345-ns-svc-port-16c06497", true}, + {defaultPrefix, "k8s1-wronguid-namespace-name-80-1e047e33", false}, + {defaultPrefix, "k8s-be-80--uid1", false}, + {defaultPrefix, "k8s-ssl-foo--uid", false}, + {defaultPrefix, "invalidk8sresourcename", false}, } { namer := NewNamerWithPrefix(tc.prefix, "uid1", "fw1") res := namer.IsNEG(tc.in) @@ -471,23 +477,3 @@ func TestIsNEG(t *testing.T) { } } } - -func TestNegBelongsToCluster(t *testing.T) { - for _, tc := range []struct { - name string - want bool - }{ - {"k8s1-uid12345-namespace-name-80-1e047e33", true}, - {"k8s1-uid12345-ns-svc-port-16c06497", true}, - {"k8s1-wronguid-namespace-name-80-1e047e33", false}, - {"k8s-be-80--uid1", false}, - {"k8s-ssl-foo--uid", false}, - {"invalidk8sresourcename", false}, - } { - namer := NewNamer("uid1234567890", "fw1") - res := namer.negBelongsToCluster(tc.name) - if res != tc.want { - t.Errorf("namer.negBelongsToCluster(%q) = %v, want %v", tc.name, res, tc.want) - } - } -}