github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/native_object_util.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package apps
    21  
    22  import (
    23  	vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
    24  	corev1 "k8s.io/api/core/v1"
    25  	storagev1 "k8s.io/api/storage/v1"
    26  	"k8s.io/apimachinery/pkg/api/resource"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/kubectl/pkg/util/storage"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  
    31  	"github.com/1aal/kubeblocks/pkg/testutil"
    32  )
    33  
    34  // ConfigMap
    35  
    36  func NewConfigMap(namespace, name string, options ...any) *corev1.ConfigMap {
    37  	cm := &corev1.ConfigMap{
    38  		ObjectMeta: metav1.ObjectMeta{
    39  			Name:      name,
    40  			Namespace: namespace,
    41  		},
    42  		Data: map[string]string{},
    43  	}
    44  	for _, option := range options {
    45  		switch f := option.(type) {
    46  		case func(*corev1.ConfigMap):
    47  			f(cm)
    48  		case func(object client.Object):
    49  			f(cm)
    50  		}
    51  	}
    52  	return cm
    53  }
    54  
    55  func SetConfigMapData(key string, value string) func(*corev1.ConfigMap) {
    56  	return func(configMap *corev1.ConfigMap) {
    57  		configMap.Data[key] = value
    58  	}
    59  }
    60  
    61  func NewPVC(size string) corev1.PersistentVolumeClaimSpec {
    62  	return corev1.PersistentVolumeClaimSpec{
    63  		AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
    64  		Resources: corev1.ResourceRequirements{
    65  			Requests: corev1.ResourceList{
    66  				corev1.ResourceStorage: resource.MustParse(size),
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  func CreateStorageClass(testCtx *testutil.TestContext, storageClassName string,
    73  	allowVolumeExpansion bool) *storagev1.StorageClass {
    74  	storageClass := &storagev1.StorageClass{
    75  		ObjectMeta: metav1.ObjectMeta{
    76  			Name: storageClassName,
    77  			Annotations: map[string]string{
    78  				storage.IsDefaultStorageClassAnnotation: "false",
    79  			},
    80  		},
    81  		Provisioner:          testutil.DefaultStorageProvisoner,
    82  		AllowVolumeExpansion: &allowVolumeExpansion,
    83  	}
    84  	return CreateK8sResource(testCtx, storageClass).(*storagev1.StorageClass)
    85  }
    86  
    87  func CreateVolumeSnapshotClass(testCtx *testutil.TestContext) {
    88  	volumeSnapshotClass := &vsv1.VolumeSnapshotClass{
    89  		ObjectMeta: metav1.ObjectMeta{
    90  			Name: "default-vs",
    91  		},
    92  		Driver:         testutil.DefaultCSIDriver,
    93  		DeletionPolicy: vsv1.VolumeSnapshotContentDelete,
    94  	}
    95  	CreateK8sResource(testCtx, volumeSnapshotClass)
    96  }