...

Source file src/github.com/redhat-developer/odo/pkg/binding/list.go

Documentation: github.com/redhat-developer/odo/pkg/binding

     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  // BindingSet represents a Set of Bindings, indexed by their names
    15  type BindingSet struct {
    16  	m map[string]api.ServiceBinding
    17  }
    18  
    19  // newBindingSet creates a new empty Set of Bindings
    20  func newBindingSet() BindingSet {
    21  	return BindingSet{
    22  		m: map[string]api.ServiceBinding{},
    23  	}
    24  }
    25  
    26  // add a new Binding to the set, overriding the value of a previous
    27  // binding with the same name
    28  func (o *BindingSet) add(binding api.ServiceBinding) {
    29  	o.m[binding.Name] = binding
    30  }
    31  
    32  // toArray returns the list of bindings in the Set as an array, ordered by their names
    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  // ListAllBindings returns the list of Service Binding resources either defined in local Devfile
    45  // or deployed in the current namespace
    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  // process gets information about the sb from the cluster
    89  // and adds the running Mode according to its labels
    90  // then adds it to the bindingList if not already in the list
    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  // setRunningMode sets the running mode in the status of the servicebinding,
   104  // initializing the status structure if necessary
   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