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

Further improve cloudLabel validation #10910

Merged
merged 1 commit into from
Feb 27, 2021
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
9 changes: 6 additions & 3 deletions pkg/apis/kops/validation/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func validateEtcdMemberUpdate(fp *field.Path, obj kops.EtcdMemberSpec, status *k

func validateClusterCloudLabels(cluster *kops.Cluster, fldPath *field.Path) (allErrs field.ErrorList) {
labels := cluster.Spec.CloudLabels
return validateCloudLabels(labels, fldPath)
}

func validateCloudLabels(labels map[string]string, fldPath *field.Path) (allErrs field.ErrorList) {
if labels == nil {
return allErrs
}
Expand All @@ -142,11 +146,10 @@ func validateClusterCloudLabels(cluster *kops.Cluster, fldPath *field.Path) (all
allErrs = append(allErrs, field.Forbidden(fldPath.Child(reservedKey), fmt.Sprintf("%q is a reserved label and cannot be used as a custom label", reservedKey)))
}
}

reservedPrefixes := []string{
"kubernetes.io/cluster/",
"k8s.io/role/",
"kops.k8s.io/",
"k8s.io/",
"kubernetes.io/",
}

for _, reservedPrefix := range reservedPrefixes {
Expand Down
14 changes: 12 additions & 2 deletions pkg/apis/kops/validation/instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,21 @@ func validateIGCloudLabels(ig *kops.InstanceGroup, fldPath *field.Path) (allErrs
return allErrs
}

genericLabels := make(map[string]string)

for key, value := range labels {
if key == aws.CloudTagInstanceGroupName && value != ig.ObjectMeta.Name {
allErrs = append(allErrs, field.Invalid(fldPath.Child(aws.CloudTagInstanceGroupName), key, "Node label may only contain a single slash"))
if key == aws.CloudTagInstanceGroupName {

if value != ig.ObjectMeta.Name {
allErrs = append(allErrs, field.Invalid(fldPath.Child(aws.CloudTagInstanceGroupName), key, "Node label may only contain a single slash"))
}
} else {
genericLabels[key] = value
}
}

allErrs = append(allErrs, validateCloudLabels(genericLabels, fldPath)...)

return allErrs
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/apis/kops/validation/instancegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,44 @@ func TestValidNodeLabels(t *testing.T) {
}
}

func TestValidateIGCloudLabels(t *testing.T) {

grid := []struct {
label string
expected []string
}{

{
label: "k8s.io/cluster-autoscaler/test.example.com",
},
{
label: "KubernetesCluster",
expected: []string{"Forbidden::spec.cloudLabels.KubernetesCluster"},
},
{
label: "MyBillingLabel",
},
{
label: "subdomain.domain.tld/foo/bar",
},
}

for _, g := range grid {
ig := &kops.InstanceGroup{
ObjectMeta: v1.ObjectMeta{
Name: "some-ig",
},
Spec: kops.InstanceGroupSpec{
Role: "Node",
CloudLabels: make(map[string]string),
},
}
ig.Spec.CloudLabels[g.label] = "placeholder"
errs := ValidateInstanceGroup(ig, nil)
testErrors(t, g.label, errs, g.expected)
}
}

func TestIGCloudLabelIsIGName(t *testing.T) {

grid := []struct {
Expand Down