Skip to content

Commit

Permalink
Remove duplicated lines
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Sep 16, 2021
1 parent b74f737 commit 75b8004
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions pkg/odo/cli/service/operator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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}, "/")
Expand All @@ -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
Expand Down Expand Up @@ -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 <operator-name>` to start
Expand All @@ -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 <operator-name>/<crd-name>")
}

// 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) {
Expand Down

0 comments on commit 75b8004

Please sign in to comment.