Skip to content

Commit

Permalink
UPSTREAM: <carry>: Do not allow nodes to set forbidden openshift labels
Browse files Browse the repository at this point in the history
Signed-off-by: Harshal Patil <harpatil@redhat.com>
  • Loading branch information
harche authored and soltysh committed Nov 3, 2023
1 parent ec434a5 commit db440e0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cmd/kubelet/app/options/options.go
Expand Up @@ -158,6 +158,9 @@ func ValidateKubeletFlags(f *KubeletFlags) error {
invalidLabelErrs := make(map[string][]string)
for k, v := range f.NodeLabels {
if isKubernetesLabel(k) && !kubeletapis.IsKubeletLabel(k) {
if kubeletapis.IsForbiddenOpenshiftLabel(k) {
continue
}
unknownLabels.Insert(k)
}

Expand Down
12 changes: 9 additions & 3 deletions plugin/pkg/admission/noderestriction/admission.go
Expand Up @@ -433,7 +433,7 @@ func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
// Don't allow a node to register with labels outside the allowed set.
// This would allow a node to add or modify its labels in a way that would let it steer privileged workloads to itself.
modifiedLabels := getModifiedLabels(node.Labels, nil)
if forbiddenLabels := p.getForbiddenLabels(modifiedLabels); len(forbiddenLabels) > 0 {
if forbiddenLabels := p.getForbiddenLabels(modifiedLabels, a.GetOperation()); len(forbiddenLabels) > 0 {
return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to set the following labels: %s", nodeName, strings.Join(forbiddenLabels.List(), ", ")))
}
}
Expand Down Expand Up @@ -464,9 +464,10 @@ func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
// Don't allow a node to update labels outside the allowed set.
// This would allow a node to add or modify its labels in a way that would let it steer privileged workloads to itself.
modifiedLabels := getModifiedLabels(node.Labels, oldNode.Labels)
if forbiddenUpdateLabels := p.getForbiddenLabels(modifiedLabels); len(forbiddenUpdateLabels) > 0 {
if forbiddenUpdateLabels := p.getForbiddenLabels(modifiedLabels, a.GetOperation()); len(forbiddenUpdateLabels) > 0 {
return admission.NewForbidden(a, fmt.Errorf("is not allowed to modify labels: %s", strings.Join(forbiddenUpdateLabels.List(), ", ")))
}

}

return nil
Expand Down Expand Up @@ -507,7 +508,7 @@ func getLabelNamespace(key string) string {
}

// getForbiddenLabels returns the set of labels that may not be added, removed, or modified by the node on create or update.
func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String) sets.String {
func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String, admissionOpn admission.Operation) sets.String {
if len(modifiedLabels) == 0 {
return nil
}
Expand All @@ -522,6 +523,11 @@ func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String) sets.String {
// forbid kubelets from setting unknown kubernetes.io and k8s.io labels on update
if isKubernetesLabel(label) && !kubeletapis.IsKubeletLabel(label) {
// TODO: defer to label policy once available
if admissionOpn == admission.Create {
if kubeletapis.IsForbiddenOpenshiftLabel(label) {
continue
}
}
forbiddenLabels.Insert(label)
}
}
Expand Down
6 changes: 0 additions & 6 deletions staging/src/k8s.io/kubelet/pkg/apis/well_known_labels.go
Expand Up @@ -45,12 +45,6 @@ var kubeletLabels = sets.NewString(

LabelOS,
LabelArch,

// These are special for OpenShift:
"node-role.kubernetes.io/control-plane",
"node-role.kubernetes.io/master",
"node-role.kubernetes.io/worker",
"node-role.kubernetes.io/etcd",
)

var kubeletLabelNamespaces = sets.NewString(
Expand Down
43 changes: 43 additions & 0 deletions staging/src/k8s.io/kubelet/pkg/apis/well_known_openshift_labels.go
@@ -0,0 +1,43 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apis

import (
"k8s.io/apimachinery/pkg/util/sets"
)

const (
NodeLabelControlPlane = "node-role.kubernetes.io/control-plane"
NodeLabelMaster = "node-role.kubernetes.io/master"
NodeLabelWorker = "node-role.kubernetes.io/worker"
NodeLabelEtcd = "node-role.kubernetes.io/etcd"
)

var openshiftNodeLabels = sets.NewString(
NodeLabelControlPlane,
NodeLabelMaster,
NodeLabelWorker,
NodeLabelEtcd,
)

func OpenShiftNodeLabels() []string {
return openshiftNodeLabels.List()
}

func IsForbiddenOpenshiftLabel(label string) bool {
return openshiftNodeLabels.Has(label)
}

0 comments on commit db440e0

Please sign in to comment.