-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
277 lines (246 loc) · 7.11 KB
/
main.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"fmt"
"log"
"time"
"github.com/operator-backing-service-samples/aws-rds/pkg/client"
"github.com/operator-backing-service-samples/aws-rds/pkg/crd"
"github.com/operator-backing-service-samples/aws-rds/pkg/kube"
"github.com/operator-backing-service-samples/aws-rds/pkg/provider"
"github.com/operator-backing-service-samples/aws-rds/pkg/rds"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
apiextcs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
const Failed = "Failed"
// return rest config, if path not specified assume in cluster config
func getClientConfig(kubeconfig string) (*rest.Config, error) {
cfg, err := rest.InClusterConfig()
if err != nil {
if kubeconfig != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
}
return cfg, err
}
func getKubectl() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
log.Println("Appears we are not running in a cluster")
config, err = clientcmd.BuildConfigFromFlags("", kube.Config())
if err != nil {
return nil, err
}
} else {
log.Println("Seems like we are running in a Kubernetes cluster!!")
}
kubectl, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return kubectl, nil
}
func main() {
var provider string
var rootCmd = &cobra.Command{
Use: "aws-rds",
Short: "Kubernetes database provisioner",
Long: `Kubernetes database provisioner`,
Run: func(cmd *cobra.Command, args []string) {
execute(provider)
},
}
rootCmd.PersistentFlags().StringVar(&provider, "provider", "aws", "Type of provider (aws)")
err := rootCmd.Execute()
if err != nil {
panic(err)
}
}
func execute(dbprovider string) {
log.Println("Starting aws-rds operator")
config, err := getClientConfig(kube.Config())
if err != nil {
panic(err.Error())
}
// create clientset
clientset, err := apiextcs.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// note: if the CRD exist our CreateCRD function is set to exit without an error
_, err = crd.EnsureCRD(clientset)
if err != nil {
panic(err)
}
// Create a new clientset which include our CRD schema
restClient, scheme, err := crd.NewRESTClient(config)
if err != nil {
panic(err)
}
// Create a CRD client interface
crClient := client.NewCRClient(restClient, scheme, "")
log.Println("Watching for database changes...")
_, controller := cache.NewInformer(
crClient.NewListWatch(),
&crd.RDSDatabase{},
time.Minute*2,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
db := obj.(*crd.RDSDatabase)
provider, err := getProvider(db, dbprovider)
if err != nil {
log.Printf("unable to get DB provider %s: %v", dbprovider, err)
return
}
crClient := client.NewCRClient(restClient, scheme, db.Namespace) // add the database namespace to the client
status := crd.RDSDatabaseStatus{
Message: "Looking around...",
State: "New",
DBConnectionConfig: "",
DBCredentials: db.Spec.Password.Name,
}
err = updateStatus(db.Name, status, crClient)
if err != nil {
log.Printf("Unable to update status: %v", err)
return
}
// create DB
dbEndpoint, err := handleCreateRDSDatabase(db, crClient, &provider)
if err != nil {
log.Printf("database creation failed: %v", err)
status.Message = fmt.Sprintf("%v", err)
status.State = "DBCreatingFailed"
err := updateStatus(db.Name, status, crClient)
if err != nil {
log.Printf("database CRD status update failed: %v", err)
}
return
}
status.Message = "DB Created - creating ConfigMap"
status.State = "CreatingConfigMap"
err = updateStatus(db.Name, status, crClient)
if err != nil {
log.Printf("Unable to update status: %v", err)
return
}
//create config map
cfm, err := ensureConfigMap(db, dbEndpoint)
status.Message = "ConfigMap Created"
status.State = "Completed"
status.DBConnectionConfig = cfm.GetName()
err = updateStatus(db.Name, status, crClient)
if err != nil {
log.Printf("Unable to update status: %v", err)
return
}
log.Printf("Creation of database %v done\n", db.Name)
},
DeleteFunc: func(obj interface{}) {
db := obj.(*crd.RDSDatabase)
log.Printf("deleting database: %s \n", db.Name)
provider, err := getProvider(db, dbprovider)
if err != nil {
log.Printf("unable to get DB provider %s: %v", dbprovider, err)
return
}
err = provider.DeleteRDSDatabase(db)
if err != nil {
log.Println(err)
}
log.Printf("Deletion of database %v done\n", db.Name)
},
UpdateFunc: func(oldObj, newObj interface{}) {
},
},
)
stop := make(chan struct{})
go controller.Run(stop)
// Wait forever
select {}
}
func getProvider(db *crd.RDSDatabase, dbprovider string) (provider.RDSDatabaseProvider, error) {
kubectl, err := getKubectl()
if err != nil {
log.Println(err)
return nil, err
}
switch dbprovider {
case "aws":
r, err := rds.New(db, kubectl)
if err != nil {
return nil, err
}
return r, nil
}
return nil, fmt.Errorf("unable to find provider for %v", dbprovider)
}
func handleCreateRDSDatabase(db *crd.RDSDatabase, crClient *client.CRClient, dbProvider *provider.RDSDatabaseProvider) (*provider.DBEndpoint, error) {
// validate dbname is only alpha numeric
err := updateStatus(db.Name, crd.RDSDatabaseStatus{Message: "Attempting to Create a DB", State: "DBCreating"}, crClient)
if err != nil {
return nil, fmt.Errorf("database CR status update failed: %v", err)
}
log.Println("Attempting to Create a DB")
dbEndpoint, err := (*dbProvider).CreateRDSDatabase(db)
if err != nil {
return nil, err
}
return dbEndpoint, nil
}
func ensureConfigMap(db *crd.RDSDatabase, dbEndpoint *provider.DBEndpoint) (*v1.ConfigMap, error) {
//Create a configMap
kubectl, err := kube.Client()
if err != nil {
return nil, err
}
configMapInterface := kubectl.CoreV1().ConfigMaps(db.Namespace)
cfm, cErr := configMapInterface.Get(db.Name, metav1.GetOptions{})
lbs := map[string]string{
"app": db.GetName(),
}
createCM := false
if cErr != nil {
cfm = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: db.Name,
Namespace: db.Namespace,
Labels: lbs,
},
Data: map[string]string{
"DB_HOST": dbEndpoint.Hostname,
"DB_PORT": fmt.Sprintf("%d", dbEndpoint.Port),
},
}
ownerRef := metav1.OwnerReference{
APIVersion: crd.CRDVersion,
Kind: crd.CRDKind,
Name: db.GetName(),
UID: db.GetUID(),
}
cfm.SetOwnerReferences([]metav1.OwnerReference{ownerRef})
createCM = true
}
if createCM {
_, err = configMapInterface.Create(cfm)
} else {
_, err = configMapInterface.Update(cfm)
}
return cfm, nil
}
func updateStatus(dbName string, status crd.RDSDatabaseStatus, crClient *client.CRClient) error {
db, err := crClient.Get(dbName)
if err != nil {
return err
}
db.Status = status
_, err = crClient.UpdateStatus(db)
if err != nil {
return err
}
return nil
}