From e616fd5076775dd108d1c09a3e86893bb2b724cd Mon Sep 17 00:00:00 2001 From: liu-657627 <837397251@qq.com> Date: Sun, 24 Sep 2023 14:29:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0url=E4=B8=8D=E7=AB=8B?= =?UTF-8?q?=E5=8D=B3=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/controller/module_controller.go | 11 ++++++++--- .../controller/module_controller_suit_test.go | 7 +++++-- .../controller/moduledeployment_controller.go | 2 +- .../controller/modulereplicaset_controller.go | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/module-controller/internal/controller/module_controller.go b/module-controller/internal/controller/module_controller.go index 2e4f3a467..08a6adef1 100644 --- a/module-controller/internal/controller/module_controller.go +++ b/module-controller/internal/controller/module_controller.go @@ -169,9 +169,14 @@ func (r *ModuleReconciler) handleTerminatingModuleInstance(ctx context.Context, } if module.Labels[label.DeleteModuleLabel] == "" { + moduleReplicaSet := &v1alpha1.ModuleReplicaSet{} + err := r.Client.Get(ctx, types.NamespacedName{Namespace: module.Namespace, Name: module.Labels[label.ModuleReplicasetLabel]}, moduleReplicaSet) + if err != nil { + return ctrl.Result{}, err + } // create a new module log.Log.Info("start to create a new module", "moduleName", module.Spec.Module.Name, "module", module.Name) - err := r.createNewModule(ctx, module) + err = r.createNewModule(ctx, module, moduleReplicaSet) if err != nil { return ctrl.Result{}, err } @@ -351,7 +356,7 @@ func (r *ModuleReconciler) handleAvailableModuleInstance(ctx context.Context, mo } // create a new module -func (r *ModuleReconciler) createNewModule(ctx context.Context, module *v1alpha1.Module) error { +func (r *ModuleReconciler) createNewModule(ctx context.Context, module *v1alpha1.Module, moduleReplicaSet *v1alpha1.ModuleReplicaSet) error { moduleLabels := module.Labels delete(moduleLabels, label.BaseInstanceIpLabel) delete(moduleLabels, label.BaseInstanceNameLabel) @@ -364,7 +369,7 @@ func (r *ModuleReconciler) createNewModule(ctx context.Context, module *v1alpha1 Namespace: module.Namespace, }, Spec: v1alpha1.ModuleSpec{ - Module: module.Spec.Module, + Module: moduleReplicaSet.Spec.Template.Spec.Module, Selector: module.Spec.Selector, }, } diff --git a/module-controller/internal/controller/module_controller_suit_test.go b/module-controller/internal/controller/module_controller_suit_test.go index a17257c7e..bdc34c9de 100644 --- a/module-controller/internal/controller/module_controller_suit_test.go +++ b/module-controller/internal/controller/module_controller_suit_test.go @@ -77,12 +77,15 @@ var _ = Describe("Module Controller", func() { }) Context("delete module deployment with ip by deleting module", func() { - + moduleReplicaSetName := "test-modulereplicaset" It("should be deleted and recreate a new one", func() { - module.Labels[label.ModuleReplicasetLabel] = "test-modulereplicaset" + module.Labels[label.ModuleReplicasetLabel] = moduleReplicaSetName module.Labels[label.ModuleNameLabel] = "test-module" Expect(k8sClient.Update(context.TODO(), &module)).Should(Succeed()) Expect(k8sClient.Delete(context.TODO(), &module)).Should(Succeed()) + moduleReplicaSet := prepareModuleReplicaSet(namespace, moduleReplicaSetName) + //utils.AddFinalizer(&moduleReplicaSet.ObjectMeta, finalizer.ModuleExistedFinalizer) + Expect(k8sClient.Create(context.TODO(), &moduleReplicaSet)).Should(Succeed()) key := types.NamespacedName{ Name: moduleName, Namespace: namespace, diff --git a/module-controller/internal/controller/moduledeployment_controller.go b/module-controller/internal/controller/moduledeployment_controller.go index a790562d4..6dc9d6124 100644 --- a/module-controller/internal/controller/moduledeployment_controller.go +++ b/module-controller/internal/controller/moduledeployment_controller.go @@ -269,7 +269,7 @@ func (r *ModuleDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error { } func isModuleChanges(module1, module2 moduledeploymentv1alpha1.ModuleInfo) bool { - return module1.Name != module2.Name || module1.Version != module2.Version + return module1.Name != module2.Name || module1.Version != module2.Version || module1.Url != module2.Url } func getModuleReplicasName(moduleDeploymentName string) string { diff --git a/module-controller/internal/controller/modulereplicaset_controller.go b/module-controller/internal/controller/modulereplicaset_controller.go index 1cc2cbbbe..05d3efcc9 100644 --- a/module-controller/internal/controller/modulereplicaset_controller.go +++ b/module-controller/internal/controller/modulereplicaset_controller.go @@ -125,7 +125,7 @@ func (r *ModuleReplicaSetReconciler) compareAndUpdateModule(ctx context.Context, desiredModule := moduleReplicaSet.Spec.Template.Spec.Module for _, existedModule := range existedModuleList.Items { - needUpgradeModule := existedModule.Spec.Module.Name != desiredModule.Name || existedModule.Spec.Module.Version != desiredModule.Version || existedModule.Spec.Module.Url != desiredModule.Url + needUpgradeModule := existedModule.Spec.Module.Name != desiredModule.Name || existedModule.Spec.Module.Version != desiredModule.Version needUninstallModule := existedModule.Spec.Module.Name != desiredModule.Name if needUpgradeModule { existedModule.Spec.Module.Name = desiredModule.Name From 96de3f901cdb3a3572d7a413b568224d2fbf7026 Mon Sep 17 00:00:00 2001 From: liu-657627 <837397251@qq.com> Date: Sun, 24 Sep 2023 14:33:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0url=E4=B8=8D=E7=AB=8B?= =?UTF-8?q?=E5=8D=B3=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/controller/module_controller_suit_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/module-controller/internal/controller/module_controller_suit_test.go b/module-controller/internal/controller/module_controller_suit_test.go index bdc34c9de..ceaed8410 100644 --- a/module-controller/internal/controller/module_controller_suit_test.go +++ b/module-controller/internal/controller/module_controller_suit_test.go @@ -84,7 +84,6 @@ var _ = Describe("Module Controller", func() { Expect(k8sClient.Update(context.TODO(), &module)).Should(Succeed()) Expect(k8sClient.Delete(context.TODO(), &module)).Should(Succeed()) moduleReplicaSet := prepareModuleReplicaSet(namespace, moduleReplicaSetName) - //utils.AddFinalizer(&moduleReplicaSet.ObjectMeta, finalizer.ModuleExistedFinalizer) Expect(k8sClient.Create(context.TODO(), &moduleReplicaSet)).Should(Succeed()) key := types.NamespacedName{ Name: moduleName, From 1278f0133e80f5f879f2a090be9db84feb2b157c Mon Sep 17 00:00:00 2001 From: liu-657627 <837397251@qq.com> Date: Sun, 24 Sep 2023 16:58:32 +0800 Subject: [PATCH 3/3] add unit test --- .../internal/controller/module_controller_suit_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/module-controller/internal/controller/module_controller_suit_test.go b/module-controller/internal/controller/module_controller_suit_test.go index ceaed8410..a7bad7cf3 100644 --- a/module-controller/internal/controller/module_controller_suit_test.go +++ b/module-controller/internal/controller/module_controller_suit_test.go @@ -78,12 +78,14 @@ var _ = Describe("Module Controller", func() { Context("delete module deployment with ip by deleting module", func() { moduleReplicaSetName := "test-modulereplicaset" + updateModuleUrl := "https://module-test-url" It("should be deleted and recreate a new one", func() { module.Labels[label.ModuleReplicasetLabel] = moduleReplicaSetName module.Labels[label.ModuleNameLabel] = "test-module" Expect(k8sClient.Update(context.TODO(), &module)).Should(Succeed()) Expect(k8sClient.Delete(context.TODO(), &module)).Should(Succeed()) moduleReplicaSet := prepareModuleReplicaSet(namespace, moduleReplicaSetName) + moduleReplicaSet.Spec.Template.Spec.Module.Url = updateModuleUrl Expect(k8sClient.Create(context.TODO(), &moduleReplicaSet)).Should(Succeed()) key := types.NamespacedName{ Name: moduleName, @@ -97,7 +99,7 @@ var _ = Describe("Module Controller", func() { err = k8sClient.List(context.TODO(), modules, &client.ListOptions{Namespace: module.Namespace, LabelSelector: selector}) if err == nil && len(modules.Items) > 0 { module = modules.Items[0] - return true + return module.Spec.Module.Url == updateModuleUrl } } return false