Skip to content

Commit

Permalink
refactor: merge image and version together
Browse files Browse the repository at this point in the history
Signed-off-by: Phoeniix Zhao <[email protected]>
  • Loading branch information
Phoenix500526 committed Jan 10, 2024
1 parent 900f7cd commit 3f9cd1a
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 25 deletions.
3 changes: 0 additions & 3 deletions api/v1alpha1/xlinecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions internal/controller/xlinecluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controller

import (
"context"
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -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",
Expand All @@ -72,7 +69,6 @@ var _ = Describe("XlineCluster controller", func() {
Namespace: XlineClusterNamespace,
},
Spec: xapi.XlineClusterSpec{
Version: version,
Image: &image,
Replicas: 3,
},
Expand All @@ -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)),
}
Expand Down
2 changes: 1 addition & 1 deletion internal/reconciler/cluster_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 1 addition & 6 deletions internal/transformer/xlinecluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down Expand Up @@ -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},
Expand Down
6 changes: 2 additions & 4 deletions internal/transformer/xlinecluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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")
})

Expand Down

0 comments on commit 3f9cd1a

Please sign in to comment.