github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/constant.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 "k8s.io/apimachinery/pkg/api/resource" 25 "k8s.io/apimachinery/pkg/util/intstr" 26 27 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 28 "github.com/1aal/kubeblocks/pkg/constant" 29 ) 30 31 const ( 32 KubeBlocks = "kubeblocks" 33 LogVolumeName = "log" 34 ConfVolumeName = "conf" 35 DataVolumeName = "data" 36 ScriptsVolumeName = "scripts" 37 ServiceDefaultName = "" 38 ServiceHeadlessName = "headless" 39 ServiceVPCName = "vpc-lb" 40 ServiceInternetName = "internet-lb" 41 42 ReplicationPodRoleVolume = "pod-role" 43 ReplicationRoleLabelFieldPath = "metadata.labels['kubeblocks.io/role']" 44 DefaultReplicationCandidateIndex = 0 45 DefaultReplicationReplicas = 2 46 47 ApeCloudMySQLImage = "docker.io/apecloud/apecloud-mysql-server:latest" 48 DefaultMySQLContainerName = "mysql" 49 50 NginxImage = "nginx" 51 DefaultNginxContainerName = "nginx" 52 53 DefaultRedisCompDefName = "redis" 54 DefaultRedisCompSpecName = "redis-rsts" 55 DefaultRedisImageName = "redis:7.0.5" 56 DefaultRedisContainerName = "redis" 57 DefaultRedisInitContainerName = "redis-init-container" 58 59 Class1c1gName = "general-1c1g" 60 Class2c4gName = "general-2c4g" 61 DefaultResourceConstraintName = "kb-resource-constraint" 62 63 StogrageClassName = "test-sc" 64 EnvKeyImageTag = "IMAGE_TAG" 65 DefaultImageTag = "test" 66 ) 67 68 var ( 69 zeroResRequirements = corev1.ResourceRequirements{ 70 Limits: map[corev1.ResourceName]resource.Quantity{ 71 corev1.ResourceCPU: resource.MustParse("0"), 72 corev1.ResourceMemory: resource.MustParse("0"), 73 }, 74 } 75 76 statelessNginxComponent = appsv1alpha1.ClusterComponentDefinition{ 77 WorkloadType: appsv1alpha1.Stateless, 78 CharacterType: "stateless", 79 Probes: &appsv1alpha1.ClusterDefinitionProbes{ 80 RoleProbe: &appsv1alpha1.ClusterDefinitionProbe{ 81 FailureThreshold: 3, 82 PeriodSeconds: 1, 83 TimeoutSeconds: 5, 84 }, 85 }, 86 VolumeProtectionSpec: &appsv1alpha1.VolumeProtectionSpec{}, 87 PodSpec: &corev1.PodSpec{ 88 Containers: []corev1.Container{{ 89 Name: DefaultNginxContainerName, 90 Image: NginxImage, 91 Resources: zeroResRequirements, 92 }}, 93 }, 94 Service: &appsv1alpha1.ServiceSpec{ 95 Ports: []appsv1alpha1.ServicePort{{ 96 Protocol: corev1.ProtocolTCP, 97 Port: 80, 98 }}, 99 }, 100 } 101 102 // defaultSvc value are corresponding to defaultMySQLContainer.Ports name mapping and 103 // corresponding to defaultConnectionCredential variable placeholder 104 defaultSvcSpec = appsv1alpha1.ServiceSpec{ 105 Ports: []appsv1alpha1.ServicePort{ 106 { 107 Name: "mysql", 108 TargetPort: intstr.IntOrString{ 109 Type: intstr.String, 110 StrVal: "mysql", 111 }, 112 Port: 3306, 113 }, 114 { 115 Name: "paxos", 116 TargetPort: intstr.IntOrString{ 117 Type: intstr.String, 118 StrVal: "paxos", 119 }, 120 Port: 13306, 121 }, 122 }, 123 } 124 125 defaultMySQLContainer = corev1.Container{ 126 Name: DefaultMySQLContainerName, 127 Image: ApeCloudMySQLImage, 128 ImagePullPolicy: corev1.PullIfNotPresent, 129 Resources: zeroResRequirements, 130 Ports: []corev1.ContainerPort{ 131 { 132 Name: "mysql", 133 Protocol: corev1.ProtocolTCP, 134 ContainerPort: 3306, 135 }, 136 { 137 Name: "paxos", 138 Protocol: corev1.ProtocolTCP, 139 ContainerPort: 13306, 140 }, 141 }, 142 VolumeMounts: []corev1.VolumeMount{ 143 { 144 Name: DataVolumeName, 145 MountPath: "/var/lib/mysql", 146 }, 147 { 148 Name: LogVolumeName, 149 MountPath: "/var/log", 150 }, 151 { 152 Name: ScriptsVolumeName, 153 MountPath: "/scripts", 154 }, 155 }, 156 Env: []corev1.EnvVar{{ 157 Name: "MYSQL_ROOT_PASSWORD", 158 ValueFrom: &corev1.EnvVarSource{ 159 SecretKeyRef: &corev1.SecretKeySelector{ 160 LocalObjectReference: corev1.LocalObjectReference{ 161 Name: constant.KBConnCredentialPlaceHolder, 162 }, 163 Key: "password", 164 }, 165 }, 166 }}, 167 Command: []string{"/scripts/setup.sh"}, 168 } 169 170 statefulMySQLComponent = appsv1alpha1.ClusterComponentDefinition{ 171 WorkloadType: appsv1alpha1.Stateful, 172 CharacterType: "mysql", 173 Probes: &appsv1alpha1.ClusterDefinitionProbes{ 174 RoleProbe: &appsv1alpha1.ClusterDefinitionProbe{ 175 FailureThreshold: 3, 176 PeriodSeconds: 1, 177 TimeoutSeconds: 5, 178 }, 179 }, 180 VolumeProtectionSpec: &appsv1alpha1.VolumeProtectionSpec{}, 181 Service: &defaultMySQLService, 182 PodSpec: &corev1.PodSpec{ 183 Containers: []corev1.Container{defaultMySQLContainer}, 184 }, 185 VolumeTypes: []appsv1alpha1.VolumeTypeSpec{{ 186 Name: DataVolumeName, 187 Type: appsv1alpha1.VolumeTypeData, 188 }}, 189 } 190 191 defaultConsensusSpec = appsv1alpha1.ConsensusSetSpec{ 192 Leader: appsv1alpha1.ConsensusMember{ 193 Name: "leader", 194 AccessMode: appsv1alpha1.ReadWrite, 195 }, 196 Followers: []appsv1alpha1.ConsensusMember{{ 197 Name: "follower", 198 AccessMode: appsv1alpha1.Readonly, 199 }}, 200 StatefulSetSpec: appsv1alpha1.StatefulSetSpec{ 201 UpdateStrategy: appsv1alpha1.BestEffortParallelStrategy, 202 }, 203 } 204 205 defaultMySQLService = appsv1alpha1.ServiceSpec{ 206 Ports: []appsv1alpha1.ServicePort{{ 207 Protocol: corev1.ProtocolTCP, 208 Port: 3306, 209 }}, 210 } 211 212 consensusMySQLComponent = appsv1alpha1.ClusterComponentDefinition{ 213 WorkloadType: appsv1alpha1.Consensus, 214 CharacterType: "mysql", 215 ConsensusSpec: &defaultConsensusSpec, 216 Probes: &appsv1alpha1.ClusterDefinitionProbes{ 217 RoleProbe: &appsv1alpha1.ClusterDefinitionProbe{ 218 FailureThreshold: 3, 219 PeriodSeconds: 1, 220 TimeoutSeconds: 5, 221 }, 222 }, 223 VolumeProtectionSpec: &appsv1alpha1.VolumeProtectionSpec{}, 224 Service: &defaultMySQLService, 225 PodSpec: &corev1.PodSpec{ 226 Containers: []corev1.Container{defaultMySQLContainer}, 227 }, 228 VolumeTypes: []appsv1alpha1.VolumeTypeSpec{{ 229 Name: DataVolumeName, 230 Type: appsv1alpha1.VolumeTypeData, 231 }}, 232 } 233 234 defaultRedisService = appsv1alpha1.ServiceSpec{ 235 Ports: []appsv1alpha1.ServicePort{{ 236 Protocol: corev1.ProtocolTCP, 237 Port: 6379, 238 }}, 239 } 240 241 defaultReplicationRedisVolumeMounts = []corev1.VolumeMount{ 242 { 243 Name: DataVolumeName, 244 MountPath: "/data", 245 }, 246 { 247 Name: ScriptsVolumeName, 248 MountPath: "/scripts", 249 }, 250 { 251 Name: ConfVolumeName, 252 MountPath: "/etc/conf", 253 }, 254 { 255 Name: ReplicationPodRoleVolume, 256 MountPath: "/etc/conf/role", 257 }, 258 } 259 260 defaultRedisInitContainer = corev1.Container{ 261 Name: DefaultRedisInitContainerName, 262 Image: DefaultRedisImageName, 263 ImagePullPolicy: corev1.PullIfNotPresent, 264 VolumeMounts: defaultReplicationRedisVolumeMounts, 265 Command: []string{"/scripts/init.sh"}, 266 Resources: zeroResRequirements, 267 } 268 269 defaultRedisContainer = corev1.Container{ 270 Name: DefaultRedisContainerName, 271 Image: DefaultRedisImageName, 272 ImagePullPolicy: corev1.PullIfNotPresent, 273 Ports: []corev1.ContainerPort{ 274 { 275 Name: "redis", 276 Protocol: corev1.ProtocolTCP, 277 ContainerPort: 6379, 278 }, 279 }, 280 VolumeMounts: defaultReplicationRedisVolumeMounts, 281 Args: []string{"/etc/conf/redis.conf"}, 282 Lifecycle: &corev1.Lifecycle{ 283 PostStart: &corev1.LifecycleHandler{ 284 Exec: &corev1.ExecAction{ 285 Command: []string{"/scripts/setup.sh"}, 286 }, 287 }, 288 }, 289 Resources: zeroResRequirements, 290 } 291 292 replicationRedisComponent = appsv1alpha1.ClusterComponentDefinition{ 293 WorkloadType: appsv1alpha1.Replication, 294 CharacterType: "redis", 295 Probes: &appsv1alpha1.ClusterDefinitionProbes{ 296 RoleProbe: &appsv1alpha1.ClusterDefinitionProbe{ 297 FailureThreshold: 3, 298 PeriodSeconds: 1, 299 TimeoutSeconds: 5, 300 }, 301 }, 302 VolumeProtectionSpec: &appsv1alpha1.VolumeProtectionSpec{}, 303 Service: &defaultRedisService, 304 VolumeTypes: []appsv1alpha1.VolumeTypeSpec{{ 305 Name: DataVolumeName, 306 Type: appsv1alpha1.VolumeTypeData, 307 }}, 308 PodSpec: &corev1.PodSpec{ 309 Volumes: []corev1.Volume{ 310 { 311 Name: ConfVolumeName, 312 VolumeSource: corev1.VolumeSource{ 313 EmptyDir: &corev1.EmptyDirVolumeSource{}, 314 }, 315 }, 316 { 317 Name: ReplicationPodRoleVolume, 318 VolumeSource: corev1.VolumeSource{ 319 DownwardAPI: &corev1.DownwardAPIVolumeSource{ 320 Items: []corev1.DownwardAPIVolumeFile{ 321 { 322 Path: "labels", 323 FieldRef: &corev1.ObjectFieldSelector{ 324 FieldPath: ReplicationRoleLabelFieldPath, 325 }, 326 }, 327 }, 328 }, 329 }, 330 }, 331 }, 332 InitContainers: []corev1.Container{defaultRedisInitContainer}, 333 Containers: []corev1.Container{defaultRedisContainer}, 334 }, 335 } 336 337 Class1c1g = appsv1alpha1.ComponentClass{ 338 Name: Class1c1gName, 339 CPU: resource.MustParse("1"), 340 Memory: resource.MustParse("1Gi"), 341 } 342 343 Class2c4g = appsv1alpha1.ComponentClass{ 344 Name: Class2c4gName, 345 CPU: resource.MustParse("2"), 346 Memory: resource.MustParse("4Gi"), 347 } 348 349 DefaultClasses = map[string]appsv1alpha1.ComponentClass{ 350 Class1c1gName: Class1c1g, 351 Class2c4gName: Class2c4g, 352 } 353 )