Skip to content

Commit

Permalink
fix odo compatibility with SBO 0.7.0 (#4613)
Browse files Browse the repository at this point in the history
* use k8s unstructured instead of SBO types

* remove service-binding-operator from vendor
  • Loading branch information
kadel authored Apr 15, 2021
1 parent c0fea1d commit 8479050
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 1,360 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/openshift/odo
go 1.13

require (
cloud.google.com/go v0.45.1 // indirect
github.com/Azure/go-autorest/autorest v0.11.4 // indirect
github.com/Microsoft/go-winio v0.4.15 // indirect
github.com/Netflix/go-expect v0.0.0-20200312175327-da48e75238e2
Expand All @@ -18,6 +19,8 @@ require (
github.com/go-git/go-git/v5 v5.2.0
github.com/gobwas/glob v0.2.4-0.20181002190808-e7a84e9525fe
github.com/golang/mock v1.4.4
github.com/gophercloud/gophercloud v0.2.0 // indirect
github.com/gorilla/mux v1.7.3 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c
github.com/imdario/mergo v0.3.11 // indirect
Expand All @@ -35,14 +38,12 @@ require (
github.com/opencontainers/image-spec v1.0.2-0.20200206005212-79b036d80240 // indirect
github.com/openshift/api v3.9.1-0.20190924102528-32369d4db2ad+incompatible
github.com/openshift/client-go v0.0.0-20200116152001-92a2713fa240
github.com/openshift/custom-resource-status v0.0.0-20200602122900-c002fd1547ca // indirect
github.com/openshift/library-go v0.0.0-20200407165825-2e79bd232e72
github.com/openshift/oc v0.0.0-alpha.0.0.20200305142246-2576e482bf00
github.com/operator-framework/operator-lifecycle-manager v0.0.0-20200422144016-a6acf50218ed
github.com/pborman/uuid v1.2.0
github.com/pkg/errors v0.9.1
github.com/posener/complete v1.1.2
github.com/redhat-developer/service-binding-operator v0.3.0
github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect
github.com/spf13/afero v1.2.2
github.com/spf13/cobra v0.0.5
Expand Down
192 changes: 0 additions & 192 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pkg/kclient/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ func (c *Client) CreateDynamicResource(exampleCustomResource map[string]interfac
Object: exampleCustomResource,
}

debugOut, _ := json.MarshalIndent(deployment, " ", " ")
klog.V(5).Infoln("Creating resource:")
klog.V(5).Infoln(string(debugOut))
// Create the dynamic resource based on the alm-example for the CRD
_, err := c.DynamicClient.Resource(deploymentRes).Namespace(c.Namespace).Create(deployment, metav1.CreateOptions{})
if err != nil {
Expand Down
84 changes: 44 additions & 40 deletions pkg/odo/cli/component/common_link.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package component

import (
"encoding/json"
"fmt"
"strings"

Expand All @@ -16,10 +15,9 @@ import (
"github.com/openshift/odo/pkg/secret"
svc "github.com/openshift/odo/pkg/service"
"github.com/openshift/odo/pkg/util"
servicebinding "github.com/redhat-developer/service-binding-operator/pkg/apis/operators/v1alpha1"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog"
)

Expand All @@ -38,7 +36,7 @@ type commonLinkOptions struct {
operationName string

// Service Binding Operator options
serviceBinding *servicebinding.ServiceBinding
serviceBinding unstructured.Unstructured
serviceType string
serviceName string
*genericclioptions.Context
Expand Down Expand Up @@ -102,36 +100,49 @@ func (o *commonLinkOptions) complete(name string, cmd *cobra.Command, args []str

componentName := o.EnvSpecificInfo.GetName()

// Assign static/hardcoded values to SBR
o.serviceBinding.Kind = kclient.ServiceBindingKind
o.serviceBinding.APIVersion = strings.Join([]string{kclient.ServiceBindingGroup, kclient.ServiceBindingVersion}, "/")

// service binding request name will be like <component-name>-<service-type>-<service-name>. For example: nodejs-etcdcluster-example
o.serviceBinding.Name = getServiceBindingName(componentName, o.serviceType, o.serviceName)
o.serviceBinding.Namespace = o.EnvSpecificInfo.GetNamespace()
truePtr := true
o.serviceBinding.Spec.DetectBindingResources = &truePtr // because we want the operator what to bind from the service

deployment, err := o.KClient.GetDeploymentByName(componentName)
if err != nil {
return err
}

// make this deployment the owner of the link we're creating so that link gets deleted upon doing "odo delete"
ownerReference := generator.GetOwnerReference(deployment)
o.serviceBinding.SetOwnerReferences(append(o.serviceBinding.GetOwnerReferences(), ownerReference))

deploymentGVR, err := o.KClient.GetDeploymentAPIVersion()
if err != nil {
return err
}

// Populate the application selector field in service binding request
o.serviceBinding.Spec.Application = &servicebinding.Application{
GroupVersionResource: deploymentGVR,
}
o.serviceBinding.SetAPIVersion(strings.Join([]string{kclient.ServiceBindingGroup, kclient.ServiceBindingVersion}, "/"))
o.serviceBinding.SetKind(kclient.ServiceBindingKind)
o.serviceBinding.SetName(getServiceBindingName(componentName, o.serviceType, o.serviceName))
o.serviceBinding.SetOwnerReferences(append(o.serviceBinding.GetOwnerReferences(), ownerReference))
o.serviceBinding.SetNamespace(o.EnvSpecificInfo.GetNamespace())

o.serviceBinding.Spec.Application.Name = componentName
err = unstructured.SetNestedField(o.serviceBinding.Object, true, "spec", "detectBindingResources")
if err != nil {
return err
}
err = unstructured.SetNestedField(o.serviceBinding.Object, false, "spec", "bindAsFiles")
if err != nil {
return err
}
err = unstructured.SetNestedField(o.serviceBinding.Object, componentName, "spec", "application", "name")
if err != nil {
return err
}
err = unstructured.SetNestedField(o.serviceBinding.Object, deploymentGVR.Group, "spec", "application", "group")
if err != nil {
return err
}
err = unstructured.SetNestedField(o.serviceBinding.Object, deploymentGVR.Version, "spec", "application", "version")
if err != nil {
return err
}
err = unstructured.SetNestedField(o.serviceBinding.Object, deploymentGVR.Resource, "spec", "application", "resource")
if err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -225,16 +236,18 @@ func (o *commonLinkOptions) validate(wait bool) (err error) {
return err
}

service := servicebinding.Service{
GroupVersionKind: metav1.GroupVersionKind{
Group: group,
Version: version,
Kind: kind,
},
Namespace: &o.KClient.Namespace,
service := map[string]interface{}{
"name": o.serviceName,
"group": group,
"version": version,
"kind": kind,
"namespace": o.KClient.Namespace,
}

err = unstructured.SetNestedSlice(o.serviceBinding.Object, []interface{}{service}, "spec", "services")
if err != nil {
return err
}
service.Name = o.serviceName
o.serviceBinding.Spec.Services = &[]servicebinding.Service{service}

return nil
}
Expand Down Expand Up @@ -288,18 +301,9 @@ func (o *commonLinkOptions) run() (err error) {
return
}

// convert service binding request into a ma[string]interface{} type so
// as to use it with dynamic client
serviceBindingMap := make(map[string]interface{})
intermediate, _ := json.Marshal(o.serviceBinding)
err = json.Unmarshal(intermediate, &serviceBindingMap)
if err != nil {
return err
}

// this creates a link by creating a service of type
// "ServiceBindingRequest" from the Operator "ServiceBindingOperator".
err = o.KClient.CreateDynamicResource(serviceBindingMap, kclient.ServiceBindingGroup, kclient.ServiceBindingVersion, kclient.ServiceBindingResource)
err = o.KClient.CreateDynamicResource(o.serviceBinding.Object, kclient.ServiceBindingGroup, kclient.ServiceBindingVersion, kclient.ServiceBindingResource)
if err != nil {
if strings.Contains(err.Error(), "already exists") {
return fmt.Errorf("component %q is already linked with the service %q", o.Context.EnvSpecificInfo.GetName(), o.suppliedName)
Expand All @@ -310,7 +314,7 @@ func (o *commonLinkOptions) run() (err error) {
// once the link is created, we need to store the information in
// env.yaml so that subsequent odo push can create a new deployment
// based on it
err = o.Context.EnvSpecificInfo.SetConfiguration("link", envinfo.EnvInfoLink{Name: o.serviceBinding.Name, ServiceKind: o.serviceType, ServiceName: o.serviceName})
err = o.Context.EnvSpecificInfo.SetConfiguration("link", envinfo.EnvInfoLink{Name: o.serviceBinding.GetName(), ServiceKind: o.serviceType, ServiceName: o.serviceName})
if err != nil {
return err
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/odo/cli/component/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import (
"fmt"
"path/filepath"

"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/odo/genericclioptions"
odoutil "github.com/openshift/odo/pkg/odo/util"
svc "github.com/openshift/odo/pkg/service"
servicebinding "github.com/redhat-developer/service-binding-operator/pkg/apis/operators/v1alpha1"
"github.com/spf13/cobra"

"github.com/openshift/odo/pkg/component"
appCmd "github.com/openshift/odo/pkg/odo/cli/application"
projectCmd "github.com/openshift/odo/pkg/odo/cli/project"
"github.com/openshift/odo/pkg/odo/genericclioptions"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/odo/util/completion"
svc "github.com/openshift/odo/pkg/service"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
ktemplates "k8s.io/kubectl/pkg/util/templates"

"github.com/spf13/cobra"
)

// LinkRecommendedCommandName is the recommended link command name
Expand Down Expand Up @@ -88,7 +87,7 @@ type LinkOptions struct {
func NewLinkOptions() *LinkOptions {
options := LinkOptions{}
options.commonLinkOptions = newCommonLinkOptions()
options.commonLinkOptions.serviceBinding = &servicebinding.ServiceBinding{}
options.commonLinkOptions.serviceBinding = unstructured.Unstructured{}
return &options
}

Expand Down
Loading

0 comments on commit 8479050

Please sign in to comment.