This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
/
deployer.go
354 lines (326 loc) · 10.8 KB
/
deployer.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
package controller
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"path/filepath"
"time"
"github.com/golang/glog"
platform "kolihub.io/koli/pkg/apis/core/v1alpha1"
"kolihub.io/koli/pkg/apis/core/v1alpha1/draft"
clientset "kolihub.io/koli/pkg/clientset"
"kolihub.io/koli/pkg/request"
"kolihub.io/koli/pkg/spec"
"kolihub.io/koli/pkg/util"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)
const (
// ignore deploying releases that have more than X minutes of life
autoDeployExpireInMinutes = 20
)
// DeployerController controller
type DeployerController struct {
kclient kubernetes.Interface
clientset clientset.CoreInterface
podInf cache.SharedIndexInformer
dpInf cache.SharedIndexInformer
relInf cache.SharedIndexInformer
config *Config
queue *TaskQueue
recorder record.EventRecorder
}
// NewDeployerController creates a new DeployerController
func NewDeployerController(
config *Config, podInf,
dpInf, relInf cache.SharedIndexInformer,
sysClient clientset.CoreInterface,
kclient kubernetes.Interface) *DeployerController {
d := &DeployerController{
kclient: kclient,
clientset: sysClient,
podInf: podInf,
dpInf: dpInf,
relInf: relInf,
config: config,
recorder: newRecorder(kclient, "apps-controller"),
}
d.queue = NewTaskQueue(d.syncHandler)
d.podInf.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: d.addPod,
UpdateFunc: d.updatePod,
// DeleteFunc: d.deletePod,
})
return d
}
func (d *DeployerController) addPod(obj interface{}) {
pod := obj.(*v1.Pod)
glog.V(2).Infof("add-pod - %s/%s", pod.Namespace, pod.Name)
d.queue.Add(pod)
}
func (d *DeployerController) updatePod(o, n interface{}) {
old := o.(*v1.Pod)
new := n.(*v1.Pod)
if old.ResourceVersion == new.ResourceVersion || old.Status.Phase == new.Status.Phase {
return
}
glog.V(2).Infof("update-pod - %s/%s", new.Namespace, new.Name)
d.queue.Add(new)
}
func (d *DeployerController) deletePod(obj interface{}) {
// pod := obj.(*v1.Pod)
// glog.Infof("delete-pod - %s/%s noop", pod.Namespace, pod.Name)
// d.queue.add(pod)
}
// Run the controller.
func (d *DeployerController) Run(workers int, stopc <-chan struct{}) {
// don't let panics crash the process
defer utilruntime.HandleCrash()
// make sure the work queue is shutdown which will trigger workers to end
defer d.queue.shutdown()
glog.Info("Starting Deployer Controller...")
if !cache.WaitForCacheSync(stopc, d.podInf.HasSynced, d.dpInf.HasSynced, d.relInf.HasSynced) {
return
}
for i := 0; i < workers; i++ {
// runWorker will loop until "something bad" happens.
// The .Until will then rekick the worker after one second
go d.queue.run(time.Second, stopc)
// go wait.Until(d.runWorker, time.Second, stopc)
}
// wait until we're told to stop
<-stopc
glog.Info("shutting down Deployer controller...")
}
func (d *DeployerController) syncHandler(key string) error {
obj, exists, err := d.podInf.GetStore().GetByKey(key)
if err != nil {
glog.Warningf("%s - failed retrieving object from store [%s]", key, err)
return err
}
if !exists {
glog.V(2).Infof("%s - the pod doesn't exists")
return nil
}
pod := obj.(*v1.Pod)
nsMeta := draft.NewNamespaceMetadata(pod.Namespace)
if !nsMeta.IsValid() {
glog.V(2).Infof("%s - noop, it's not a valid namespace", key)
return nil
}
releaseName := pod.Annotations[spec.KoliPrefix("releasename")]
if releaseName == "" {
msg := "Couldn't find the release for the build, an annotation is missing on pod. " +
"The build must be manually deployed."
d.recorder.Event(pod, v1.EventTypeWarning, "ReleaseNotFound", msg)
glog.Warningf("%s - missing 'releasename' annotation on pod", key)
return nil
}
releaseQ := &platform.Release{}
releaseQ.SetName(releaseName)
releaseQ.SetNamespace(pod.Namespace)
releaseO, exists, err := d.relInf.GetStore().Get(releaseQ)
if err != nil {
d.recorder.Eventf(pod, v1.EventTypeWarning, "DeployError",
"Found an error retrieving the release from cache [%s].", err)
glog.Warningf("%s - found an error retrieving the release from cache [%s]", key, err)
return err
}
if !exists {
d.recorder.Event(pod, v1.EventTypeWarning, "ReleaseNotFound",
"The release wasn't found for this build, the build must be manually deployed.")
glog.Warningf("%s - release '%s' doesn't exist anymore", key, releaseName)
return nil
}
release := releaseO.(*platform.Release)
// turn-off build, otherwise it will trigger unwanted builds
defer func() {
patchData := []byte(`{"spec": {"build": false}}`)
_, err = d.clientset.Release(pod.Namespace).Patch(release.Name, types.MergePatchType, patchData)
if err != nil {
glog.Warningf("%s - failed turning off build", key)
}
}()
var gitcli *request.Request
info := &platform.GitInfo{}
// var err error
if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
gitcli, err = newGitAPIClient(
d.config.GitReleaseHost,
release.Spec.DeployName,
d.config.PlatformJWTSecret,
nsMeta,
)
if err != nil {
glog.Errorf("%s - failed retrieving git api client [%v]", key, err)
return nil
}
respBody, err := gitcli.Resource("seek").Get().
AddQuery("q", pod.Name).
AddQuery("in", "kubeRef").
Do().Raw()
if err != nil {
return fmt.Errorf("failed searching for release, %v", err)
}
infoList := &platform.GitInfoList{}
if err := json.Unmarshal(respBody, infoList); err != nil {
glog.Warningf("%s - failed deserializing releases: %v, [%v]", key, err, string(respBody))
return nil
}
if len(infoList.Items) == 0 {
glog.Infof(`%s - git api release not found`, key)
return nil
}
// Ignore if multiple are found for now
info = &infoList.Items[0]
}
if pod.Status.Phase == v1.PodSucceeded {
deploymentQ := &extensions.Deployment{}
deploymentQ.SetName(release.Spec.DeployName)
deploymentQ.SetNamespace(release.Namespace)
deploymentO, exists, err := d.dpInf.GetStore().Get(deploymentQ)
if err != nil {
return fmt.Errorf("failed retrieving deploy from cache [%s]", err)
}
if !exists {
d.recorder.Eventf(release, v1.EventTypeWarning,
"DeployNotFound", "Deploy '%s' not found", release.Spec.DeployName)
// There's any resource to deploy, do not requeue
return nil
}
deploy := deploymentO.(*extensions.Deployment)
glog.V(3).Infof("%s - Found a release candidate [%s]", key, info.HeadCommit.ID)
if isAutoDeploy(deploy, info.HeadCommit.ID) {
releaseURL := release.GitReleaseURL(d.config.GitReleaseHost)
if err := d.deploySlug(releaseURL, info.HeadCommit.ID, info.Lang, deploy); err != nil {
d.recorder.Eventf(release, v1.EventTypeWarning, "DeployError",
"Failed deploying release [%s]", err)
return fmt.Errorf("failed deploying release [%s]", err)
}
ref := info.HeadCommit.ID
if len(ref) > 7 {
ref = ref[:7]
}
d.recorder.Eventf(release, v1.EventTypeNormal,
"Deployed", "Deploy '%s' updated with the new revision [%s]", deploy.Name, ref)
}
}
// Update the release in Git API
if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
info.Name = release.Spec.DeployName
info.Namespace = pod.Namespace
info.Status = pod.Status.Phase
if len(pod.Status.ContainerStatuses) > 0 {
terminatedState := pod.Status.ContainerStatuses[0].State.Terminated
if terminatedState != nil {
delta := terminatedState.FinishedAt.Sub(terminatedState.StartedAt.Time)
info.BuildDuration = delta.Round(time.Second)
}
}
if len(info.HeadCommit.ID) == 0 {
glog.Warningf("%s - Commit ID is empty, release metadata will not be updated", key)
return nil
}
_, err = gitcli.Reset().Put().
Resource("objects").
Name(info.HeadCommit.ID).
Body(info).
Do().Raw()
if err != nil {
return fmt.Errorf("failed updating git api release [%v]", err)
}
glog.Infof(`%s - Removing pod and release "%s"`, key, release.Name)
d.kclient.Core().Pods(pod.Namespace).Delete(pod.Name, &metav1.DeleteOptions{})
d.clientset.Release(pod.Namespace).Delete(release.Name, &metav1.DeleteOptions{})
}
return nil
}
func (d *DeployerController) deploySlug(releaseURL, commitID, appLang string, deploy *extensions.Deployment) error {
dpCopy := deploy.DeepCopy()
if dpCopy == nil {
return fmt.Errorf("failed deep copying: %v", deploy)
}
dpCopy.Spec.Paused = false
if dpCopy.Annotations == nil {
dpCopy.Annotations = make(map[string]string)
}
dpCopy.Annotations["kolihub.io/deployed-git-revision"] = commitID
dpCopy.Labels["kolihub.io/lang"] = appLang
dpCopy.Spec.Template.Labels["kolihub.io/lang"] = appLang
dpCopy.Spec.Selector.MatchLabels["kolihub.io/lang"] = appLang
c := dpCopy.Spec.Template.Spec.Containers
// TODO: hard-coded
c[0].Ports = []v1.ContainerPort{
{
Name: "http",
ContainerPort: 5000,
Protocol: v1.ProtocolTCP,
},
}
c[0].Args = []string{"start", "web"} // TODO: hard-coded, it must come from Procfile
c[0].Image = d.config.SlugRunnerImage
c[0].Name = dpCopy.Name
slugURL := fmt.Sprintf("%s/%s/slug.tgz", releaseURL, commitID)
c[0].Env = []v1.EnvVar{
{
Name: "SLUG_URL",
Value: slugURL,
},
// A dynamic JWT Token secret provisioned by a controller
{
Name: "AUTH_TOKEN",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: platform.SystemSecretName,
},
Key: "token.jwt", // TODO: hard-coded
},
},
},
{
Name: "DEBUG",
Value: "TRUE",
},
}
_, err := d.kclient.Extensions().Deployments(dpCopy.Namespace).Update(dpCopy)
if err != nil {
return fmt.Errorf("failed update deployment: %s", err)
}
return nil
}
func isAutoDeploy(d *extensions.Deployment, commitID string) bool {
return d.Annotations[platform.AnnotationAutoDeploy] == "true" &&
d.Annotations["kolihub.io/deployed-git-revision"] != commitID
}
func newGitAPIClient(host, deployName, jwtSecret string, nsMeta *draft.NamespaceMeta) (*request.Request, error) {
// Generate a system token based on the customer and organization of the namespace.
// The access token has limited access to the resources of the platform
namespace := nsMeta.KubernetesNamespace()
systemToken, err := util.GenerateNewJwtToken(
jwtSecret,
nsMeta.Customer(),
nsMeta.Organization(),
platform.SystemTokenType,
time.Now().UTC().Add(time.Minute*5), // hard-coded exp time
)
if err != nil {
return nil, err
}
requestURL, err := url.Parse(host)
if err != nil {
return nil, err
}
basePath := filepath.Join("releases", "v1beta1", namespace, deployName)
requestURL.Path = basePath
basicAuth := base64.StdEncoding.EncodeToString([]byte("dumb:" + systemToken))
return request.NewRequest(nil, requestURL).
SetHeader("Authorization", fmt.Sprintf("Basic %s", basicAuth)), nil
}