k8s.io/kubernetes@v1.29.3/test/e2e/storage/csi_mock/csi_storage_capacity.go (about)

     1  /*
     2  Copyright 2022 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 csi_mock
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"time"
    24  
    25  	"github.com/onsi/ginkgo/v2"
    26  	"github.com/onsi/gomega"
    27  	"google.golang.org/grpc/codes"
    28  	"google.golang.org/grpc/status"
    29  	v1 "k8s.io/api/core/v1"
    30  	storagev1 "k8s.io/api/storage/v1"
    31  	"k8s.io/apimachinery/pkg/api/resource"
    32  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    33  	"k8s.io/apimachinery/pkg/util/wait"
    34  	"k8s.io/apimachinery/pkg/watch"
    35  	cachetools "k8s.io/client-go/tools/cache"
    36  	watchtools "k8s.io/client-go/tools/watch"
    37  	"k8s.io/kubernetes/test/e2e/framework"
    38  	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
    39  	"k8s.io/kubernetes/test/e2e/storage/drivers"
    40  	"k8s.io/kubernetes/test/e2e/storage/utils"
    41  	admissionapi "k8s.io/pod-security-admission/api"
    42  )
    43  
    44  var _ = utils.SIGDescribe("CSI Mock volume storage capacity", func() {
    45  	f := framework.NewDefaultFramework("csi-mock-volumes-capacity")
    46  	f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
    47  	m := newMockDriverSetup(f)
    48  
    49  	ginkgo.Context("storage capacity", func() {
    50  		tests := []struct {
    51  			name              string
    52  			resourceExhausted bool
    53  			lateBinding       bool
    54  			topology          bool
    55  		}{
    56  			{
    57  				name: "unlimited",
    58  			},
    59  			{
    60  				name:              "exhausted, immediate binding",
    61  				resourceExhausted: true,
    62  			},
    63  			{
    64  				name:              "exhausted, late binding, no topology",
    65  				resourceExhausted: true,
    66  				lateBinding:       true,
    67  			},
    68  			{
    69  				name:              "exhausted, late binding, with topology",
    70  				resourceExhausted: true,
    71  				lateBinding:       true,
    72  				topology:          true,
    73  			},
    74  		}
    75  
    76  		createVolume := "CreateVolume"
    77  		deleteVolume := "DeleteVolume"
    78  		// publishVolume := "NodePublishVolume"
    79  		// unpublishVolume := "NodeUnpublishVolume"
    80  		// stageVolume := "NodeStageVolume"
    81  		// unstageVolume := "NodeUnstageVolume"
    82  
    83  		// These calls are assumed to occur in this order for
    84  		// each test run. NodeStageVolume and
    85  		// NodePublishVolume should also be deterministic and
    86  		// only get called once, but sometimes kubelet calls
    87  		// both multiple times, which breaks this test
    88  		// (https://github.com/kubernetes/kubernetes/issues/90250).
    89  		// Therefore they are temporarily commented out until
    90  		// that issue is resolved.
    91  		//
    92  		// NodeUnpublishVolume and NodeUnstageVolume are racing
    93  		// with DeleteVolume, so we cannot assume a deterministic
    94  		// order and have to ignore them
    95  		// (https://github.com/kubernetes/kubernetes/issues/94108).
    96  		deterministicCalls := []string{
    97  			createVolume,
    98  			// stageVolume,
    99  			// publishVolume,
   100  			// unpublishVolume,
   101  			// unstageVolume,
   102  			deleteVolume,
   103  		}
   104  
   105  		for _, t := range tests {
   106  			test := t
   107  			ginkgo.It(test.name, ginkgo.NodeTimeout(csiPodRunningTimeout), func(ctx context.Context) {
   108  				var err error
   109  				params := testParameters{
   110  					lateBinding:    test.lateBinding,
   111  					enableTopology: test.topology,
   112  
   113  					// Not strictly necessary, but runs a bit faster this way
   114  					// and for a while there also was a problem with a two minuted delay
   115  					// due to a bug (https://github.com/kubernetes-csi/csi-test/pull/250).
   116  					disableAttach:  true,
   117  					registerDriver: true,
   118  				}
   119  
   120  				if test.resourceExhausted {
   121  					params.hooks = createPreHook("CreateVolume", func(counter int64) error {
   122  						if counter%2 != 0 {
   123  							return status.Error(codes.ResourceExhausted, "fake error")
   124  						}
   125  						return nil
   126  					})
   127  				}
   128  
   129  				m.init(ctx, params)
   130  				ginkgo.DeferCleanup(m.cleanup)
   131  
   132  				// In contrast to the raw watch, RetryWatcher is expected to deliver all events even
   133  				// when the underlying raw watch gets closed prematurely
   134  				// (https://github.com/kubernetes/kubernetes/pull/93777#discussion_r467932080).
   135  				// This is important because below the test is going to make assertions about the
   136  				// PVC state changes.
   137  				initResource, err := f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).List(ctx, metav1.ListOptions{})
   138  				framework.ExpectNoError(err, "Failed to fetch initial PVC resource")
   139  				listWatcher := &cachetools.ListWatch{
   140  					WatchFunc: func(listOptions metav1.ListOptions) (watch.Interface, error) {
   141  						return f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Watch(ctx, listOptions)
   142  					},
   143  				}
   144  				pvcWatch, err := watchtools.NewRetryWatcher(initResource.GetResourceVersion(), listWatcher)
   145  				framework.ExpectNoError(err, "create PVC watch")
   146  				defer pvcWatch.Stop()
   147  
   148  				sc, claim, pod := m.createPod(ctx, pvcReference)
   149  				gomega.Expect(pod).NotTo(gomega.BeNil(), "while creating pod")
   150  				bindingMode := storagev1.VolumeBindingImmediate
   151  				if test.lateBinding {
   152  					bindingMode = storagev1.VolumeBindingWaitForFirstConsumer
   153  				}
   154  				gomega.Expect(*sc.VolumeBindingMode).To(gomega.Equal(bindingMode), "volume binding mode")
   155  
   156  				err = e2epod.WaitForPodNameRunningInNamespace(ctx, m.cs, pod.Name, pod.Namespace)
   157  				framework.ExpectNoError(err, "failed to start pod")
   158  				err = e2epod.DeletePodWithWait(ctx, m.cs, pod)
   159  				framework.ExpectNoError(err, "failed to delete pod")
   160  				err = m.cs.CoreV1().PersistentVolumeClaims(claim.Namespace).Delete(ctx, claim.Name, metav1.DeleteOptions{})
   161  				framework.ExpectNoError(err, "failed to delete claim")
   162  
   163  				normal := []csiCall{}
   164  				for _, method := range deterministicCalls {
   165  					normal = append(normal, csiCall{expectedMethod: method})
   166  				}
   167  				expected := normal
   168  				// When simulating limited capacity,
   169  				// we expect exactly two CreateVolume
   170  				// calls because the first one should
   171  				// have failed.
   172  				if test.resourceExhausted {
   173  					expected = []csiCall{
   174  						{expectedMethod: createVolume, expectedError: codes.ResourceExhausted},
   175  					}
   176  					expected = append(expected, normal...)
   177  				}
   178  
   179  				var calls []drivers.MockCSICall
   180  				err = wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
   181  					c, index, err := compareCSICalls(ctx, deterministicCalls, expected, m.driver.GetCalls)
   182  					if err != nil {
   183  						return true, fmt.Errorf("error waiting for expected CSI calls: %w", err)
   184  					}
   185  					calls = c
   186  					if index == 0 {
   187  						// No CSI call received yet
   188  						return false, nil
   189  					}
   190  					if len(expected) == index {
   191  						// all calls received
   192  						return true, nil
   193  					}
   194  					return false, nil
   195  				})
   196  				framework.ExpectNoError(err, "while waiting for all CSI calls")
   197  
   198  				// The capacity error is dealt with in two different ways.
   199  				//
   200  				// For delayed binding, the external-provisioner should unset the node annotation
   201  				// to give the scheduler the opportunity to reschedule the pod onto a different
   202  				// node.
   203  				//
   204  				// For immediate binding, the external-scheduler must keep retrying.
   205  				//
   206  				// Unfortunately, the call log is the same in both cases. We have to collect
   207  				// additional evidence that rescheduling really happened. What we have observed
   208  				// above is how the PVC changed over time. Now we can analyze that.
   209  				ginkgo.By("Checking PVC events")
   210  				nodeAnnotationSet := false
   211  				nodeAnnotationReset := false
   212  				watchFailed := false
   213  			loop:
   214  				for {
   215  					select {
   216  					case event, ok := <-pvcWatch.ResultChan():
   217  						if !ok {
   218  							watchFailed = true
   219  							break loop
   220  						}
   221  
   222  						framework.Logf("PVC event %s: %#v", event.Type, event.Object)
   223  						switch event.Type {
   224  						case watch.Modified:
   225  							pvc, ok := event.Object.(*v1.PersistentVolumeClaim)
   226  							if !ok {
   227  								framework.Failf("PVC watch sent %#v instead of a PVC", event.Object)
   228  							}
   229  							_, set := pvc.Annotations["volume.kubernetes.io/selected-node"]
   230  							if set {
   231  								nodeAnnotationSet = true
   232  							} else if nodeAnnotationSet {
   233  								nodeAnnotationReset = true
   234  							}
   235  						case watch.Deleted:
   236  							break loop
   237  						case watch.Error:
   238  							watchFailed = true
   239  							break
   240  						}
   241  					case <-ctx.Done():
   242  						framework.Failf("Timeout while waiting to observe PVC list")
   243  					}
   244  				}
   245  
   246  				// More tests when capacity is limited.
   247  				if test.resourceExhausted {
   248  					for _, call := range calls {
   249  						if call.Method == createVolume {
   250  							gomega.Expect(call.Error).To(gomega.ContainSubstring("code = ResourceExhausted"), "first CreateVolume error in\n%s", calls)
   251  							break
   252  						}
   253  					}
   254  
   255  					switch {
   256  					case watchFailed:
   257  						// If the watch failed or stopped prematurely (which can happen at any time), then we cannot
   258  						// verify whether the annotation was set as expected. This is still considered a successful
   259  						// test.
   260  						framework.Logf("PVC watch delivered incomplete data, cannot check annotation")
   261  					case test.lateBinding:
   262  						gomega.Expect(nodeAnnotationSet).To(gomega.BeTrue(), "selected-node should have been set")
   263  						// Whether it gets reset depends on whether we have topology enabled. Without
   264  						// it, rescheduling is unnecessary.
   265  						if test.topology {
   266  							gomega.Expect(nodeAnnotationReset).To(gomega.BeTrue(), "selected-node should have been set")
   267  						} else {
   268  							gomega.Expect(nodeAnnotationReset).To(gomega.BeFalse(), "selected-node should not have been reset")
   269  						}
   270  					default:
   271  						gomega.Expect(nodeAnnotationSet).To(gomega.BeFalse(), "selected-node should not have been set")
   272  						gomega.Expect(nodeAnnotationReset).To(gomega.BeFalse(), "selected-node should not have been reset")
   273  					}
   274  				}
   275  			})
   276  		}
   277  	})
   278  
   279  	// These tests *only* work on a cluster which has the CSIStorageCapacity feature enabled.
   280  	ginkgo.Context("CSIStorageCapacity", func() {
   281  		var (
   282  			err error
   283  			yes = true
   284  			no  = false
   285  		)
   286  		// Tests that expect a failure are slow because we have to wait for a while
   287  		// to be sure that the volume isn't getting created.
   288  		tests := []struct {
   289  			name            string
   290  			storageCapacity *bool
   291  			capacities      []string
   292  			expectFailure   bool
   293  		}{
   294  			{
   295  				name: "CSIStorageCapacity unused",
   296  			},
   297  			{
   298  				name:            "CSIStorageCapacity disabled",
   299  				storageCapacity: &no,
   300  			},
   301  			{
   302  				name:            "CSIStorageCapacity used, no capacity",
   303  				storageCapacity: &yes,
   304  				expectFailure:   true,
   305  			},
   306  			{
   307  				name:            "CSIStorageCapacity used, insufficient capacity",
   308  				storageCapacity: &yes,
   309  				expectFailure:   true,
   310  				capacities:      []string{"1Mi"},
   311  			},
   312  			{
   313  				name:            "CSIStorageCapacity used, have capacity",
   314  				storageCapacity: &yes,
   315  				capacities:      []string{"100Gi"},
   316  			},
   317  			// We could add more test cases here for
   318  			// various situations, but covering those via
   319  			// the scheduler binder unit tests is faster.
   320  		}
   321  		for _, t := range tests {
   322  			test := t
   323  			ginkgo.It(t.name, ginkgo.NodeTimeout(f.Timeouts.PodStart), func(ctx context.Context) {
   324  				scName := "mock-csi-storage-capacity-" + f.UniqueName
   325  				m.init(ctx, testParameters{
   326  					registerDriver:  true,
   327  					scName:          scName,
   328  					storageCapacity: test.storageCapacity,
   329  					lateBinding:     true,
   330  				})
   331  				ginkgo.DeferCleanup(m.cleanup)
   332  
   333  				// The storage class uses a random name, therefore we have to create it first
   334  				// before adding CSIStorageCapacity objects for it.
   335  				for _, capacityStr := range test.capacities {
   336  					capacityQuantity := resource.MustParse(capacityStr)
   337  					capacity := &storagev1.CSIStorageCapacity{
   338  						ObjectMeta: metav1.ObjectMeta{
   339  							GenerateName: "fake-capacity-",
   340  						},
   341  						// Empty topology, usable by any node.
   342  						StorageClassName: scName,
   343  						NodeTopology:     &metav1.LabelSelector{},
   344  						Capacity:         &capacityQuantity,
   345  					}
   346  					createdCapacity, err := f.ClientSet.StorageV1().CSIStorageCapacities(f.Namespace.Name).Create(ctx, capacity, metav1.CreateOptions{})
   347  					framework.ExpectNoError(err, "create CSIStorageCapacity %+v", *capacity)
   348  					ginkgo.DeferCleanup(framework.IgnoreNotFound(f.ClientSet.StorageV1().CSIStorageCapacities(f.Namespace.Name).Delete), createdCapacity.Name, metav1.DeleteOptions{})
   349  				}
   350  
   351  				// kube-scheduler may need some time before it gets the CSIDriver and CSIStorageCapacity objects.
   352  				// Without them, scheduling doesn't run as expected by the test.
   353  				syncDelay := 5 * time.Second
   354  				time.Sleep(syncDelay)
   355  
   356  				sc, _, pod := m.createPod(ctx, pvcReference) // late binding as specified above
   357  				gomega.Expect(sc.Name).To(gomega.Equal(scName), "pre-selected storage class name not used")
   358  
   359  				condition := anyOf(
   360  					podRunning(ctx, f.ClientSet, pod.Name, pod.Namespace),
   361  					// We only just created the CSIStorageCapacity objects, therefore
   362  					// we have to ignore all older events, plus the syncDelay as our
   363  					// safety margin.
   364  					podHasStorage(ctx, f.ClientSet, pod.Name, pod.Namespace, time.Now().Add(syncDelay)),
   365  				)
   366  				err = wait.PollImmediateUntil(poll, condition, ctx.Done())
   367  				if test.expectFailure {
   368  					switch {
   369  					case errors.Is(err, context.DeadlineExceeded),
   370  						errors.Is(err, wait.ErrorInterrupted(errors.New("timed out waiting for the condition"))),
   371  						errors.Is(err, errNotEnoughSpace):
   372  						// Okay, we expected that.
   373  					case err == nil:
   374  						framework.Fail("pod unexpectedly started to run")
   375  					default:
   376  						framework.Failf("unexpected error while waiting for pod: %v", err)
   377  					}
   378  				} else {
   379  					framework.ExpectNoError(err, "failed to start pod")
   380  				}
   381  
   382  				ginkgo.By("Deleting the previously created pod")
   383  				err = e2epod.DeletePodWithWait(ctx, m.cs, pod)
   384  				framework.ExpectNoError(err, "while deleting")
   385  			})
   386  		}
   387  	})
   388  })