Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-4.13] OCPBUGS-20114: UPSTREAM: <carry>: Do not allow nodes to set forbidden openshift labels #1746

Merged
merged 1 commit into from Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/kubelet/app/options/options.go
Expand Up @@ -164,6 +164,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 @@ -407,7 +407,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 @@ -438,9 +438,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 @@ -481,7 +482,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 @@ -496,6 +497,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)
}