Skip to content

Commit 49c941f

Browse files
committed
DBAAS-831: update DBaaSConnection status to conform to the Provisioned Service ducktype defined in the Service Binding Specification for Kubernetes
1 parent 0980243 commit 49c941f

File tree

9 files changed

+1291
-169
lines changed

9 files changed

+1291
-169
lines changed

bundle/manifests/dbaas.redhat.com_rdsconnections.yaml

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ spec:
5757
status:
5858
description: DBaaSConnectionStatus defines the observed state of DBaaSConnection
5959
properties:
60+
binding:
61+
description: 'Binding exposes a secret containing the binding information
62+
for the instance. It implements the service binding Provisioned
63+
Service duck type. See: https://github.com/servicebinding/spec#provisioned-service'
64+
properties:
65+
name:
66+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
67+
TODO: Add other useful fields. apiVersion, kind, uid?'
68+
type: string
69+
type: object
6070
conditions:
6171
items:
6272
description: "Condition contains details for one aspect of the current
@@ -125,24 +135,6 @@ spec:
125135
- type
126136
type: object
127137
type: array
128-
connectionInfoRef:
129-
description: A ConfigMap holding non-sensitive information needed
130-
for connecting to the DB instance
131-
properties:
132-
name:
133-
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
134-
TODO: Add other useful fields. apiVersion, kind, uid?'
135-
type: string
136-
type: object
137-
credentialsRef:
138-
description: Secret holding the credentials needed for accessing the
139-
DB instance
140-
properties:
141-
name:
142-
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
143-
TODO: Add other useful fields. apiVersion, kind, uid?'
144-
type: string
145-
type: object
146138
type: object
147139
type: object
148140
served: true

config/crd/bases/dbaas.redhat.com_rdsconnections.yaml

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ spec:
5858
status:
5959
description: DBaaSConnectionStatus defines the observed state of DBaaSConnection
6060
properties:
61+
binding:
62+
description: 'Binding exposes a secret containing the binding information
63+
for the instance. It implements the service binding Provisioned
64+
Service duck type. See: https://github.com/servicebinding/spec#provisioned-service'
65+
properties:
66+
name:
67+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
68+
TODO: Add other useful fields. apiVersion, kind, uid?'
69+
type: string
70+
type: object
6171
conditions:
6272
items:
6373
description: "Condition contains details for one aspect of the current
@@ -126,24 +136,6 @@ spec:
126136
- type
127137
type: object
128138
type: array
129-
connectionInfoRef:
130-
description: A ConfigMap holding non-sensitive information needed
131-
for connecting to the DB instance
132-
properties:
133-
name:
134-
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
135-
TODO: Add other useful fields. apiVersion, kind, uid?'
136-
type: string
137-
type: object
138-
credentialsRef:
139-
description: Secret holding the credentials needed for accessing the
140-
DB instance
141-
properties:
142-
name:
143-
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
144-
TODO: Add other useful fields. apiVersion, kind, uid?'
145-
type: string
146-
type: object
147139
type: object
148140
type: object
149141
served: true

