Skip to content

Commit 8e340ea

Browse files
committed
ref(builder): remove yams template files
1 parent e72b96e commit 8e340ea

7 files changed

Lines changed: 232 additions & 204 deletions

File tree

pkg/gitreceive/build.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"strings"
13+
"time"
1314

1415
"github.com/aws/aws-sdk-go/service/s3"
1516
"github.com/deis/builder/pkg"
@@ -20,9 +21,6 @@ import (
2021

2122
"k8s.io/kubernetes/pkg/api"
2223
client "k8s.io/kubernetes/pkg/client/unversioned"
23-
"k8s.io/kubernetes/pkg/fields"
24-
"k8s.io/kubernetes/pkg/labels"
25-
"k8s.io/kubernetes/pkg/watch"
2624
)
2725

2826
// repoCmd returns exec.Command(first, others...) with its current working directory repoDir
@@ -170,26 +168,15 @@ func build(conf *Config, s3Client *s3.S3, kubeClient *client.Client, builderKey,
170168
return fmt.Errorf("creating builder pod (%s)", err)
171169
}
172170

173-
watcher, err := podsInterface.Watch(labels.Nothing(), fields.OneTermEqualSelector("name", pod.Name), newPod.ResourceVersion)
174-
if err != nil {
175-
return fmt.Errorf("watching events for builder pod startup")
176-
}
177-
178-
ch := watcher.ResultChan()
179-
for evt := range ch {
180-
if evt.Type == watch.Added {
181-
watcher.Stop()
182-
break
183-
} else if evt.Type == watch.Error {
184-
watcher.Stop()
185-
return fmt.Errorf("builder pod failed to launch with ERROR")
186-
}
171+
timeout := time.Duration(5 * time.Minute)
172+
tick := time.Duration(500 * time.Millisecond)
173+
if err := waitForPod(kubeClient, newPod.Namespace, newPod.Name, tick, timeout); err != nil {
174+
return fmt.Errorf("watching events for builder pod startup (%s)", err)
187175
}
188176

189-
req := kubeClient.Get().Namespace(conf.PodNamespace).Name(newPod.Name).Resource("pods").SubResource("log").VersionedParams(
177+
req := kubeClient.Get().Namespace(newPod.Namespace).Name(newPod.Name).Resource("pods").SubResource("log").VersionedParams(
190178
&api.PodLogOptions{
191-
Follow: true,
192-
Previous: true,
179+
Follow: true,
193180
}, api.Scheme)
194181

195182
rc, err := req.Stream()
@@ -198,10 +185,11 @@ func build(conf *Config, s3Client *s3.S3, kubeClient *client.Client, builderKey,
198185
}
199186
defer rc.Close()
200187

201-
_, err = io.Copy(os.Stdout, rc)
188+
size, err := io.Copy(os.Stdout, rc)
202189
if err != nil {
203190
return fmt.Errorf("fetching builder logs (%s)", err)
204191
}
192+
log.Debug("size of logs streamed %v", size)
205193

206194
// poll the s3 server to ensure the slug exists
207195
// TODO: time out looking

pkg/gitreceive/k8s_util.go

Lines changed: 132 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@ package gitreceive
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"time"
66

77
"github.com/pborman/uuid"
88
"k8s.io/kubernetes/pkg/api"
9-
utilyaml "k8s.io/kubernetes/pkg/util/yaml"
9+
apierrs "k8s.io/kubernetes/pkg/api/errors"
10+
client "k8s.io/kubernetes/pkg/client/unversioned"
11+
"k8s.io/kubernetes/pkg/util/wait"
1012
)
1113

12-
var (
13-
// yamlTempl path to deis yaml files (to allow override during tests)
14-
yamlTempl = "/etc/deis-%v%v.yaml"
14+
const (
15+
slugBuilderName = "deis-slugbuilder"
16+
slugBuilderImage = "quay.io/deisci/slugbuilder:v2-beta"
17+
dockerBuilderName = "deis-dockerbuilder"
18+
dockerBuilderImage = "quay.io/deisci/dockerbuilder:v2-beta"
19+
20+
tarURLKey = "TAR_URL"
21+
putURLKey = "put_url"
22+
debugKey = "DEBUG"
23+
minioUser = "minio-user"
24+
dockerSocketName = "docker-socket"
25+
dockerSocketPath = "/var/run/docker.sock"
1526
)
1627

1728
func dockerBuilderPodName(appName, shortSha string) string {
@@ -25,50 +36,92 @@ func slugBuilderPodName(appName, shortSha string) string {
2536
}
2637

2738
func dockerBuilderPod(debug, withAuth bool, name, namespace string, env map[string]interface{}, tarURL, imageName string) *api.Pod {
28-
pod := buildPod("dockerbuilder", withAuth, name, namespace, env)
29-
return &pod
30-
}
39+
pod := buildPod(debug, withAuth, name, namespace, env)
40+
41+
pod.Spec.Containers[0].Name = dockerBuilderName
42+
pod.Spec.Containers[0].Image = dockerBuilderImage
43+
44+
addEnvToPod(pod, "ACCESS_KEY_FILE", "/var/run/secrets/object/store/access_key")
45+
addEnvToPod(pod, "ACCESS_SECRET_FILE", "/var/run/secrets/object/store/access_secret")
46+
47+
if !withAuth {
48+
addEnvToPod(pod, tarURLKey, tarURL)
49+
addEnvToPod(pod, "IMG_NAME", imageName)
50+
}
51+
52+
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, api.VolumeMount{
53+
Name: dockerSocketName,
54+
MountPath: dockerSocketPath,
55+
})
56+
57+
pod.Spec.Volumes = append(pod.Spec.Volumes, api.Volume{
58+
Name: dockerSocketName,
59+
VolumeSource: api.VolumeSource{
60+
HostPath: &api.HostPathVolumeSource{
61+
Path: dockerSocketPath,
62+
},
63+
},
64+
})
3165

32-
func slugbuilderPod(debug, withAuth bool, name, namespace string, env map[string]interface{}, tarURL, putURL, buildpackURL string) *api.Pod {
33-
pod := buildPod("slugbuilder", withAuth, name, namespace, env)
3466
return &pod
3567
}
3668

37-
func buildPod(buildType string, withAuth bool, name, namespace string, env map[string]interface{}) api.Pod {
38-
useCreds := "-no-creds"
39-
if withAuth {
40-
useCreds = ""
41-
}
42-
fileName := fmt.Sprintf(yamlTempl, buildType, useCreds)
69+
func slugbuilderPod(debug, withAuth bool, name, namespace string, env map[string]interface{}, tarURL, putURL, buildpackURL string) *api.Pod {
70+
pod := buildPod(debug, withAuth, name, namespace, env)
4371

44-
pod := podFromFile(fileName)
72+
pod.Spec.Containers[0].Name = slugBuilderName
73+
pod.Spec.Containers[0].Image = slugBuilderImage
4574

46-
addEnvToPod(pod, env)
75+
addEnvToPod(pod, tarURLKey, tarURL)
76+
addEnvToPod(pod, putURLKey, putURL)
4777

48-
pod.ObjectMeta.Name = name
49-
pod.ObjectMeta.Namespace = namespace
78+
if buildpackURL != "" {
79+
addEnvToPod(pod, "BUILDPACK_URL", buildpackURL)
80+
}
5081

51-
return pod
82+
return &pod
5283
}
5384

54-
func podFromFile(fileName string) api.Pod {
55-
var pod api.Pod
56-
data, err := ioutil.ReadFile(fileName)
57-
if err != nil {
58-
fmt.Errorf("missing pod template %v (%v)", fileName, err)
85+
func buildPod(debug, withAuth bool, name, namespace string, env map[string]interface{}) api.Pod {
86+
pod := api.Pod{
87+
Spec: api.PodSpec{
88+
RestartPolicy: api.RestartPolicyNever,
89+
Containers: []api.Container{
90+
api.Container{
91+
ImagePullPolicy: api.PullAlways,
92+
},
93+
},
94+
Volumes: []api.Volume{},
95+
},
96+
ObjectMeta: api.ObjectMeta{
97+
Name: name,
98+
Namespace: namespace,
99+
Labels: map[string]string{
100+
"heritage": "deis",
101+
"version": "2.0.0-beta",
102+
},
103+
},
59104
}
60105

61-
json, err := utilyaml.ToJSON(data)
62-
if err != nil {
63-
fmt.Errorf("invalid pod template %v (%v)", fileName, err)
106+
if withAuth {
107+
pod.Spec.Volumes = append(pod.Spec.Volumes, api.Volume{
108+
Name: minioUser,
109+
VolumeSource: api.VolumeSource{
110+
Secret: &api.SecretVolumeSource{
111+
SecretName: minioUser,
112+
},
113+
},
114+
})
115+
116+
pod.Spec.Containers[0].VolumeMounts = []api.VolumeMount{
117+
api.VolumeMount{
118+
Name: minioUser,
119+
MountPath: "/var/run/secrets/object/store",
120+
ReadOnly: true,
121+
},
122+
}
64123
}
65124

66-
api.Scheme.DecodeInto(json, &pod)
67-
68-
return pod
69-
}
70-
71-
func addEnvToPod(pod api.Pod, env map[string]interface{}) {
72125
if len(pod.Spec.Containers) > 0 {
73126
for k, v := range env {
74127
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, api.EnvVar{
@@ -77,4 +130,48 @@ func addEnvToPod(pod api.Pod, env map[string]interface{}) {
77130
})
78131
}
79132
}
133+
134+
if debug {
135+
addEnvToPod(pod, debugKey, "1")
136+
}
137+
138+
return pod
139+
}
140+
141+
func addEnvToPod(pod api.Pod, key, value string) {
142+
if len(pod.Spec.Containers) > 0 {
143+
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, api.EnvVar{
144+
Name: key,
145+
Value: value,
146+
})
147+
}
148+
}
149+
150+
// waitForPod waits for a pod in state running or failed
151+
func waitForPod(c *client.Client, ns, podName string, interval, timeout time.Duration) error {
152+
return wait.PollImmediate(interval, timeout, func() (bool, error) {
153+
pod, err := c.Pods(ns).Get(podName)
154+
if err != nil {
155+
if apierrs.IsNotFound(err) {
156+
return true, err
157+
}
158+
}
159+
160+
condition := func(pod *api.Pod) (bool, error) {
161+
if pod.Status.Phase == api.PodRunning {
162+
return true, nil
163+
}
164+
if pod.Status.Phase == api.PodFailed {
165+
return true, fmt.Errorf("Giving up; pod went into failed status: \n%s", fmt.Sprintf("%#v", pod))
166+
}
167+
return false, nil
168+
}
169+
170+
done, err := condition(pod)
171+
if done {
172+
return true, nil
173+
}
174+
175+
return false, nil
176+
})
80177
}

0 commit comments

Comments
 (0)