This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcontroller.go
261 lines (214 loc) · 5.47 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
package main
import (
"log"
"regexp"
"time"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/workqueue"
)
const (
nthreads = 1
)
type DatabaseController struct {
Clientset *kubernetes.Clientset
DBConfig *DBConfig
config *rest.Config
client *rest.RESTClient
indexer cache.Indexer
queue workqueue.RateLimitingInterface
informer cache.Controller
}
type QueueItem struct {
event string
item *Database
}
func (cllr *DatabaseController) setError(db *Database, err string) {
db.Status.Phase = "error"
db.Status.Error = err
cllr.client.Put().Namespace(db.Namespace).Resource("databases").Name(db.Name).Body(db).Do()
}
func (cllr *DatabaseController) handleAdd(obj interface{}) {
db := obj.(*Database)
/* Skip if we've already provisioned this database */
if db.Status.Phase == "error" || db.Status.Phase == "done" {
return
}
if db.Spec.Secret == "" {
cllr.setError(db, "secretName not found in spec")
return
}
if db.Spec.Class == "" {
db.Spec.Class = "default"
}
matched, err := regexp.MatchString("[a-z][a-z0-9-]+", db.Namespace)
if !matched || err != nil {
cllr.setError(db, "invalid namespace name")
return
}
matched, err = regexp.MatchString("[a-z][a-z0-9-]+", db.Name)
if !matched || err != nil {
cllr.setError(db, "invalid name: must only contain a-z, 0-9 and '-'")
return
}
log.Printf("%s/%s: provisioning new database\n",
db.Namespace,
db.Name)
switch db.Spec.Type {
case "mysql":
cllr.handleAddMysql(db)
break
case "postgresql":
cllr.handleAddPostgresql(db)
break
default:
log.Printf("%s/%s: provisioning failed: unrecognised database type \"%s\"\n",
db.Namespace,
db.Name,
db.Spec.Type)
cllr.setError(db, "unrecognised database type")
return
}
}
func (cllr *DatabaseController) handleDelete(obj interface{}) {
db := obj.(*Database)
/* Skip if this database has not been provisioned */
if db.Status.Phase != "done" {
return
}
matched, err := regexp.MatchString("[a-z][a-z0-9-]+", db.Namespace)
if !matched || err != nil {
log.Printf("%s/%s: delete ignored: invalid namespace",
db.Namespace,
db.Name)
return
}
matched, err = regexp.MatchString("[a-z][a-z0-9-]+", db.Name)
if !matched || err != nil {
log.Printf("%s/%s: delete ignored: invalid name",
db.Namespace,
db.Name)
return
}
log.Printf("%s/%s: dropping database\n",
db.Namespace,
db.Name)
switch db.Spec.Type {
case "mysql":
cllr.handleDeleteMysql(db)
break
case "postgresql":
cllr.handleDeletePostgresql(db)
break
default:
log.Printf("%s/%s: delete ignored: unrecognised database type \"%s\"\n",
db.Namespace,
db.Name,
db.Spec.Type)
return
}
}
func createController(kubeconfig string, dbconfig *DBConfig) *DatabaseController {
var err error
cllr := &DatabaseController{
DBConfig: dbconfig,
}
if kubeconfig != "" {
cllr.config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
cllr.config, err = rest.InClusterConfig()
}
if err != nil {
panic(err)
}
cllr.Clientset, err = kubernetes.NewForConfig(cllr.config)
if err != nil {
panic(err)
}
groupversion := schema.GroupVersion{
Group: "kubejam",
Version: "v1",
}
scheme := runtime.NewScheme()
scheme.AddKnownTypes(groupversion,
&Database{},
&DatabaseList{},
)
metav1.AddToGroupVersion(scheme, groupversion)
cllr.config.GroupVersion = &groupversion
cllr.config.APIPath = "/apis"
cllr.config.ContentType = runtime.ContentTypeJSON
cllr.config.NegotiatedSerializer = serializer.DirectCodecFactory{
CodecFactory: serializer.NewCodecFactory(scheme),
}
cllr.client, err = rest.RESTClientFor(cllr.config)
if err != nil {
panic(err)
}
return cllr
}
func (cllr *DatabaseController) processNextItem() bool {
qitem, quit := cllr.queue.Get()
if quit {
return false
}
item := qitem.(*QueueItem)
defer cllr.queue.Done(qitem)
switch item.event {
case "add":
cllr.handleAdd(item.item)
case "delete":
cllr.handleDelete(item.item)
}
return true
}
func (cllr *DatabaseController) runWorker() {
for cllr.processNextItem() {
}
}
func (cllr *DatabaseController) run() {
watchlist := cache.NewListWatchFromClient(cllr.client,
"databases", apiv1.NamespaceAll, fields.Everything())
cllr.indexer, cllr.informer = cache.NewIndexerInformer(
watchlist,
&Database{},
time.Second*0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
cllr.queue.Add(&QueueItem{"add", obj.(*Database)})
},
DeleteFunc: func(obj interface{}) {
cllr.queue.Add(&QueueItem{"delete", obj.(*Database)})
},
UpdateFunc: func(oldobj, obj interface{}) {
/* no update handling right now */
//cllr.queue.Add(&QueueItem{"update", obj.(Database)})
},
},
cache.Indexers{},
)
cllr.queue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
log.Println("running")
defer cllr.queue.ShutDown()
stop := make(chan struct{})
defer close(stop)
go cllr.informer.Run(stop)
if !cache.WaitForCacheSync(stop, cllr.informer.HasSynced) {
log.Println("Timed out waiting for caches to sync")
return
}
for i := 0; i < nthreads; i++ {
go wait.Until(cllr.runWorker, time.Second, stop)
}
<-stop
log.Println("shutting down")
}