-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelationaldatabaseprovider_controller.go
More file actions
366 lines (333 loc) · 13.7 KB
/
relationaldatabaseprovider_controller.go
File metadata and controls
366 lines (333 loc) · 13.7 KB
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
/*
Copyright 2024.
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 controller
import (
"context"
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"github.com/prometheus/client_golang/prometheus"
crdv1alpha1 "github.com/uselagoon/dbaas-controller/api/v1alpha1"
"github.com/uselagoon/dbaas-controller/internal/database"
)
const databaseProviderFinalizer = "relationaldatabaseprovider.crd.lagoon.sh/finalizer"
var (
// Prometheus metrics
// promRelationalDatabaseProviderReconcileErrorCounter counter for the reconciled relational database providers errors
promRelationalDatabaseProviderReconcileErrorCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "relationaldatabaseprovider_reconcile_error_total",
Help: "The total number of reconciled relational database providers errors",
},
[]string{"type", "name", "scope", "error"},
)
// promRelationalDatabaseProviderStatus is the gauge for the relational database provider status
promRelationalDatabaseProviderStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "relationaldatabaseprovider_status",
Help: "The status of the relational database provider",
},
[]string{"type", "name", "scope"},
)
// promRelationalDatabaseProviderConnectionVersion is the gauge for the relational database provider connection version
promRelationalDatabaseProviderConnectionVersion = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "relationaldatabaseprovider_connection_version",
Help: "The version of the relational database provider connection",
},
[]string{"type", "name", "scope", "hostname", "username", "version"},
)
)
// RelationalDatabaseProviderReconciler reconciles a RelationalDatabaseProvider object
type RelationalDatabaseProviderReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
RelDBClient database.RelationalDatabaseInterface
}
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
// nolint:lll
//+kubebuilder:rbac:groups=crd.lagoon.sh,resources=relationaldatabaseproviders,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=crd.lagoon.sh,resources=relationaldatabaseproviders/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=crd.lagoon.sh,resources=relationaldatabaseproviders/finalizers,verbs=update
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *RelationalDatabaseProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx).WithName("relationaldatabaseprovider_controller")
logger.Info("Reconciling RelationalDatabaseProvider")
// Fetch the RelationalDatabaseProvider instance
instance := &crdv1alpha1.RelationalDatabaseProvider{}
if err := r.Get(ctx, req.NamespacedName, instance); err != nil {
if client.IgnoreNotFound(err) == nil {
logger.Info("RelationalDatabaseProvider not found")
return ctrl.Result{}, nil
}
promRelationalDatabaseProviderReconcileErrorCounter.WithLabelValues(
"", req.Name, "", "get-relationaldbprovider").Inc()
return ctrl.Result{}, err
}
logger = logger.WithValues("type", instance.Spec.Type, "scope", instance.Spec.Scope)
if instance.DeletionTimestamp != nil && !instance.DeletionTimestamp.IsZero() {
// The object is being deleted
// To be discussed whether we need to delete all the database requests using this provider...
if controllerutil.RemoveFinalizer(instance, databaseProviderFinalizer) {
if err := r.Update(ctx, instance); err != nil {
return r.handleError(ctx, instance, "remove-finalizer", err)
}
}
return ctrl.Result{}, nil
}
// Check if we need to reconcile based on Generation and ObservedGeneration but only if
// the status condition is not false. This makes sure that in case of an error the controller
// will try to reconcile again.
if instance.Status.Conditions != nil && meta.IsStatusConditionTrue(instance.Status.Conditions, "Ready") {
if instance.Status.ObservedGeneration >= instance.Generation {
logger.Info("No updates to reconcile")
r.Recorder.Event(instance, v1.EventTypeNormal, "ReconcileSkipped", "No updates to reconcile")
return ctrl.Result{}, nil
}
}
if controllerutil.AddFinalizer(instance, databaseProviderFinalizer) {
if err := r.Update(ctx, instance); err != nil {
return r.handleError(ctx, instance, "add-finalizer", err)
}
}
// Reconcile the RelationalDatabaseProvider and check the unique name of the Connections
uniqueNames := make(map[string]struct{}, len(instance.Spec.Connections))
conns := make([]reldbConn, 0, len(instance.Spec.Connections))
for _, conn := range instance.Spec.Connections {
secret := &v1.Secret{}
if err := r.Get(ctx, types.NamespacedName{
Name: conn.PasswordSecretRef.Name,
Namespace: conn.PasswordSecretRef.Namespace,
}, secret); err != nil {
return r.handleError(ctx, instance, "get-secret", err)
}
password := string(secret.Data["password"])
if password == "" {
return r.handleError(
ctx,
instance,
fmt.Sprintf("%s-empty-password", instance.Spec.Type),
fmt.Errorf(
"%s connection secret %s in namespace %s has empty password",
instance.Spec.Type, secret.Name, secret.Namespace,
),
)
}
conns = append(conns, reldbConn{
dbType: instance.Spec.Type,
name: conn.Name,
hostname: conn.Hostname,
replicaHostnames: conn.ReplicaHostnames,
password: password,
port: conn.Port,
username: conn.Username,
enabled: conn.Enabled,
})
uniqueNames[conn.Name] = struct{}{}
}
if len(uniqueNames) != len(instance.Spec.Connections) {
return r.handleError(
ctx,
instance,
fmt.Sprintf("%s-unique-name-error", instance.Spec.Type),
fmt.Errorf("%s database connections must have unique names", instance.Spec.Type),
)
}
dbStatus := make([]crdv1alpha1.ConnectionStatus, 0, len(conns))
errors := make([]error, 0, len(conns))
foundEnabledDatabase := false
for _, conn := range conns {
// make a ping to the database to check if it's up and running and we can connect to it
// if not, we should return an error and set the status to 0
// Note we could periodically check the status of the database and update the status accordingly...
if err := r.RelDBClient.Ping(ctx, conn.getDSN(false), instance.Spec.Type); err != nil {
errors = append(errors, err)
dbStatus = append(dbStatus, crdv1alpha1.ConnectionStatus{
Name: conn.name,
Hostname: conn.hostname,
Status: "unavailable",
Enabled: conn.enabled,
})
promRelationalDatabaseProviderConnectionVersion.WithLabelValues(
instance.Spec.Type, req.Name, instance.Spec.Scope, conn.hostname, conn.username, "").Set(0)
logger.Error(err, "Failed to ping the database", "hostname", conn.hostname)
continue
}
version, err := r.RelDBClient.Version(ctx, conn.getDSN(false), instance.Spec.Type)
if err != nil {
errors = append(errors, err)
dbStatus = append(dbStatus, crdv1alpha1.ConnectionStatus{
Name: conn.name,
Hostname: conn.hostname,
Status: "unavailable",
Enabled: conn.enabled,
})
logger.Error(err, "Failed to get the database version", "hostname", conn.hostname)
promRelationalDatabaseProviderConnectionVersion.WithLabelValues(
instance.Spec.Type, req.Name, instance.Spec.Scope, conn.hostname, conn.username, version).Set(0)
continue
}
// check if the database is initialized
err = r.RelDBClient.Initialize(ctx, conn.getDSN(false), instance.Spec.Type)
if err != nil {
errors = append(errors, err)
dbStatus = append(dbStatus, crdv1alpha1.ConnectionStatus{
Name: conn.name,
Hostname: conn.hostname,
Status: "unavailable",
Enabled: conn.enabled,
})
promRelationalDatabaseProviderConnectionVersion.WithLabelValues(
instance.Spec.Type, req.Name, instance.Spec.Scope, conn.hostname, conn.username, version).Set(0)
continue
}
dbStatus = append(dbStatus, crdv1alpha1.ConnectionStatus{
Name: conn.name,
Hostname: conn.hostname,
DatabaseVersion: version,
Status: "available",
Enabled: conn.enabled,
})
if conn.enabled {
foundEnabledDatabase = true
promRelationalDatabaseProviderConnectionVersion.WithLabelValues(
instance.Spec.Type, req.Name, instance.Spec.Scope, conn.hostname, conn.username, version).Set(1)
} else {
promRelationalDatabaseProviderConnectionVersion.WithLabelValues(
instance.Spec.Type, req.Name, instance.Spec.Scope, conn.hostname, conn.username, version).Set(0)
}
}
instance.Status.ConnectionStatus = dbStatus
instance.Status.ObservedGeneration = instance.Generation
if len(errors) == len(conns) {
return r.handleError(
ctx,
instance,
fmt.Sprintf("%s-connection-error", instance.Spec.Type),
fmt.Errorf("failed to connect to any of the %s databases: %v", instance.Spec.Type, errors),
)
}
if !foundEnabledDatabase {
return r.handleError(
ctx,
instance,
fmt.Sprintf("%s-connection-not-any-enabled", instance.Spec.Type),
fmt.Errorf("no enabled working %s database found", instance.Spec.Type),
)
}
// update the status condition to ready
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: "Ready",
Status: metav1.ConditionTrue,
Reason: "Reconciled",
Message: "RelationalDatabaseProvider reconciled",
})
// update the status
if err := r.Status().Update(ctx, instance); err != nil {
promRelationalDatabaseProviderReconcileErrorCounter.WithLabelValues(
instance.Spec.Type, req.Name, instance.Spec.Scope, "update-status").Inc()
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Type, req.Name, instance.Spec.Scope).Set(0)
return ctrl.Result{}, err
}
r.Recorder.Event(instance, "Normal", "Reconciled", "RelationalDatabaseProvider reconciled")
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Type, req.Name, instance.Spec.Scope).Set(1)
return ctrl.Result{}, nil
}
// handleError handles the error and returns the result and the error
func (r *RelationalDatabaseProviderReconciler) handleError(
ctx context.Context,
instance *crdv1alpha1.RelationalDatabaseProvider,
promErr string,
err error,
) (ctrl.Result, error) {
promRelationalDatabaseProviderReconcileErrorCounter.WithLabelValues(
instance.Spec.Type, instance.Name, instance.Spec.Scope, promErr).Inc()
promRelationalDatabaseProviderStatus.WithLabelValues(instance.Spec.Type, instance.Name, instance.Spec.Scope).Set(0)
r.Recorder.Event(instance, v1.EventTypeWarning, errTypeToEventReason(promErr), err.Error())
// set the status condition to false
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: "Ready",
Status: metav1.ConditionFalse,
Reason: errTypeToEventReason(promErr),
Message: err.Error(),
})
// update the status
if err := r.Status().Update(ctx, instance); err != nil {
promRelationalDatabaseProviderReconcileErrorCounter.WithLabelValues(
instance.Spec.Type, instance.Name, instance.Spec.Scope, "update-status").Inc()
log.FromContext(ctx).Error(err, "Failed to update status")
}
return ctrl.Result{}, err
}
// reldbConn is the connection to a MySQL or PostgreSQL database
type reldbConn struct {
dbType string
name string
hostname string
replicaHostnames []string
password string
port int
username string
enabled bool
}
// getDSN constructs the DSN string for the MySQL or PostgreSQL connection.
func (rc *reldbConn) getDSN(useDatabase bool) string {
dsn := ""
switch rc.dbType {
case "mysql":
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/", rc.username, rc.password, rc.hostname, rc.port)
if useDatabase {
dsn += rc.name
}
case "postgres":
dsn = fmt.Sprintf(
"host=%s port=%d user=%s password=%s sslmode=disable",
rc.hostname, rc.port, rc.username, rc.password,
)
if useDatabase {
dsn += fmt.Sprintf(" dbname=%s", rc.name)
}
}
return dsn
}
// SetupWithManager sets up the controller with the Manager.
func (r *RelationalDatabaseProviderReconciler) SetupWithManager(mgr ctrl.Manager) error {
// register metrics
metrics.Registry.MustRegister(
promRelationalDatabaseProviderReconcileErrorCounter,
promRelationalDatabaseProviderStatus,
promRelationalDatabaseProviderConnectionVersion,
)
r.Recorder = mgr.GetEventRecorderFor("relationaldatabaseprovider_controller")
return ctrl.NewControllerManagedBy(mgr).
For(&crdv1alpha1.RelationalDatabaseProvider{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
// let's set the max concurrent reconciles to 1 as we don't want to run multiple reconciles at the same time
// although we could also change this and guard it by the name of the database provider
WithOptions(controller.Options{MaxConcurrentReconciles: 1}).
Complete(r)
}