Skip to content

Commit fb17216

Browse files
stttsbertinatto
authored andcommitted
UPSTREAM: <carry>: noderestrictions: add node-role.kubernetes.io/* to 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 <[email protected]>
1 parent 886c58c commit fb17216

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

cmd/kubelet/app/options/options.go

+3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ func ValidateKubeletFlags(f *KubeletFlags) error {
155155
invalidLabelErrs := make(map[string][]string)
156156
for k, v := range f.NodeLabels {
157157
if isKubernetesLabel(k) && !kubeletapis.IsKubeletLabel(k) {
158+
if kubeletapis.IsForbiddenOpenshiftLabel(k) {
159+
continue
160+
}
158161
unknownLabels.Insert(k)
159162
}
160163

plugin/pkg/admission/noderestriction/admission.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
486486
// Don't allow a node to register with labels outside the allowed set.
487487
// This would allow a node to add or modify its labels in a way that would let it steer privileged workloads to itself.
488488
modifiedLabels := getModifiedLabels(node.Labels, nil)
489-
if forbiddenLabels := p.getForbiddenLabels(modifiedLabels); len(forbiddenLabels) > 0 {
489+
if forbiddenLabels := p.getForbiddenLabels(modifiedLabels, a.GetOperation()); len(forbiddenLabels) > 0 {
490490
return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to set the following labels: %s", nodeName, strings.Join(forbiddenLabels.List(), ", ")))
491491
}
492492
}
@@ -517,9 +517,10 @@ func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
517517
// Don't allow a node to update labels outside the allowed set.
518518
// This would allow a node to add or modify its labels in a way that would let it steer privileged workloads to itself.
519519
modifiedLabels := getModifiedLabels(node.Labels, oldNode.Labels)
520-
if forbiddenUpdateLabels := p.getForbiddenLabels(modifiedLabels); len(forbiddenUpdateLabels) > 0 {
520+
if forbiddenUpdateLabels := p.getForbiddenLabels(modifiedLabels, a.GetOperation()); len(forbiddenUpdateLabels) > 0 {
521521
return admission.NewForbidden(a, fmt.Errorf("is not allowed to modify labels: %s", strings.Join(forbiddenUpdateLabels.List(), ", ")))
522522
}
523+
523524
}
524525

525526
return nil
@@ -560,7 +561,7 @@ func getLabelNamespace(key string) string {
560561
}
561562

562563
// getForbiddenLabels returns the set of labels that may not be added, removed, or modified by the node on create or update.
563-
func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String) sets.String {
564+
func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String, admissionOpn admission.Operation) sets.String {
564565
if len(modifiedLabels) == 0 {
565566
return nil
566567
}
@@ -575,6 +576,11 @@ func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String) sets.String {
575576
// forbid kubelets from setting unknown kubernetes.io and k8s.io labels on update
576577
if isKubernetesLabel(label) && !kubeletapis.IsKubeletLabel(label) {
577578
// TODO: defer to label policy once available
579+
if admissionOpn == admission.Create {
580+
if kubeletapis.IsForbiddenOpenshiftLabel(label) {
581+
continue
582+
}
583+
}
578584
forbiddenLabels.Insert(label)
579585
}
580586
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package apis
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/util/sets"
21+
)
22+
23+
const (
24+
NodeLabelControlPlane = "node-role.kubernetes.io/control-plane"
25+
NodeLabelMaster = "node-role.kubernetes.io/master"
26+
NodeLabelWorker = "node-role.kubernetes.io/worker"
27+
NodeLabelEtcd = "node-role.kubernetes.io/etcd"
28+
)
29+
30+
var openshiftNodeLabels = sets.NewString(
31+
NodeLabelControlPlane,
32+
NodeLabelMaster,
33+
NodeLabelWorker,
34+
NodeLabelEtcd,
35+
)
36+
37+
func OpenShiftNodeLabels() []string {
38+
return openshiftNodeLabels.List()
39+
}
40+
41+
func IsForbiddenOpenshiftLabel(label string) bool {
42+
return openshiftNodeLabels.Has(label)
43+
}

0 commit comments

Comments
 (0)