-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
util.go
444 lines (393 loc) · 12.5 KB
/
util.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package testutil
import (
"fmt"
"time"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kapi "k8s.io/kubernetes/pkg/api"
kapisext "k8s.io/kubernetes/pkg/apis/extensions"
deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
)
const (
Layer1 = "tarsum.dev+sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
Layer2 = "tarsum.dev+sha256:b194de3772ebbcdc8f244f663669799ac1cb141834b7cb8b69100285d357a2b0"
Layer3 = "tarsum.dev+sha256:c937c4bb1c1a21cc6d94340812262c6472092028972ae69b551b1a70d4276171"
Layer4 = "tarsum.dev+sha256:2aaacc362ac6be2b9e9ae8c6029f6f616bb50aec63746521858e47841b90fabd"
Layer5 = "tarsum.dev+sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
)
var (
Config1 = "sha256:2b8fd9751c4c0f5dd266fcae00707e67a2545ef34f9a29354585f93dac906749"
Config2 = "sha256:8ddc19f16526912237dd8af81971d5e4dd0587907234be2b83e249518d5b673f"
)
// ImageList turns the given images into ImageList.
func ImageList(images ...imageapi.Image) imageapi.ImageList {
return imageapi.ImageList{
Items: images,
}
}
// AgedImage creates a test image with specified age.
func AgedImage(id, ref string, ageInMinutes int64) imageapi.Image {
return CreatedImage(id, ref, time.Now().Add(time.Duration(ageInMinutes)*time.Minute*-1))
}
// CreatedImage creates a test image with the CreationTime set to the given timestamp.
func CreatedImage(id, ref string, created time.Time) imageapi.Image {
image := ImageWithLayers(id, ref, nil, Layer1, Layer2, Layer3, Layer4, Layer5)
image.CreationTimestamp = metav1.NewTime(created)
return image
}
// SizedImage returns a test image of given size.
func SizedImage(id, ref string, size int64, configName *string) imageapi.Image {
image := ImageWithLayers(id, ref, configName, Layer1, Layer2, Layer3, Layer4, Layer5)
image.CreationTimestamp = metav1.NewTime(metav1.Now().Add(time.Duration(-1) * time.Minute))
image.DockerImageMetadata.Size = size
return image
}
// Image returns a default test image object 120 minutes old.
func Image(id, ref string) imageapi.Image {
return AgedImage(id, ref, 120)
}
// Image returns a default test image referencing the given layers.
func ImageWithLayers(id, ref string, configName *string, layers ...string) imageapi.Image {
image := imageapi.Image{
ObjectMeta: metav1.ObjectMeta{
Name: id,
Annotations: map[string]string{
imageapi.ManagedByOpenShiftAnnotation: "true",
},
},
DockerImageReference: ref,
DockerImageManifestMediaType: schema1.MediaTypeManifest,
}
if configName != nil {
image.DockerImageMetadata = imageapi.DockerImage{
ID: *configName,
}
image.DockerImageConfig = fmt.Sprintf("{Digest: %s}", *configName)
image.DockerImageManifestMediaType = schema2.MediaTypeManifest
}
image.DockerImageLayers = []imageapi.ImageLayer{}
for _, layer := range layers {
image.DockerImageLayers = append(image.DockerImageLayers, imageapi.ImageLayer{Name: layer})
}
return image
}
// UnmanagedImage creates a test image object lacking managed by OpenShift annotation.
func UnmanagedImage(id, ref string, hasAnnotations bool, annotation, value string) imageapi.Image {
image := ImageWithLayers(id, ref, nil)
if !hasAnnotations {
image.Annotations = nil
} else {
delete(image.Annotations, imageapi.ManagedByOpenShiftAnnotation)
image.Annotations[annotation] = value
}
return image
}
// PodList turns the given pods into PodList.
func PodList(pods ...kapi.Pod) kapi.PodList {
return kapi.PodList{
Items: pods,
}
}
// Pod creates and returns a pod having the given docker image references.
func Pod(namespace, name string, phase kapi.PodPhase, containerImages ...string) kapi.Pod {
return AgedPod(namespace, name, phase, -1, containerImages...)
}
// AgedPod creates and returns a pod of particular age.
func AgedPod(namespace, name string, phase kapi.PodPhase, ageInMinutes int64, containerImages ...string) kapi.Pod {
pod := kapi.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
SelfLink: "/pod/" + name,
},
Spec: PodSpec(containerImages...),
Status: kapi.PodStatus{
Phase: phase,
},
}
if ageInMinutes >= 0 {
pod.CreationTimestamp = metav1.NewTime(metav1.Now().Add(time.Duration(-1*ageInMinutes) * time.Minute))
}
return pod
}
// PodSpec creates a pod specification having the given docker image references.
func PodSpec(containerImages ...string) kapi.PodSpec {
spec := kapi.PodSpec{
Containers: []kapi.Container{},
}
for _, image := range containerImages {
container := kapi.Container{
Image: image,
}
spec.Containers = append(spec.Containers, container)
}
return spec
}
// StreamList turns the given streams into StreamList.
func StreamList(streams ...imageapi.ImageStream) imageapi.ImageStreamList {
return imageapi.ImageStreamList{
Items: streams,
}
}
// Stream creates and returns a test ImageStream object 1 minute old
func Stream(registry, namespace, name string, tags map[string]imageapi.TagEventList) imageapi.ImageStream {
return AgedStream(registry, namespace, name, -1, tags)
}
// Stream creates and returns a test ImageStream object of given age.
func AgedStream(registry, namespace, name string, ageInMinutes int64, tags map[string]imageapi.TagEventList) imageapi.ImageStream {
stream := imageapi.ImageStream{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
Status: imageapi.ImageStreamStatus{
DockerImageRepository: fmt.Sprintf("%s/%s/%s", registry, namespace, name),
Tags: tags,
},
}
if ageInMinutes >= 0 {
stream.CreationTimestamp = metav1.NewTime(metav1.Now().Add(time.Duration(-1*ageInMinutes) * time.Minute))
}
return stream
}
// Stream creates an ImageStream object and returns a pointer to it.
func StreamPtr(registry, namespace, name string, tags map[string]imageapi.TagEventList) *imageapi.ImageStream {
s := Stream(registry, namespace, name, tags)
return &s
}
// Tags creates a map of tags for image stream status.
func Tags(list ...namedTagEventList) map[string]imageapi.TagEventList {
m := make(map[string]imageapi.TagEventList, len(list))
for _, tag := range list {
m[tag.name] = tag.events
}
return m
}
type namedTagEventList struct {
name string
events imageapi.TagEventList
}
// Tag creates tag entries for Tags function.
func Tag(name string, events ...imageapi.TagEvent) namedTagEventList {
return namedTagEventList{
name: name,
events: imageapi.TagEventList{
Items: events,
},
}
}
// TagEvent creates a TagEvent object.
func TagEvent(id, ref string) imageapi.TagEvent {
return imageapi.TagEvent{
Image: id,
DockerImageReference: ref,
}
}
// YoungTagEvent creates a TagEvent with the given created timestamp.
func YoungTagEvent(id, ref string, created metav1.Time) imageapi.TagEvent {
return imageapi.TagEvent{
Image: id,
Created: created,
DockerImageReference: ref,
}
}
// RCList turns the given replication controllers into RCList.
func RCList(rcs ...kapi.ReplicationController) kapi.ReplicationControllerList {
return kapi.ReplicationControllerList{
Items: rcs,
}
}
// RC creates and returns a ReplicationController.
func RC(namespace, name string, containerImages ...string) kapi.ReplicationController {
return kapi.ReplicationController{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
SelfLink: "/rc/" + name,
},
Spec: kapi.ReplicationControllerSpec{
Template: &kapi.PodTemplateSpec{
Spec: PodSpec(containerImages...),
},
},
}
}
// DSList turns the given daemon sets into DaemonSetList.
func DSList(dss ...kapisext.DaemonSet) kapisext.DaemonSetList {
return kapisext.DaemonSetList{
Items: dss,
}
}
// DS creates and returns a DaemonSet object.
func DS(namespace, name string, containerImages ...string) kapisext.DaemonSet {
return kapisext.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
SelfLink: "/ds/" + name,
},
Spec: kapisext.DaemonSetSpec{
Template: kapi.PodTemplateSpec{
Spec: PodSpec(containerImages...),
},
},
}
}
// DeploymentList turns the given deployments into DeploymentList.
func DeploymentList(deployments ...kapisext.Deployment) kapisext.DeploymentList {
return kapisext.DeploymentList{
Items: deployments,
}
}
// Deployment creates and returns aDeployment object.
func Deployment(namespace, name string, containerImages ...string) kapisext.Deployment {
return kapisext.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
SelfLink: "/deployment/" + name,
},
Spec: kapisext.DeploymentSpec{
Template: kapi.PodTemplateSpec{
Spec: PodSpec(containerImages...),
},
},
}
}
// DCList turns the given deployment configs into DeploymentConfigList.
func DCList(dcs ...deployapi.DeploymentConfig) deployapi.DeploymentConfigList {
return deployapi.DeploymentConfigList{
Items: dcs,
}
}
// DC creates and returns a DeploymentConfig object.
func DC(namespace, name string, containerImages ...string) deployapi.DeploymentConfig {
return deployapi.DeploymentConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
SelfLink: "/dc/" + name,
},
Spec: deployapi.DeploymentConfigSpec{
Template: &kapi.PodTemplateSpec{
Spec: PodSpec(containerImages...),
},
},
}
}
// RSList turns the given replica set into ReplicaSetList.
func RSList(rss ...kapisext.ReplicaSet) kapisext.ReplicaSetList {
return kapisext.ReplicaSetList{
Items: rss,
}
}
// RS creates and returns a ReplicaSet object.
func RS(namespace, name string, containerImages ...string) kapisext.ReplicaSet {
return kapisext.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
SelfLink: "/rs/" + name,
},
Spec: kapisext.ReplicaSetSpec{
Template: kapi.PodTemplateSpec{
Spec: PodSpec(containerImages...),
},
},
}
}
// BCList turns the given build configs into BuildConfigList.
func BCList(bcs ...buildapi.BuildConfig) buildapi.BuildConfigList {
return buildapi.BuildConfigList{
Items: bcs,
}
}
// BC creates and returns a BuildConfig object.
func BC(namespace, name, strategyType, fromKind, fromNamespace, fromName string) buildapi.BuildConfig {
return buildapi.BuildConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
SelfLink: "/bc/" + name,
},
Spec: buildapi.BuildConfigSpec{
CommonSpec: CommonSpec(strategyType, fromKind, fromNamespace, fromName),
},
}
}
// BuildList turns the given builds into BuildList.
func BuildList(builds ...buildapi.Build) buildapi.BuildList {
return buildapi.BuildList{
Items: builds,
}
}
// Build creates and returns a Build object.
func Build(namespace, name, strategyType, fromKind, fromNamespace, fromName string) buildapi.Build {
return buildapi.Build{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
SelfLink: "/build/" + name,
},
Spec: buildapi.BuildSpec{
CommonSpec: CommonSpec(strategyType, fromKind, fromNamespace, fromName),
},
}
}
// LimitList turns the given limits into LimitRanges.
func LimitList(limits ...int64) []*kapi.LimitRange {
list := make([]*kapi.LimitRange, len(limits))
for _, limit := range limits {
quantity := resource.NewQuantity(limit, resource.BinarySI)
list = append(list, &kapi.LimitRange{
Spec: kapi.LimitRangeSpec{
Limits: []kapi.LimitRangeItem{
{
Type: imageapi.LimitTypeImage,
Max: kapi.ResourceList{
kapi.ResourceStorage: *quantity,
},
},
},
},
})
}
return list
}
// CommonSpec creates and returns CommonSpec object.
func CommonSpec(strategyType, fromKind, fromNamespace, fromName string) buildapi.CommonSpec {
spec := buildapi.CommonSpec{
Strategy: buildapi.BuildStrategy{},
}
switch strategyType {
case "source":
spec.Strategy.SourceStrategy = &buildapi.SourceBuildStrategy{
From: kapi.ObjectReference{
Kind: fromKind,
Namespace: fromNamespace,
Name: fromName,
},
}
case "docker":
spec.Strategy.DockerStrategy = &buildapi.DockerBuildStrategy{
From: &kapi.ObjectReference{
Kind: fromKind,
Namespace: fromNamespace,
Name: fromName,
},
}
case "custom":
spec.Strategy.CustomStrategy = &buildapi.CustomBuildStrategy{
From: kapi.ObjectReference{
Kind: fromKind,
Namespace: fromNamespace,
Name: fromName,
},
}
}
return spec
}