github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/cluster_replication_test_util.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 "context" 24 "fmt" 25 26 "github.com/onsi/gomega" 27 appsv1 "k8s.io/api/apps/v1" 28 corev1 "k8s.io/api/core/v1" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 31 "github.com/1aal/kubeblocks/pkg/constant" 32 "github.com/1aal/kubeblocks/pkg/testutil" 33 ) 34 35 // MockReplicationComponentPod mocks to create pod of the replication StatefulSet, just using in envTest 36 func MockReplicationComponentPod( 37 g gomega.Gomega, 38 testCtx testutil.TestContext, 39 sts *appsv1.StatefulSet, 40 clusterName, 41 compName, 42 podName, 43 roleName string) *corev1.Pod { 44 pod := NewPodFactory(testCtx.DefaultNamespace, podName). 45 SetOwnerReferences("apps/v1", constant.StatefulSetKind, sts). 46 AddAppInstanceLabel(clusterName). 47 AddAppComponentLabel(compName). 48 AddAppManagedByLabel(). 49 AddRoleLabel(roleName). 50 AddControllerRevisionHashLabel(sts.Status.UpdateRevision). 51 AddContainer(corev1.Container{Name: DefaultRedisContainerName, Image: DefaultRedisImageName}). 52 Create(&testCtx).GetObject() 53 patch := client.MergeFrom(pod.DeepCopy()) 54 pod.Status.Conditions = []corev1.PodCondition{ 55 { 56 Type: corev1.PodReady, 57 Status: corev1.ConditionTrue, 58 }, 59 } 60 if g != nil { 61 g.Expect(testCtx.Cli.Status().Patch(context.Background(), pod, patch)).Should(gomega.Succeed()) 62 } else { 63 gomega.Expect(testCtx.Cli.Status().Patch(context.Background(), pod, patch)).Should(gomega.Succeed()) 64 } 65 return pod 66 } 67 68 // MockReplicationComponentPods mocks to create pods of the component, just using in envTest. If roleByIdx is empty, 69 // will have implicit pod-0 being "primary" role and others to "secondary" role. 70 func MockReplicationComponentPods( 71 g gomega.Gomega, 72 testCtx testutil.TestContext, 73 sts *appsv1.StatefulSet, 74 clusterName, 75 compName string, 76 roleByIdx map[int32]string) []*corev1.Pod { 77 78 var pods []*corev1.Pod 79 for i := int32(0); i < *sts.Spec.Replicas; i++ { 80 podName := fmt.Sprintf("%s-%d", sts.Name, i) 81 role := "secondary" 82 if podRole, ok := roleByIdx[i]; ok && podRole != "" { 83 role = podRole 84 } else if i == 0 { 85 role = "primary" 86 } 87 pods = append(pods, MockReplicationComponentPod(g, testCtx, sts, clusterName, compName, podName, role)) 88 } 89 return pods 90 }