...
1 package binding
2
3 import (
4 "sort"
5 "strings"
6
7 "github.com/devfile/library/v2/pkg/devfile/parser"
8 "github.com/redhat-developer/odo/pkg/api"
9 "github.com/redhat-developer/odo/pkg/labels"
10
11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12 )
13
14
15 type BindingSet struct {
16 m map[string]api.ServiceBinding
17 }
18
19
20 func newBindingSet() BindingSet {
21 return BindingSet{
22 m: map[string]api.ServiceBinding{},
23 }
24 }
25
26
27
28 func (o *BindingSet) add(binding api.ServiceBinding) {
29 o.m[binding.Name] = binding
30 }
31
32
33 func (o *BindingSet) toArray() []api.ServiceBinding {
34 var result []api.ServiceBinding
35 for _, v := range o.m {
36 result = append(result, v)
37 }
38 sort.Slice(result, func(i, j int) bool {
39 return result[i].Name < result[j].Name
40 })
41 return result
42 }
43
44
45
46 func (o *BindingClient) ListAllBindings(devfileObj *parser.DevfileObj, context string) ([]api.ServiceBinding, []string, error) {
47
48 bindingList := newBindingSet()
49 var namesInDevfile []string
50
51 if devfileObj != nil {
52 var err error
53 var bindingsInDevfile []api.ServiceBinding
54 bindingsInDevfile, err = o.GetBindingsFromDevfile(*devfileObj, context)
55 if err != nil {
56 return nil, nil, err
57 }
58 for _, binding := range bindingsInDevfile {
59 bindingList.add(binding)
60 namesInDevfile = append(namesInDevfile, binding.Name)
61 }
62 }
63
64 if o.kubernetesClient != nil {
65 specs, bindings, err := o.kubernetesClient.ListServiceBindingsFromAllGroups()
66 if err != nil {
67 return nil, nil, err
68 }
69
70 for i := range specs {
71 bindingList, err = o.process(bindingList, &specs[i])
72 if err != nil {
73 return nil, nil, err
74 }
75 }
76
77 for i := range bindings {
78 bindingList, err = o.process(bindingList, &bindings[i])
79 if err != nil {
80 return nil, nil, err
81 }
82 }
83 }
84
85 return bindingList.toArray(), namesInDevfile, nil
86 }
87
88
89
90
91 func (o *BindingClient) process(bindingList BindingSet, sb metav1.Object) (BindingSet, error) {
92 name := sb.GetName()
93 var info api.ServiceBinding
94 info, err := o.GetBindingFromCluster(name)
95 if err != nil {
96 return bindingList, err
97 }
98 setRunningMode(info, labels.GetMode(sb.GetLabels()))
99 bindingList.add(info)
100 return bindingList, nil
101 }
102
103
104
105 func setRunningMode(binding api.ServiceBinding, mode string) api.ServiceBinding {
106 if binding.Status == nil {
107 binding.Status = &api.ServiceBindingStatus{}
108 }
109 binding.Status.RunningIn = api.NewRunningModes()
110 binding.Status.RunningIn.AddRunningMode(api.RunningMode(strings.ToLower(mode)))
111 return binding
112 }
113
View as plain text