/
controller.go
368 lines (317 loc) · 10.6 KB
/
controller.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package controller
import (
"fmt"
"strings"
"sync"
"time"
"github.com/spf13/viper"
customv1 "github.com/wish/kubetel/gok8s/apis/custom/v1"
clientset "github.com/wish/kubetel/gok8s/client/clientset/versioned"
informer "github.com/wish/kubetel/gok8s/client/informers/externalversions"
kcdutil "github.com/wish/kubetel/gok8s/kcdutil"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
k8sinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
batchv1Client "k8s.io/client-go/kubernetes/typed/batch/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
//Controller object
type Controller struct {
k8sClient kubernetes.Interface
customCS clientset.Interface
kcdcInformer cache.SharedIndexInformer
kcdcSynced cache.InformerSynced
jobcInformer cache.SharedIndexInformer
jobcSynced cache.InformerSynced
k8sIF k8sinformers.SharedInformerFactory
batchClient batchv1Client.BatchV1Interface
queue workqueue.RateLimitingInterface
kcdStates map[string]string
endpointMap map[string]string
jobDeleteStatus map[string]chan int
sync.Mutex
jobCleaner JobCleaner
}
//NewController Creates a new deyployment controller
func NewController(k8sClient kubernetes.Interface, customCS clientset.Interface, customIF informer.SharedInformerFactory, k8sIF k8sinformers.SharedInformerFactory) (*Controller, error) {
kcdInformer := customIF.Custom().V1().KCDs()
jobInformer := k8sIF.Batch().V1().Jobs()
batchClient := k8sClient.BatchV1()
c := &Controller{
kcdcInformer: kcdInformer.Informer(),
jobcInformer: jobInformer.Informer(),
customCS: customCS,
kcdcSynced: kcdInformer.Informer().HasSynced,
jobcSynced: jobInformer.Informer().HasSynced,
jobDeleteStatus: make(map[string](chan int)),
k8sIF: k8sIF,
batchClient: batchClient,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "kubedeployController"),
kcdStates: make(map[string]string),
endpointMap: viper.GetStringMapString("tracker.appendpoints"),
}
c.kcdcInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.trackAddKcd,
UpdateFunc: c.trackKcd,
})
c.jobcInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
DeleteFunc: c.deleteJob,
})
c.jobCleaner = *NewJobCleaner(c.jobcInformer, c.batchClient)
return c, nil
}
//Adds a new job request into the work queue
func (c *Controller) enqueue(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
runtime.HandleError(fmt.Errorf("error obtaining key for object being enqueue: %s", err.Error()))
log.Errorf("Failed to obtain key for object being enqueue: %v", err)
return
}
log.Tracef("Enququing object with key: %s", key)
c.queue.AddRateLimited(key)
}
// Run starts the controller
func (c *Controller) Run(threadCount int, stopCh <-chan struct{}) error {
defer runtime.HandleCrash()
defer c.queue.ShutDown()
log.Info("Starting Controller")
go c.kcdcInformer.Run(stopCh)
go c.jobcInformer.Run(stopCh)
if !cache.WaitForCacheSync(stopCh, c.kcdcSynced) {
return errors.New("Fail to wait for (secondary) cache sync")
}
if !cache.WaitForCacheSync(stopCh, c.jobcSynced) {
return errors.New("Fail to wait for (secondary) cache sync")
}
log.Info("Cache sync completed")
for i := 0; i < threadCount; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
}
log.Info("Started Controller")
<-stopCh
log.Info("Shutting down container version controller")
return nil
}
func (c *Controller) runWorker() {
log.Info("Running worker")
for c.processNextItem() {
}
}
//dequeues workqueue with retry if failed
func (c *Controller) processNextItem() bool {
key, shutdown := c.queue.Get()
if shutdown {
return false
}
c.queue.Done(key)
err := c.processItem(key.(string))
maxRetries := viper.GetInt("controller.maxretries")
if err == nil {
c.queue.Forget(key)
} else if c.queue.NumRequeues(key) < maxRetries {
log.Errorf("Error processing %s (will retry): %v", key, err)
c.queue.AddRateLimited(key)
} else {
log.Errorf("Error processing %s (giving up): %v", key, err)
c.queue.Forget(key)
runtime.HandleError(err)
}
return true
}
//Process the next KCD in the queue
func (c *Controller) processItem(key string) error {
obj, exists, err := c.kcdcInformer.GetIndexer().GetByKey(key)
if err != nil {
return err
}
if !exists {
return errors.New("Object does not exists")
}
kcd, ok := obj.(*customv1.KCD)
if !ok {
return err
}
log.Infof("Deququing KCD: %s ", kcd.Name)
namespace, _, err := cache.SplitMetaNamespaceKey(key)
name := kcd.Spec.Selector["kcdapp"]
version := kcd.Status.CurrVersion
jobName := fmt.Sprintf("deploy-tracker-%s-%s", name, version)
//Delete existing job if it already completed
jobsClient := c.batchClient.Jobs(namespace)
var gracePeriodSeconds int64
var propagationPolicy metav1.DeletionPropagation = metav1.DeletePropagationForeground
exJob, err := jobsClient.Get(jobName, metav1.GetOptions{})
if err == nil {
for _, condition := range exJob.Status.Conditions {
if (condition.Type == "Complete" || condition.Type == "Failed") &&
condition.Status == "True" {
//Here we pass map the jobname we want to delelete to a channel
//Once the jobcInformer get the deletion event it will write to the channel
jobDeleted := make(chan int)
c.Lock()
c.jobDeleteStatus[jobName] = jobDeleted
c.Unlock()
err = jobsClient.Delete(jobName, &metav1.DeleteOptions{GracePeriodSeconds: &gracePeriodSeconds, PropagationPolicy: &propagationPolicy})
if err != nil {
//If this fails, the job probably was deleted someone else before we got the chance
log.Warnf("failed to delete job %s with error: %s", jobName, err)
} else {
//Otherwise wiat for the deletion conformation
<-jobDeleted
}
//Cleanup
c.Lock()
delete(c.jobDeleteStatus, jobName)
c.Unlock()
close(jobDeleted)
}
}
}
//Check if there is a specific endpoint override for the app else use default
var kubeDeployEndpoint string
if kubeDeployEndpoint, ok = c.endpointMap[name]; !ok {
kubeDeployEndpoint = viper.GetString("tracker.kubedeploy_sqs_endpoint")
} else {
log.Infof("Blaclisted project, skipping %s......", name)
return nil
}
// This is configured based on env, no need to hack
robbieEndpoint := viper.GetString("tracker.robbie_sqs_endpoint")
args := []string{"tracker",
fmt.Sprintf("--cluster=%s", viper.GetString("cluster")),
fmt.Sprintf("--sqsregion=%s", viper.GetString("sqsregion")),
fmt.Sprintf("--log=%s", viper.GetString("log.level")),
fmt.Sprintf("--use-config=false"),
fmt.Sprintf("--tracker-kcdapp=%s", kcd.Spec.Selector["kcdapp"]),
fmt.Sprintf("--tracker-version=%s", version),
fmt.Sprintf("--tracker-namespace=%s", namespace),
fmt.Sprintf("--tracker-endpointtype=%s", viper.GetString("tracker.endpointtype")),
fmt.Sprintf("--tracker-kubedeploy-endpoint=%s", kubeDeployEndpoint),
fmt.Sprintf("--tracker-robbie-endpoint=%s", robbieEndpoint),
fmt.Sprintf("--tracker-maxretries=%d", viper.GetInt("tracker.maxretries")),
fmt.Sprintf("--tracker-workercount=%d", viper.GetInt("tracker.workercount")),
fmt.Sprintf("--server-port=%d", viper.GetInt("server.port")),
}
//Number of time job will retry upon failure
var backoffLimit int32 = 10
log.Infof("Creating job for : %s", jobName)
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
Namespace: namespace,
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
},
Spec: corev1.PodSpec{
NodeSelector: viper.GetStringMapString("nodeSelector"),
Containers: []corev1.Container{
{
Name: fmt.Sprintf("%s-container", jobName),
Image: viper.GetString("image"),
Args: args,
ImagePullPolicy: corev1.PullAlways,
Ports: []corev1.ContainerPort{
{
Name: "https",
ContainerPort: 443,
Protocol: "TCP",
},
{
Name: "http",
ContainerPort: 80,
Protocol: "TCP",
},
},
},
},
RestartPolicy: corev1.RestartPolicyOnFailure,
ServiceAccountName: "kubetel-tracker",
},
},
BackoffLimit: &backoffLimit,
},
}
_, err = jobsClient.Create(job)
if err != nil {
//Occasionally this is caused by a race condition and is fine
if strings.Contains(err.Error(), "already exists") {
log.Infof("Job : %s : already exists, skipping", jobName)
return nil
}
return errors.Wrapf(err, "Failed to create job %s", job.Name)
}
return nil
}
//Handler for kcdcInformer Update
func (c *Controller) trackKcd(oldObj interface{}, newObj interface{}) {
//Check that we actully have KCD objects
newKCD, ok := newObj.(*customv1.KCD)
if !ok {
log.Errorf("Not a KCD object")
return
}
oldKCD, ok := oldObj.(*customv1.KCD)
if !ok {
log.Errorf("Not a KCD object")
return
}
log.Infof("oldKCD status: %v vs newKCD status: %v", oldKCD.Status.CurrStatus, newKCD.Status.CurrStatus)
log.Infof("in-memory kcdStates: %v", c.kcdStates)
if oldKCD.Status.CurrStatus == newKCD.Status.CurrStatus {
log.Info("oldKCD and newKCD status unchanged")
return
}
if newKCD.Status.CurrStatus == c.kcdStates[newKCD.Name] {
log.Info("newKCD and in-memory kcdStates status unchanged")
return
}
if newKCD.Status.CurrStatus == kcdutil.StatusProgressing && c.kcdStates[newKCD.Name] != kcdutil.StatusProgressing {
c.enqueue(newObj)
}
//We need to keep track of the last known state in kubetel because most of the time
//the api server will bundle multiple events into one update and the oldObj and newObj
//will be identical
c.kcdStates[newKCD.Name] = newKCD.Status.CurrStatus
}
//Handler for kcdcInformer Add
//This occours if the tracker crashes and restarts to find a completed deployment
func (c *Controller) trackAddKcd(newObj interface{}) {
newKCD, ok := newObj.(*customv1.KCD)
if !ok {
log.Errorf("Not a KCD object")
return
}
c.kcdStates[newKCD.Name] = newKCD.Status.CurrStatus
if newKCD.Status.CurrStatus == kcdutil.StatusProgressing {
c.enqueue(newObj)
}
}
//Handler for JobcInformer Delete
func (c *Controller) deleteJob(obj interface{}) {
job, ok := obj.(*batchv1.Job)
if !ok {
log.Errorf("Not a job object")
return
}
var ch chan int
//If there is a chan mapped to this jobname it then a worker thread
//Is waiting for a delete conformation and by writing to the the mapped
//chan
c.Lock()
ch, ok = c.jobDeleteStatus[job.Name]
if ok {
ch <- 1
}
c.Unlock()
}