forked from kluctl/kluctl
/
utils_resources.go
171 lines (156 loc) · 4.19 KB
/
utils_resources.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
package e2e
import (
"bytes"
"github.com/codablock/kluctl/pkg/utils/uo"
"text/template"
)
type resourceOpts struct {
name string
namespace string
tags []string
labels map[string]string
annotations map[string]string
}
func mergeMetadata(o *uo.UnstructuredObject, opts resourceOpts) {
if opts.name != "" {
o.SetK8sName(opts.name)
}
if opts.namespace != "" {
o.SetK8sNamespace(opts.namespace)
}
if opts.labels != nil {
o.SetK8sLabels(opts.labels)
}
if opts.annotations != nil {
o.SetK8sAnnotations(opts.annotations)
}
}
func renderTemplateHelper(tmpl string, m map[string]interface{}) string {
t := template.Must(template.New("").Parse(tmpl))
r := bytes.NewBuffer(nil)
err := t.Execute(r, m)
if err != nil {
panic(err)
}
return r.String()
}
func renderTemplateObjectHelper(tmpl string, m map[string]interface{}) []*uo.UnstructuredObject {
s := renderTemplateHelper(tmpl, m)
ret, err := uo.FromStringMulti(s)
if err != nil {
panic(err)
}
return ret
}
func addConfigMapDeployment(p *testProject, dir string, data map[string]string, opts resourceOpts) {
o := uo.New()
o.SetK8sGVKs("", "v1", "ConfigMap")
mergeMetadata(o, opts)
if data != nil {
o.SetNestedField(data, "data")
}
p.addKustomizeDeployment(dir, []kustomizeResource{
{"configmap.yml", o},
}, opts.tags)
}
func addDeploymentHelper(p *testProject, dir string, o *uo.UnstructuredObject, opts resourceOpts) {
rbac := renderTemplateObjectHelper(podRbacTemplate, map[string]interface{}{
"name": o.GetK8sName(),
"namespace": o.GetK8sNamespace(),
})
for _, x := range rbac {
mergeMetadata(x, opts)
}
mergeMetadata(o, opts)
resources := []kustomizeResource{
{"rbac.yml", rbac},
{"deploy.yml", o},
}
p.addKustomizeDeployment(dir, resources, opts.tags)
}
func addDeploymentDeployment(p *testProject, dir string, opts resourceOpts, image string, command []string, args []string) {
o := renderTemplateObjectHelper(deploymentTemplate, map[string]interface{}{
"name": opts.name,
"namespace": opts.namespace,
"image": image,
})
o[0].SetNestedField(command, "spec", "template", "spec", "containers", 0, "command")
o[0].SetNestedField(args, "spec", "template", "spec", "containers", 0, "args")
addDeploymentHelper(p, dir, o[0], opts)
}
func addJobDeployment(p *testProject, dir string, opts resourceOpts, image string, command []string, args []string) {
o := renderTemplateObjectHelper(jobTemplate, map[string]interface{}{
"name": opts.name,
"namespace": opts.namespace,
"image": image,
})
o[0].SetNestedField(command, "spec", "template", "spec", "containers", 0, "command")
o[0].SetNestedField(args, "spec", "template", "spec", "containers", 0, "args")
addDeploymentHelper(p, dir, o[0], opts)
}
func addBusyboxDeployment(p *testProject, dir string, opts resourceOpts) {
addDeploymentDeployment(p, dir, opts, "busybox", []string{"sleep"}, []string{"1000"})
}
const podRbacTemplate = `
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .name }}
namespace: {{ .namespace }}
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: "{{ .name }}"
namespace: "{{ .namespace }}"
subjects:
- kind: ServiceAccount
name: "{{ .name }}"
namespace: "{{ .namespace }}"
roleRef:
kind: ClusterRole
name: "cluster-admin"
apiGroup: rbac.authorization.k8s.io
`
const deploymentTemplate = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .name }}
namespace: {{ .namespace }}
labels:
app.kubernetes.io/name: {{ .name }}
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: {{ .name }}
template:
metadata:
labels:
app.kubernetes.io/name: {{ .name }}
spec:
terminationGracePeriodSeconds: 0
serviceAccountName: {{ .name }}
containers:
- image: {{ .image }}
imagePullPolicy: IfNotPresent
name: container
`
const jobTemplate = `
apiVersion: batch/v1
kind: Job
metadata:
name: {{ .name }}
namespace: {{ .namespace }}
spec:
template:
spec:
terminationGracePeriodSeconds: 0
restartPolicy: OnFailure
serviceAccountName: {{ .name }}
containers:
- image: {{ .image }}
imagePullPolicy: IfNotPresent
name: container
`