-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathk8s_util.go
More file actions
195 lines (163 loc) · 5.06 KB
/
k8s_util.go
File metadata and controls
195 lines (163 loc) · 5.06 KB
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 gitreceive
import (
"fmt"
"time"
"github.com/pborman/uuid"
"k8s.io/kubernetes/pkg/api"
apierrs "k8s.io/kubernetes/pkg/api/errors"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/util/wait"
)
const (
slugBuilderName = "deis-slugbuilder"
dockerBuilderName = "deis-dockerbuilder"
tarURLKey = "TAR_URL"
putURLKey = "put_url"
debugKey = "DEBUG"
minioUser = "objectstorage-keyfile"
dockerSocketName = "docker-socket"
dockerSocketPath = "/var/run/docker.sock"
)
func dockerBuilderPodName(appName, shortSha string) string {
uid := uuid.New()[:8]
return fmt.Sprintf("dockerbuild-%s-%s-%s", appName, shortSha, uid)
}
func slugBuilderPodName(appName, shortSha string) string {
uid := uuid.New()[:8]
return fmt.Sprintf("slugbuild-%s-%s-%s", appName, shortSha, uid)
}
func dockerBuilderPod(debug, withAuth bool, name, namespace string, env map[string]interface{}, tarURL, imageName, region, dockerBuilderImage string) *api.Pod {
pod := buildPod(debug, withAuth, name, namespace, env)
pod.Spec.Containers[0].Name = dockerBuilderName
pod.Spec.Containers[0].Image = dockerBuilderImage
addEnvToPod(pod, "ACCESS_KEY_FILE", "/var/run/secrets/object/store/access_key")
addEnvToPod(pod, "ACCESS_SECRET_FILE", "/var/run/secrets/object/store/access_secret")
addEnvToPod(pod, tarURLKey, tarURL)
addEnvToPod(pod, "IMG_NAME", imageName)
addEnvToPod(pod, "REGION", region)
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, api.VolumeMount{
Name: dockerSocketName,
MountPath: dockerSocketPath,
})
pod.Spec.Volumes = append(pod.Spec.Volumes, api.Volume{
Name: dockerSocketName,
VolumeSource: api.VolumeSource{
HostPath: &api.HostPathVolumeSource{
Path: dockerSocketPath,
},
},
})
return &pod
}
func slugbuilderPod(debug, withAuth bool, name, namespace string, env map[string]interface{}, tarKey, putURL, buildpackURL, slugBuilderImage, storageType string) *api.Pod {
pod := buildPod(debug, withAuth, name, namespace, env)
pod.Spec.Containers[0].Name = slugBuilderName
pod.Spec.Containers[0].Image = slugBuilderImage
addEnvToPod(pod, tarURLKey, tarKey)
addEnvToPod(pod, putURLKey, putURL)
addEnvToPod(pod, "BUILDER_STORAGE", storageType)
if buildpackURL != "" {
addEnvToPod(pod, "BUILDPACK_URL", buildpackURL)
}
return &pod
}
func buildPod(debug, withAuth bool, name, namespace string, env map[string]interface{}) api.Pod {
pod := api.Pod{
Spec: api.PodSpec{
RestartPolicy: api.RestartPolicyNever,
Containers: []api.Container{
api.Container{
ImagePullPolicy: api.PullAlways,
},
},
Volumes: []api.Volume{},
},
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"heritage": name,
},
},
}
if withAuth {
pod.Spec.Volumes = append(pod.Spec.Volumes, api.Volume{
Name: minioUser,
VolumeSource: api.VolumeSource{
Secret: &api.SecretVolumeSource{
SecretName: minioUser,
},
},
})
pod.Spec.Containers[0].VolumeMounts = []api.VolumeMount{
api.VolumeMount{
Name: minioUser,
MountPath: "/var/run/secrets/deis/objectstore/creds",
ReadOnly: true,
},
}
}
if len(pod.Spec.Containers) > 0 {
for k, v := range env {
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, api.EnvVar{
Name: k,
Value: fmt.Sprintf("%v", v),
})
}
}
if debug {
addEnvToPod(pod, debugKey, "1")
}
return pod
}
func addEnvToPod(pod api.Pod, key, value string) {
if len(pod.Spec.Containers) > 0 {
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, api.EnvVar{
Name: key,
Value: value,
})
}
}
// waitForPod waits for a pod in state running or failed
func waitForPod(c *client.Client, ns, podName string, interval, timeout time.Duration) error {
condition := func(pod *api.Pod) (bool, error) {
if pod.Status.Phase == api.PodRunning {
return true, nil
}
if pod.Status.Phase == api.PodFailed {
return true, fmt.Errorf("Giving up; pod went into failed status: \n%s", fmt.Sprintf("%#v", pod))
}
return false, nil
}
return waitForPodCondition(c, ns, podName, condition, interval, timeout)
}
// waitForPodEnd waits for a pod in state succeeded or failed
func waitForPodEnd(c *client.Client, ns, podName string, interval, timeout time.Duration) error {
condition := func(pod *api.Pod) (bool, error) {
if pod.Status.Phase == api.PodSucceeded {
return true, nil
}
if pod.Status.Phase == api.PodFailed {
return true, nil
}
return false, nil
}
return waitForPodCondition(c, ns, podName, condition, interval, timeout)
}
// waitForPodCondition waits for a pod in state defined by a condition (func)
func waitForPodCondition(c *client.Client, ns, podName string, condition func(pod *api.Pod) (bool, error),
interval, timeout time.Duration) error {
return wait.PollImmediate(interval, timeout, func() (bool, error) {
pod, err := c.Pods(ns).Get(podName)
if err != nil {
if apierrs.IsNotFound(err) {
return false, nil
}
}
done, err := condition(pod)
if done {
return true, nil
}
return false, nil
})
}