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