k8s.io/kubernetes@v1.29.3/test/e2e/storage/framework/driver_operations.go (about)

     1  /*
     2  Copyright 2018 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 framework
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	storagev1 "k8s.io/api/storage/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apiserver/pkg/storage/names"
    26  	"k8s.io/kubernetes/pkg/volume/util"
    27  	"k8s.io/kubernetes/test/e2e/framework"
    28  )
    29  
    30  // GetDriverNameWithFeatureTags returns parameters that can be passed to framework.Context.
    31  // For example:
    32  //   - [Driver: nfs]
    33  //   - [Driver: rbd], feature.Volumes
    34  func GetDriverNameWithFeatureTags(driver TestDriver) []interface{} {
    35  	dInfo := driver.GetDriverInfo()
    36  
    37  	return append([]interface{}{fmt.Sprintf("[Driver: %s]", dInfo.Name)}, dInfo.TestTags...)
    38  }
    39  
    40  // CreateVolume creates volume for test unless dynamicPV or CSI ephemeral inline volume test
    41  func CreateVolume(ctx context.Context, driver TestDriver, config *PerTestConfig, volType TestVolType) TestVolume {
    42  	switch volType {
    43  	case InlineVolume, PreprovisionedPV:
    44  		if pDriver, ok := driver.(PreprovisionedVolumeTestDriver); ok {
    45  			return pDriver.CreateVolume(ctx, config, volType)
    46  		}
    47  	case CSIInlineVolume, GenericEphemeralVolume, DynamicPV:
    48  		// No need to create volume
    49  	default:
    50  		framework.Failf("Invalid volType specified: %v", volType)
    51  	}
    52  	return nil
    53  }
    54  
    55  // CopyStorageClass constructs a new StorageClass instance
    56  // with a unique name that is based on namespace + suffix
    57  // using the same storageclass setting from the parameter
    58  func CopyStorageClass(sc *storagev1.StorageClass, ns string, suffix string) *storagev1.StorageClass {
    59  	copy := sc.DeepCopy()
    60  	copy.ObjectMeta.Name = names.SimpleNameGenerator.GenerateName(ns + "-" + suffix)
    61  	copy.ResourceVersion = ""
    62  
    63  	// Remove the default annotation from the storage class if they exists.
    64  	// Multiple storage classes with this annotation will result in failure.
    65  	delete(copy.Annotations, util.BetaIsDefaultStorageClassAnnotation)
    66  	delete(copy.Annotations, util.IsDefaultStorageClassAnnotation)
    67  	return copy
    68  }
    69  
    70  // GetStorageClass constructs a new StorageClass instance
    71  // with a unique name that is based on namespace + suffix.
    72  func GetStorageClass(
    73  	provisioner string,
    74  	parameters map[string]string,
    75  	bindingMode *storagev1.VolumeBindingMode,
    76  	ns string,
    77  ) *storagev1.StorageClass {
    78  	if bindingMode == nil {
    79  		defaultBindingMode := storagev1.VolumeBindingImmediate
    80  		bindingMode = &defaultBindingMode
    81  	}
    82  	return &storagev1.StorageClass{
    83  		TypeMeta: metav1.TypeMeta{
    84  			Kind: "StorageClass",
    85  		},
    86  		ObjectMeta: metav1.ObjectMeta{
    87  			// Name must be unique, so let's base it on namespace name and use GenerateName
    88  			Name: names.SimpleNameGenerator.GenerateName(ns),
    89  		},
    90  		Provisioner:       provisioner,
    91  		Parameters:        parameters,
    92  		VolumeBindingMode: bindingMode,
    93  	}
    94  }