Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print yaml definition of CR when asked for dry-run #2810

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 29 additions & 5 deletions pkg/odo/cli/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
olmv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/pkg/errors"

"github.com/ghodss/yaml"
"github.com/golang/glog"
scv1beta1 "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/openshift/odo/pkg/log"
Expand Down Expand Up @@ -87,6 +88,8 @@ type ServiceCreateOptions struct {
version string
// Resource of the GVR
resource string
// If set to true, DryRun prints the yaml that will create the service
DryRun bool
}

// NewServiceCreateOptions creates a new ServiceCreateOptions instance
Expand Down Expand Up @@ -292,23 +295,43 @@ func (o *ServiceCreateOptions) Validate() (err error) {

// Run contains the logic for the odo service create command
func (o *ServiceCreateOptions) Run() (err error) {
s := &log.Status{}
if experimental.IsExperimentalModeEnabled() {
// in case of an opertor backed service, name of the service is
// provided by the yaml specification in alm-examples. It might also
// happen that a user spins up Service Catalog based service in
// experimental mode but we're taking a bet against that for now, so
// the user won't get to see service name in the log message
log.Infof("Deploying service of type: %s", o.ServiceType)
if !o.DryRun {
log.Infof("Deploying service of type: %s", o.ServiceType)
s = log.Spinner("Deploying service")
defer s.End(false)
}
} else {
log.Infof("Deploying service %s of type: %s", o.ServiceName, o.ServiceType)
}

s := log.Spinner("Deploying service")
defer s.End(false)

if experimental.IsExperimentalModeEnabled() && o.CustomResource != "" {
// if experimental mode is enabled and o.CustomResource is not empty, we're expected to create an Operator backed service
err = svc.CreateOperatorService(o.KClient, o.group, o.version, o.resource, o.CustomResourceDefinition)
if o.DryRun {
// if it's dry run, only print the alm-example (o.CustomResourceDefinition) and exit
jsonCR, err := json.MarshalIndent(o.CustomResourceDefinition, "", " ")
if err != nil {
return err
}

// convert json to yaml
yamlCR, err := yaml.JSONToYAML(jsonCR)
if err != nil {
return err
}

log.Info(string(yamlCR))

return nil
} else {
err = svc.CreateOperatorService(o.KClient, o.group, o.version, o.resource, o.CustomResourceDefinition)
}
} else {
// otherwise just create a ServiceInstance
err = svc.CreateService(o.Client, o.ServiceName, o.ServiceType, o.Plan, o.ParametersMap, o.Application)
Expand Down Expand Up @@ -359,6 +382,7 @@ func NewCmdServiceCreate(name, fullName string) *cobra.Command {
serviceCreateCmd.Use += fmt.Sprintf(" [flags]\n %s <operator_type> --crd <crd_name> [service_name] [flags]", o.CmdFullName)
serviceCreateCmd.Example += fmt.Sprintf("\n\n") + fmt.Sprintf(createOperatorExample, fullName)
serviceCreateCmd.Flags().StringVar(&o.CustomResource, "crd", "", "The name of the CRD of the operator to be used to create the service")
serviceCreateCmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print the yaml specificiation that will be used to create the service")
}

serviceCreateCmd.Flags().StringVar(&o.Plan, "plan", "", "The name of the plan of the service to be created")
Expand Down
14 changes: 13 additions & 1 deletion tests/integration/operatorhub/cmd_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,22 @@ var _ = Describe("odo service command tests for OperatorHub", func() {
})

// Delete the pods created. This should idealy be done by `odo
// service delete` but that's implemented for operator backed
// service delete` but that's not implemented for operator backed
// services yet.
helper.CmdShouldPass("oc", "delete", "EtcdCluster", "example")
})
})

Context("When using dry-run option to create operator backed service", func() {
It("should only output the definition of the CR that will be used to start service", func() {
// First let's grab the etcd operator's name from "odo catalog list services" output
operators := helper.CmdShouldPass("odo", "catalog", "list", "services")
etcdOperator := regexp.MustCompile(`etcdoperator\.*[a-z][0-9]\.[0-9]\.[0-9]`).FindString(operators)

stdout := helper.CmdShouldPass("odo", "service", "create", etcdOperator, "--crd", "EtcdCluster", "--dry-run")
Expect(stdout).To(ContainSubstring("apiVersion"))
Expect(stdout).To(ContainSubstring("kind"))
})
})

})