github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/cluster_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 MockClusterFactory struct {
    29  	BaseFactory[appsv1alpha1.Cluster, *appsv1alpha1.Cluster, MockClusterFactory]
    30  }
    31  
    32  func NewClusterFactory(namespace, name, cdRef, cvRef string) *MockClusterFactory {
    33  	f := &MockClusterFactory{}
    34  	f.Init(namespace, name,
    35  		&appsv1alpha1.Cluster{
    36  			Spec: appsv1alpha1.ClusterSpec{
    37  				ClusterDefRef:     cdRef,
    38  				ClusterVersionRef: cvRef,
    39  				ComponentSpecs:    []appsv1alpha1.ClusterComponentSpec{},
    40  				TerminationPolicy: appsv1alpha1.WipeOut,
    41  			},
    42  		}, f)
    43  	return f
    44  }
    45  
    46  func (factory *MockClusterFactory) SetClusterAffinity(affinity *appsv1alpha1.Affinity) *MockClusterFactory {
    47  	factory.Get().Spec.Affinity = affinity
    48  	return factory
    49  }
    50  
    51  func (factory *MockClusterFactory) AddClusterToleration(toleration corev1.Toleration) *MockClusterFactory {
    52  	tolerations := factory.Get().Spec.Tolerations
    53  	if len(tolerations) == 0 {
    54  		tolerations = []corev1.Toleration{}
    55  	}
    56  	tolerations = append(tolerations, toleration)
    57  	factory.Get().Spec.Tolerations = tolerations
    58  	return factory
    59  }
    60  
    61  func (factory *MockClusterFactory) AddComponent(compName string, compDefName string) *MockClusterFactory {
    62  	comp := appsv1alpha1.ClusterComponentSpec{
    63  		Name:            compName,
    64  		ComponentDefRef: compDefName,
    65  	}
    66  	factory.Get().Spec.ComponentSpecs = append(factory.Get().Spec.ComponentSpecs, comp)
    67  	return factory
    68  }
    69  
    70  type updateFn func(comp *appsv1alpha1.ClusterComponentSpec)
    71  
    72  func (factory *MockClusterFactory) lastComponentRef(update updateFn) *MockClusterFactory {
    73  	comps := factory.Get().Spec.ComponentSpecs
    74  	if len(comps) > 0 {
    75  		update(&comps[len(comps)-1])
    76  	}
    77  	factory.Get().Spec.ComponentSpecs = comps
    78  	return factory
    79  }
    80  
    81  func (factory *MockClusterFactory) SetReplicas(replicas int32) *MockClusterFactory {
    82  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
    83  		comp.Replicas = replicas
    84  	})
    85  }
    86  
    87  func (factory *MockClusterFactory) SetServiceAccountName(serviceAccountName string) *MockClusterFactory {
    88  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
    89  		comp.ServiceAccountName = serviceAccountName
    90  	})
    91  }
    92  
    93  func (factory *MockClusterFactory) SetResources(resources corev1.ResourceRequirements) *MockClusterFactory {
    94  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
    95  		comp.Resources = resources
    96  	})
    97  }
    98  
    99  func (factory *MockClusterFactory) SetComponentAffinity(affinity *appsv1alpha1.Affinity) *MockClusterFactory {
   100  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   101  		comp.Affinity = affinity
   102  	})
   103  }
   104  
   105  func (factory *MockClusterFactory) SetEnabledLogs(logName ...string) *MockClusterFactory {
   106  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   107  		comp.EnabledLogs = logName
   108  	})
   109  }
   110  
   111  func (factory *MockClusterFactory) SetClassDefRef(classDefRef *appsv1alpha1.ClassDefRef) *MockClusterFactory {
   112  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   113  		comp.ClassDefRef = classDefRef
   114  	})
   115  }
   116  
   117  func (factory *MockClusterFactory) AddComponentToleration(toleration corev1.Toleration) *MockClusterFactory {
   118  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   119  		tolerations := comp.Tolerations
   120  		if len(tolerations) == 0 {
   121  			tolerations = []corev1.Toleration{}
   122  		}
   123  		tolerations = append(tolerations, toleration)
   124  		comp.Tolerations = tolerations
   125  	})
   126  }
   127  
   128  func (factory *MockClusterFactory) AddVolumeClaimTemplate(volumeName string,
   129  	pvcSpec appsv1alpha1.PersistentVolumeClaimSpec) *MockClusterFactory {
   130  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   131  		comp.VolumeClaimTemplates = append(comp.VolumeClaimTemplates,
   132  			appsv1alpha1.ClusterComponentVolumeClaimTemplate{
   133  				Name: volumeName,
   134  				Spec: pvcSpec,
   135  			})
   136  	})
   137  }
   138  
   139  func (factory *MockClusterFactory) SetMonitor(monitor bool) *MockClusterFactory {
   140  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   141  		comp.Monitor = monitor
   142  	})
   143  }
   144  
   145  func (factory *MockClusterFactory) SetSwitchPolicy(switchPolicy *appsv1alpha1.ClusterSwitchPolicy) *MockClusterFactory {
   146  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   147  		comp.SwitchPolicy = switchPolicy
   148  	})
   149  }
   150  
   151  func (factory *MockClusterFactory) SetTLS(tls bool) *MockClusterFactory {
   152  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   153  		comp.TLS = tls
   154  	})
   155  }
   156  
   157  func (factory *MockClusterFactory) SetIssuer(issuer *appsv1alpha1.Issuer) *MockClusterFactory {
   158  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   159  		comp.Issuer = issuer
   160  	})
   161  }
   162  
   163  func (factory *MockClusterFactory) AddService(serviceName string, serviceType corev1.ServiceType) *MockClusterFactory {
   164  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   165  		comp.Services = append(comp.Services,
   166  			appsv1alpha1.ClusterComponentService{
   167  				Name:        serviceName,
   168  				ServiceType: serviceType,
   169  			})
   170  	})
   171  }
   172  
   173  func (factory *MockClusterFactory) SetBackup(backup *appsv1alpha1.ClusterBackup) *MockClusterFactory {
   174  	factory.Get().Spec.Backup = backup
   175  	return factory
   176  }
   177  
   178  func (factory *MockClusterFactory) SetServiceRefs(serviceRefs []appsv1alpha1.ServiceRef) *MockClusterFactory {
   179  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   180  		comp.ServiceRefs = serviceRefs
   181  	})
   182  }
   183  
   184  func (factory *MockClusterFactory) AddUserSecretVolume(name, mountPoint, resName, containerName string) *MockClusterFactory {
   185  	secretResource := appsv1alpha1.SecretRef{
   186  		ResourceMeta: appsv1alpha1.ResourceMeta{
   187  			Name:         name,
   188  			MountPoint:   mountPoint,
   189  			AsVolumeFrom: []string{containerName},
   190  		},
   191  		Secret: corev1.SecretVolumeSource{
   192  			SecretName: resName,
   193  		},
   194  	}
   195  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   196  		userResourcesRefs := comp.UserResourceRefs
   197  		if userResourcesRefs == nil {
   198  			userResourcesRefs = &appsv1alpha1.UserResourceRefs{}
   199  			comp.UserResourceRefs = userResourcesRefs
   200  		}
   201  		userResourcesRefs.SecretRefs = append(userResourcesRefs.SecretRefs, secretResource)
   202  	})
   203  }
   204  
   205  func (factory *MockClusterFactory) AddUserConfigmapVolume(name, mountPoint, resName, containerName string) *MockClusterFactory {
   206  	cmResource := appsv1alpha1.ConfigMapRef{
   207  		ResourceMeta: appsv1alpha1.ResourceMeta{
   208  			Name:         name,
   209  			MountPoint:   mountPoint,
   210  			AsVolumeFrom: []string{containerName},
   211  		},
   212  		ConfigMap: corev1.ConfigMapVolumeSource{
   213  			LocalObjectReference: corev1.LocalObjectReference{
   214  				Name: resName,
   215  			},
   216  		},
   217  	}
   218  	return factory.lastComponentRef(func(comp *appsv1alpha1.ClusterComponentSpec) {
   219  		userResourcesRefs := comp.UserResourceRefs
   220  		if userResourcesRefs == nil {
   221  			userResourcesRefs = &appsv1alpha1.UserResourceRefs{}
   222  			comp.UserResourceRefs = userResourcesRefs
   223  		}
   224  		userResourcesRefs.ConfigMapRefs = append(userResourcesRefs.ConfigMapRefs, cmResource)
   225  	})
   226  }