Skip to content

Commit

Permalink
merge negBelongsToCluster into IsNEG
Browse files Browse the repository at this point in the history
  • Loading branch information
freehan committed Oct 15, 2018
1 parent 2d2c91a commit 29bc4fd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
26 changes: 11 additions & 15 deletions pkg/utils/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
}
Expand Down
26 changes: 6 additions & 20 deletions pkg/utils/namer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
}

0 comments on commit 29bc4fd

Please sign in to comment.