Skip to content

Commit 0bb57fe

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 c4ee9ce commit 0bb57fe

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
@@ -504,7 +504,7 @@ func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
504504
// Don't allow a node to register with labels outside the allowed set.
505505
// This would allow a node to add or modify its labels in a way that would let it steer privileged workloads to itself.
506506
modifiedLabels := getModifiedLabels(node.Labels, nil)
507-
if forbiddenLabels := p.getForbiddenLabels(modifiedLabels); len(forbiddenLabels) > 0 {
507+
if forbiddenLabels := p.getForbiddenLabels(modifiedLabels, a.GetOperation()); len(forbiddenLabels) > 0 {
508508
return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to set the following labels: %s", nodeName, strings.Join(forbiddenLabels.List(), ", ")))
509509
}
510510
}
@@ -535,9 +535,10 @@ func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
535535
// Don't allow a node to update labels outside the allowed set.
536536
// This would allow a node to add or modify its labels in a way that would let it steer privileged workloads to itself.
537537
modifiedLabels := getModifiedLabels(node.Labels, oldNode.Labels)
538-
if forbiddenUpdateLabels := p.getForbiddenLabels(modifiedLabels); len(forbiddenUpdateLabels) > 0 {
538+
if forbiddenUpdateLabels := p.getForbiddenLabels(modifiedLabels, a.GetOperation()); len(forbiddenUpdateLabels) > 0 {
539539
return admission.NewForbidden(a, fmt.Errorf("is not allowed to modify labels: %s", strings.Join(forbiddenUpdateLabels.List(), ", ")))
540540
}
541+
541542
}
542543

543544
return nil
@@ -578,7 +579,7 @@ func getLabelNamespace(key string) string {
578579
}
579580

580581
// getForbiddenLabels returns the set of labels that may not be added, removed, or modified by the node on create or update.
581-
func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String) sets.String {
582+
func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String, admissionOpn admission.Operation) sets.String {
582583
if len(modifiedLabels) == 0 {
583584
return nil
584585
}
@@ -593,6 +594,11 @@ func (p *Plugin) getForbiddenLabels(modifiedLabels sets.String) sets.String {
593594
// forbid kubelets from setting unknown kubernetes.io and k8s.io labels on update
594595
if isKubernetesLabel(label) && !kubeletapis.IsKubeletLabel(label) {
595596
// TODO: defer to label policy once available
597+
if admissionOpn == admission.Create {
598+
if kubeletapis.IsForbiddenOpenshiftLabel(label) {
599+
continue
600+
}
601+
}
596602
forbiddenLabels.Insert(label)
597603
}
598604
}
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)