k8s.io/kubernetes@v1.29.3/test/e2e/common/node/pod_admission.go (about)

     1  /*
     2  Copyright 2019 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 node
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/onsi/ginkgo/v2"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/labels"
    27  	"k8s.io/kubernetes/test/e2e/framework"
    28  	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
    29  	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
    30  	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
    31  	imageutils "k8s.io/kubernetes/test/utils/image"
    32  	admissionapi "k8s.io/pod-security-admission/api"
    33  )
    34  
    35  var _ = SIGDescribe("PodOSRejection", framework.WithNodeConformance(), func() {
    36  	f := framework.NewDefaultFramework("pod-os-rejection")
    37  	f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
    38  	ginkgo.Context("Kubelet", func() {
    39  		ginkgo.It("should reject pod when the node OS doesn't match pod's OS", func(ctx context.Context) {
    40  			linuxNode, err := findLinuxNode(ctx, f)
    41  			framework.ExpectNoError(err)
    42  			pod := &v1.Pod{
    43  				ObjectMeta: metav1.ObjectMeta{
    44  					Name:      "wrong-pod-os",
    45  					Namespace: f.Namespace.Name,
    46  				},
    47  				Spec: v1.PodSpec{
    48  					OS: &v1.PodOS{
    49  						Name: "windows", // explicitly set the pod OS to a wrong but valid value
    50  					},
    51  					Containers: []v1.Container{
    52  						{
    53  							Name:  "wrong-pod-os",
    54  							Image: imageutils.GetPauseImageName(),
    55  						},
    56  					},
    57  					NodeName: linuxNode.Name, // Set the node to an node which doesn't support
    58  				},
    59  			}
    60  			pod = e2epod.NewPodClient(f).Create(ctx, pod)
    61  			// Check the pod is still not running
    62  			err = e2epod.WaitForPodFailedReason(ctx, f.ClientSet, pod, "PodOSNotSupported", f.Timeouts.PodStartShort)
    63  			framework.ExpectNoError(err)
    64  		})
    65  	})
    66  })
    67  
    68  // findLinuxNode finds a Linux node that is Ready and Schedulable
    69  func findLinuxNode(ctx context.Context, f *framework.Framework) (v1.Node, error) {
    70  	selector := labels.Set{"kubernetes.io/os": "linux"}.AsSelector()
    71  	nodeList, err := f.ClientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{LabelSelector: selector.String()})
    72  
    73  	if err != nil {
    74  		return v1.Node{}, err
    75  	}
    76  
    77  	var targetNode v1.Node
    78  	foundNode := false
    79  	for _, n := range nodeList.Items {
    80  		if e2enode.IsNodeReady(&n) && e2enode.IsNodeSchedulable(&n) {
    81  			targetNode = n
    82  			foundNode = true
    83  			break
    84  		}
    85  	}
    86  
    87  	if !foundNode {
    88  		e2eskipper.Skipf("Could not find and ready and schedulable Linux nodes")
    89  	}
    90  
    91  	return targetNode, nil
    92  }