diff --git a/pkg/cmd/cluster/operations.go b/pkg/cmd/cluster/operations.go index 0633b755e..c472bdaa2 100755 --- a/pkg/cmd/cluster/operations.go +++ b/pkg/cmd/cluster/operations.go @@ -605,8 +605,29 @@ 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 + } + + if componentName == "" { + componentName = cluster.Spec.ComponentSpecs[0].Name + } + 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() } 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() {