github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/cluster_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 25 "github.com/onsi/gomega" 26 corev1 "k8s.io/api/core/v1" 27 "k8s.io/apimachinery/pkg/api/resource" 28 "k8s.io/apimachinery/pkg/types" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 31 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 32 "github.com/1aal/kubeblocks/pkg/testutil" 33 ) 34 35 // InitClusterWithHybridComps initializes a cluster environment for testing, includes ClusterDefinition/ClusterVersion/Cluster resources. 36 func InitClusterWithHybridComps( 37 testCtx *testutil.TestContext, 38 clusterDefName, 39 clusterVersionName, 40 clusterName, 41 statelessCompDefName, 42 statefulCompDefName, 43 consensusCompDefName string) (*appsv1alpha1.ClusterDefinition, *appsv1alpha1.ClusterVersion, *appsv1alpha1.Cluster) { 44 clusterDef := NewClusterDefFactory(clusterDefName). 45 AddComponentDef(StatelessNginxComponent, statelessCompDefName). 46 AddComponentDef(ConsensusMySQLComponent, consensusCompDefName). 47 AddComponentDef(StatefulMySQLComponent, statefulCompDefName). 48 Create(testCtx).GetObject() 49 clusterVersion := NewClusterVersionFactory(clusterVersionName, clusterDefName). 50 AddComponentVersion(statelessCompDefName).AddContainerShort(DefaultNginxContainerName, NginxImage). 51 AddComponentVersion(consensusCompDefName).AddContainerShort(DefaultMySQLContainerName, NginxImage). 52 AddComponentVersion(statefulCompDefName).AddContainerShort(DefaultMySQLContainerName, NginxImage). 53 Create(testCtx).GetObject() 54 pvcSpec := NewPVCSpec("1Gi") 55 cluster := NewClusterFactory(testCtx.DefaultNamespace, clusterName, clusterDefName, clusterVersionName). 56 AddComponent(statelessCompDefName, statelessCompDefName).SetReplicas(1). 57 AddComponent(consensusCompDefName, consensusCompDefName).SetReplicas(3). 58 AddComponent(statefulCompDefName, statefulCompDefName).SetReplicas(3). 59 AddVolumeClaimTemplate(DataVolumeName, pvcSpec). 60 Create(testCtx).GetObject() 61 return clusterDef, clusterVersion, cluster 62 } 63 64 func CreateK8sResource(testCtx *testutil.TestContext, obj client.Object) client.Object { 65 gomega.Expect(testCtx.CreateObj(testCtx.Ctx, obj)).Should(gomega.Succeed()) 66 // wait until cluster created 67 gomega.Eventually(CheckObjExists(testCtx, client.ObjectKeyFromObject(obj), 68 obj, true)).Should(gomega.Succeed()) 69 return obj 70 } 71 72 func CheckedCreateK8sResource(testCtx *testutil.TestContext, obj client.Object) client.Object { 73 gomega.Expect(testCtx.CheckedCreateObj(testCtx.Ctx, obj)).Should(gomega.Succeed()) 74 // wait until cluster created 75 gomega.Eventually(CheckObjExists(testCtx, client.ObjectKeyFromObject(obj), 76 obj, true)).Should(gomega.Succeed()) 77 return obj 78 } 79 80 // GetClusterComponentPhase gets the component phase of testing cluster for verification. 81 func GetClusterComponentPhase(testCtx *testutil.TestContext, clusterKey types.NamespacedName, componentName string) func(g gomega.Gomega) appsv1alpha1.ClusterComponentPhase { 82 return func(g gomega.Gomega) appsv1alpha1.ClusterComponentPhase { 83 tmpCluster := &appsv1alpha1.Cluster{} 84 g.Expect(testCtx.Cli.Get(context.Background(), client.ObjectKey{Name: clusterKey.Name, 85 Namespace: clusterKey.Namespace}, tmpCluster)).Should(gomega.Succeed()) 86 return tmpCluster.Status.Components[componentName].Phase 87 } 88 } 89 90 // GetClusterPhase gets the testing cluster's phase in status for verification. 91 func GetClusterPhase(testCtx *testutil.TestContext, clusterKey types.NamespacedName) func(gomega.Gomega) appsv1alpha1.ClusterPhase { 92 return func(g gomega.Gomega) appsv1alpha1.ClusterPhase { 93 cluster := &appsv1alpha1.Cluster{} 94 g.Expect(testCtx.Cli.Get(testCtx.Ctx, clusterKey, cluster)).Should(gomega.Succeed()) 95 return cluster.Status.Phase 96 } 97 } 98 99 // GetClusterGeneration gets the testing cluster's metadata.generation. 100 func GetClusterGeneration(testCtx *testutil.TestContext, clusterKey types.NamespacedName) func(gomega.Gomega) int64 { 101 return func(g gomega.Gomega) int64 { 102 cluster := &appsv1alpha1.Cluster{} 103 g.Expect(testCtx.Cli.Get(testCtx.Ctx, clusterKey, cluster)).Should(gomega.Succeed()) 104 return cluster.GetGeneration() 105 } 106 } 107 108 // GetClusterObservedGeneration gets the testing cluster's ObservedGeneration in status for verification. 109 func GetClusterObservedGeneration(testCtx *testutil.TestContext, clusterKey types.NamespacedName) func(gomega.Gomega) int64 { 110 return func(g gomega.Gomega) int64 { 111 cluster := &appsv1alpha1.Cluster{} 112 g.Expect(testCtx.Cli.Get(testCtx.Ctx, clusterKey, cluster)).Should(gomega.Succeed()) 113 return cluster.Status.ObservedGeneration 114 } 115 } 116 117 // NewPVCSpec creates appsv1alpha1.PersistentVolumeClaimSpec. 118 func NewPVCSpec(size string) appsv1alpha1.PersistentVolumeClaimSpec { 119 return appsv1alpha1.PersistentVolumeClaimSpec{ 120 AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}, 121 Resources: corev1.ResourceRequirements{ 122 Requests: corev1.ResourceList{ 123 corev1.ResourceStorage: resource.MustParse(size), 124 }, 125 }, 126 } 127 }