@@ -2,16 +2,27 @@ package gitreceive
22
33import (
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
1728func dockerBuilderPodName (appName , shortSha string ) string {
@@ -25,50 +36,92 @@ func slugBuilderPodName(appName, shortSha string) string {
2536}
2637
2738func 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