github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/statefulset_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 MockStatefulSetFactory struct {
    31  	BaseFactory[appsv1.StatefulSet, *appsv1.StatefulSet, MockStatefulSetFactory]
    32  }
    33  
    34  func NewStatefulSetFactory(namespace, name string, clusterName string, componentName string) *MockStatefulSetFactory {
    35  	f := &MockStatefulSetFactory{}
    36  	f.Init(namespace, name,
    37  		&appsv1.StatefulSet{
    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.StatefulSetSpec{
    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  				UpdateStrategy: appsv1.StatefulSetUpdateStrategy{
    63  					Type: appsv1.OnDeleteStatefulSetStrategyType,
    64  				},
    65  			},
    66  		}, f)
    67  	return f
    68  }
    69  
    70  func (factory *MockStatefulSetFactory) SetReplicas(replicas int32) *MockStatefulSetFactory {
    71  	factory.Get().Spec.Replicas = &replicas
    72  	return factory
    73  }
    74  
    75  func (factory *MockStatefulSetFactory) AddVolume(volume corev1.Volume) *MockStatefulSetFactory {
    76  	volumes := &factory.Get().Spec.Template.Spec.Volumes
    77  	*volumes = append(*volumes, volume)
    78  	return factory
    79  }
    80  
    81  func (factory *MockStatefulSetFactory) AddConfigmapVolume(volumeName string, configmapName string) *MockStatefulSetFactory {
    82  	volume := corev1.Volume{
    83  		Name: volumeName,
    84  		VolumeSource: corev1.VolumeSource{
    85  			ConfigMap: &corev1.ConfigMapVolumeSource{
    86  				LocalObjectReference: corev1.LocalObjectReference{Name: configmapName},
    87  			},
    88  		},
    89  	}
    90  	factory.AddVolume(volume)
    91  	return factory
    92  }
    93  
    94  func (factory *MockStatefulSetFactory) AddVolumeClaimTemplate(pvc corev1.PersistentVolumeClaim) *MockStatefulSetFactory {
    95  	volumeClaimTpls := &factory.Get().Spec.VolumeClaimTemplates
    96  	*volumeClaimTpls = append(*volumeClaimTpls, pvc)
    97  	return factory
    98  }
    99  
   100  func (factory *MockStatefulSetFactory) AddContainer(container corev1.Container) *MockStatefulSetFactory {
   101  	containers := &factory.Get().Spec.Template.Spec.Containers
   102  	*containers = append(*containers, container)
   103  	return factory
   104  }