Skip to content

Commit f787db2

Browse files
committed
return err directly when nodename is not consistent in cert
1 parent d5f76a9 commit f787db2

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

plugin/pkg/admission/noderestriction/admission.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ func (p *Plugin) admitPVCStatus(nodeName string, a admission.Attributes) error {
390390

391391
func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
392392
requestedName := a.GetName()
393+
394+
if requestedName != nodeName {
395+
return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to modify node %q", nodeName, requestedName))
396+
}
397+
393398
if a.GetOperation() == admission.Create {
394399
node, ok := a.GetObject().(*api.Node)
395400
if !ok {
@@ -409,9 +414,6 @@ func (p *Plugin) admitNode(nodeName string, a admission.Attributes) error {
409414
return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to set the following labels: %s", nodeName, strings.Join(forbiddenLabels.List(), ", ")))
410415
}
411416
}
412-
if requestedName != nodeName {
413-
return admission.NewForbidden(a, fmt.Errorf("node %q is not allowed to modify node %q", nodeName, requestedName))
414-
}
415417

416418
if a.GetOperation() == admission.Update {
417419
node, ok := a.GetObject().(*api.Node)

plugin/pkg/admission/noderestriction/admission_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ func Test_nodePlugin_Admit(t *testing.T) {
920920
{
921921
name: "forbid create of my node with forbidden labels",
922922
podsGetter: noExistingPods,
923-
attributes: admission.NewAttributesRecord(setForbiddenCreateLabels(mynodeObj, ""), nil, nodeKind, mynodeObj.Namespace, "", nodeResource, "", admission.Create, &metav1.CreateOptions{}, false, mynode),
923+
attributes: admission.NewAttributesRecord(setForbiddenCreateLabels(mynodeObj, ""), nil, nodeKind, mynodeObj.Namespace, "mynode", nodeResource, "", admission.Create, &metav1.CreateOptions{}, false, mynode),
924924
err: `is not allowed to set the following labels: foo.node-restriction.kubernetes.io/foo, node-restriction.kubernetes.io/foo, other.k8s.io/foo, other.kubernetes.io/foo`,
925925
},
926926
{

0 commit comments

Comments
 (0)