From a3a57cd53cc9a3f207b76c7ad03c69cc500fb088 Mon Sep 17 00:00:00 2001 From: Kirill Ilin Date: Wed, 20 Mar 2024 16:08:40 +0500 Subject: [PATCH] Add validation minimum and change type to int32 for compatibility --- api/v1alpha1/etcdcluster_types.go | 3 ++- api/v1alpha1/etcdcluster_webhook_test.go | 4 ++-- config/crd/bases/etcd.aenix.io_etcdclusters.yaml | 2 ++ internal/controller/etcdcluster_controller.go | 4 ++-- internal/controller/etcdcluster_controller_test.go | 4 ++-- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/api/v1alpha1/etcdcluster_types.go b/api/v1alpha1/etcdcluster_types.go index b61352a5..affc7d84 100644 --- a/api/v1alpha1/etcdcluster_types.go +++ b/api/v1alpha1/etcdcluster_types.go @@ -31,7 +31,8 @@ type EtcdClusterSpec struct { // Replicas is the count of etcd instances in cluster. // +optional // +kubebuilder:default:=3 - Replicas uint `json:"replicas,omitempty"` + // +kubebuilder:validation:Minimum:=0 + Replicas int32 `json:"replicas,omitempty"` Storage Storage `json:"storage,omitempty"` } diff --git a/api/v1alpha1/etcdcluster_webhook_test.go b/api/v1alpha1/etcdcluster_webhook_test.go index b6a88ccd..cea6adb3 100644 --- a/api/v1alpha1/etcdcluster_webhook_test.go +++ b/api/v1alpha1/etcdcluster_webhook_test.go @@ -28,7 +28,7 @@ var _ = Describe("EtcdCluster Webhook", func() { It("Should fill in the default value if a required field is empty", func() { etcdCluster := &EtcdCluster{} etcdCluster.Default() - gomega.Expect(etcdCluster.Spec.Replicas).To(gomega.Equal(uint(3))) + gomega.Expect(etcdCluster.Spec.Replicas).To(gomega.Equal(int32(3))) gomega.Expect(etcdCluster.Spec.Storage.Size).To(gomega.Equal(resource.MustParse("4Gi"))) }) @@ -43,7 +43,7 @@ var _ = Describe("EtcdCluster Webhook", func() { }, } etcdCluster.Default() - gomega.Expect(etcdCluster.Spec.Replicas).To(gomega.Equal(uint(5))) + gomega.Expect(etcdCluster.Spec.Replicas).To(gomega.Equal(int32(5))) gomega.Expect(etcdCluster.Spec.Storage.Size).To(gomega.Equal(resource.MustParse("10Gi"))) }) }) diff --git a/config/crd/bases/etcd.aenix.io_etcdclusters.yaml b/config/crd/bases/etcd.aenix.io_etcdclusters.yaml index 0f0a39ad..07cfb934 100644 --- a/config/crd/bases/etcd.aenix.io_etcdclusters.yaml +++ b/config/crd/bases/etcd.aenix.io_etcdclusters.yaml @@ -42,6 +42,8 @@ spec: replicas: default: 3 description: Replicas is the count of etcd instances in cluster. + format: int32 + minimum: 0 type: integer storage: properties: diff --git a/internal/controller/etcdcluster_controller.go b/internal/controller/etcdcluster_controller.go index bda0fa6a..fdf25533 100644 --- a/internal/controller/etcdcluster_controller.go +++ b/internal/controller/etcdcluster_controller.go @@ -302,7 +302,7 @@ func (r *EtcdClusterReconciler) ensureClusterStatefulSet( notFound = true // prepare initial cluster members initialCluster := "" - for i := uint(0); i < cluster.Spec.Replicas; i++ { + for i := int32(0); i < cluster.Spec.Replicas; i++ { if i > 0 { initialCluster += "," } @@ -441,7 +441,7 @@ func (r *EtcdClusterReconciler) ensureClusterStatefulSet( }, }, } - *statefulSet.Spec.Replicas = int32(cluster.Spec.Replicas) + *statefulSet.Spec.Replicas = cluster.Spec.Replicas if err := ctrl.SetControllerReference(cluster, statefulSet, r.Scheme); err != nil { return fmt.Errorf("cannot set controller reference: %w", err) } diff --git a/internal/controller/etcdcluster_controller_test.go b/internal/controller/etcdcluster_controller_test.go index 70aa8234..77ff24bf 100644 --- a/internal/controller/etcdcluster_controller_test.go +++ b/internal/controller/etcdcluster_controller_test.go @@ -144,8 +144,8 @@ var _ = Describe("EtcdCluster Controller", func() { err = k8sClient.Get(ctx, typeNamespacedName, sts) Expect(err).NotTo(HaveOccurred(), "cluster statefulset should exist") // mark sts as ready - sts.Status.ReadyReplicas = int32(etcdcluster.Spec.Replicas) - sts.Status.Replicas = int32(etcdcluster.Spec.Replicas) + sts.Status.ReadyReplicas = etcdcluster.Spec.Replicas + sts.Status.Replicas = etcdcluster.Spec.Replicas Expect(k8sClient.Status().Update(ctx, sts)).To(Succeed()) // reconcile and check EtcdCluster status _, err = controllerReconciler.Reconcile(ctx, reconcile.Request{