-
Notifications
You must be signed in to change notification settings - Fork 90
/
hooks.go
90 lines (76 loc) · 2.4 KB
/
hooks.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
package client
import (
"context"
"strings"
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/logger"
batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
)
// runHooksInformer will create goroutines to start various informers for kots objects
func (c *Client) runHooksInformer(namespace string) error {
logger.Infof("running hooks informer for namespace %s", namespace)
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get clientset")
}
restClient := clientset.BatchV1().RESTClient()
// Watch jobs
go func() {
jobWatchList := cache.NewListWatchFromClient(restClient, "jobs", namespace, fields.Everything())
resyncPeriod := 30 * time.Second
_, controller := cache.NewInformer(jobWatchList, &batchv1.Job{}, resyncPeriod,
cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
job, ok := newObj.(*batchv1.Job)
if !ok {
logger.Errorf("expected batchv1.Job, but got %T", newObj)
return
}
// if the job doesn't contain our annotation, ignore it
hookValue, ok := job.Annotations["kots.io/hook-delete-policy"]
if !ok {
return
}
cleanUpJob := false
reason := ""
if job.Status.Active == 0 && job.Status.Succeeded > 0 && strings.Contains(hookValue, "hook-succeeded") {
cleanUpJob = true
reason = "successful"
}
if job.Status.Active == 0 && job.Status.Failed > 0 && strings.Contains(hookValue, "hook-failed") {
cleanUpJob = true
reason = "failed"
}
if !cleanUpJob {
return
}
grace := int64(0)
policy := metav1.DeletePropagationBackground
opts := metav1.DeleteOptions{
GracePeriodSeconds: &grace,
PropagationPolicy: &policy,
}
if err := clientset.BatchV1().Jobs(job.Namespace).Delete(context.TODO(), job.Name, opts); err != nil {
logger.Error(errors.Wrap(err, "failed to delete job"))
return
}
logger.Debugf("deleted %s job %s\n", reason, job.Name)
},
},
)
stopChan := make(chan struct{})
c.HookStopChans = append(c.HookStopChans, stopChan)
controller.Run(stopChan)
}()
return nil
}
func (c *Client) shutdownHooksInformer() {
for _, stopChan := range c.HookStopChans {
stopChan <- struct{}{}
}
}