github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/pv_factoy.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 corev1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/resource" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 ) 27 28 type MockPersistentVolumeFactory struct { 29 BaseFactory[corev1.PersistentVolume, *corev1.PersistentVolume, MockPersistentVolumeFactory] 30 } 31 32 func NewPersistentVolumeFactory(namespace, name, pvcName string) *MockPersistentVolumeFactory { 33 f := &MockPersistentVolumeFactory{} 34 volumeMode := corev1.PersistentVolumeFilesystem 35 f.Init(namespace, name, 36 &corev1.PersistentVolume{ 37 Spec: corev1.PersistentVolumeSpec{ 38 PersistentVolumeSource: corev1.PersistentVolumeSource{ 39 HostPath: &corev1.HostPathVolumeSource{ 40 Path: "/tmp/hostpath-provisioner/default/" + pvcName, 41 }, 42 }, 43 VolumeMode: &volumeMode, 44 AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}, 45 PersistentVolumeReclaimPolicy: corev1.PersistentVolumeReclaimDelete, 46 }, 47 }, f) 48 return f 49 } 50 51 func (f *MockPersistentVolumeFactory) SetStorageClass(storageClassName string) *MockPersistentVolumeFactory { 52 f.Get().Spec.StorageClassName = storageClassName 53 return f 54 } 55 56 func (f *MockPersistentVolumeFactory) SetStorage(storageSize string) *MockPersistentVolumeFactory { 57 f.Get().Spec.Capacity = corev1.ResourceList{ 58 corev1.ResourceStorage: resource.MustParse(storageSize), 59 } 60 return f 61 } 62 63 func (f *MockPersistentVolumeFactory) SetPersistentVolumeReclaimPolicy(reclaimPolicy corev1.PersistentVolumeReclaimPolicy) *MockPersistentVolumeFactory { 64 f.Get().Spec.PersistentVolumeReclaimPolicy = reclaimPolicy 65 return f 66 } 67 68 func (f *MockPersistentVolumeFactory) SetClaimRef(obj client.Object) *MockPersistentVolumeFactory { 69 f.Get().Spec.ClaimRef = &corev1.ObjectReference{ 70 Kind: obj.GetObjectKind().GroupVersionKind().Kind, 71 Namespace: obj.GetNamespace(), 72 Name: obj.GetName(), 73 UID: obj.GetUID(), 74 APIVersion: "v1", 75 ResourceVersion: obj.GetResourceVersion(), 76 } 77 return f 78 } 79 80 func (f *MockPersistentVolumeFactory) SetCSIDriver(driverName string) *MockPersistentVolumeFactory { 81 f.Get().Spec.CSI = &corev1.CSIPersistentVolumeSource{ 82 Driver: driverName, 83 VolumeHandle: f.Get().Name, 84 } 85 // clear default persistentVolumeSource 86 f.Get().Spec.PersistentVolumeSource.HostPath = nil 87 return f 88 }