/
controller.go
131 lines (118 loc) · 3.88 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
/*
Copyright AppsCode Inc. and Contributors
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 statefulset
import (
"fmt"
"kubedb.dev/apimachinery/apis/kubedb"
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
db_cs "kubedb.dev/apimachinery/client/clientset/versioned"
amc "kubedb.dev/apimachinery/pkg/controller"
"gomodules.xyz/x/log"
apps "k8s.io/api/apps/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
core_util "kmodules.xyz/client-go/core/v1"
"kmodules.xyz/client-go/tools/queue"
)
type Controller struct {
*amc.Controller
*amc.Config
}
func NewController(
config *amc.Config,
client kubernetes.Interface,
dbClient db_cs.Interface,
dmClient dynamic.Interface,
) *Controller {
return &Controller{
Controller: &amc.Controller{
Client: client,
DBClient: dbClient,
DynamicClient: dmClient,
},
Config: config,
}
}
func (c *Controller) InitStsWatcher() {
log.Infoln("Initializing StatefulSet watcher.....")
// Initialize RestoreSession Watcher
c.StsInformer = c.KubeInformerFactory.Apps().V1().StatefulSets().Informer()
c.StsQueue = queue.New(api.ResourceKindStatefulSet, c.MaxNumRequeues, c.NumThreads, c.processStatefulSet)
c.StsLister = c.KubeInformerFactory.Apps().V1().StatefulSets().Lister()
c.StsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if sts, ok := obj.(*apps.StatefulSet); ok {
c.enqueueOnlyKubeDBSts(sts)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
if sts, ok := newObj.(*apps.StatefulSet); ok {
c.enqueueOnlyKubeDBSts(sts)
}
},
DeleteFunc: func(obj interface{}) {
if sts, ok := obj.(*apps.StatefulSet); ok {
ok, _, err := core_util.IsOwnerOfGroup(metav1.GetControllerOf(sts), kubedb.GroupName)
if err != nil || !ok {
log.Warningln(err)
return
}
dbInfo, err := c.extractDatabaseInfo(sts)
if err != nil {
if !kerr.IsNotFound(err) {
log.Warningf("failed to extract database info from StatefulSet: %s/%s. Reason: %v", sts.Namespace, sts.Name, err)
}
return
}
err = c.ensureReadyReplicasCond(dbInfo)
if err != nil {
log.Warningf("failed to update ReadyReplicas condition. Reason: %v", err)
return
}
}
},
})
}
func (c *Controller) enqueueOnlyKubeDBSts(sts *apps.StatefulSet) {
// only enqueue if the controlling owner is a KubeDB resource
ok, _, err := core_util.IsOwnerOfGroup(metav1.GetControllerOf(sts), kubedb.GroupName)
if err != nil {
log.Warningf("failed to enqueue StatefulSet: %s/%s. Reason: %v", sts.Namespace, sts.Name, err)
return
}
if ok {
queue.Enqueue(c.StsQueue.GetQueue(), cache.ExplicitKey(sts.Namespace+"/"+sts.Name))
}
}
func (c *Controller) processStatefulSet(key string) error {
log.Infof("Started processing, key: %v", key)
obj, exists, err := c.StsInformer.GetIndexer().GetByKey(key)
if err != nil {
log.Errorf("Fetching object with key %s from store failed with %v", key, err)
return err
}
if !exists {
log.Debugf("StatefulSet %s does not exist anymore", key)
} else {
sts := obj.(*apps.StatefulSet).DeepCopy()
dbInfo, err := c.extractDatabaseInfo(sts)
if err != nil {
return fmt.Errorf("failed to extract database info from StatefulSet: %s/%s. Reason: %v", sts.Namespace, sts.Name, err)
}
return c.ensureReadyReplicasCond(dbInfo)
}
return nil
}