Skip to content

Commit

Permalink
Modifies the usage of service create, link and unlink by using separa…
Browse files Browse the repository at this point in the history
…te files for Kubernetes resources
  • Loading branch information
mik-dass committed Sep 1, 2021
1 parent 8c184a1 commit 0a33e93
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 39 deletions.
4 changes: 2 additions & 2 deletions pkg/component/component_full_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (cfd *ComponentFullDescription) fillEmptyFields(componentDesc Component, co
}

// NewComponentFullDescriptionFromClientAndLocalConfig gets the complete description of the component from both localconfig and cluster
func NewComponentFullDescriptionFromClientAndLocalConfig(client *occlient.Client, localConfigInfo *config.LocalConfigInfo, envInfo *envinfo.EnvSpecificInfo, componentName string, applicationName string, projectName string) (*ComponentFullDescription, error) {
func NewComponentFullDescriptionFromClientAndLocalConfig(client *occlient.Client, localConfigInfo *config.LocalConfigInfo, envInfo *envinfo.EnvSpecificInfo, componentName string, applicationName string, projectName string, context string) (*ComponentFullDescription, error) {
cfd := &ComponentFullDescription{}
var state State
if client == nil {
Expand All @@ -126,7 +126,7 @@ func NewComponentFullDescriptionFromClientAndLocalConfig(client *occlient.Client
if err != nil {
return cfd, err
}
configLinks, err = service.ListDevfileLinks(devfile)
configLinks, err = service.ListDevfileLinks(devfile, context)
} else {
componentDesc, err = GetComponentFromConfig(localConfigInfo)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
log.Infof("\nCreating Services for component %s", a.ComponentName)

// create the Kubernetes objects from the manifest and delete the ones not in the devfile
err = service.PushServices(a.Client.GetKubeClient(), k8sComponents, labels)
err = service.PushServices(a.Client.GetKubeClient(), k8sComponents, labels, a.Context)
if err != nil {
return errors.Wrap(err, "failed to create service(s) associated with the component")
}
Expand Down Expand Up @@ -230,13 +230,13 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
}
}

err = service.UpdateServicesWithOwnerReferences(a.Client.GetKubeClient(), k8sComponents, ownerReference)
err = service.UpdateServicesWithOwnerReferences(a.Client.GetKubeClient(), k8sComponents, ownerReference, a.Context)
if err != nil {
return err
}

// create the Kubernetes objects from the manifest and delete the ones not in the devfile
needRestart, err := service.PushLinks(a.Client.GetKubeClient(), k8sComponents, labels, a.deployment)
needRestart, err := service.PushLinks(a.Client.GetKubeClient(), k8sComponents, labels, a.deployment, a.Context)
if err != nil {
return errors.Wrap(err, "failed to create service(s) associated with the component")
}
Expand Down
24 changes: 17 additions & 7 deletions pkg/odo/cli/component/common_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type commonLinkOptions struct {
*genericclioptions.Context
// choose between Operator Hub and Service Catalog. If true, Operator Hub
csvSupport bool

inlined bool
}

func newCommonLinkOptions() *commonLinkOptions {
Expand Down Expand Up @@ -371,7 +373,7 @@ func (o *commonLinkOptions) validateForOperator() (err error) {
}

if o.operationName == unlink {
_, found, err := svc.FindDevfileServiceBinding(o.EnvSpecificInfo.GetDevfileObj(), o.serviceType, o.serviceName)
_, found, err := svc.FindDevfileServiceBinding(o.EnvSpecificInfo.GetDevfileObj(), o.serviceType, o.serviceName, o.ComponentContext)
if err != nil {
return err
}
Expand Down Expand Up @@ -446,16 +448,24 @@ func (o *commonLinkOptions) linkOperator() (err error) {
return err
}

_, found, err := svc.FindDevfileServiceBinding(o.EnvSpecificInfo.GetDevfileObj(), o.serviceType, o.serviceName)
_, found, err := svc.FindDevfileServiceBinding(o.EnvSpecificInfo.GetDevfileObj(), o.serviceType, o.serviceName, o.ComponentContext)
if err != nil {
return err
}
if found {
return fmt.Errorf("component %q is already linked with the %s %q", o.Context.EnvSpecificInfo.GetName(), o.getLinkType(), o.suppliedName)
}
err = svc.AddKubernetesComponentToDevfile(string(yamlDesc), o.serviceBinding.Name, o.EnvSpecificInfo.GetDevfileObj())
if err != nil {
return err

if o.inlined {
err = svc.AddKubernetesComponentToDevfile(string(yamlDesc), o.serviceBinding.Name, o.EnvSpecificInfo.GetDevfileObj())
if err != nil {
return err
}
} else {
err = svc.AddKubernetesComponent(string(yamlDesc), o.serviceBinding.Name, o.ComponentContext, o.EnvSpecificInfo.GetDevfileObj())
if err != nil {
return err
}
}

log.Successf("Successfully created link between component %q and %s %q\n", o.Context.EnvSpecificInfo.GetName(), o.getLinkType(), o.suppliedName)
Expand All @@ -467,12 +477,12 @@ func (o *commonLinkOptions) linkOperator() (err error) {
func (o *commonLinkOptions) unlinkOperator() (err error) {

// We already tested `found` in `validateForOperator`
name, _, err := svc.FindDevfileServiceBinding(o.EnvSpecificInfo.GetDevfileObj(), o.serviceType, o.serviceName)
name, _, err := svc.FindDevfileServiceBinding(o.EnvSpecificInfo.GetDevfileObj(), o.serviceType, o.serviceName, o.ComponentContext)
if err != nil {
return err
}

err = svc.DeleteKubernetesComponentFromDevfile(name, o.EnvSpecificInfo.GetDevfileObj())
err = svc.DeleteKubernetesComponentFromDevfile(name, o.EnvSpecificInfo.GetDevfileObj(), o.ComponentContext)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/component/create_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (co *CreateOptions) DevfileJSON() error {
return err
}

cfd, err := component.NewComponentFullDescriptionFromClientAndLocalConfig(co.Client, co.LocalConfigInfo, envInfo, envInfo.GetName(), envInfo.GetApplication(), co.Project)
cfd, err := component.NewComponentFullDescriptionFromClientAndLocalConfig(co.Client, co.LocalConfigInfo, envInfo, envInfo.GetName(), envInfo.GetApplication(), co.Project, co.ComponentContext)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/odo/cli/component/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package component

import (
"fmt"

"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/machineoutput"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"os"

"github.com/openshift/odo/pkg/component"
appCmd "github.com/openshift/odo/pkg/odo/cli/application"
Expand Down Expand Up @@ -38,6 +38,12 @@ func NewDescribeOptions() *DescribeOptions {

// Complete completes describe args
func (do *DescribeOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
if do.componentContext == "" {
do.componentContext, err = os.Getwd()
if err != nil {
return err
}
}
err = do.ComponentOptions.Complete(name, cmd, args)
if err != nil {
return err
Expand All @@ -58,7 +64,7 @@ func (do *DescribeOptions) Validate() (err error) {
// Run has the logic to perform the required actions as part of command
func (do *DescribeOptions) Run(cmd *cobra.Command) (err error) {

cfd, err := component.NewComponentFullDescriptionFromClientAndLocalConfig(do.Context.Client, do.LocalConfigInfo, do.EnvSpecificInfo, do.componentName, do.Context.Application, do.Context.Project)
cfd, err := component.NewComponentFullDescriptionFromClientAndLocalConfig(do.Context.Client, do.LocalConfigInfo, do.EnvSpecificInfo, do.componentName, do.Context.Application, do.Context.Project, do.componentContext)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/odo/cli/component/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func NewCmdLink(name, fullName string) *cobra.Command {
},
}

linkCmd.PersistentFlags().BoolVarP(&o.inlined, "inlined", "", false, "sientgagdsk")
linkCmd.PersistentFlags().StringVar(&o.port, "port", "", "Port of the backend to which to link")
linkCmd.PersistentFlags().BoolVarP(&o.wait, "wait", "w", false, "If enabled the link will return only when the component is fully running after the link is created")
linkCmd.PersistentFlags().BoolVar(&o.waitForTarget, "wait-for-target", false, "If enabled, the link command will wait for the service to be provisioned (has no effect when linking to a component)")
Expand Down
3 changes: 3 additions & 0 deletions pkg/odo/cli/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type CreateOptions struct {
fromFile string
// Backend is the service provider backend providing the service requested by the user
Backend ServiceProviderBackend

inlined bool
}

// NewCreateOptions creates a new CreateOptions instance
Expand Down Expand Up @@ -137,6 +139,7 @@ func NewCmdServiceCreate(name, fullName string) *cobra.Command {
},
}

serviceCreateCmd.Flags().BoolVar(&o.inlined, "inlined", false, "something")
serviceCreateCmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print the yaml specificiation that will be used to create the operator backed service")
// remove this feature after enabling service create interactive mode for operator backed services
serviceCreateCmd.Flags().StringVar(&o.fromFile, "from-file", "", "Path to the file containing yaml specification to use to start operator backed service")
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/service/list_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (o *ServiceListOptions) listOperatorServices() (err error) {
var devfileList []string
var devfileComponent string
if o.EnvSpecificInfo != nil {
devfileList, err = svc.ListDevfileServices(o.EnvSpecificInfo.GetDevfileObj())
devfileList, err = svc.ListDevfileServices(o.componentContext, o.EnvSpecificInfo.GetDevfileObj())
if err != nil {
return fmt.Errorf("error reading devfile")
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/odo/cli/service/operator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/spf13/cobra"
)

// This CompleteServiceCreate contains logic to complete the "odo service create" call for the case of Operator backend
// CompleteServiceCreate contains logic to complete the "odo service create" call for the case of Operator backend
func (b *OperatorBackend) CompleteServiceCreate(o *CreateOptions, cmd *cobra.Command, args []string) (err error) {
// since interactive mode is not supported for Operators yet, set it to false
o.interactive = false
Expand Down Expand Up @@ -210,7 +210,7 @@ func (b *OperatorBackend) RunServiceCreate(o *CreateOptions) (err error) {
log.Info(string(yamlCR))

return nil
} else {
} else if o.inlined {
crdYaml, err := yaml.Marshal(b.CustomResourceDefinition)
if err != nil {
return err
Expand All @@ -220,6 +220,16 @@ func (b *OperatorBackend) RunServiceCreate(o *CreateOptions) (err error) {
if err != nil {
return err
}
} else {
crdYaml, err := yaml.Marshal(b.CustomResourceDefinition)
if err != nil {
return err
}

err = svc.AddKubernetesComponent(string(crdYaml), o.ServiceName, o.componentContext, o.EnvSpecificInfo.GetDevfileObj())
if err != nil {
return err
}
}
s.End(true)

Expand Down Expand Up @@ -247,7 +257,7 @@ func (b *OperatorBackend) DeleteService(o *DeleteOptions, name string, applicati
return err
}

err = svc.DeleteKubernetesComponentFromDevfile(instanceName, o.EnvSpecificInfo.GetDevfileObj())
err = svc.DeleteKubernetesComponentFromDevfile(instanceName, o.EnvSpecificInfo.GetDevfileObj(), o.componentContext)
if err != nil {
return errors.Wrap(err, "failed to delete service from the devfile")
}
Expand Down
22 changes: 17 additions & 5 deletions pkg/service/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ import (
// PushLinks updates Link(s) from Kubernetes Inlined component in a devfile by creating new ones or removing old ones
// returns true if the component needs to be restarted (when a link has been created or deleted)
// if service binding operator is not present, it will call pushLinksWithoutOperator to create the links without it.
func PushLinks(client *kclient.Client, k8sComponents []devfile.Component, labels map[string]string, deployment *v1.Deployment) (bool, error) {
func PushLinks(client *kclient.Client, k8sComponents []devfile.Component, labels map[string]string, deployment *v1.Deployment, context string) (bool, error) {
serviceBindingSupport, err := client.IsServiceBindingSupported()
if err != nil {
return false, err
}

if !serviceBindingSupport {
return pushLinksWithoutOperator(client, k8sComponents, labels, deployment)
return pushLinksWithoutOperator(client, k8sComponents, labels, deployment, context)
}

return pushLinksWithOperator(client, k8sComponents, labels, deployment)
return pushLinksWithOperator(client, k8sComponents, labels, deployment, context)
}

// pushLinksWithOperator creates links or deletes links (if service binding operator is installed) between components and services
// returns true if the component needs to be restarted (a secret was generated and added to the deployment)
func pushLinksWithOperator(client *kclient.Client, k8sComponents []devfile.Component, labels map[string]string, deployment *v1.Deployment) (bool, error) {
func pushLinksWithOperator(client *kclient.Client, k8sComponents []devfile.Component, labels map[string]string, deployment *v1.Deployment, context string) (bool, error) {

ownerReference := generator.GetOwnerReference(deployment)
deployed, err := ListDeployedServices(client, labels)
Expand All @@ -62,6 +62,12 @@ func pushLinksWithOperator(client *kclient.Client, k8sComponents []devfile.Compo
for _, c := range k8sComponents {
// get the string representation of the YAML definition of a CRD
strCRD := c.Kubernetes.Inlined
if c.Kubernetes.Uri != "" {
strCRD, err = getDataFromURI(c.Kubernetes.Uri, context)
if err != nil {
return false, err
}
}

// convert the YAML definition into map[string]interface{} since it's needed to create dynamic resource
d := NewDynamicCRD()
Expand Down Expand Up @@ -120,7 +126,7 @@ func pushLinksWithOperator(client *kclient.Client, k8sComponents []devfile.Compo

// pushLinksWithoutOperator creates links or deletes links (if service binding operator is not installed) between components and services
// returns true if the component needs to be restarted (a secret was generated and added to the deployment)
func pushLinksWithoutOperator(client *kclient.Client, k8sComponents []devfile.Component, labels map[string]string, deployment *v1.Deployment) (bool, error) {
func pushLinksWithoutOperator(client *kclient.Client, k8sComponents []devfile.Component, labels map[string]string, deployment *v1.Deployment, context string) (bool, error) {

// check csv support before proceeding
csvSupport, err := IsCSVSupported()
Expand All @@ -147,6 +153,12 @@ func pushLinksWithoutOperator(client *kclient.Client, k8sComponents []devfile.Co
for _, c := range k8sComponents {
// get the string representation of the YAML definition of a CRD
strCRD := c.Kubernetes.Inlined
if c.Kubernetes.Uri != "" {
strCRD, err = getDataFromURI(c.Kubernetes.Uri, context)
if err != nil {
return false, err
}
}

// convert the YAML definition into map[string]interface{} since it's needed to create dynamic resource
d := NewDynamicCRD()
Expand Down
Loading

0 comments on commit 0a33e93

Please sign in to comment.