...
1 package binding
2
3 import (
4 "fmt"
5 "path/filepath"
6 "strings"
7
8 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
9 "github.com/devfile/library/v2/pkg/devfile/parser"
10 "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
11 devfilefs "github.com/devfile/library/v2/pkg/testingutil/filesystem"
12 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13
14 backendpkg "github.com/redhat-developer/odo/pkg/binding/backend"
15 "github.com/redhat-developer/odo/pkg/kclient"
16 "github.com/redhat-developer/odo/pkg/libdevfile"
17 )
18
19
20 func (o *BindingClient) ValidateRemoveBinding(flags map[string]string) error {
21 if flags[backendpkg.FLAG_NAME] == "" {
22 return fmt.Errorf("you must specify the service binding name with --%s flag", backendpkg.FLAG_NAME)
23 }
24 return nil
25 }
26
27
28 func (o *BindingClient) RemoveBinding(servicebindingName string, obj parser.DevfileObj) (parser.DevfileObj, error) {
29
30 k8sComponents, err := obj.Data.GetComponents(common.DevfileOptions{
31 ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.KubernetesComponentType},
32 })
33 if err != nil {
34 return obj, err
35 }
36
37 ocpComponents, err := obj.Data.GetComponents(common.DevfileOptions{
38 ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.OpenshiftComponentType},
39 })
40 if err != nil {
41 return obj, err
42 }
43
44 allComponents := make([]v1alpha2.Component, 0, len(k8sComponents)+len(ocpComponents))
45 allComponents = append(allComponents, k8sComponents...)
46 allComponents = append(allComponents, ocpComponents...)
47
48 var componentName string
49 var options []string
50 for _, component := range allComponents {
51 var unstructuredObjs []unstructured.Unstructured
52
53 unstructuredObjs, err = libdevfile.GetK8sComponentAsUnstructuredList(obj, component.Name, filepath.Dir(obj.Ctx.GetAbsPath()), devfilefs.DefaultFs{})
54 if err != nil || len(unstructuredObjs) == 0 {
55 continue
56 }
57
58
59 unstructuredObj := unstructuredObjs[0]
60 if unstructuredObj.GetKind() == kclient.ServiceBindingKind {
61 options = append(options, unstructuredObj.GetName())
62 if unstructuredObj.GetName() == servicebindingName {
63 componentName = component.Name
64 break
65 }
66 }
67 }
68 if componentName == "" {
69 return obj, fmt.Errorf("Service Binding %q not found in the devfile. Available Service Bindings: %s", servicebindingName, strings.Join(options, ", "))
70 }
71
72 err = obj.Data.DeleteComponent(componentName)
73 return obj, err
74 }
75
View as plain text