controllers/rdsconnection_controller.go

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import (
4343
const (
4444
instanceIDKey = ".spec.instanceID"
4545

46-
databaseProvider = "Red Hat DBaaS / Amazon Relational Database Service (RDS)"
46+
databaseProvider = "rhoda/amazon rds"
4747

4848
connectionConditionReady = "ReadyForBinding"
4949

@@ -57,7 +57,6 @@ const (
5757
connectionStatusMessageUpdateError = "Failed to update Connection"
5858
connectionStatusMessageUpdating = "Updating Connection"
5959
connectionStatusMessageSecretError = "Failed to create or update secret"
60-
connectionStatusMessageConfigMapError = "Failed to create or update configmap"
6160
connectionStatusMessageInstanceNotFound = "Instance not found"
6261
connectionStatusMessageInstanceNotReady = "Instance not ready"
6362
connectionStatusMessageGetInstanceError = "Failed to get Instance"
@@ -221,15 +220,7 @@ func (r *RDSConnectionReconciler) Reconcile(ctx context.Context, req ctrl.Reques
221220
return true
222221
}
223222

224-
dbConfigMap, e := r.createOrUpdateConfigMap(ctx, &connection, &dbInstance)
225-
if e != nil {
226-
logger.Error(e, "Failed to create or update configmap for Connection")
227-
returnError(e, connectionStatusReasonBackendError, connectionStatusMessageConfigMapError)
228-
return true
229-
}
230-
231-
connection.Status.CredentialsRef = &v1.LocalObjectReference{Name: userSecret.Name}
232-
connection.Status.ConnectionInfoRef = &v1.LocalObjectReference{Name: dbConfigMap.Name}
223+
connection.Status.Binding = &v1.LocalObjectReference{Name: userSecret.Name}
233224
if e := r.Status().Update(ctx, &connection); e != nil {
234225
if errors.IsConflict(e) {
235226
logger.Info("Connection modified, retry reconciling")
@@ -290,7 +281,7 @@ func (r *RDSConnectionReconciler) Reconcile(ctx context.Context, req ctrl.Reques
290281

291282
func (r *RDSConnectionReconciler) createOrUpdateSecret(ctx context.Context, connection *rdsdbaasv1alpha1.RDSConnection,
292283
dbSecret *v1.Secret, dbInstance *rdsv1alpha1.DBInstance) (*v1.Secret, error) {
293-
secretName := fmt.Sprintf("%s-credentials", connection.Name)
284+
secretName := fmt.Sprintf("%s-connection-credentials", connection.Name)
294285
secret := &v1.Secret{
295286
ObjectMeta: metav1.ObjectMeta{
296287
Name: secretName,
@@ -303,6 +294,7 @@ func (r *RDSConnectionReconciler) createOrUpdateSecret(ctx context.Context, conn
303294
if err := ctrl.SetControllerReference(connection, secret, r.Scheme); err != nil {
304295
return err
305296
}
297+
secret.Type = v1.SecretType(fmt.Sprintf("servicebinding.io/%s", generateBindingType(*dbInstance.Spec.Engine)))
306298
setSecret(secret, dbSecret, dbInstance)
307299
return nil
308300
})
@@ -316,72 +308,43 @@ func setSecret(secret *v1.Secret, dbSecret *v1.Secret, dbInstance *rdsv1alpha1.D
316308
data := map[string][]byte{
317309
"username": []byte(*dbInstance.Spec.MasterUsername),
318310
"password": dbSecret.Data[dbInstance.Spec.MasterUserPassword.Key],
311+
"type": []byte(generateBindingType(*dbInstance.Spec.Engine)),
312+
"provider": []byte(databaseProvider),
313+
"host": []byte(*dbInstance.Status.Endpoint.Address),
314+
"port": []byte(strconv.FormatInt(*dbInstance.Status.Endpoint.Port, 10)),
319315
}
320-
secret.Data = data
321-
}
322316

323-
func (r *RDSConnectionReconciler) createOrUpdateConfigMap(ctx context.Context, connection *rdsdbaasv1alpha1.RDSConnection,
324-
dbInstance *rdsv1alpha1.DBInstance) (*v1.ConfigMap, error) {
325-
cmName := fmt.Sprintf("%s-configs", connection.Name)
326-
cm := &v1.ConfigMap{
327-
ObjectMeta: metav1.ObjectMeta{
328-
Name: cmName,
329-
Namespace: connection.Namespace,
330-
},
331-
}
332-
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, cm, func() error {
333-
cm.ObjectMeta.Labels = buildConnectionLabels()
334-
cm.ObjectMeta.Annotations = buildConnectionAnnotations(connection, &cm.ObjectMeta)
335-
if err := ctrl.SetControllerReference(connection, cm, r.Scheme); err != nil {
336-
return err
337-
}
338-
setConfigMap(cm, dbInstance)
339-
return nil
340-
})
341-
if err != nil {
342-
return nil, err
343-
}
344-
return cm, nil
345-
}
346-
347-
func setConfigMap(cm *v1.ConfigMap, dbInstance *rdsv1alpha1.DBInstance) {
348-
dataMap := map[string]string{
349-
"type": generateBindingType(*dbInstance.Spec.Engine),
350-
"provider": databaseProvider,
351-
"host": *dbInstance.Status.Endpoint.Address,
352-
"port": strconv.FormatInt(*dbInstance.Status.Endpoint.Port, 10),
353-
}
354317
if dbInstance.Spec.DBName != nil {
355-
dataMap["database"] = *dbInstance.Spec.DBName
318+
data["database"] = []byte(*dbInstance.Spec.DBName)
356319
} else if dbInstance.Spec.Engine != nil {
357320
if dbName := getDefaultDBName(*dbInstance.Spec.Engine); dbName != nil {
358-
dataMap["database"] = *dbName
321+
data["database"] = []byte(*dbName)
359322
}
360323
}
361324

362325
if dbInstance.Spec.Engine != nil {
363-
host := dataMap["host"]
364-
port := dataMap["port"]
365-
db, dbOk := dataMap["database"]
326+
host := data["host"]
327+
port := data["port"]
328+
db, dbOk := data["database"]
366329

367330
switch *dbInstance.Spec.Engine {
368331
case oracleSe2, oracleSe2Cdb, oracleEe, oracleEeCdb, customOracleEe:
369332
if dbOk {
370-
dataMap["jdbc-url"] = fmt.Sprintf("jdbc:oracle:thin:@%s:%s/%s", host, port, db)
333+
data["jdbc-url"] = []byte(fmt.Sprintf("jdbc:oracle:thin:@%s:%s/%s", host, port, db))
371334
} else {
372-
dataMap["jdbc-url"] = fmt.Sprintf("jdbc:oracle:thin:@%s:%s", host, port)
335+
data["jdbc-url"] = []byte(fmt.Sprintf("jdbc:oracle:thin:@%s:%s", host, port))
373336
}
374337
case sqlserverEe, sqlserverSe, sqlserverEx, sqlserverWeb, customSqlserverEe, customSqlserverSe, customSqlserverWeb:
375338
if dbOk {
376-
dataMap["jdbc-url"] = fmt.Sprintf("jdbc:sqlserver://%s:%s;databaseName=%s", host, port, db)
339+
data["jdbc-url"] = []byte(fmt.Sprintf("jdbc:sqlserver://%s:%s;databaseName=%s", host, port, db))
377340
} else {
378-
dataMap["jdbc-url"] = fmt.Sprintf("jdbc:sqlserver://%s:%s", host, port)
341+
data["jdbc-url"] = []byte(fmt.Sprintf("jdbc:sqlserver://%s:%s", host, port))
379342
}
380343
default:
381344
}
382345
}
383346

384-
cm.Data = dataMap
347+
secret.Data = data
385348
}
386349

387350
func buildConnectionLabels() map[string]string {

0 commit comments

Comments
 (0)