/
databaseserver_controller.go
374 lines (328 loc) · 9.97 KB
/
databaseserver_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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
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 controllers
import (
"context"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/go-logr/logr"
"golang.org/x/crypto/ssh/terminal"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/scheme"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
mariadbv1beta1 "github.com/bjornmagnusson/mariadb-operator/api/v1beta1"
)
// DatabaseServerReconciler reconciles a DatabaseServer object
type DatabaseServerReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
// +kubebuilder:rbac:groups=mariadb.bjornmagnusson.com,resources=databaseservers,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=mariadb.bjornmagnusson.com,resources=databaseservers/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=*,resources=pods,verbs=get;list
// +kubebuilder:rbac:groups=*,resources=pods/exec,verbs=create
func (r *DatabaseServerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("databaseserver", req.NamespacedName)
log.Info("Reconsile started")
var instance mariadbv1beta1.DatabaseServer
if err := r.Get(ctx, req.NamespacedName, &instance); err != nil {
if errors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
log.Error(err, "unable to find DatabaseServer")
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
spec := instance.Spec
var replicas int32
replicas = 1
labels := map[string]string{
"app.kubernetes.io/name": instance.Name,
"app.kubernetes.io/managed-by": "mariadb-operator",
}
log.Info("-- DEPLOYMENT START --")
deploy := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: spec.Name,
Namespace: "default",
Labels: labels,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Replicas: &replicas,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: v1.PodSpec{
Containers: []corev1.Container{
{
Name: spec.Name,
Image: spec.Image,
Ports: []corev1.ContainerPort{
{
Name: "db",
ContainerPort: 3306,
},
},
Env: []corev1.EnvVar{
{
Name: "MYSQL_ROOT_PASSWORD",
Value: spec.Password,
},
},
ReadinessProbe: &corev1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{
"mysql",
"-uroot",
"-p" + spec.Password,
"-estatus",
},
},
},
},
},
},
},
},
},
}
found := &appsv1.Deployment{}
var isNew bool
err := r.Get(ctx, types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
log.Info("Creating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
err = r.Create(ctx, deploy)
isNew = true
if err != nil {
return reconcile.Result{}, err
}
} else if err != nil {
return reconcile.Result{}, err
}
if !reflect.DeepEqual(deploy.Spec, found.Spec) && !isNew {
log.Info("Deleting Deployment (update)", "namespace", deploy.Namespace, "name", deploy.Name)
_ = r.Delete(ctx, found)
log.Info("Creating Deployment (update)", "namespace", deploy.Namespace, "name", deploy.Name)
_ = r.Create(ctx, deploy)
}
log.Info("-- DEPLOYMENT END --")
log.Info("-- SERVICE START --")
var svcPort int32
if spec.Port > 0 {
svcPort = spec.Port
} else {
svcPort = 3306
}
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: spec.Name + "-service",
Namespace: "default",
Labels: labels,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "db",
Port: svcPort,
TargetPort: intstr.IntOrString{
IntVal: 3306,
},
},
},
Selector: labels,
Type: "LoadBalancer",
},
}
foundSvc := &corev1.Service{}
errSvc := r.Get(ctx, types.NamespacedName{Name: service.Name, Namespace: service.Namespace}, foundSvc)
if errSvc != nil && errors.IsNotFound(errSvc) {
log.Info("Creating Service", "namespace", service.Namespace, "name", service.Name)
err = r.Create(ctx, service)
} else if err != nil {
return reconcile.Result{}, err
}
log.Info("-- SERVICE END --")
log.Info("-- DEPLOYMENT STATUS START --")
log.Info("Waiting 10s to check status")
time.Sleep(10 * time.Second)
deploymentIsRunning := false
for !deploymentIsRunning {
foundStatus := &appsv1.Deployment{}
err := r.Get(ctx, types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, foundStatus)
if err != nil {
log.Error(err, "Failed to lookup deployment status")
} else if foundStatus.Status.UnavailableReplicas == 0 {
log.Info("Deployment is ready")
deploymentIsRunning = true
} else {
log.Info("MariaDB is not running, retrying in 10s",
"replicas", foundStatus.Status.Replicas,
"ready-replicas", foundStatus.Status.ReadyReplicas,
"unavailable-replicas", foundStatus.Status.UnavailableReplicas)
time.Sleep(10 * time.Second)
}
}
log.Info("-- DEPLOYMENT STATUS END --")
log.Info("-- DATABASE START --")
var childPods corev1.PodList
if err := r.List(ctx, &childPods, client.InNamespace(req.Namespace), client.MatchingLabels{
"app.kubernetes.io/managed-by": "mariadb-operator",
"app.kubernetes.io/name": req.Name}); err != nil {
log.Error(err, "unable to list child ReplicaSet")
return ctrl.Result{}, err
}
var dbPod corev1.Pod
for i, pod := range childPods.Items {
log.Info(strconv.Itoa(i+1) + ") " + pod.Name)
if pod.Status.Phase == corev1.PodRunning {
dbPod = pod
}
}
log.Info("MariaDB: Will connect to " + dbPod.Name)
baseExecOptions := corev1.PodExecOptions{
Stdin: true,
Stdout: true,
Stderr: true,
TTY: true,
}
statusExecOptions := baseExecOptions.DeepCopy()
statusExecOptions.Command = []string{
"mysql",
"-uroot",
"-p" + spec.Password,
"-estatus",
}
executeCommand(dbPod, *statusExecOptions, log)
showDatabaseExecOptions := baseExecOptions.DeepCopy()
showDatabaseExecOptions.Command = []string{
"mysql",
"-uroot",
"-p" + spec.Password,
"-e",
"SHOW DATABASES",
}
executeCommand(dbPod, *showDatabaseExecOptions, log)
for i, db := range spec.Databases {
log.Info(strconv.Itoa(i+1) + ") " + db.Name)
createDatabaseExecOptions := baseExecOptions.DeepCopy()
createDatabaseExecOptions.Command = []string{
"mysql",
"-uroot",
"-p" + spec.Password,
"-e",
"CREATE DATABASE IF NOT EXISTS " + db.Name,
}
executeCommand(dbPod, *createDatabaseExecOptions, log)
}
executeCommand(dbPod, *showDatabaseExecOptions, log)
log.Info("-- DATABASE END --")
log.Info("Reconsile finished")
return reconcile.Result{}, nil
}
var (
jobOwnerKey = ".metadata.controller"
apiGVStr = appsv1.SchemeGroupVersion.Version
)
func executeCommand(pod corev1.Pod, execOptions corev1.PodExecOptions, log logr.Logger) {
log.Info("Executing " + strings.Join(execOptions.Command, " "))
// Instantiate loader for kubeconfig file.
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)
// Get a rest.Config from the kubeconfig file. This will be passed into all
// the client objects we create.
restconfig, err := kubeconfig.ClientConfig()
if err != nil {
panic(err)
}
// Create a Kubernetes core/v1 client.
coreclient, err := corev1client.NewForConfig(restconfig)
if err != nil {
panic(err)
}
req := coreclient.RESTClient().
Post().
Namespace(pod.Namespace).
Resource("pods").
Name(pod.Name).
SubResource("exec").
VersionedParams(&execOptions, scheme.ParameterCodec)
log.Info("Command: " + req.URL().String())
exec, err := remotecommand.NewSPDYExecutor(restconfig, "POST", req.URL())
if err != nil {
panic(err)
}
// Put the terminal into raw mode to prevent it echoing characters twice.
oldState, err := terminal.MakeRaw(0)
if err != nil {
panic(err)
}
defer terminal.Restore(0, oldState)
// Connect this process' std{in,out,err} to the remote shell process.
err = exec.Stream(remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: true,
})
if err != nil {
panic(err)
}
fmt.Println()
}
func (r *DatabaseServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(&appsv1.ReplicaSet{}, jobOwnerKey, func(rawObj runtime.Object) []string {
deployment := rawObj.(*appsv1.ReplicaSet)
owner := metav1.GetControllerOf(deployment)
if owner == nil {
return nil
}
// ...make sure it's a CronJob...
if owner.APIVersion != apiGVStr || owner.Kind != "Deployment" {
return nil
}
// ...and if so, return it
return []string{owner.Name}
}); err != nil {
return err
}
return ctrl.NewControllerManagedBy(mgr).
For(&mariadbv1beta1.DatabaseServer{}).
Owns(&appsv1.ReplicaSet{}).
Complete(r)
}