-
Notifications
You must be signed in to change notification settings - Fork 24
/
template_funcs.go
221 lines (199 loc) · 4.85 KB
/
template_funcs.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package kubegen
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
"reflect"
"strconv"
"strings"
"text/template"
kapi "k8s.io/api/core/v1"
)
var Funcs = template.FuncMap{
"add": add,
"allPodsReady": allPodsReady,
"anyPodReady": anyPodReady,
"closest": arrayClosest,
"coalesce": coalesce,
"combine": combine,
"dir": dirList,
"exists": exists,
"first": first,
"groupBy": groupBy,
"groupByKeys": groupByKeys,
"groupByMulti": groupByMulti,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"hasField": hasField,
"intersect": intersect,
"isPodReady": isPodReady,
"isValidJson": isValidJSON,
"json": marshalJSON,
"pathJoin": filepath.Join,
"pathJoinSlice": pathJoinSlice,
"keys": keys,
"last": last,
"dict": dict,
"mapContains": mapContains,
"parseBool": strconv.ParseBool,
"parseJson": unmarshalJSON,
"parseJsonSafe": unmarshalJSONSafe,
"readyPods": readyPods,
"replace": strings.Replace,
"shell": execShell,
"slice": slice,
"split": strings.Split,
"splitN": strings.SplitN,
"strContains": strings.Contains,
"trim": strings.TrimSpace,
"trimPrefix": strings.TrimPrefix,
"trimSuffix": strings.TrimSuffix,
"values": values,
"when": when,
"where": where,
"whereExist": whereExist,
"whereNotExist": whereNotExist,
"whereAny": whereAny,
"whereAll": whereAll,
}
func pathJoinSlice(input []string) string {
return filepath.Join(input...)
}
func add(input, delta int) int {
return input + delta
}
// returns a slice of the input array/slice containing elements between begin (inclusive) and end (exclusive) indices
func slice(input any, begin, end int) (any, error) {
if input == nil {
return input, nil
}
arr := reflect.ValueOf(input)
if arr.Kind() != reflect.Slice && arr.Kind() != reflect.Array {
return nil, fmt.Errorf("slice can only be called with slice types. received: %v", arr.Kind())
}
if arr.Len() == 0 {
return input, nil
}
return arr.Slice(begin, end).Interface(), nil
}
// combine multiple slices into a single slice
func combine(slices ...any) ([]any, error) {
var cnt int
for _, s := range slices {
val := reflect.ValueOf(s)
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
return nil, fmt.Errorf("combine can only be called with slice types. received: %v", val.Kind())
}
cnt += val.Len()
}
ret := make([]any, 0, cnt)
for _, s := range slices {
val := reflect.ValueOf(s)
for i := 0; i < val.Len(); i++ {
ret = append(ret, val.Index(i).Interface())
}
}
return ret, nil
}
// returns bool indicating whether the provided value contains the specified field
func hasField(input any, field string) bool {
return deepGet(input, field) != nil
}
func values(input any) (any, error) {
if input == nil {
return nil, nil //nolint:nilnil
}
val := reflect.ValueOf(input)
if val.Kind() != reflect.Map {
return nil, fmt.Errorf("cannot call values on a non-map value: %v", input)
}
keys := val.MapKeys()
vals := make([]any, val.Len())
for i := range keys {
vals[i] = val.MapIndex(keys[i]).Interface()
}
return vals, nil
}
func marshalJSON(input any) (string, error) {
if b, err := json.Marshal(input); err != nil {
return "", err
} else {
return string(bytes.TrimRight(b, "\n")), nil
}
}
func unmarshalJSON(input string) (any, error) {
var v any
if err := json.Unmarshal([]byte(input), &v); err != nil {
return nil, err
}
return v, nil
}
// unmarshalJSONSafe is the same as unmarshalJson, but returns nil if
// json.Unmarshal returns an error
func unmarshalJSONSafe(input string) any {
var v any
if err := json.Unmarshal([]byte(input), &v); err != nil {
return nil
}
return v
}
func isValidJSON(input string) bool {
_, err := unmarshalJSON(input)
return err == nil
}
type ShellResult struct {
Success bool
Stdout string
Stderr string
}
func execShell(cs string) *ShellResult {
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
cmd := exec.Command(shellExe, shellArg, cs)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
res := &ShellResult{
Success: err == nil,
Stdout: stdout.String(),
Stderr: stderr.String(),
}
return res
}
func isPodReady(i any) bool {
if p, ok := i.(kapi.Pod); ok {
return IsPodReady(&p)
} else if p, ok := i.(*kapi.Pod); ok {
return IsPodReady(p)
}
return false
}
func allPodsReady(pods []kapi.Pod) bool {
for _, p := range pods {
if !isPodReady(p) {
return false
}
}
return true
}
func anyPodReady(pods []kapi.Pod) bool {
for _, p := range pods {
if isPodReady(p) {
return true
}
}
return false
}
func readyPods(pods []kapi.Pod) []kapi.Pod {
var ready []kapi.Pod
for _, p := range pods {
if isPodReady(p) {
ready = append(ready, p)
}
}
return ready
}