k8s.io/kubernetes@v1.29.3/test/e2e/framework/node/runtimeclass/runtimeclass.go (about)

     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 runtimeclass
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/onsi/ginkgo/v2"
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/kubernetes/test/e2e/framework"
    27  	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
    28  	storageutils "k8s.io/kubernetes/test/e2e/storage/utils"
    29  	imageutils "k8s.io/kubernetes/test/utils/image"
    30  	utilpointer "k8s.io/utils/pointer"
    31  )
    32  
    33  const (
    34  	// PreconfiguredRuntimeClassHandler is the name of the runtime handler
    35  	// that is expected to be preconfigured in the test environment.
    36  	PreconfiguredRuntimeClassHandler = "test-handler"
    37  )
    38  
    39  // NewRuntimeClassPod returns a test pod with the given runtimeClassName
    40  func NewRuntimeClassPod(runtimeClassName string) *v1.Pod {
    41  	return &v1.Pod{
    42  		ObjectMeta: metav1.ObjectMeta{
    43  			GenerateName: fmt.Sprintf("test-runtimeclass-%s-", runtimeClassName),
    44  		},
    45  		Spec: v1.PodSpec{
    46  			RuntimeClassName: &runtimeClassName,
    47  			Containers: []v1.Container{{
    48  				Name:    "test",
    49  				Image:   imageutils.GetE2EImage(imageutils.BusyBox),
    50  				Command: []string{"true"},
    51  			}},
    52  			RestartPolicy:                v1.RestartPolicyNever,
    53  			AutomountServiceAccountToken: utilpointer.BoolPtr(false),
    54  		},
    55  	}
    56  }
    57  
    58  // NodeSupportsPreconfiguredRuntimeClassHandler checks if test-handler is configured by reading the configuration from container runtime config.
    59  // If no error is returned, the container runtime is assumed to support the test-handler, otherwise an error will be returned.
    60  func NodeSupportsPreconfiguredRuntimeClassHandler(ctx context.Context, f *framework.Framework) error {
    61  	node, err := e2enode.GetRandomReadySchedulableNode(ctx, f.ClientSet)
    62  	framework.ExpectNoError(err)
    63  	hostExec := storageutils.NewHostExec(f)
    64  	ginkgo.DeferCleanup(hostExec.Cleanup)
    65  
    66  	// This is a hacky check that greps the container runtime config to determine if the test-handler is the underlying container runtime config.
    67  	// For containerd, this is configured in kube-up for GCE clusters here: https://github.com/kubernetes/kubernetes/blob/eb729620c522753bc7ae61fc2c7b7ea19d4aad2f/cluster/gce/gci/configure-helper.sh#L3069-L3076
    68  	// For cri-o, see configuration here: https://github.com/cri-o/cri-o/blob/5dc6a035c940f5dde3a727b2e2d8d4b7e371ad55/contrib/test/ci/e2e-base.yml#L2-L13
    69  	// If the `runtimes.test-handler` substring is found in the runtime config, it is assumed that the handler is configured.
    70  	cmd := fmt.Sprintf(`if [ -e '/etc/containerd/config.toml' ]; then
    71  grep -q 'runtimes.%s' /etc/containerd/config.toml
    72    exit
    73  fi
    74  
    75  if [ -e '/etc/crio/crio.conf' ]; then
    76    grep -q 'runtimes.%s' /etc/crio/crio.conf
    77    exit
    78  fi
    79  
    80  exit 1
    81  `, PreconfiguredRuntimeClassHandler, PreconfiguredRuntimeClassHandler)
    82  
    83  	_, err = hostExec.IssueCommandWithResult(ctx, cmd, node)
    84  	return err
    85  }