From 096bd5f5d86cb0f2003432350d840505c8194255 Mon Sep 17 00:00:00 2001 From: Gustavo Paiva Date: Wed, 26 Jun 2019 19:24:30 -0300 Subject: [PATCH] block not allowed node labels on kubelet --- cmd/kubelet/app/options/options.go | 5 +-- cmd/kubelet/app/options/options_test.go | 45 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/cmd/kubelet/app/options/options.go b/cmd/kubelet/app/options/options.go index 69139621fd6..8d61426cedf 100644 --- a/cmd/kubelet/app/options/options.go +++ b/cmd/kubelet/app/options/options.go @@ -30,7 +30,6 @@ import ( "k8s.io/apimachinery/pkg/util/sets" utilfeature "k8s.io/apiserver/pkg/util/feature" cliflag "k8s.io/component-base/cli/flag" - "k8s.io/klog" "k8s.io/kubelet/config/v1beta1" "k8s.io/kubernetes/pkg/apis/core" "k8s.io/kubernetes/pkg/features" @@ -247,9 +246,7 @@ func ValidateKubeletFlags(f *KubeletFlags) error { } } if len(unknownLabels) > 0 { - // TODO(liggitt): in 1.16, return an error - klog.Warningf("unknown 'kubernetes.io' or 'k8s.io' labels specified with --node-labels: %v", unknownLabels.List()) - klog.Warningf("in 1.16, --node-labels in the 'kubernetes.io' namespace must begin with an allowed prefix (%s) or be in the specifically allowed set (%s)", strings.Join(kubeletapis.KubeletLabelNamespaces(), ", "), strings.Join(kubeletapis.KubeletLabels(), ", ")) + return fmt.Errorf("unknown 'kubernetes.io' or 'k8s.io' labels specified with --node-labels: %v\n--node-labels in the 'kubernetes.io' namespace must begin with an allowed prefix (%s) or be in the specifically allowed set (%s)", unknownLabels.List(), strings.Join(kubeletapis.KubeletLabelNamespaces(), ", "), strings.Join(kubeletapis.KubeletLabels(), ", ")) } return nil diff --git a/cmd/kubelet/app/options/options_test.go b/cmd/kubelet/app/options/options_test.go index 1c976658129..dd78175e595 100644 --- a/cmd/kubelet/app/options/options_test.go +++ b/cmd/kubelet/app/options/options_test.go @@ -145,3 +145,48 @@ func asArgs(fn, defaultFn func(*pflag.FlagSet)) []string { }) return args } + +func TestValidateKubeletFlags(t *testing.T) { + tests := []struct { + name string + error bool + labels map[string]string + }{ + { + name: "Invalid kubernetes.io label", + error: true, + labels: map[string]string{ + "beta.kubernetes.io/metadata-proxy-ready": "true", + }, + }, + { + name: "Valid label outside of kubernetes.io and k8s.io", + error: false, + labels: map[string]string{ + "cloud.google.com/metadata-proxy-ready": "true", + }, + }, + { + name: "Empty label list", + error: false, + labels: map[string]string{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := ValidateKubeletFlags(&KubeletFlags{ + NodeLabels: tt.labels, + }) + + if tt.error && err == nil { + t.Errorf("ValidateKubeletFlags should have failed with labels: %+v", tt.labels) + } + + if !tt.error && err != nil { + t.Errorf("ValidateKubeletFlags should not have failed with labels: %+v", tt.labels) + } + }) + } + +}