Skip to content

Commit

Permalink
UPSTREAM: <carry>: noderestrictions: add node-role.kubernetes.io/* to…
Browse files Browse the repository at this point in the history
… allowed node labels

Server side validation of node labels was added in kubernetes#90307. We only disabled kubelet-side validation before to make our node role labels work.

UPSTREAM: <carry>: add control plane to allow roles

OpenShift-Rebase-Source: 38bfed3
OpenShift-Rebase-Source: aff4434

UPSTREAM: <carry>: Do not allow nodes to set forbidden openshift labels

Signed-off-by: Harshal Patil <harpatil@redhat.com>
  • Loading branch information
sttts authored and bertinatto committed Nov 27, 2024
1 parent c97537e commit 7b5d8bd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cmd/kubelet/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,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
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,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 @@ -517,9 +517,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 @@ -560,7 +561,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 @@ -575,6 +576,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
43 changes: 43 additions & 0 deletions staging/src/k8s.io/kubelet/pkg/apis/well_known_openshift_labels.go
Original file line number Diff line number Diff line change
@@ -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 7b5d8bd

Please sign in to comment.