Skip to content

Commit a5d059f

Browse files
Merge pull request openstack-k8s-operators#308 from stuggi/tls_db
[tlse] TLS database connection
2 parents 85c7d07 + 3e32b01 commit a5d059f

File tree

6 files changed

+146
-168
lines changed

6 files changed

+146
-168
lines changed

controllers/neutronapi_controller.go

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -397,72 +397,20 @@ func (r *NeutronAPIReconciler) reconcileInit(
397397
Log := r.GetLogger(ctx)
398398
Log.Info("Reconciling Service init")
399399

400-
// create neutron DB instance
401-
//
402-
db := mariadbv1.NewDatabaseWithNamespace(
403-
neutronapi.Database,
404-
instance.Spec.DatabaseUser,
405-
instance.Spec.Secret,
406-
map[string]string{
407-
"dbName": instance.Spec.DatabaseInstance,
408-
},
409-
neutronapi.Database,
410-
instance.Namespace,
411-
)
412-
// create or patch the DB
413-
ctrlResult, err := db.CreateOrPatchDBByName(
414-
ctx,
415-
helper,
416-
instance.Spec.DatabaseInstance,
417-
)
400+
db, result, err := r.ensureDB(ctx, helper, instance)
418401
if err != nil {
419-
instance.Status.Conditions.Set(condition.FalseCondition(
420-
condition.DBReadyCondition,
421-
condition.ErrorReason,
422-
condition.SeverityWarning,
423-
condition.DBReadyErrorMessage,
424-
err.Error()))
425402
return ctrl.Result{}, err
403+
} else if (result != ctrl.Result{}) {
404+
return result, nil
426405
}
427-
if (ctrlResult != ctrl.Result{}) {
428-
instance.Status.Conditions.Set(condition.FalseCondition(
429-
condition.DBReadyCondition,
430-
condition.RequestedReason,
431-
condition.SeverityInfo,
432-
condition.DBReadyRunningMessage))
433-
return ctrlResult, nil
434-
}
435-
// wait for the DB to be setup
436-
ctrlResult, err = db.WaitForDBCreatedWithTimeout(ctx, helper, time.Second*5)
437-
if err != nil {
438-
instance.Status.Conditions.Set(condition.FalseCondition(
439-
condition.DBReadyCondition,
440-
condition.ErrorReason,
441-
condition.SeverityWarning,
442-
condition.DBReadyErrorMessage,
443-
err.Error()))
444-
return ctrlResult, err
445-
}
446-
if (ctrlResult != ctrl.Result{}) {
447-
instance.Status.Conditions.Set(condition.FalseCondition(
448-
condition.DBReadyCondition,
449-
condition.RequestedReason,
450-
condition.SeverityInfo,
451-
condition.DBReadyRunningMessage))
452-
return ctrlResult, nil
453-
}
454-
// update Status.DatabaseHostname, used to bootstrap/config the service
455-
instance.Status.DatabaseHostname = db.GetDatabaseHostname()
456-
instance.Status.Conditions.MarkTrue(condition.DBReadyCondition, condition.DBReadyMessage)
457-
// create neutron DB - end
458406

459407
// Create Secrets required as input for the Service and calculate an overall hash of hashes
460408
//
461409

462410
//
463411
// create Secret required for neutronapi and dbsync input. It contains minimal neutron config required
464412
// to get the service up, user can add additional files to be added to the service.
465-
err = r.generateServiceSecrets(ctx, helper, instance, ospSecret, &secretVars)
413+
err = r.generateServiceSecrets(ctx, helper, instance, ospSecret, &secretVars, db)
466414
if err != nil {
467415
instance.Status.Conditions.Set(condition.FalseCondition(
468416
condition.ServiceConfigReadyCondition,
@@ -1404,6 +1352,7 @@ func (r *NeutronAPIReconciler) generateServiceSecrets(
14041352
instance *neutronv1beta1.NeutronAPI,
14051353
ospSecret *corev1.Secret,
14061354
envVars *map[string]env.Setter,
1355+
db *mariadbv1.Database,
14071356
) error {
14081357
// Create/update secrets from templates
14091358
cmLabels := labels.GetLabels(instance, labels.GetGroupLabel(neutronapi.ServiceName), map[string]string{})
@@ -1425,13 +1374,19 @@ func (r *NeutronAPIReconciler) generateServiceSecrets(
14251374
if err != nil {
14261375
return err
14271376
}
1428-
1377+
var tlsCfg *tls.Service
1378+
if instance.Spec.TLS.Ca.CaBundleSecretName != "" {
1379+
tlsCfg = &tls.Service{}
1380+
}
14291381
// customData hold any customization for the service.
14301382
// 02-neutron-custom.conf is going to /etc/<service>.conf.d
14311383
// 01-neutron.conf is going to /etc/<service>.conf.d such that it gets loaded before custom one
14321384
// all other files get placed into /etc/<service> to allow overwrite of e.g. logging.conf or policy.json
14331385
// TODO: make sure custom.conf can not be overwritten
1434-
customData := map[string]string{"02-neutron-custom.conf": instance.Spec.CustomServiceConfig}
1386+
customData := map[string]string{
1387+
"02-neutron-custom.conf": instance.Spec.CustomServiceConfig,
1388+
"my.cnf": db.GetDatabaseClientConfig(tlsCfg), //(mschuppert) for now just get the default my.cnf
1389+
}
14351390
for key, data := range instance.Spec.DefaultConfigOverwrite {
14361391
customData[key] = data
14371392
}
@@ -1602,3 +1557,68 @@ func (r *NeutronAPIReconciler) getNeutronMemcached(
16021557
}
16031558
return memcached, err
16041559
}
1560+
1561+
// ensureDB - create neutron DB instance
1562+
func (r *NeutronAPIReconciler) ensureDB(
1563+
ctx context.Context,
1564+
h *helper.Helper,
1565+
instance *neutronv1beta1.NeutronAPI,
1566+
) (*mariadbv1.Database, ctrl.Result, error) {
1567+
db := mariadbv1.NewDatabaseWithNamespace(
1568+
neutronapi.Database,
1569+
instance.Spec.DatabaseUser,
1570+
instance.Spec.Secret,
1571+
map[string]string{
1572+
"dbName": instance.Spec.DatabaseInstance,
1573+
},
1574+
neutronapi.Database,
1575+
instance.Namespace,
1576+
)
1577+
// create or patch the DB
1578+
ctrlResult, err := db.CreateOrPatchDBByName(
1579+
ctx,
1580+
h,
1581+
instance.Spec.DatabaseInstance,
1582+
)
1583+
if err != nil {
1584+
instance.Status.Conditions.Set(condition.FalseCondition(
1585+
condition.DBReadyCondition,
1586+
condition.ErrorReason,
1587+
condition.SeverityWarning,
1588+
condition.DBReadyErrorMessage,
1589+
err.Error()))
1590+
return db, ctrl.Result{}, err
1591+
}
1592+
if (ctrlResult != ctrl.Result{}) {
1593+
instance.Status.Conditions.Set(condition.FalseCondition(
1594+
condition.DBReadyCondition,
1595+
condition.RequestedReason,
1596+
condition.SeverityInfo,
1597+
condition.DBReadyRunningMessage))
1598+
return db, ctrlResult, nil
1599+
}
1600+
// wait for the DB to be setup
1601+
ctrlResult, err = db.WaitForDBCreatedWithTimeout(ctx, h, time.Second*5)
1602+
if err != nil {
1603+
instance.Status.Conditions.Set(condition.FalseCondition(
1604+
condition.DBReadyCondition,
1605+
condition.ErrorReason,
1606+
condition.SeverityWarning,
1607+
condition.DBReadyErrorMessage,
1608+
err.Error()))
1609+
return db, ctrlResult, err
1610+
}
1611+
if (ctrlResult != ctrl.Result{}) {
1612+
instance.Status.Conditions.Set(condition.FalseCondition(
1613+
condition.DBReadyCondition,
1614+
condition.RequestedReason,
1615+
condition.SeverityInfo,
1616+
condition.DBReadyRunningMessage))
1617+
return db, ctrlResult, nil
1618+
}
1619+
// update Status.DatabaseHostname, used to bootstrap/config the service
1620+
instance.Status.DatabaseHostname = db.GetDatabaseHostname()
1621+
instance.Status.Conditions.MarkTrue(condition.DBReadyCondition, condition.DBReadyMessage)
1622+
1623+
return db, ctrlResult, nil
1624+
}

templates/neutronapi/config/01-neutron.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ api_workers = 2
1313
rpc_workers = 1
1414

1515
[database]
16-
connection=mysql+pymysql://{{ .DbUser }}:{{ .DbPassword }}@{{ .DbHost }}/{{ .Db }}
16+
connection=mysql+pymysql://{{ .DbUser }}:{{ .DbPassword }}@{{ .DbHost }}/{{ .Db }}?read_default_file=/etc/my.cnf
1717
# NOTE(ykarel): It is required to be set for multi master galera, without it set
1818
# there can be reads from not up to date db instance and that leads to various issues.
1919
mysql_wsrep_sync_wait = 1

templates/neutronapi/config/db-sync-config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
"dest": "/etc/neutron/neutron.conf.d/02-neutron-custom.conf",
1313
"owner": "root:neutron",
1414
"perm": "0640"
15+
},
16+
{
17+
"source": "/var/lib/config-data/my.cnf",
18+
"dest": "/etc/my.cnf",
19+
"owner": "neutron",
20+
"perm": "0644"
1521
}
1622
]
1723
}

templates/neutronapi/config/neutron-api-config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
"dest": "/etc/neutron/neutron.conf.d/02-neutron-custom.conf",
1313
"owner": "root:neutron",
1414
"perm": "0640"
15+
},
16+
{
17+
"source": "/var/lib/config-data/my.cnf",
18+
"dest": "/etc/my.cnf",
19+
"owner": "neutron",
20+
"perm": "0644"
1521
}
1622
]
1723
}

test/functional/base_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/google/uuid"
2323
. "github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
2425
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/types"
@@ -165,3 +166,14 @@ func GetOVNDBCluster(name types.NamespacedName) *ovnv1.OVNDBCluster {
165166
}, timeout, interval).Should(Succeed())
166167
return instance
167168
}
169+
170+
func CreateNeutronAPISecret(namespace string, name string) *corev1.Secret {
171+
return th.CreateSecret(
172+
types.NamespacedName{Namespace: namespace, Name: name},
173+
map[string][]byte{
174+
"NeutronPassword": []byte("12345678"),
175+
"NeutronDatabasePassword": []byte("12345678"),
176+
"transport_url": []byte("rabbit://user@svc:1234"),
177+
},
178+
)
179+
}

0 commit comments

Comments
 (0)