-
Notifications
You must be signed in to change notification settings - Fork 85
/
jobs.go
93 lines (78 loc) · 2.94 KB
/
jobs.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
/*
Copyright The Stash Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"context"
"fmt"
"time"
"stash.appscode.dev/apimachinery/apis"
stash_rbac "stash.appscode.dev/stash/pkg/rbac"
"github.com/golang/glog"
batch "k8s.io/api/batch/v1"
core "k8s.io/api/core/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
batch_informers "k8s.io/client-go/informers/batch/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"kmodules.xyz/client-go/tools/queue"
)
func (c *StashController) initJobWatcher() {
c.jobInformer = c.kubeInformerFactory.InformerFor(&batch.Job{}, func(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
return batch_informers.NewFilteredJobInformer(
client,
core.NamespaceAll,
resyncPeriod,
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
func(options *metav1.ListOptions) {
options.LabelSelector = labels.SelectorFromSet(map[string]string{
apis.KeyDeleteJobOnCompletion: apis.AllowDeletingJobOnCompletion,
}).String()
},
)
})
c.jobQueue = queue.New("Job", c.MaxNumRequeues, c.NumThreads, c.runJobInjector)
c.jobInformer.AddEventHandler(queue.DefaultEventHandler(c.jobQueue.GetQueue()))
c.jobLister = c.kubeInformerFactory.Batch().V1().Jobs().Lister()
}
func (c *StashController) runJobInjector(key string) error {
obj, exists, err := c.jobInformer.GetIndexer().GetByKey(key)
if err != nil {
glog.Errorf("Fetching object with key %s from store failed with %v", key, err)
return err
}
if !exists {
glog.Warningf("Job %s does not exist anymore\n", key)
return nil
} else {
job := obj.(*batch.Job)
glog.Infof("Sync/Add/Update for Job %s", job.GetName())
if job.Status.Succeeded > 0 {
glog.Infof("Deleting succeeded job %s", job.GetName())
deletePolicy := metav1.DeletePropagationBackground
err := c.kubeClient.BatchV1().Jobs(job.Namespace).Delete(context.TODO(), job.Name, metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
if err != nil && !kerr.IsNotFound(err) {
return fmt.Errorf("failed to delete job: %s, reason: %s", job.Name, err)
}
glog.Infof("Deleted stash job: %s", job.GetName())
err = stash_rbac.EnsureRepoReaderRolebindingDeleted(c.kubeClient, c.stashClient, &job.ObjectMeta)
if err != nil {
return fmt.Errorf("failed to delete repo-reader rolebinding. reason: %s", err)
}
}
}
return nil
}