diff --git a/pkg/odo/cli/service/operator_backend.go b/pkg/odo/cli/service/operator_backend.go index 7c624a70b9a..15c2445276f 100644 --- a/pkg/odo/cli/service/operator_backend.go +++ b/pkg/odo/cli/service/operator_backend.go @@ -63,6 +63,9 @@ func (b *OperatorBackend) ValidateServiceCreate(o *CreateOptions) (err error) { // if the user wants to create service from a file, we check for // existence of file and validate if the requested operator and CR // exist on the cluster + + var csv olm.ClusterServiceVersion + if o.fromFile != "" { if _, err := os.Stat(o.fromFile); err != nil { return errors.Wrap(err, "unable to find specified file") @@ -80,7 +83,6 @@ func (b *OperatorBackend) ValidateServiceCreate(o *CreateOptions) (err error) { } // Check if the operator and the CR exist on cluster - var csv olm.ClusterServiceVersion b.CustomResource, csv, err = svc.GetCSV(o.KClient, d.OriginalCRD) if err != nil { return err @@ -97,22 +99,6 @@ func (b *OperatorBackend) ValidateServiceCreate(o *CreateOptions) (err error) { return err } - // Validate spec - hasCR, cr := o.KClient.CheckCustomResourceInCSV(b.CustomResource, &csv) - if !hasCR { - return fmt.Errorf("the %q resource doesn't exist in specified %q operator", b.CustomResource, b.group) - } - - crd, err := o.KClient.GetCRDSpec(cr, b.group, b.CustomResource) - if err != nil { - return err - } - - err = validate.AgainstSchema(crd, d.OriginalCRD["spec"], strfmt.Default) - if err != nil { - return err - } - if o.ServiceName != "" && !o.DryRun { // First check if service with provided name already exists svcFullName := strings.Join([]string{b.CustomResource, o.ServiceName}, "/") @@ -135,10 +121,10 @@ func (b *OperatorBackend) ValidateServiceCreate(o *CreateOptions) (err error) { // CRD is valid. We can use it further to create a service from it. b.CustomResourceDefinition = d.OriginalCRD - return nil + // return nil } else if b.CustomResource != "" { // make sure that CSV of the specified ServiceType exists - csv, err := o.KClient.GetClusterServiceVersion(o.ServiceType) + csv, err = o.KClient.GetClusterServiceVersion(o.ServiceType) if err != nil { // error only occurs when OperatorHub is not installed. // k8s does't have it installed by default but OCP does @@ -215,7 +201,7 @@ func (b *OperatorBackend) ValidateServiceCreate(o *CreateOptions) (err error) { } } - return nil + // return nil } else { // This block is executed only when user has neither provided a // file nor a valid `odo service create ` to start @@ -225,6 +211,24 @@ func (b *OperatorBackend) ValidateServiceCreate(o *CreateOptions) (err error) { return fmt.Errorf("please use a valid command to start an Operator backed service; desired format: %q", "odo service create /") } + + // Validate spec + hasCR, cr := o.KClient.CheckCustomResourceInCSV(b.CustomResource, &csv) + if !hasCR { + return fmt.Errorf("the %q resource doesn't exist in specified %q operator", b.CustomResource, b.group) + } + + crd, err := o.KClient.GetCRDSpec(cr, b.group, b.CustomResource) + if err != nil { + return err + } + + err = validate.AgainstSchema(crd, d.OriginalCRD["spec"], strfmt.Default) + if err != nil { + return err + } + + return nil } func (b *OperatorBackend) RunServiceCreate(o *CreateOptions) (err error) {