sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/kubernetes/pvc/pvc.go (about)

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2022 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 pvc
    21  
    22  import (
    23  	"context"
    24  	"log"
    25  	"time"
    26  
    27  	. "github.com/onsi/gomega"
    28  	corev1 "k8s.io/api/core/v1"
    29  	"k8s.io/apimachinery/pkg/api/resource"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	"k8s.io/client-go/kubernetes"
    32  	"k8s.io/utils/ptr"
    33  )
    34  
    35  const (
    36  	pvcOperationTimeout             = 30 * time.Second
    37  	pvcOperationSleepBetweenRetries = 3 * time.Second
    38  )
    39  
    40  /*
    41  Example PVC spec with sc name in annotation
    42  ---
    43  kind: PersistentVolumeClaim
    44  apiVersion: v1
    45  metadata:
    46    name: dd-managed-hdd-5g
    47    annotations:
    48      volume.beta.kubernetes.io/storage-class: managedhdd
    49  spec:
    50    accessModes:
    51    - ReadWriteOnce
    52    resources:
    53      requests:
    54        storage: 5Gi
    55  */
    56  
    57  /*
    58  Example PVC spec with sc name in storageClassName field
    59  ---
    60  apiVersion: v1
    61  kind: PersistentVolumeClaim
    62  metadata:
    63    name: pvc-azuredisk
    64  spec:
    65    accessModes:
    66      - ReadWriteOnce
    67    resources:
    68      requests:
    69        storage: 5Gi
    70    storageClassName: managed-csi
    71  */
    72  
    73  type Builder struct {
    74  	pvc *corev1.PersistentVolumeClaim
    75  }
    76  
    77  func Create(pvcName string, storageRequest string) (*Builder, error) {
    78  	qunatity, err := resource.ParseQuantity(storageRequest)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	pvcBuilder := &Builder{
    83  		pvc: &corev1.PersistentVolumeClaim{
    84  			ObjectMeta: metav1.ObjectMeta{
    85  				Name: pvcName,
    86  			},
    87  			Spec: corev1.PersistentVolumeClaimSpec{
    88  				AccessModes: []corev1.PersistentVolumeAccessMode{
    89  					corev1.ReadWriteOnce,
    90  				},
    91  				Resources: corev1.ResourceRequirements{
    92  					Requests: map[corev1.ResourceName]resource.Quantity{
    93  						corev1.ResourceStorage: qunatity,
    94  					},
    95  				},
    96  			},
    97  		},
    98  	}
    99  	return pvcBuilder, nil
   100  }
   101  
   102  func (b *Builder) WithAnnotations(annotations map[string]string) *Builder {
   103  	b.pvc.Annotations = annotations
   104  	return b
   105  }
   106  
   107  func (b *Builder) WithStorageClass(scName string) *Builder {
   108  	b.pvc.Spec.StorageClassName = ptr.To(scName)
   109  	return b
   110  }
   111  
   112  func (b *Builder) DeployPVC(clientset *kubernetes.Clientset) error {
   113  	Eventually(func(g Gomega) {
   114  		_, err := clientset.CoreV1().PersistentVolumeClaims("default").Create(context.TODO(), b.pvc, metav1.CreateOptions{})
   115  		if err != nil {
   116  			log.Printf("Error trying to deploy storage class %s in namespace %s:%s\n", b.pvc.Name, b.pvc.ObjectMeta.Namespace, err.Error())
   117  		}
   118  		g.Expect(err).To(HaveOccurred())
   119  	}, pvcOperationTimeout, pvcOperationSleepBetweenRetries).Should(Succeed())
   120  
   121  	return nil
   122  }