-
Notifications
You must be signed in to change notification settings - Fork 88
/
kurl_context.go
193 lines (151 loc) · 4.25 KB
/
kurl_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package template
import (
"context"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"text/template"
kurlclientset "github.com/replicatedhq/kurl/kurlkinds/client/kurlclientset/typed/cluster/v1beta1"
kurlv1beta1 "github.com/replicatedhq/kurl/kurlkinds/pkg/apis/cluster/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
)
func GetKurlValues(installerName, nameSpace string) *kurlv1beta1.Installer {
cfg, err := k8sconfig.GetConfig()
if err != nil {
return nil
}
clientset := kurlclientset.NewForConfigOrDie(cfg)
installers := clientset.Installers(nameSpace)
retrieved, err := installers.Get(context.TODO(), installerName, metav1.GetOptions{})
if err != nil {
return nil
}
return retrieved
}
func newKurlContext(installerName, nameSpace string) *kurlCtx {
ctx := &kurlCtx{
KurlValues: make(map[string]interface{}),
}
retrieved := GetKurlValues(installerName, nameSpace)
if retrieved != nil {
ctx.AddValuesToKurlContext(retrieved)
}
return ctx
}
func (ctx kurlCtx) AddValuesToKurlContext(retrieved *kurlv1beta1.Installer) {
Spec := reflect.ValueOf(retrieved.Spec)
for i := 0; i < Spec.NumField(); i++ {
Category := reflect.ValueOf(Spec.Field(i).Interface())
TypeOfCategory := Category.Type()
RawCategoryName := Category.String()
TrimmedRight := strings.Split(RawCategoryName, ".")[1]
CategoryName := strings.Split(TrimmedRight, " ")[0]
for i := 0; i < Category.NumField(); i++ {
if Category.Field(i).CanInterface() {
ctx.KurlValues[CategoryName+"."+TypeOfCategory.Field(i).Name] = Category.Field(i).Interface()
}
}
}
}
type kurlCtx struct {
KurlValues map[string]interface{}
}
func (ctx kurlCtx) FuncMap() template.FuncMap {
return template.FuncMap{
"KurlString": ctx.kurlString,
"KurlInt": ctx.kurlInt,
"KurlBool": ctx.kurlBool,
"KurlOption": ctx.kurlOption,
"KurlAll": ctx.kurlAll,
}
}
func (ctx kurlCtx) kurlBool(yamlPath string) bool {
if len(ctx.KurlValues) == 0 {
return false
}
result, ok := ctx.KurlValues[yamlPath]
if !ok {
fmt.Printf("There is no value found at the yamlPath ''%s'\n", yamlPath)
return false
}
b, ok := result.(bool)
if !ok {
fmt.Printf("The yamlPath '%s' corresponds to value '%v' of type '%T'. The KurlBool function supports only boolean values\n", yamlPath, result, result)
return false
}
return b
}
func (ctx kurlCtx) kurlInt(yamlPath string) int {
if len(ctx.KurlValues) == 0 {
return 0
}
result, ok := ctx.KurlValues[yamlPath]
if !ok {
fmt.Printf("There is no value found at the yamlPath '%s'\n", yamlPath)
return 0
}
i, ok := result.(int)
if !ok {
fmt.Printf("The yamlPath '%s' corresponds to value '%v' of type '%T'. The KurlInt function supports only integer values\n", yamlPath, result, result)
return 0
}
return i
}
func (ctx kurlCtx) kurlString(yamlPath string) string {
if len(ctx.KurlValues) == 0 {
return ""
}
result, ok := ctx.KurlValues[yamlPath]
if !ok {
fmt.Printf("There is no value found at the yamlPath '%s'\n", yamlPath)
return ""
}
s, ok := result.(string)
if !ok {
fmt.Printf("The yamlPath '%s' corresponds to value '%v' of type '%T'. The KurlString function supports only string values\n", yamlPath, result, result)
return ""
}
return s
}
func (ctx kurlCtx) kurlOption(yamlPath string) string {
if len(ctx.KurlValues) == 0 {
return ""
}
result, ok := ctx.KurlValues[yamlPath]
if !ok {
fmt.Printf("There is no value found at the yamlPath '%s'\n", yamlPath)
return ""
}
switch t := interface{}(result).(type) {
case int:
return strconv.Itoa(t)
case string:
return t
case bool:
return strconv.FormatBool(t)
default:
fmt.Printf("The yamlPath '%s' corresponds to value '%v' of type '%T'. The KurlOption function supports only string, integer, and boolean values\n", yamlPath, result, result)
return ""
}
}
func (ctx kurlCtx) kurlAll() string {
//debug function to show all supported k:v pairs
keys := make([]string, len(ctx.KurlValues))
i := 0
for k, v := range ctx.KurlValues {
switch t := interface{}(v).(type) {
case int:
keys[i] = k + ":" + strconv.Itoa(t)
case string:
keys[i] = k + ":" + t
case bool:
keys[i] = k + ":" + strconv.FormatBool(t)
}
i++
}
sort.Strings(keys)
return strings.Join(keys, " ")
}