github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/component/lorry_utils_test.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 component
    21  
    22  import (
    23  	"encoding/json"
    24  	"reflect"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  
    29  	corev1 "k8s.io/api/core/v1"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  
    32  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    33  	"github.com/1aal/kubeblocks/pkg/constant"
    34  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    35  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    36  )
    37  
    38  var _ = Describe("probe_utils", func() {
    39  
    40  	Context("build probe containers", func() {
    41  		var container *corev1.Container
    42  		var component *SynthesizedComponent
    43  		var probeServiceHTTPPort int
    44  		var probeServiceGRPCPort int
    45  		var clusterDefProbe *appsv1alpha1.ClusterDefinitionProbe
    46  
    47  		BeforeEach(func() {
    48  			container = buildBasicContainer()
    49  			probeServiceHTTPPort = 3501
    50  			probeServiceGRPCPort = 50001
    51  
    52  			clusterDefProbe = &appsv1alpha1.ClusterDefinitionProbe{}
    53  			clusterDefProbe.PeriodSeconds = 1
    54  			clusterDefProbe.TimeoutSeconds = 1
    55  			clusterDefProbe.FailureThreshold = 1
    56  			component = &SynthesizedComponent{}
    57  			component.CharacterType = "mysql"
    58  			component.Services = append(component.Services, corev1.Service{
    59  				ObjectMeta: metav1.ObjectMeta{
    60  					Name: "mysql",
    61  				},
    62  				Spec: corev1.ServiceSpec{
    63  					Ports: []corev1.ServicePort{{
    64  						Protocol: corev1.ProtocolTCP,
    65  						Port:     3306,
    66  					}},
    67  				},
    68  			})
    69  			component.ConsensusSpec = &appsv1alpha1.ConsensusSetSpec{
    70  				Leader: appsv1alpha1.ConsensusMember{
    71  					Name:       "leader",
    72  					AccessMode: appsv1alpha1.ReadWrite,
    73  				},
    74  				Followers: []appsv1alpha1.ConsensusMember{{
    75  					Name:       "follower",
    76  					AccessMode: appsv1alpha1.Readonly,
    77  				}},
    78  				Learner: &appsv1alpha1.ConsensusMember{
    79  					Name:       "learner",
    80  					AccessMode: appsv1alpha1.Readonly,
    81  				},
    82  			}
    83  			component.Probes = &appsv1alpha1.ClusterDefinitionProbes{
    84  				RunningProbe: &appsv1alpha1.ClusterDefinitionProbe{},
    85  				StatusProbe:  &appsv1alpha1.ClusterDefinitionProbe{},
    86  				RoleProbe:    &appsv1alpha1.ClusterDefinitionProbe{},
    87  			}
    88  			component.PodSpec = &corev1.PodSpec{
    89  				Containers: []corev1.Container{},
    90  			}
    91  		})
    92  
    93  		It("should build multiple probe containers", func() {
    94  			reqCtx := intctrlutil.RequestCtx{
    95  				Ctx: ctx,
    96  				Log: logger,
    97  			}
    98  			Expect(buildLorryContainers(reqCtx, component)).Should(Succeed())
    99  			Expect(len(component.PodSpec.Containers) >= 2).Should(BeTrue())
   100  			Expect(component.PodSpec.Containers[0].Command).ShouldNot(BeEmpty())
   101  		})
   102  
   103  		It("should build role service container", func() {
   104  			buildLorryServiceContainer(component, container, probeServiceHTTPPort, probeServiceGRPCPort)
   105  			Expect(container.Command).ShouldNot(BeEmpty())
   106  		})
   107  
   108  		It("should build status probe container", func() {
   109  			buildStatusProbeContainer("wesql", container, clusterDefProbe, probeServiceHTTPPort)
   110  			Expect(container.ReadinessProbe.HTTPGet).ShouldNot(BeNil())
   111  		})
   112  
   113  		It("should build running probe container", func() {
   114  			buildRunningProbeContainer("wesql", container, clusterDefProbe, probeServiceHTTPPort)
   115  			Expect(container.ReadinessProbe.HTTPGet).ShouldNot(BeNil())
   116  		})
   117  
   118  		It("build volume protection probe container without RBAC", func() {
   119  			reqCtx := intctrlutil.RequestCtx{
   120  				Ctx: ctx,
   121  				Log: logger,
   122  			}
   123  			zeroWatermark := 0
   124  			component.VolumeProtection = &appsv1alpha1.VolumeProtectionSpec{
   125  				HighWatermark: 90,
   126  				Volumes: []appsv1alpha1.ProtectedVolume{
   127  					{
   128  						Name: "volume-001",
   129  					},
   130  					{
   131  						Name:          "volume-002",
   132  						HighWatermark: &zeroWatermark,
   133  					},
   134  				},
   135  			}
   136  			Expect(buildLorryContainers(reqCtx, component)).Should(Succeed())
   137  			Expect(len(component.PodSpec.Containers) >= 3).Should(BeTrue())
   138  		})
   139  
   140  		It("build volume protection probe container with RBAC", func() {
   141  			reqCtx := intctrlutil.RequestCtx{
   142  				Ctx: ctx,
   143  				Log: logger,
   144  			}
   145  			zeroWatermark := 0
   146  			component.VolumeProtection = &appsv1alpha1.VolumeProtectionSpec{
   147  				HighWatermark: 90,
   148  				Volumes: []appsv1alpha1.ProtectedVolume{
   149  					{
   150  						Name: "volume-001",
   151  					},
   152  					{
   153  						Name:          "volume-002",
   154  						HighWatermark: &zeroWatermark,
   155  					},
   156  				},
   157  			}
   158  			viper.SetDefault(constant.EnableRBACManager, true)
   159  			Expect(buildLorryContainers(reqCtx, component)).Should(Succeed())
   160  			Expect(len(component.PodSpec.Containers) >= 3).Should(BeTrue())
   161  			spec := &appsv1alpha1.VolumeProtectionSpec{}
   162  			for _, e := range component.PodSpec.Containers[0].Env {
   163  				if e.Name == constant.KBEnvVolumeProtectionSpec {
   164  					Expect(json.Unmarshal([]byte(e.Value), spec)).Should(Succeed())
   165  					break
   166  				}
   167  			}
   168  			Expect(reflect.DeepEqual(component.VolumeProtection, spec)).Should(BeTrue())
   169  		})
   170  	})
   171  })