Skip to content

Commit 9f95fdd

Browse files
committed
Mirror pod without OwnerReference should not be created
Signed-off-by: Ted Yu <[email protected]>
1 parent 4c8207d commit 9f95fdd

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

plugin/pkg/admission/noderestriction/admission.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ func (p *Plugin) admitPodCreate(nodeName string, a admission.Attributes) error {
224224
if len(pod.OwnerReferences) > 1 {
225225
return admission.NewForbidden(a, fmt.Errorf("node %q can only create pods with a single owner reference set to itself", nodeName))
226226
}
227+
if len(pod.OwnerReferences) == 0 {
228+
return admission.NewForbidden(a, fmt.Errorf("node %q can only create pods with an owner reference set to itself", nodeName))
229+
}
227230
if len(pod.OwnerReferences) == 1 {
228231
owner := pod.OwnerReferences[0]
229232
if owner.APIVersion != v1.SchemeGroupVersion.String() ||

plugin/pkg/admission/noderestriction/admission_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,9 @@ func Test_nodePlugin_Admit_OwnerReference(t *testing.T) {
13141314
expectErr string
13151315
}{
13161316
{
1317-
name: "no owner",
1318-
owners: nil,
1317+
name: "no owner",
1318+
owners: nil,
1319+
expectErr: "pods \"test\" is forbidden: node \"mynode\" can only create pods with an owner reference set to itself",
13191320
},
13201321
{
13211322
name: "valid owner",

test/integration/auth/node_test.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,26 @@ func TestNodeAuthorizer(t *testing.T) {
224224

225225
createNode2MirrorPod := func(client clientset.Interface) func() error {
226226
return func() error {
227-
_, err := client.CoreV1().Pods("ns").Create(context.TODO(), &corev1.Pod{
227+
const nodeName = "node2"
228+
node, err := client.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
229+
if err != nil {
230+
return err
231+
}
232+
controller := true
233+
_, err = client.CoreV1().Pods("ns").Create(context.TODO(), &corev1.Pod{
228234
ObjectMeta: metav1.ObjectMeta{
229235
Name: "node2mirrorpod",
230236
Annotations: map[string]string{corev1.MirrorPodAnnotationKey: "true"},
237+
OwnerReferences: []metav1.OwnerReference{{
238+
APIVersion: corev1.SchemeGroupVersion.String(),
239+
Kind: "Node",
240+
Name: nodeName,
241+
UID: node.UID,
242+
Controller: &controller,
243+
}},
231244
},
232245
Spec: corev1.PodSpec{
233-
NodeName: "node2",
246+
NodeName: nodeName,
234247
Containers: []corev1.Container{{Name: "image", Image: "busybox"}},
235248
},
236249
}, metav1.CreateOptions{})
@@ -462,9 +475,7 @@ func TestNodeAuthorizer(t *testing.T) {
462475
expectForbidden(t, getPVC(nodeanonClient))
463476
expectForbidden(t, getPV(nodeanonClient))
464477
expectForbidden(t, createNode2NormalPod(nodeanonClient))
465-
expectForbidden(t, createNode2MirrorPod(nodeanonClient))
466478
expectForbidden(t, deleteNode2NormalPod(nodeanonClient))
467-
expectForbidden(t, deleteNode2MirrorPod(nodeanonClient))
468479
expectForbidden(t, createNode2MirrorPodEviction(nodeanonClient))
469480
expectForbidden(t, createNode2(nodeanonClient))
470481
expectForbidden(t, updateNode2Status(nodeanonClient))
@@ -476,8 +487,6 @@ func TestNodeAuthorizer(t *testing.T) {
476487
expectForbidden(t, getPVC(node1Client))
477488
expectForbidden(t, getPV(node1Client))
478489
expectForbidden(t, createNode2NormalPod(nodeanonClient))
479-
expectForbidden(t, createNode2MirrorPod(node1Client))
480-
expectNotFound(t, deleteNode2MirrorPod(node1Client))
481490
expectNotFound(t, createNode2MirrorPodEviction(node1Client))
482491
expectForbidden(t, createNode2(node1Client))
483492
expectNotFound(t, updateNode2Status(node1Client))
@@ -492,21 +501,23 @@ func TestNodeAuthorizer(t *testing.T) {
492501

493502
expectForbidden(t, createNode2NormalPod(nodeanonClient))
494503
// mirror pod and self node lifecycle is allowed
504+
expectAllowed(t, createNode2(node2Client))
505+
expectAllowed(t, updateNode2Status(node2Client))
506+
expectForbidden(t, createNode2MirrorPod(nodeanonClient))
507+
expectForbidden(t, deleteNode2MirrorPod(nodeanonClient))
508+
expectForbidden(t, createNode2MirrorPod(node1Client))
509+
expectNotFound(t, deleteNode2MirrorPod(node1Client))
510+
// create a pod as an admin to add object references
511+
expectAllowed(t, createNode2NormalPod(superuserClient))
512+
495513
expectAllowed(t, createNode2MirrorPod(node2Client))
496514
expectAllowed(t, deleteNode2MirrorPod(node2Client))
497515
expectAllowed(t, createNode2MirrorPod(node2Client))
498516
expectAllowed(t, createNode2MirrorPodEviction(node2Client))
499-
expectAllowed(t, createNode2(node2Client))
500-
expectAllowed(t, updateNode2Status(node2Client))
501517
// self deletion is not allowed
502518
expectForbidden(t, deleteNode2(node2Client))
503519
// modification of another node's status is not allowed
504520
expectForbidden(t, updateNode2Status(node1Client))
505-
// clean up node2
506-
expectAllowed(t, deleteNode2(superuserClient))
507-
508-
// create a pod as an admin to add object references
509-
expectAllowed(t, createNode2NormalPod(superuserClient))
510521

511522
// unidentifiable node and node1 are still forbidden
512523
expectForbidden(t, getSecret(nodeanonClient))
@@ -553,6 +564,8 @@ func TestNodeAuthorizer(t *testing.T) {
553564
expectAllowed(t, createNode2MirrorPod(superuserClient))
554565
expectAllowed(t, createNode2NormalPodEviction(node2Client))
555566
expectAllowed(t, createNode2MirrorPodEviction(node2Client))
567+
// clean up node2
568+
expectAllowed(t, deleteNode2(superuserClient))
556569

557570
// re-create a pod as an admin to add object references
558571
expectAllowed(t, createNode2NormalPod(superuserClient))

0 commit comments

Comments
 (0)