github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/builder/builder_stateful_set_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 builder
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	"k8s.io/apimachinery/pkg/util/intstr"
    26  
    27  	apps "k8s.io/api/apps/v1"
    28  	corev1 "k8s.io/api/core/v1"
    29  	"k8s.io/apimachinery/pkg/api/resource"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  )
    32  
    33  var _ = Describe("stateful_set builder", func() {
    34  	It("should work well", func() {
    35  		const (
    36  			name                         = "foo"
    37  			ns                           = "default"
    38  			selectorKey1, selectorValue1 = "foo-1", "bar-1"
    39  			selectorKey2, selectorValue2 = "foo-2", "bar-2"
    40  			selectorKey3, selectorValue3 = "foo-3", "bar-3"
    41  			selectorKey4, selectorValue4 = "foo-4", "bar-4"
    42  			port                         = int32(12345)
    43  			serviceName                  = "foo"
    44  			replicas                     = int32(5)
    45  			minReadySeconds              = int32(37)
    46  			policy                       = apps.OrderedReadyPodManagement
    47  		)
    48  		selectors := map[string]string{selectorKey4: selectorValue4}
    49  		pod := NewPodBuilder(ns, "foo").
    50  			AddContainer(corev1.Container{
    51  				Name:  "foo",
    52  				Image: "bar",
    53  				Ports: []corev1.ContainerPort{
    54  					{
    55  						Name:          "foo",
    56  						Protocol:      corev1.ProtocolTCP,
    57  						ContainerPort: port,
    58  					},
    59  				},
    60  			}).GetObject()
    61  		template := corev1.PodTemplateSpec{
    62  			ObjectMeta: pod.ObjectMeta,
    63  			Spec:       pod.Spec,
    64  		}
    65  		vcs := []corev1.PersistentVolumeClaim{
    66  			{
    67  				ObjectMeta: metav1.ObjectMeta{
    68  					Name:      "foo-1",
    69  					Namespace: ns,
    70  				},
    71  				Spec: corev1.PersistentVolumeClaimSpec{
    72  					VolumeName: "foo-1",
    73  					Resources: corev1.ResourceRequirements{
    74  						Requests: corev1.ResourceList{
    75  							corev1.ResourceCPU: resource.MustParse("500m"),
    76  						},
    77  					},
    78  				},
    79  			},
    80  		}
    81  		vc := corev1.PersistentVolumeClaim{
    82  			ObjectMeta: metav1.ObjectMeta{
    83  				Name:      "foo-2",
    84  				Namespace: ns,
    85  			},
    86  			Spec: corev1.PersistentVolumeClaimSpec{
    87  				VolumeName: "foo-2",
    88  				Resources: corev1.ResourceRequirements{
    89  					Requests: corev1.ResourceList{
    90  						corev1.ResourceCPU: resource.MustParse("600m"),
    91  					},
    92  				},
    93  			},
    94  		}
    95  		partition, maxUnavailable := int32(3), intstr.FromInt(2)
    96  		strategy := apps.StatefulSetUpdateStrategy{
    97  			Type: apps.RollingUpdateStatefulSetStrategyType,
    98  			RollingUpdate: &apps.RollingUpdateStatefulSetStrategy{
    99  				Partition:      &partition,
   100  				MaxUnavailable: &maxUnavailable,
   101  			},
   102  		}
   103  		strategyType := apps.OnDeleteStatefulSetStrategyType
   104  		sts := NewStatefulSetBuilder(ns, name).
   105  			AddMatchLabel(selectorKey1, selectorValue1).
   106  			AddMatchLabels(selectorKey2, selectorValue2, selectorKey3, selectorValue3).
   107  			AddMatchLabelsInMap(selectors).
   108  			SetServiceName(serviceName).
   109  			SetReplicas(replicas).
   110  			SetMinReadySeconds(minReadySeconds).
   111  			SetPodManagementPolicy(policy).
   112  			SetTemplate(template).
   113  			SetVolumeClaimTemplates(vcs...).
   114  			AddVolumeClaimTemplates(vc).
   115  			SetUpdateStrategy(strategy).
   116  			SetUpdateStrategyType(strategyType).
   117  			GetObject()
   118  
   119  		Expect(sts.Name).Should(Equal(name))
   120  		Expect(sts.Namespace).Should(Equal(ns))
   121  		Expect(sts.Spec.Selector).ShouldNot(BeNil())
   122  		Expect(sts.Spec.Selector.MatchLabels).Should(HaveLen(4))
   123  		Expect(sts.Spec.Selector.MatchLabels[selectorKey1]).Should(Equal(selectorValue1))
   124  		Expect(sts.Spec.Selector.MatchLabels[selectorKey2]).Should(Equal(selectorValue2))
   125  		Expect(sts.Spec.Selector.MatchLabels[selectorKey3]).Should(Equal(selectorValue3))
   126  		Expect(sts.Spec.Selector.MatchLabels[selectorKey4]).Should(Equal(selectorValue4))
   127  		Expect(sts.Spec.ServiceName).Should(Equal(serviceName))
   128  		Expect(sts.Spec.Replicas).ShouldNot(BeNil())
   129  		Expect(*sts.Spec.Replicas).Should(Equal(replicas))
   130  		Expect(sts.Spec.PodManagementPolicy).Should(Equal(policy))
   131  		Expect(sts.Spec.Template).Should(Equal(template))
   132  		Expect(sts.Spec.VolumeClaimTemplates).Should(HaveLen(2))
   133  		Expect(sts.Spec.VolumeClaimTemplates[0]).Should(Equal(vcs[0]))
   134  		Expect(sts.Spec.VolumeClaimTemplates[1]).Should(Equal(vc))
   135  		Expect(sts.Spec.UpdateStrategy.Type).Should(Equal(strategyType))
   136  		Expect(sts.Spec.UpdateStrategy.RollingUpdate).ShouldNot(BeNil())
   137  		Expect(sts.Spec.UpdateStrategy.RollingUpdate.Partition).ShouldNot(BeNil())
   138  		Expect(*sts.Spec.UpdateStrategy.RollingUpdate.Partition).Should(Equal(partition))
   139  		Expect(sts.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable).ShouldNot(BeNil())
   140  		Expect(sts.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable).ShouldNot(Equal(maxUnavailable))
   141  
   142  		labelSelector := &metav1.LabelSelector{
   143  			MatchLabels: selectors,
   144  		}
   145  		sts = NewStatefulSetBuilder(ns, name).SetSelector(labelSelector).GetObject()
   146  		Expect(sts.Spec.Selector).Should(Equal(labelSelector))
   147  	})
   148  })