Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge negBelongsToCluster into IsNEG #501

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}