forked from grafana/xk6-kubernetes
/
kube_resources.go
326 lines (311 loc) · 7.86 KB
/
kube_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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Package testutils contains shared functionality for unit tests which are not included in build artifacts
package testutils
import (
appsV1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
coreV1 "k8s.io/api/core/v1"
networkV1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/resource"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// NewConfigMap creates a basic ConfigMap instance having the provided namespace and name
func NewConfigMap(name string, namespace string) *coreV1.ConfigMap {
return &coreV1.ConfigMap{
TypeMeta: metaV1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{},
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Data: map[string]string{
"key-1": "value-1",
"key-2": "value-2",
},
}
}
// NewDeployment is a helper to build a new Deployment instance
func NewDeployment(name string, namespace string) *appsV1.Deployment {
return &appsV1.Deployment{
TypeMeta: metaV1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Spec: appsV1.DeploymentSpec{
Replicas: nil,
Selector: &metaV1.LabelSelector{
MatchLabels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Template: coreV1.PodTemplateSpec{
ObjectMeta: metaV1.ObjectMeta{
Labels: map[string]string{
"app": "nginx",
},
},
Spec: coreV1.PodSpec{
Containers: []coreV1.Container{
{
Name: "nginx",
Image: "nginx:1.14.2",
Ports: []coreV1.ContainerPort{
{ContainerPort: 80},
},
},
},
},
},
},
}
}
// NewIngress is a helper to build a new Ingress instance
func NewIngress(name string, namespace string) *networkV1.Ingress {
return &networkV1.Ingress{
TypeMeta: metaV1.TypeMeta{
APIVersion: "networking.k8s.io/v1",
Kind: "Ingress",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Spec: networkV1.IngressSpec{
IngressClassName: nil,
Rules: []networkV1.IngressRule{},
},
}
}
// NewJob is a helper to build a new Job instance
func NewJob(name string, namespace string) *batchv1.Job {
return &batchv1.Job{
TypeMeta: metaV1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Spec: batchv1.JobSpec{
BackoffLimit: nil,
Template: coreV1.PodTemplateSpec{},
},
Status: batchv1.JobStatus{
Conditions: []batchv1.JobCondition{},
},
}
}
// NewJobWithStatus creates a job with a given condition
func NewJobWithStatus(name string, namespace string, status string) *batchv1.Job {
job := NewJob(name, namespace)
job.Status.Conditions = []batchv1.JobCondition{
{
Type: batchv1.JobConditionType(status),
Status: coreV1.ConditionTrue,
},
}
return job
}
// NewNamespace is a helper to build a new Namespace instance
func NewNamespace(name string) *coreV1.Namespace {
return &coreV1.Namespace{
TypeMeta: metaV1.TypeMeta{
APIVersion: "v1",
Kind: "Namespace",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
}
}
// NewNodes is a helper to build a new Node instance
func NewNodes(name string) *coreV1.Node {
return &coreV1.Node{
TypeMeta: metaV1.TypeMeta{
APIVersion: "v1",
Kind: "Node",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Spec: coreV1.NodeSpec{},
}
}
// NewPersistentVolumeClaim is a helper to build a new PersistentVolumeClaim instance
func NewPersistentVolumeClaim(name string, namespace string) *coreV1.PersistentVolumeClaim {
return &coreV1.PersistentVolumeClaim{
TypeMeta: metaV1.TypeMeta{
APIVersion: "v1",
Kind: "PersistentVolumeClaim",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Spec: coreV1.PersistentVolumeClaimSpec{
StorageClassName: nil,
AccessModes: []coreV1.PersistentVolumeAccessMode{
coreV1.ReadWriteMany,
},
Resources: coreV1.ResourceRequirements{},
},
}
}
// NewPersistentVolume is a helper to build a new PersistentVolume instance
func NewPersistentVolume(name string) *coreV1.PersistentVolume {
return &coreV1.PersistentVolume{
TypeMeta: metaV1.TypeMeta{
APIVersion: "v1",
Kind: "PersistentVolume",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Spec: coreV1.PersistentVolumeSpec{
Capacity: map[coreV1.ResourceName]resource.Quantity{
coreV1.ResourceStorage: {
Format: "10G",
},
},
PersistentVolumeSource: coreV1.PersistentVolumeSource{
HostPath: &coreV1.HostPathVolumeSource{
Path: "/tmp/" + name,
},
},
StorageClassName: "local-storage",
},
}
}
// NewPod is a helper to build a new Pod instance
func NewPod(name string, namespace string) *coreV1.Pod {
return &coreV1.Pod{
TypeMeta: metaV1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Spec: coreV1.PodSpec{
Containers: []coreV1.Container{
{
Name: "busybox",
Image: "busybox",
Command: []string{"sh", "-c", "sleep 300"},
},
},
EphemeralContainers: nil,
},
Status: coreV1.PodStatus{
Phase: coreV1.PodRunning,
},
}
}
// NewPodWithStatus is a helper for building Pods with a given Status
func NewPodWithStatus(name string, namespace string, phase string) *coreV1.Pod {
pod := NewPod(name, namespace)
pod.Status.Phase = coreV1.PodPhase(phase)
return pod
}
// PodWithEphemeralContainerStatus is a helper for building Pods with an ephemeral container having the given Status
func PodWithEphemeralContainerStatus(name string, namespace string, container string, state string) *coreV1.Pod {
pod := NewPod(name, namespace)
var containerState coreV1.ContainerState
switch state {
case "Running":
containerState = coreV1.ContainerState{
Running: &coreV1.ContainerStateRunning{},
}
case "Waiting":
containerState = coreV1.ContainerState{
Waiting: &coreV1.ContainerStateWaiting{},
}
}
pod.Status.EphemeralContainerStatuses = []coreV1.ContainerStatus{
{
Name: container,
State: containerState,
},
}
return pod
}
// NewSecret is a helper to build a new Secret instance
func NewSecret(name string, namespace string) *coreV1.Secret {
return &coreV1.Secret{
TypeMeta: metaV1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
StringData: map[string]string{
"secret-key-1": "secret-value-1",
"secret-key-2": "secret-value-2",
},
Type: "Opaque",
}
}
// NewService is a helper to build a new Service instance
func NewService(name string, namespace string) *coreV1.Service {
return &coreV1.Service{
TypeMeta: metaV1.TypeMeta{
APIVersion: "v1",
Kind: "Service",
},
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "xk6-kubernetes/unit-test",
},
},
Spec: coreV1.ServiceSpec{
Selector: map[string]string{
"app": "MyApp",
},
Ports: []coreV1.ServicePort{
{Port: 80, Protocol: coreV1.ProtocolTCP},
},
},
}
}