github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/deployment_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 appsv1 "k8s.io/api/apps/v1" 24 corev1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 27 "github.com/1aal/kubeblocks/pkg/constant" 28 ) 29 30 type MockDeploymentFactory struct { 31 BaseFactory[appsv1.Deployment, *appsv1.Deployment, MockDeploymentFactory] 32 } 33 34 func NewDeploymentFactory(namespace, name, clusterName, componentName string) *MockDeploymentFactory { 35 f := &MockDeploymentFactory{} 36 f.Init(namespace, name, 37 &appsv1.Deployment{ 38 ObjectMeta: metav1.ObjectMeta{ 39 Labels: map[string]string{ 40 constant.AppInstanceLabelKey: clusterName, 41 constant.KBAppComponentLabelKey: componentName, 42 constant.AppManagedByLabelKey: constant.AppName, 43 }, 44 }, 45 Spec: appsv1.DeploymentSpec{ 46 Selector: &metav1.LabelSelector{ 47 MatchLabels: map[string]string{ 48 constant.AppInstanceLabelKey: clusterName, 49 constant.KBAppComponentLabelKey: componentName, 50 constant.AppManagedByLabelKey: constant.AppName, 51 }, 52 }, 53 Template: corev1.PodTemplateSpec{ 54 ObjectMeta: metav1.ObjectMeta{ 55 Labels: map[string]string{ 56 constant.AppInstanceLabelKey: clusterName, 57 constant.KBAppComponentLabelKey: componentName, 58 constant.AppManagedByLabelKey: constant.AppName, 59 }, 60 }, 61 }, 62 }, 63 }, f) 64 return f 65 } 66 67 func (factory *MockDeploymentFactory) SetMinReadySeconds(minReadySeconds int32) *MockDeploymentFactory { 68 factory.Get().Spec.MinReadySeconds = minReadySeconds 69 return factory 70 } 71 72 func (factory *MockDeploymentFactory) SetReplicas(replicas int32) *MockDeploymentFactory { 73 factory.Get().Spec.Replicas = &replicas 74 return factory 75 } 76 77 func (factory *MockDeploymentFactory) AddVolume(volume corev1.Volume) *MockDeploymentFactory { 78 volumes := &factory.Get().Spec.Template.Spec.Volumes 79 *volumes = append(*volumes, volume) 80 return factory 81 } 82 83 func (factory *MockDeploymentFactory) AddConfigmapVolume(volumeName, configmapName string) *MockDeploymentFactory { 84 volume := corev1.Volume{ 85 Name: volumeName, 86 VolumeSource: corev1.VolumeSource{ 87 ConfigMap: &corev1.ConfigMapVolumeSource{ 88 LocalObjectReference: corev1.LocalObjectReference{Name: configmapName}, 89 }, 90 }, 91 } 92 factory.AddVolume(volume) 93 return factory 94 } 95 96 func (factory *MockDeploymentFactory) AddContainer(container corev1.Container) *MockDeploymentFactory { 97 containers := &factory.Get().Spec.Template.Spec.Containers 98 *containers = append(*containers, container) 99 return factory 100 }