github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/clusterversion_factory.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 25 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 26 ) 27 28 type MockClusterVersionFactory struct { 29 BaseFactory[appsv1alpha1.ClusterVersion, *appsv1alpha1.ClusterVersion, MockClusterVersionFactory] 30 } 31 32 func NewClusterVersionFactory(name, cdRef string) *MockClusterVersionFactory { 33 f := &MockClusterVersionFactory{} 34 f.Init("", name, 35 &appsv1alpha1.ClusterVersion{ 36 Spec: appsv1alpha1.ClusterVersionSpec{ 37 ClusterDefinitionRef: cdRef, 38 ComponentVersions: []appsv1alpha1.ClusterComponentVersion{}, 39 }, 40 }, f) 41 return f 42 } 43 44 func (factory *MockClusterVersionFactory) AddComponentVersion(compDefName string) *MockClusterVersionFactory { 45 comp := appsv1alpha1.ClusterComponentVersion{ 46 ComponentDefRef: compDefName, 47 } 48 factory.Get().Spec.ComponentVersions = append(factory.Get().Spec.ComponentVersions, comp) 49 return factory 50 } 51 52 func (factory *MockClusterVersionFactory) AddInitContainer(container corev1.Container) *MockClusterVersionFactory { 53 comps := factory.Get().Spec.ComponentVersions 54 if len(comps) > 0 { 55 comp := comps[len(comps)-1] 56 comp.VersionsCtx.InitContainers = append(comp.VersionsCtx.InitContainers, container) 57 comps[len(comps)-1] = comp 58 } 59 factory.Get().Spec.ComponentVersions = comps 60 return factory 61 } 62 63 func (factory *MockClusterVersionFactory) AddInitContainerShort(name string, image string) *MockClusterVersionFactory { 64 return factory.AddInitContainer(corev1.Container{ 65 Name: name, 66 Image: image, 67 }) 68 } 69 70 func (factory *MockClusterVersionFactory) AddContainer(container corev1.Container) *MockClusterVersionFactory { 71 comps := factory.Get().Spec.ComponentVersions 72 if len(comps) > 0 { 73 comp := comps[len(comps)-1] 74 comp.VersionsCtx.Containers = append(comp.VersionsCtx.Containers, container) 75 comps[len(comps)-1] = comp 76 } 77 factory.Get().Spec.ComponentVersions = comps 78 return factory 79 } 80 81 func (factory *MockClusterVersionFactory) AddContainerShort(name string, image string) *MockClusterVersionFactory { 82 return factory.AddContainer(corev1.Container{ 83 Name: name, 84 Image: image, 85 }) 86 } 87 88 func (factory *MockClusterVersionFactory) AddConfigTemplate(name string, 89 configTemplateRef string, configConstraintRef string, volumeName string) *MockClusterVersionFactory { 90 comps := factory.Get().Spec.ComponentVersions 91 if len(comps) > 0 { 92 comp := comps[len(comps)-1] 93 comp.ConfigSpecs = append(comp.ConfigSpecs, 94 appsv1alpha1.ComponentConfigSpec{ 95 ComponentTemplateSpec: appsv1alpha1.ComponentTemplateSpec{ 96 Name: name, 97 TemplateRef: configTemplateRef, 98 VolumeName: volumeName, 99 }, 100 ConfigConstraintRef: configConstraintRef, 101 }) 102 comps[len(comps)-1] = comp 103 } 104 factory.Get().Spec.ComponentVersions = comps 105 return factory 106 }