From 1efb0a3cdd0ac38ab19cd96987275b6bd2334fd7 Mon Sep 17 00:00:00 2001 From: sophon Date: Mon, 10 Mar 2025 18:48:36 +0800 Subject: [PATCH 1/2] fix: switchover validate fail --- pkg/cmd/cluster/operations.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/cluster/operations.go b/pkg/cmd/cluster/operations.go index 0633b755e..14dd06b9b 100755 --- a/pkg/cmd/cluster/operations.go +++ b/pkg/cmd/cluster/operations.go @@ -605,8 +605,26 @@ func (o *OperationsOptions) validatePromote(cluster *appsv1alpha1.Cluster) error return nil } - if cluster.Spec.ComponentSpecs[0].ComponentDef != "" { - return validateBaseOnCompDef(cluster.Spec.ComponentSpecs[0].ComponentDef) + resolveComponent := func(cluster *appsv1alpha1.Cluster, componentName string) *appsv1alpha1.ClusterComponentSpec { + componentSpec := cluster.Spec.GetComponentByName(componentName) + if componentSpec != nil { + return componentSpec + } + for i, spec := range cluster.Spec.ShardingSpecs { + if spec.Name == componentName { + return &cluster.Spec.ShardingSpecs[i].Template + } + } + return nil + } + + componentSpec := resolveComponent(cluster, componentName) + if componentSpec == nil { + return fmt.Errorf("component %s not found", componentName) + } + + if componentSpec.ComponentDef != "" { + return validateBaseOnCompDef(componentSpec.ComponentDef) } else { return validateBaseOnClusterCompDef() } From 63de5ca7081cd4ef71a5edd497fed90f38f5ef1b Mon Sep 17 00:00:00 2001 From: sophon Date: Mon, 10 Mar 2025 19:15:42 +0800 Subject: [PATCH 2/2] fix ut --- pkg/cmd/cluster/operations.go | 3 +++ pkg/cmd/cluster/operations_test.go | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/cluster/operations.go b/pkg/cmd/cluster/operations.go index 14dd06b9b..c472bdaa2 100755 --- a/pkg/cmd/cluster/operations.go +++ b/pkg/cmd/cluster/operations.go @@ -618,6 +618,9 @@ func (o *OperationsOptions) validatePromote(cluster *appsv1alpha1.Cluster) error return nil } + if componentName == "" { + componentName = cluster.Spec.ComponentSpecs[0].Name + } componentSpec := resolveComponent(cluster, componentName) if componentSpec == nil { return fmt.Errorf("component %s not found", componentName) diff --git a/pkg/cmd/cluster/operations_test.go b/pkg/cmd/cluster/operations_test.go index 78069b9e1..b41f0ec3e 100644 --- a/pkg/cmd/cluster/operations_test.go +++ b/pkg/cmd/cluster/operations_test.go @@ -362,7 +362,7 @@ var _ = Describe("operations", func() { o.Instance = "" o.Component = testing.ComponentDefName Expect(o.Validate()).ShouldNot(Succeed()) - Expect(testing.ContainExpectStrings(o.Validate().Error(), "is invalid")).Should(BeTrue()) + Expect(testing.ContainExpectStrings(o.Validate().Error(), "component fake-component-type not found")).Should(BeTrue()) }) It("Switchover ops base on component definition", func() { @@ -405,7 +405,7 @@ var _ = Describe("operations", func() { o.Component = testing.ComponentDefName Expect(o.Validate()).ShouldNot(Succeed()) fmt.Println(o.Validate().Error()) - Expect(testing.ContainExpectStrings(o.Validate().Error(), "is invalid")).Should(BeTrue()) + Expect(testing.ContainExpectStrings(o.Validate().Error(), "component fake-component-type not found")).Should(BeTrue()) }) It("Custom ops base on component definition", func() {