Files
odo/pkg/kclient/utils.go
Parthvi Vala 6cf24bbc33 Create binding between devfile component and existing service (#5641)
* Skeleton for odo create binding

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add logic

* Add functions to add data to devfile and create files

* Dharmit's review + make it work with <name>/<kind>.<apigroup>

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add support for more formats

* Add integration tests and supporting helper functions

* create > add

* Add default ServiceBinding name

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Fix timeout, and move cli/add/binding.go to cli/add/binding/binding.go

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Update pkg/binding/binding.go

Co-authored-by: Philippe Martin <contact@elol.fr>

* Update pkg/binding/binding.go

Co-authored-by: Philippe Martin <contact@elol.fr>

* Update pkg/binding/interface.go

Co-authored-by: Philippe Martin <contact@elol.fr>

* Philippe's review

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Self review

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Fix unit test failure

* Philippe's review: round 2

* Tomas review; self review

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Fix windows test failure

* Randomize binding name for test

Co-authored-by: Philippe Martin <phmartin@redhat.com>
Co-authored-by: Philippe Martin <contact@elol.fr>
2022-05-12 15:05:17 +02:00

101 lines
2.8 KiB
Go

package kclient
import (
"errors"
"fmt"
"strconv"
"strings"
olm "github.com/operator-framework/api/pkg/operators/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/json"
"github.com/olekukonko/tablewriter"
corev1 "k8s.io/api/core/v1"
)
const FieldManager = "odo"
// GetInputEnvVarsFromStrings generates corev1.EnvVar values from the array of string key=value pairs
// envVars is the array containing the key=value pairs
func GetInputEnvVarsFromStrings(envVars []string) ([]corev1.EnvVar, error) {
var inputEnvVars []corev1.EnvVar
var keys = make(map[string]int)
for _, env := range envVars {
splits := strings.SplitN(env, "=", 2)
if len(splits) < 2 {
return nil, errors.New("invalid syntax for env, please specify a VariableName=Value pair")
}
_, ok := keys[splits[0]]
if ok {
return nil, fmt.Errorf("multiple values found for VariableName: %s", splits[0])
}
keys[splits[0]] = 1
inputEnvVars = append(inputEnvVars, corev1.EnvVar{
Name: splits[0],
Value: splits[1],
})
}
return inputEnvVars, nil
}
// getErrorMessageFromEvents generates a error message from the given events
func getErrorMessageFromEvents(failedEvents map[string]corev1.Event) strings.Builder {
// Create an output table
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
// Header
table.SetHeader([]string{"Name", "Count", "Reason", "Message"})
// List of events
for name, event := range failedEvents {
table.Append([]string{name, strconv.Itoa(int(event.Count)), event.Reason, event.Message})
}
// Here we render the table as well as a helpful error message
table.Render()
return *tableString
}
// GetGVRFromCR parses and returns the values for group, version and resource
// for a given Custom Resource (CR).
func GetGVRFromCR(cr *olm.CRDDescription) schema.GroupVersionResource {
var group, version, resource string
version = cr.Version
gr := strings.SplitN(cr.Name, ".", 2)
resource = gr[0]
group = gr[1]
return schema.GroupVersionResource{
Group: group,
Version: version,
Resource: resource,
}
}
// ConvertK8sResourceToUnstructured converts any K8s resource to unstructured.Unstructured format
// TODO: Remove this method and use https://github.com/redhat-developer/service-binding-operator/blob/master/pkg/converter/unstructured.go#L11
func ConvertK8sResourceToUnstructured(resource interface{}) (unstructuredResource unstructured.Unstructured, err error) {
var data []byte
data, err = json.Marshal(&resource)
if err != nil {
return
}
err = json.Unmarshal(data, &unstructuredResource.Object)
if err != nil {
return
}
return unstructuredResource, nil
}