forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pod.go
104 lines (90 loc) · 2.44 KB
/
pod.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
package test
import (
"testing"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/admission"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/v1"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
)
type TestPod v1.Pod
func Pod() *TestPod {
return (*TestPod)(&v1.Pod{})
}
func (p *TestPod) WithAnnotation(name, value string) *TestPod {
if p.Annotations == nil {
p.Annotations = map[string]string{}
}
p.Annotations[name] = value
return p
}
func (p *TestPod) WithEnvVar(name, value string) *TestPod {
if len(p.Spec.InitContainers) == 0 {
p.Spec.InitContainers = append(p.Spec.InitContainers, v1.Container{})
}
if len(p.Spec.Containers) == 0 {
p.Spec.Containers = append(p.Spec.Containers, v1.Container{})
}
p.Spec.InitContainers[0].Env = append(p.Spec.InitContainers[0].Env, v1.EnvVar{Name: name, Value: value})
p.Spec.Containers[0].Env = append(p.Spec.Containers[0].Env, v1.EnvVar{Name: name, Value: value})
return p
}
func (p *TestPod) WithBuild(t *testing.T, build *buildapi.Build, version string) *TestPod {
gv, err := schema.ParseGroupVersion(version)
if err != nil {
t.Fatalf("%v", err)
}
encodedBuild, err := runtime.Encode(kapi.Codecs.LegacyCodec(gv), build)
if err != nil {
t.Fatalf("%v", err)
}
return p.WithAnnotation(buildapi.BuildAnnotation, build.Name).WithEnvVar("BUILD", string(encodedBuild))
}
func (p *TestPod) InitEnvValue(name string) string {
if len(p.Spec.InitContainers) == 0 {
return ""
}
for _, ev := range p.Spec.InitContainers[0].Env {
if ev.Name == name {
return ev.Value
}
}
return ""
}
func (p *TestPod) EnvValue(name string) string {
if len(p.Spec.Containers) == 0 {
return ""
}
for _, ev := range p.Spec.Containers[0].Env {
if ev.Name == name {
return ev.Value
}
}
return ""
}
func (p *TestPod) GetBuild(t *testing.T) *buildapi.Build {
obj, err := runtime.Decode(kapi.Codecs.UniversalDecoder(), []byte(p.EnvValue("BUILD")))
if err != nil {
t.Fatalf("Could not decode build: %v", err)
}
build, ok := obj.(*buildapi.Build)
if !ok {
t.Fatalf("Not a build object: %#v", obj)
}
return build
}
func (p *TestPod) ToAttributes() admission.Attributes {
return admission.NewAttributesRecord((*v1.Pod)(p),
nil,
kapi.Kind("Pod").WithVersion("version"),
"default",
"TestPod",
kapi.Resource("pods").WithVersion("version"),
"",
admission.Create,
nil)
}
func (p *TestPod) AsPod() *v1.Pod {
return (*v1.Pod)(p)
}