k8s.io/kubernetes@v1.29.3/test/e2e_node/lock_contention_linux_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2021 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package e2enode
    21  
    22  import (
    23  	"context"
    24  	"time"
    25  
    26  	"golang.org/x/sys/unix"
    27  
    28  	"github.com/onsi/ginkgo/v2"
    29  	"github.com/onsi/gomega"
    30  	"k8s.io/kubernetes/test/e2e/framework"
    31  )
    32  
    33  const contentionLockFile = "/var/run/kubelet.lock"
    34  
    35  // Kubelet Lock contention tests the lock contention feature.
    36  // Disruptive because the kubelet is restarted in the test.
    37  // NodeSpecialFeature:LockContention because we don't want the test to be picked up by any other
    38  // test suite, hence the unique name "LockContention".
    39  var _ = SIGDescribe("Lock contention", framework.WithSlow(), framework.WithDisruptive(), "[NodeSpecialFeature:LockContention]", func() {
    40  
    41  	// Requires `--lock-file` & `--exit-on-lock-contention` flags to be set on the Kubelet.
    42  	ginkgo.It("Kubelet should stop when the test acquires the lock on lock file and restart once the lock is released", func(ctx context.Context) {
    43  
    44  		ginkgo.By("perform kubelet health check to check if kubelet is healthy and running.")
    45  		// Precautionary check that kubelet is healthy before running the test.
    46  		gomega.Expect(kubeletHealthCheck(kubeletHealthCheckURL)).To(gomega.BeTrue())
    47  
    48  		ginkgo.By("acquiring the lock on lock file i.e /var/run/kubelet.lock")
    49  		// Open the file with the intention to acquire the lock, this would imitate the behaviour
    50  		// of the another kubelet(self-hosted) trying to start. When this lock contention happens
    51  		// it is expected that the running kubelet must terminate and wait until the lock on the
    52  		// lock file is released.
    53  		// Kubelet uses the same approach to acquire the lock on lock file as shown here:
    54  		// https://github.com/kubernetes/kubernetes/blob/9d2b361ebc7ef28f7cb75596ef40b7c239732d37/cmd/kubelet/app/server.go#L512-#L523
    55  		// and the function definition of Acquire is here:
    56  		// https://github.com/kubernetes/kubernetes/blob/9d2b361ebc7ef28f7cb75596ef40b7c239732d37/pkg/util/flock/flock_unix.go#L26
    57  		fd, err := unix.Open(contentionLockFile, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0600)
    58  		framework.ExpectNoError(err)
    59  		// Defer the lock release in case test fails and we don't reach the step of the release
    60  		// lock. This ensures that we release the lock for sure.
    61  		defer func() {
    62  			err = unix.Flock(fd, unix.LOCK_UN)
    63  			framework.ExpectNoError(err)
    64  		}()
    65  		// Acquire lock.
    66  		err = unix.Flock(fd, unix.LOCK_EX)
    67  		framework.ExpectNoError(err)
    68  
    69  		ginkgo.By("verifying the kubelet is not healthy as there was a lock contention.")
    70  		// Once the lock is acquired, check if the kubelet is in healthy state or not.
    71  		// It should not be as the lock contention forces the kubelet to stop.
    72  		gomega.Eventually(ctx, func() bool {
    73  			return kubeletHealthCheck(kubeletHealthCheckURL)
    74  		}, 10*time.Second, time.Second).Should(gomega.BeFalse())
    75  	})
    76  })