From e33803a0f73d0a8f9825507ddabc101d16343ce0 Mon Sep 17 00:00:00 2001 From: liu-657627 <837397251@qq.com> Date: Tue, 29 Aug 2023 08:28:05 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0moduledeployments?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E5=B1=9E=E6=80=A7=E6=A0=A1=E9=AA=8C=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0controller=5Futils=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...rverless.alipay.com_moduledeployments.yaml | 7 +++ module-controller/go.mod | 2 + .../controller/utils/controller_utils_test.go | 54 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 module-controller/internal/controller/utils/controller_utils_test.go diff --git a/module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml b/module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml index 8d4a319ba..98462a8b0 100644 --- a/module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml +++ b/module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml @@ -36,10 +36,14 @@ spec: properties: deployType: type: string + enum: + - symmetric + - asymmetric deploymentName: description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster Important: Run "make" to regenerate code after modifying this file' type: string + minLength: 1 minReadySeconds: format: int32 type: integer @@ -94,12 +98,15 @@ spec: type: string name: type: string + minLength: 1 type: type: string url: type: string + format: uri version: type: string + minLength: 1 required: - name - url diff --git a/module-controller/go.mod b/module-controller/go.mod index 7fa646ea1..465b6bc1f 100644 --- a/module-controller/go.mod +++ b/module-controller/go.mod @@ -5,6 +5,7 @@ go 1.20 require ( github.com/onsi/ginkgo/v2 v2.9.5 github.com/onsi/gomega v1.27.7 + github.com/stretchr/testify v1.8.1 k8s.io/api v0.27.2 k8s.io/apimachinery v0.27.2 k8s.io/client-go v0.27.2 @@ -42,6 +43,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.15.1 // indirect github.com/prometheus/client_model v0.4.0 // indirect github.com/prometheus/common v0.42.0 // indirect diff --git a/module-controller/internal/controller/utils/controller_utils_test.go b/module-controller/internal/controller/utils/controller_utils_test.go new file mode 100644 index 000000000..6b4faa5ab --- /dev/null +++ b/module-controller/internal/controller/utils/controller_utils_test.go @@ -0,0 +1,54 @@ +package utils + +import ( + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "testing" + "time" +) + +const ( + ExistingModuleReplicaSetFinalizer = "existing-module-replicaset" +) + +func TestFinalizerFunTrue(t *testing.T) { + meta := &metav1.ObjectMeta{} + assert.True(t, AddFinalizer(meta, ExistingModuleReplicaSetFinalizer)) + assert.True(t, HasFinalizer(meta, ExistingModuleReplicaSetFinalizer)) + assert.True(t, RemoveFinalizer(meta, ExistingModuleReplicaSetFinalizer)) +} + +func TestFinalizerFunFalse(t *testing.T) { + meta := &metav1.ObjectMeta{} + meta.Finalizers = []string{ExistingModuleReplicaSetFinalizer} + assert.False(t, AddFinalizer(meta, ExistingModuleReplicaSetFinalizer)) + assert.False(t, RemoveFinalizer(meta, "test")) + RemoveFinalizer(meta, ExistingModuleReplicaSetFinalizer) + assert.False(t, HasFinalizer(meta, ExistingModuleReplicaSetFinalizer)) +} + +func TestKeyFun(t *testing.T) { + request := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: "default", Name: "test"}} + key := Key(request) + assert.Equal(t, "default/test", key) +} + +func TestGetNextReconcileTime(t *testing.T) { + now := time.Now() + reconcileTime := GetNextReconcileTime(now) + assert.Equal(t, "10s", reconcileTime.String()) + + m, _ := time.ParseDuration("-11m") + reconcileTime2 := GetNextReconcileTime(now.Add(m)) + assert.Equal(t, "1m0s", reconcileTime2.String()) + + m31, _ := time.ParseDuration("-31m") + reconcileTime3 := GetNextReconcileTime(now.Add(m31)) + assert.Equal(t, "5m0s", reconcileTime3.String()) + + h1, _ := time.ParseDuration("-61m") + reconcileTime4 := GetNextReconcileTime(now.Add(h1)) + assert.Equal(t, "10m0s", reconcileTime4.String()) +} From a52d5b7d64b7c17b8a7fbc4a26acb9d7003aeafb Mon Sep 17 00:00:00 2001 From: liu-657627 <837397251@qq.com> Date: Tue, 5 Sep 2023 08:34:03 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8D=95=E6=B5=8B?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/utils/controller_utils_test.go | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/module-controller/internal/utils/controller_utils_test.go b/module-controller/internal/utils/controller_utils_test.go index 6b4faa5ab..f5cd24645 100644 --- a/module-controller/internal/utils/controller_utils_test.go +++ b/module-controller/internal/utils/controller_utils_test.go @@ -1,6 +1,7 @@ package utils import ( + "github.com/sofastack/sofa-serverless/internal/constants/finalizer" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -9,27 +10,40 @@ import ( "time" ) -const ( - ExistingModuleReplicaSetFinalizer = "existing-module-replicaset" -) +func TestAddNotExistedFinalizer(t *testing.T) { + meta := &metav1.ObjectMeta{} + assert.True(t, AddFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer)) +} + +func TestHasNotExistedFinalizer(t *testing.T) { + meta := &metav1.ObjectMeta{} + assert.False(t, HasFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer)) +} + +func TestRemoveNotExistedFinalizer(t *testing.T) { + meta := &metav1.ObjectMeta{} + assert.False(t, RemoveFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer)) +} + +func TestAddExistedFinalizer(t *testing.T) { + meta := &metav1.ObjectMeta{} + meta.Finalizers = []string{finalizer.ModuleReplicaSetExistedFinalizer} + assert.False(t, AddFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer)) +} -func TestFinalizerFunTrue(t *testing.T) { +func TestHasExistedFinalizer(t *testing.T) { meta := &metav1.ObjectMeta{} - assert.True(t, AddFinalizer(meta, ExistingModuleReplicaSetFinalizer)) - assert.True(t, HasFinalizer(meta, ExistingModuleReplicaSetFinalizer)) - assert.True(t, RemoveFinalizer(meta, ExistingModuleReplicaSetFinalizer)) + meta.Finalizers = []string{finalizer.ModuleReplicaSetExistedFinalizer} + assert.True(t, HasFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer)) } -func TestFinalizerFunFalse(t *testing.T) { +func TestRemoveExistedFinalizer(t *testing.T) { meta := &metav1.ObjectMeta{} - meta.Finalizers = []string{ExistingModuleReplicaSetFinalizer} - assert.False(t, AddFinalizer(meta, ExistingModuleReplicaSetFinalizer)) - assert.False(t, RemoveFinalizer(meta, "test")) - RemoveFinalizer(meta, ExistingModuleReplicaSetFinalizer) - assert.False(t, HasFinalizer(meta, ExistingModuleReplicaSetFinalizer)) + meta.Finalizers = []string{finalizer.ModuleReplicaSetExistedFinalizer} + assert.True(t, RemoveFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer)) } -func TestKeyFun(t *testing.T) { +func TestKeyEqual(t *testing.T) { request := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: "default", Name: "test"}} key := Key(request) assert.Equal(t, "default/test", key) From 900ac84b74c5aece3dce4df77e4d8367bd036052 Mon Sep 17 00:00:00 2001 From: liu-657627 <837397251@qq.com> Date: Tue, 5 Sep 2023 11:33:46 +0800 Subject: [PATCH 3/4] =?UTF-8?q?1.=20=E6=B7=BB=E5=8A=A0=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E3=80=82=202.=20=E9=87=8D=E6=96=B0=E7=94=9F?= =?UTF-8?q?=E6=88=90base=20yaml=E3=80=82=203.=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module-controller/api/v1alpha1/module_types.go | 11 +++++++---- .../api/v1alpha1/moduledeployment_types.go | 2 ++ .../serverless.alipay.com_moduledeployments.yaml | 14 +++++++------- .../serverless.alipay.com_modulereplicasets.yaml | 3 +++ .../crd/bases/serverless.alipay.com_modules.yaml | 3 +++ .../serverless.alipay.com_moduletemplates.yaml | 3 +++ .../moduledeployment_controller_suit_test.go | 1 + 7 files changed, 26 insertions(+), 11 deletions(-) diff --git a/module-controller/api/v1alpha1/module_types.go b/module-controller/api/v1alpha1/module_types.go index 5cff9e4d5..5686295c3 100644 --- a/module-controller/api/v1alpha1/module_types.go +++ b/module-controller/api/v1alpha1/module_types.go @@ -59,11 +59,14 @@ type ModuleTemplateSpec struct { } type ModuleInfo struct { - Name string `json:"name"` + // +kubebuilder:validation:MinLength=1 + Name string `json:"name"` + // +kubebuilder:validation:MinLength=1 Version string `json:"version"` - Url string `json:"url"` - Type string `json:"type,omitempty"` - Md5 string `json:"md5,omitempty"` + // +kubebuilder:validation:Format=uri + Url string `json:"url"` + Type string `json:"type,omitempty"` + Md5 string `json:"md5,omitempty"` } // ModuleSpec defines the desired state of Module diff --git a/module-controller/api/v1alpha1/moduledeployment_types.go b/module-controller/api/v1alpha1/moduledeployment_types.go index ea293b842..4f80dc3de 100644 --- a/module-controller/api/v1alpha1/moduledeployment_types.go +++ b/module-controller/api/v1alpha1/moduledeployment_types.go @@ -103,10 +103,12 @@ type ModuleDeploymentStrategy struct { type ModuleDeploymentSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file + // +kubebuilder:validation:MinLength=1 BaseDeploymentName string `json:"baseDeploymentName"` Template ModuleTemplateSpec `json:"template,omitempty"` + // +kubebuilder:validation:Enum={"symmetric","asymmetric"} DeployType string `json:"deployType"` Replicas int32 `json:"replicas,omitempty"` diff --git a/module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml b/module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml index c8f0bedc8..640b48d96 100644 --- a/module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml +++ b/module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml @@ -37,13 +37,13 @@ spec: BaseDeploymentName: description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster Important: Run "make" to regenerate code after modifying this file' - type: string minLength: 1 - deployType: type: string + deployType: enum: - - symmetric - - asymmetric + - symmetric + - asymmetric + type: string minReadySeconds: format: int32 type: integer @@ -97,16 +97,16 @@ spec: md5: type: string name: - type: string minLength: 1 + type: string type: type: string url: - type: string format: uri - version: type: string + version: minLength: 1 + type: string required: - name - url diff --git a/module-controller/config/crd/bases/serverless.alipay.com_modulereplicasets.yaml b/module-controller/config/crd/bases/serverless.alipay.com_modulereplicasets.yaml index c3aaf6ec5..53d45acfb 100644 --- a/module-controller/config/crd/bases/serverless.alipay.com_modulereplicasets.yaml +++ b/module-controller/config/crd/bases/serverless.alipay.com_modulereplicasets.yaml @@ -101,12 +101,15 @@ spec: md5: type: string name: + minLength: 1 type: string type: type: string url: + format: uri type: string version: + minLength: 1 type: string required: - name diff --git a/module-controller/config/crd/bases/serverless.alipay.com_modules.yaml b/module-controller/config/crd/bases/serverless.alipay.com_modules.yaml index 54032276b..860fcc8b6 100644 --- a/module-controller/config/crd/bases/serverless.alipay.com_modules.yaml +++ b/module-controller/config/crd/bases/serverless.alipay.com_modules.yaml @@ -41,12 +41,15 @@ spec: md5: type: string name: + minLength: 1 type: string type: type: string url: + format: uri type: string version: + minLength: 1 type: string required: - name diff --git a/module-controller/config/crd/bases/serverless.alipay.com_moduletemplates.yaml b/module-controller/config/crd/bases/serverless.alipay.com_moduletemplates.yaml index 891bd46c2..17b76395d 100644 --- a/module-controller/config/crd/bases/serverless.alipay.com_moduletemplates.yaml +++ b/module-controller/config/crd/bases/serverless.alipay.com_moduletemplates.yaml @@ -49,12 +49,15 @@ spec: md5: type: string name: + minLength: 1 type: string type: type: string url: + format: uri type: string version: + minLength: 1 type: string required: - name diff --git a/module-controller/internal/controller/moduledeployment_controller_suit_test.go b/module-controller/internal/controller/moduledeployment_controller_suit_test.go index b624e9dad..8568b0026 100644 --- a/module-controller/internal/controller/moduledeployment_controller_suit_test.go +++ b/module-controller/internal/controller/moduledeployment_controller_suit_test.go @@ -97,6 +97,7 @@ func prepareModuleDeployment(namespace, moduleDeploymentName string) v1alpha1.Mo moduleDeployment := v1alpha1.ModuleDeployment{ Spec: v1alpha1.ModuleDeploymentSpec{ BaseDeploymentName: baseAppName, + DeployType: "symmetric", Template: v1alpha1.ModuleTemplateSpec{ Spec: v1alpha1.ModuleSpec{ Module: v1alpha1.ModuleInfo{ From 66f7818acf929953bc1f94b63d3eb25f9cdaebc2 Mon Sep 17 00:00:00 2001 From: liu-657627 <837397251@qq.com> Date: Tue, 5 Sep 2023 15:52:01 +0800 Subject: [PATCH 4/4] =?UTF-8?q?1.=20DeployType=E5=AE=9A=E4=B9=89=E4=B8=BA?= =?UTF-8?q?=E5=B8=B8=E9=87=8F=202.=20=E4=BF=AE=E5=A4=8D=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module-controller/api/v1alpha1/moduledeployment_types.go | 9 ++++++++- .../controller/moduledeployment_controller_suit_test.go | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/module-controller/api/v1alpha1/moduledeployment_types.go b/module-controller/api/v1alpha1/moduledeployment_types.go index 4f80dc3de..a6e561eaa 100644 --- a/module-controller/api/v1alpha1/moduledeployment_types.go +++ b/module-controller/api/v1alpha1/moduledeployment_types.go @@ -51,6 +51,13 @@ const ( CafeDeploymentReleaseProgressTermed ReleaseProgress = "Terminated" ) +type DeployType string + +const ( + ModuleDeploymentDeployTypeSymmetric DeployType = "symmetric" + ModuleDeploymentDeployTypeAsymmetric DeployType = "asymmetric" +) + type ReleaseStatus struct { // Records the latest revision. // +optional @@ -109,7 +116,7 @@ type ModuleDeploymentSpec struct { Template ModuleTemplateSpec `json:"template,omitempty"` // +kubebuilder:validation:Enum={"symmetric","asymmetric"} - DeployType string `json:"deployType"` + DeployType DeployType `json:"deployType"` Replicas int32 `json:"replicas,omitempty"` diff --git a/module-controller/internal/controller/moduledeployment_controller_suit_test.go b/module-controller/internal/controller/moduledeployment_controller_suit_test.go index 27c3be29c..77a04a177 100644 --- a/module-controller/internal/controller/moduledeployment_controller_suit_test.go +++ b/module-controller/internal/controller/moduledeployment_controller_suit_test.go @@ -5,6 +5,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/sofastack/sofa-serverless/api/v1alpha1" + moduledeploymentv1alpha1 "github.com/sofastack/sofa-serverless/api/v1alpha1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -97,7 +98,7 @@ func prepareModuleDeployment(namespace, moduleDeploymentName string) v1alpha1.Mo moduleDeployment := v1alpha1.ModuleDeployment{ Spec: v1alpha1.ModuleDeploymentSpec{ BaseDeploymentName: baseDeploymentName, - DeployType: "symmetric", + DeployType: moduledeploymentv1alpha1.ModuleDeploymentDeployTypeSymmetric, Template: v1alpha1.ModuleTemplateSpec{ Spec: v1alpha1.ModuleSpec{ Module: v1alpha1.ModuleInfo{