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

Add validations for managed nodegroup labels #6947

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
22 changes: 13 additions & 9 deletions pkg/apis/eksctl.io/v1alpha5/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func ValidateNodeGroup(i int, ng *NodeGroup, cfg *ClusterConfig) error {
return err
}

if err := validateNodeGroupLabels(ng.Labels); err != nil {
if err := validateLabels(ng.Labels); err != nil {
return err
}

Expand Down Expand Up @@ -918,16 +918,16 @@ func validateOutpostARN(val string) error {
return nil
}

// validateNodeGroupLabels uses proper Kubernetes label validation,
// it's designed to make sure users don't pass weird labels to the
// nodes, which would prevent kubelets to startup properly
func validateNodeGroupLabels(labels map[string]string) error {
// validateLabels uses proper Kubernetes label validation,
// it's designed to make sure users don't pass invalid or disallowed labels,
// which would prevent kubelets to startup properly
func validateLabels(labels map[string]string) error {
// compact version based on:
// - https://github.com/kubernetes/kubernetes/blob/v1.13.2/cmd/kubelet/app/options/options.go#L257-L267
// - https://github.com/kubernetes/kubernetes/blob/v1.13.2/pkg/kubelet/apis/well_known_labels.go
// we cannot import those packages because they break other dependencies

unknownKubernetesLabels := []string{}
disallowedKubernetesLabels := []string{}

for label := range labels {
labelParts := strings.Split(label, "/")
Expand All @@ -946,13 +946,13 @@ func validateNodeGroupLabels(labels map[string]string) error {
if len(labelParts) == 2 {
namespace := labelParts[0]
if isKubernetesLabel(namespace) && !kubeletapis.IsKubeletLabel(label) {
unknownKubernetesLabels = append(unknownKubernetesLabels, label)
disallowedKubernetesLabels = append(disallowedKubernetesLabels, label)
}
}
}

if len(unknownKubernetesLabels) > 0 {
return fmt.Errorf("unknown 'kubernetes.io' or 'k8s.io' labels were specified: %v", unknownKubernetesLabels)
if len(disallowedKubernetesLabels) > 0 {
return fmt.Errorf("the following nodegroup labels are disallowed as they have reserved prefixes [kubernetes.io/, k8s.io/]: %v", disallowedKubernetesLabels)
}
return nil
}
Expand Down Expand Up @@ -1170,6 +1170,10 @@ func ValidateManagedNodeGroup(index int, ng *ManagedNodeGroup) error {
return err
}

if err := validateLabels(ng.Labels); err != nil {
return err
}

switch {
case ng.LaunchTemplate != nil:
if ng.LaunchTemplate.ID == "" {
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/eksctl.io/v1alpha5/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2086,12 +2086,24 @@ var _ = Describe("ClusterConfig validation", func() {
ng := newNodeGroup()
ng.Labels = e.labels
ng.Taints = e.taints

mng := api.NewManagedNodeGroup()
mng.Labels = e.labels
mng.Taints = e.taints

err := api.ValidateNodeGroup(0, ng, api.NewClusterConfig())
if e.valid {
Expect(err).NotTo(HaveOccurred())
} else {
Expect(err).To(HaveOccurred())
}

err = api.ValidateManagedNodeGroup(0, mng)
if e.valid {
Expect(err).NotTo(HaveOccurred())
} else {
Expect(err).To(HaveOccurred())
}
},
Entry("disallowed label", labelsTaintsEntry{
labels: map[string]string{
Expand Down