-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobcontroller.go
123 lines (100 loc) · 3.7 KB
/
jobcontroller.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
package job
/*
Copyright 2017 - 2020 Crunchy Data Solutions, Inc.
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.
*/
import (
"github.com/crunchydata/postgres-operator/internal/config"
log "github.com/sirupsen/logrus"
apiv1 "k8s.io/api/batch/v1"
batchinformers "k8s.io/client-go/informers/batch/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)
// Controller holds the connections for the controller
type Controller struct {
JobConfig *rest.Config
JobClient *rest.RESTClient
JobClientset kubernetes.Interface
Informer batchinformers.JobInformer
}
const (
patchResource = "pgtasks"
patchURL = "/spec/status"
)
// onAdd is called when a postgresql operator job is created and an associated add event is
// generated
func (c *Controller) onAdd(obj interface{}) {
job := obj.(*apiv1.Job)
labels := job.GetObjectMeta().GetLabels()
//only process jobs with with vendor=crunchydata label
if labels[config.LABEL_VENDOR] != "crunchydata" {
return
}
log.Debugf("Job Controller: onAdd ns=%s jobName=%s", job.ObjectMeta.Namespace, job.ObjectMeta.SelfLink)
}
// onUpdate is called when a postgresql operator job is created and an associated update event is
// generated
func (c *Controller) onUpdate(oldObj, newObj interface{}) {
var err error
job := newObj.(*apiv1.Job)
labels := job.GetObjectMeta().GetLabels()
//only process jobs with with vendor=crunchydata label
if labels[config.LABEL_VENDOR] != "crunchydata" {
return
}
log.Debugf("[Job Controller] onUpdate ns=%s %s active=%d succeeded=%d conditions=[%v]",
job.ObjectMeta.Namespace, job.ObjectMeta.SelfLink, job.Status.Active, job.Status.Succeeded,
job.Status.Conditions)
labelExists := func(k string) bool { _, ok := labels[k]; return ok }
// determine determine which handler to route the update event to
switch {
case labels[config.LABEL_RMDATA] == "true":
err = c.handleRMDataUpdate(job)
case labels[config.LABEL_BACKREST] == "true" ||
labels[config.LABEL_BACKREST_RESTORE] == "true":
err = c.handleBackrestUpdate(job)
case labels[config.LABEL_BACKUP_TYPE_PGDUMP] == "true":
err = c.handlePGDumpUpdate(job)
case labels[config.LABEL_RESTORE_TYPE_PGRESTORE] == "true":
err = c.handlePGRestoreUpdate(job)
case labels[config.LABEL_PGO_LOAD] == "true":
err = c.handleLoadUpdate(job)
case labels[config.LABEL_PGO_CLONE_STEP_1] == "true":
err = c.handleRepoSyncUpdate(job)
case labelExists(config.LABEL_PGHA_BOOTSTRAP):
err = c.handleBootstrapUpdate(job)
}
if err != nil {
log.Error(err)
}
return
}
// onDelete is called when a postgresql operator job is deleted
func (c *Controller) onDelete(obj interface{}) {
job := obj.(*apiv1.Job)
labels := job.GetObjectMeta().GetLabels()
//only process jobs with with vendor=crunchydata label
if labels[config.LABEL_VENDOR] != "crunchydata" {
return
}
log.Debugf("[Job Controller] onDelete ns=%s %s", job.ObjectMeta.Namespace, job.ObjectMeta.SelfLink)
}
// AddJobEventHandler adds the job event handler to the job informer
func (c *Controller) AddJobEventHandler() {
c.Informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.onAdd,
UpdateFunc: c.onUpdate,
DeleteFunc: c.onDelete,
})
log.Debugf("Job Controller: added event handler to informer")
}