From 3f9cd1a6536ee54f400a86bfb04a71ae376f3255 Mon Sep 17 00:00:00 2001 From: Phoeniix Zhao Date: Wed, 10 Jan 2024 13:26:44 +0800 Subject: [PATCH] refactor: merge image and version together Signed-off-by: Phoeniix Zhao --- api/v1alpha1/xlinecluster_types.go | 3 --- .../bases/xline.kvstore.datenlord.com_xlineclusters.yaml | 4 ---- internal/controller/xlinecluster_controller_test.go | 9 ++------- internal/reconciler/cluster_sync.go | 2 +- internal/transformer/xlinecluster_resource.go | 7 +------ internal/transformer/xlinecluster_resource_test.go | 6 ++---- 6 files changed, 6 insertions(+), 25 deletions(-) diff --git a/api/v1alpha1/xlinecluster_types.go b/api/v1alpha1/xlinecluster_types.go index e9cfce4b..d373570a 100644 --- a/api/v1alpha1/xlinecluster_types.go +++ b/api/v1alpha1/xlinecluster_types.go @@ -54,9 +54,6 @@ type XlineClusterList struct { // XlineClusterSpec defines the desired state of XlineCluster // +k8s:openapi-gen=true type XlineClusterSpec struct { - // Xline cluster image version - Version string `json:"version"` - // Xline cluster image Image *string `json:"image,omitempty"` diff --git a/config/crd/bases/xline.kvstore.datenlord.com_xlineclusters.yaml b/config/crd/bases/xline.kvstore.datenlord.com_xlineclusters.yaml index c105cb8e..04f711a2 100644 --- a/config/crd/bases/xline.kvstore.datenlord.com_xlineclusters.yaml +++ b/config/crd/bases/xline.kvstore.datenlord.com_xlineclusters.yaml @@ -46,12 +46,8 @@ spec: format: int32 minimum: 3 type: integer - version: - description: Xline cluster image version - type: string required: - replicas - - version type: object status: description: XlineClusterStatus defines the observed state of XlineCluster diff --git a/internal/controller/xlinecluster_controller_test.go b/internal/controller/xlinecluster_controller_test.go index 07d24c9c..b8747c30 100644 --- a/internal/controller/xlinecluster_controller_test.go +++ b/internal/controller/xlinecluster_controller_test.go @@ -18,7 +18,6 @@ package controller import ( "context" - "fmt" "time" . "github.com/onsi/ginkgo/v2" @@ -59,9 +58,7 @@ var _ = Describe("XlineCluster controller", func() { It("Should increase XlineCluster Status count when a new XlineCluster resource is created", func() { By("By creating a new XlineCluster") ctx := context.Background() - image := "test-image" - version := "latest" - full_image := fmt.Sprintf("%s:%s", image, version) + image := "test-image:latest" xlineCluster := &xapi.XlineCluster{ TypeMeta: metav1.TypeMeta{ APIVersion: "xline.kvstore.datenlord.com/v1alpha1", @@ -72,7 +69,6 @@ var _ = Describe("XlineCluster controller", func() { Namespace: XlineClusterNamespace, }, Spec: xapi.XlineClusterSpec{ - Version: version, Image: &image, Replicas: 3, }, @@ -89,14 +85,13 @@ var _ = Describe("XlineCluster controller", func() { }, timeout, interval).Should(BeTrue()) // Let's make sure our Schedule string value was properly converted/handled. Expect(createdXlineCluster.Spec.Replicas).Should(Equal(int32(3))) - Expect(createdXlineCluster.Spec.Version).Should(Equal(version)) Expect(*createdXlineCluster.Spec.Image).Should(Equal(image)) By("XlinCluster Status should be updated") expected_status := ExpectClusterStatus{ Stage: xapi.StageComplete, StageStatus: xapi.StageResultSucceeded, - Image: full_image, + Image: image, StatefulSetRef: xapi.NewNamespacedName(xlineNamespaceName(XlineClusterStsName, XlineClusterNamespace)), ServiceRef: xapi.NewNamespacedName(xlineNamespaceName(XlineClusterSvcName, XlineClusterNamespace)), } diff --git a/internal/reconciler/cluster_sync.go b/internal/reconciler/cluster_sync.go index a2d0a889..5bfe3a7c 100644 --- a/internal/reconciler/cluster_sync.go +++ b/internal/reconciler/cluster_sync.go @@ -33,7 +33,7 @@ func (r *XlineClusterReconciler) syncXlineStatus(xlineStatus *xapi.XlineClusterS return err } if exist { - xlineStatus.Image = tran.GetXlineImage(r.CR) + xlineStatus.Image = *r.CR.Spec.Image xlineStatus.StatefulSetRef = xapi.NewNamespacedName(stsRef) xlineStatus.Conditions = sts.Status.Conditions } diff --git a/internal/transformer/xlinecluster_resource.go b/internal/transformer/xlinecluster_resource.go index 8c6ce49a..8265930e 100644 --- a/internal/transformer/xlinecluster_resource.go +++ b/internal/transformer/xlinecluster_resource.go @@ -31,11 +31,6 @@ func GetXlineInstanceLabels(xlineClusterName types.NamespacedName) map[string]st return MakeResourceLabels(xlineClusterName.Name) } -func GetXlineImage(r *xapi.XlineCluster) string { - version := r.Spec.Version - return fmt.Sprintf("%s:%s", *r.Spec.Image, version) -} - func GetMemberTopology(stsRef types.NamespacedName, svcName string, replicas int) string { members := make([]string, replicas) for i := 0; i < replicas; i++ { @@ -78,7 +73,7 @@ func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.State // pod template: main container mainContainer := corev1.Container{ Name: "xline", - Image: GetXlineImage(cr), + Image: *cr.Spec.Image, ImagePullPolicy: cr.Spec.ImagePullPolicy, Ports: []corev1.ContainerPort{ {Name: "xline-port", ContainerPort: 2379}, diff --git a/internal/transformer/xlinecluster_resource_test.go b/internal/transformer/xlinecluster_resource_test.go index b007c03f..02d9e8fe 100644 --- a/internal/transformer/xlinecluster_resource_test.go +++ b/internal/transformer/xlinecluster_resource_test.go @@ -11,15 +11,13 @@ import ( ) func TestXlineClusterFunc(t *testing.T) { - test_image := "xline-img" - test_image_version := "latest" + test_image := "xline-img:latest" xlineCluster := xapi.XlineCluster{ ObjectMeta: metav1.ObjectMeta{ Name: "xline", Namespace: "default", }, Spec: xapi.XlineClusterSpec{ - Version: test_image_version, Image: &test_image, Replicas: 3, }, @@ -40,7 +38,7 @@ func TestXlineClusterFunc(t *testing.T) { }) t.Run("GetXlineImage should work properly", func(t *testing.T) { - xline_image := GetXlineImage(&xlineCluster) + xline_image := *xlineCluster.Spec.Image assert.Equal(t, xline_image, "xline-img:latest") })