github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/pvc_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  	"github.com/vmware-tanzu/velero/pkg/util/kube"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/api/resource"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  
    28  	"github.com/1aal/kubeblocks/pkg/constant"
    29  )
    30  
    31  type MockPersistentVolumeClaimFactory struct {
    32  	BaseFactory[corev1.PersistentVolumeClaim, *corev1.PersistentVolumeClaim, MockPersistentVolumeClaimFactory]
    33  }
    34  
    35  func NewPersistentVolumeClaimFactory(namespace, name, clusterName, componentName, vctName string) *MockPersistentVolumeClaimFactory {
    36  	f := &MockPersistentVolumeClaimFactory{}
    37  	volumeMode := corev1.PersistentVolumeFilesystem
    38  	f.Init(namespace, name,
    39  		&corev1.PersistentVolumeClaim{
    40  			ObjectMeta: metav1.ObjectMeta{
    41  				Labels: map[string]string{
    42  					constant.AppInstanceLabelKey:             clusterName,
    43  					constant.KBAppComponentLabelKey:          componentName,
    44  					constant.AppManagedByLabelKey:            constant.AppName,
    45  					constant.VolumeClaimTemplateNameLabelKey: vctName,
    46  					constant.VolumeTypeLabelKey:              vctName,
    47  				},
    48  				Annotations: map[string]string{
    49  					kube.KubeAnnBindCompleted: "yes",
    50  				},
    51  			},
    52  			Spec: corev1.PersistentVolumeClaimSpec{
    53  				VolumeMode:  &volumeMode,
    54  				AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
    55  			},
    56  		}, f)
    57  	return f
    58  }
    59  
    60  func (factory *MockPersistentVolumeClaimFactory) SetStorageClass(storageClassName string) *MockPersistentVolumeClaimFactory {
    61  	factory.Get().Spec.StorageClassName = &storageClassName
    62  	return factory
    63  }
    64  
    65  func (factory *MockPersistentVolumeClaimFactory) SetStorage(storageSize string) *MockPersistentVolumeClaimFactory {
    66  	factory.Get().Spec.Resources = corev1.ResourceRequirements{
    67  		Requests: corev1.ResourceList{
    68  			corev1.ResourceStorage: resource.MustParse(storageSize),
    69  		},
    70  	}
    71  	return factory
    72  }
    73  
    74  func (factory *MockPersistentVolumeClaimFactory) SetVolumeName(volName string) *MockPersistentVolumeClaimFactory {
    75  	factory.Get().Spec.VolumeName = volName
    76  	return factory
    77  }
    78  
    79  func (factory *MockPersistentVolumeClaimFactory) SetAnnotations(annotations map[string]string) *MockPersistentVolumeClaimFactory {
    80  	factory.Get().Annotations = annotations
    81  	return factory
    82  }
    83  
    84  func (factory *MockPersistentVolumeClaimFactory) SetDataSourceRef(apiGroup, kind, name string) *MockPersistentVolumeClaimFactory {
    85  	factory.Get().Spec.DataSourceRef = &corev1.TypedObjectReference{
    86  		Name:     name,
    87  		APIGroup: &apiGroup,
    88  		Kind:     kind,
    89  	}
    90  	return factory
    91  }