forked from kylemcc/kube-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_funcs.go
195 lines (176 loc) · 4.25 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
package kubegen
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
"reflect"
"strconv"
"strings"
"text/template"
kapi "k8s.io/client-go/pkg/api/v1"
)
var Funcs = template.FuncMap{
"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,
"keys": keys,
"last": last,
"dict": dict,
"mapContains": mapContains,
"parseBool": strconv.ParseBool,
"parseJson": unmarshalJson,
"parseJsonSafe": unmarshalJsonSafe,
"readyPods": readyPods,
"replace": strings.Replace,
"shell": execShell,
"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,
}
// combine multiple slices into a single slice
func combine(slices ...interface{}) ([]interface{}, 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([]interface{}, 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 interface{}, field string) bool {
return deepGet(input, field) != nil
}
func values(input interface{}) (interface{}, error) {
if input == nil {
return nil, nil
}
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([]interface{}, val.Len())
for i := range keys {
vals[i] = val.MapIndex(keys[i]).Interface()
}
return vals, nil
}
func marshalJson(input interface{}) (string, error) {
if b, err := json.Marshal(input); err != nil {
return "", err
} else {
return string(bytes.TrimRight(b, "\n")), nil
}
}
func unmarshalJson(input string) (interface{}, error) {
var v interface{}
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) interface{} {
var v interface{}
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(SHELL_EXE, SHELL_ARG, 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 interface{}) bool {
if p, ok := i.(kapi.Pod); ok {
return isV1PodReady(&p)
} else if p, ok := i.(*kapi.Pod); ok {
return isV1PodReady(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
}