...
1 package kclient
2
3 import (
4 "errors"
5 "fmt"
6 "strconv"
7 "strings"
8
9 olm "github.com/operator-framework/api/pkg/operators/v1alpha1"
10 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11 "k8s.io/apimachinery/pkg/runtime/schema"
12 "k8s.io/apimachinery/pkg/util/json"
13
14 "github.com/olekukonko/tablewriter"
15 corev1 "k8s.io/api/core/v1"
16 )
17
18 const FieldManager = "odo"
19
20
21
22 func GetInputEnvVarsFromStrings(envVars []string) ([]corev1.EnvVar, error) {
23 var inputEnvVars []corev1.EnvVar
24 var keys = make(map[string]int)
25 for _, env := range envVars {
26 splits := strings.SplitN(env, "=", 2)
27 if len(splits) < 2 {
28 return nil, errors.New("invalid syntax for env, please specify a VariableName=Value pair")
29 }
30 _, ok := keys[splits[0]]
31 if ok {
32 return nil, fmt.Errorf("multiple values found for VariableName: %s", splits[0])
33 }
34
35 keys[splits[0]] = 1
36
37 inputEnvVars = append(inputEnvVars, corev1.EnvVar{
38 Name: splits[0],
39 Value: splits[1],
40 })
41 }
42 return inputEnvVars, nil
43 }
44
45
46 func getErrorMessageFromEvents(failedEvents map[string]corev1.Event) strings.Builder {
47
48 tableString := &strings.Builder{}
49 table := tablewriter.NewWriter(tableString)
50 table.SetAlignment(tablewriter.ALIGN_LEFT)
51 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
52 table.SetCenterSeparator("")
53 table.SetColumnSeparator("")
54 table.SetRowSeparator("")
55
56
57 table.SetHeader([]string{"Name", "Count", "Reason", "Message"})
58
59
60 for name, event := range failedEvents {
61 table.Append([]string{name, strconv.Itoa(int(event.Count)), event.Reason, event.Message})
62 }
63
64
65 table.Render()
66
67 return *tableString
68 }
69
70
71
72 func GetGVRFromCR(cr *olm.CRDDescription) schema.GroupVersionResource {
73 var group, version, resource string
74 version = cr.Version
75
76 gr := strings.SplitN(cr.Name, ".", 2)
77 resource = gr[0]
78 group = gr[1]
79
80 return schema.GroupVersionResource{
81 Group: group,
82 Version: version,
83 Resource: resource,
84 }
85 }
86
87
88
89 func ConvertK8sResourceToUnstructured(resource interface{}) (unstructuredResource unstructured.Unstructured, err error) {
90 var data []byte
91 data, err = json.Marshal(&resource)
92 if err != nil {
93 return
94 }
95 err = json.Unmarshal(data, &unstructuredResource.Object)
96 if err != nil {
97 return
98 }
99 return unstructuredResource, nil
100 }
101
View as plain text