Skip to content

Commit 6010e3b

Browse files
committed
[tlse] TLS database connection
The my.cnf file gets added to the secret holding the service configs. The content of my.cnf is centrally managed in the mariadb-operator and retrieved calling db.GetDatabaseClientConfig(tlsCfg) Depends-On: openstack-k8s-operators/mariadb-operator#190 Depends-On: openstack-k8s-operators/mariadb-operator#191 Jira: OSPRH-4547
1 parent 79ba7c1 commit 6010e3b

File tree

7 files changed

+156
-66
lines changed

7 files changed

+156
-66
lines changed

controllers/cinder_controller.go

Lines changed: 92 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
common_rbac "github.com/openstack-k8s-operators/lib-common/modules/common/rbac"
5252
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
5353
"github.com/openstack-k8s-operators/lib-common/modules/common/service"
54+
"github.com/openstack-k8s-operators/lib-common/modules/common/tls"
5455
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
5556
mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
5657

@@ -380,63 +381,6 @@ func (r *CinderReconciler) reconcileInit(
380381

381382
Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name))
382383

383-
//
384-
// create service DB instance
385-
//
386-
db := mariadbv1.NewDatabase(
387-
instance.Name,
388-
instance.Spec.DatabaseUser,
389-
instance.Spec.Secret,
390-
map[string]string{
391-
"dbName": instance.Spec.DatabaseInstance,
392-
},
393-
)
394-
// create or patch the DB
395-
ctrlResult, err := db.CreateOrPatchDB(
396-
ctx,
397-
helper,
398-
)
399-
if err != nil {
400-
instance.Status.Conditions.Set(condition.FalseCondition(
401-
condition.DBReadyCondition,
402-
condition.ErrorReason,
403-
condition.SeverityWarning,
404-
condition.DBReadyErrorMessage,
405-
err.Error()))
406-
return ctrl.Result{}, err
407-
}
408-
if (ctrlResult != ctrl.Result{}) {
409-
instance.Status.Conditions.Set(condition.FalseCondition(
410-
condition.DBReadyCondition,
411-
condition.RequestedReason,
412-
condition.SeverityInfo,
413-
condition.DBReadyRunningMessage))
414-
return ctrlResult, nil
415-
}
416-
// wait for the DB to be setup
417-
ctrlResult, err = db.WaitForDBCreated(ctx, helper)
418-
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()))
425-
return ctrlResult, err
426-
}
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-
// update Status.DatabaseHostname, used to config the service
436-
instance.Status.DatabaseHostname = db.GetDatabaseHostname()
437-
instance.Status.Conditions.MarkTrue(condition.DBReadyCondition, condition.DBReadyMessage)
438-
// create service DB - end
439-
440384
//
441385
// run Cinder db sync
442386
//
@@ -450,7 +394,7 @@ func (r *CinderReconciler) reconcileInit(
450394
time.Duration(5)*time.Second,
451395
dbSyncHash,
452396
)
453-
ctrlResult, err = dbSyncjob.DoJob(
397+
ctrlResult, err := dbSyncjob.DoJob(
454398
ctx,
455399
helper,
456400
)
@@ -615,10 +559,17 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder
615559
instance.Status.Conditions.MarkTrue(condition.InputReadyCondition, condition.InputReadyMessage)
616560
// run check OpenStack secret - end
617561

562+
db, result, err := r.ensureDB(ctx, helper, instance)
563+
if err != nil {
564+
return ctrl.Result{}, err
565+
} else if (result != ctrl.Result{}) {
566+
return result, nil
567+
}
568+
618569
//
619570
// Create Secrets required as input for the Service and calculate an overall hash of hashes
620571
//
621-
err = r.generateServiceConfigs(ctx, helper, instance, &configVars, serviceLabels, memcached)
572+
err = r.generateServiceConfigs(ctx, helper, instance, &configVars, serviceLabels, memcached, db)
622573
if err != nil {
623574
instance.Status.Conditions.Set(condition.FalseCondition(
624575
condition.ServiceConfigReadyCondition,
@@ -911,6 +862,7 @@ func (r *CinderReconciler) generateServiceConfigs(
911862
envVars *map[string]env.Setter,
912863
serviceLabels map[string]string,
913864
memcached *memcachedv1.Memcached,
865+
db *mariadbv1.Database,
914866
) error {
915867
//
916868
// create Secret required for cinder input
@@ -920,8 +872,20 @@ func (r *CinderReconciler) generateServiceConfigs(
920872

921873
labels := labels.GetLabels(instance, labels.GetGroupLabel(cinder.ServiceName), serviceLabels)
922874

875+
db, err := mariadbv1.GetDatabaseByName(ctx, h, cinder.DatabaseName)
876+
if err != nil {
877+
return err
878+
}
879+
880+
var tlsCfg *tls.Service
881+
if instance.Spec.CinderAPI.TLS.Ca.CaBundleSecretName != "" {
882+
tlsCfg = &tls.Service{}
883+
}
923884
// customData hold any customization for all cinder services.
924-
customData := map[string]string{cinder.CustomConfigFileName: instance.Spec.CustomServiceConfig}
885+
customData := map[string]string{
886+
cinder.CustomConfigFileName: instance.Spec.CustomServiceConfig,
887+
cinder.MyCnfFileName: db.GetDatabaseClientConfig(tlsCfg), //(mschuppert) for now just get the default my.cnf
888+
}
925889

926890
keystoneAPI, err := keystonev1.GetKeystoneAPI(ctx, h, instance.Namespace, map[string]string{})
927891
if err != nil {
@@ -952,7 +916,7 @@ func (r *CinderReconciler) generateServiceConfigs(
952916
templateParameters["KeystoneInternalURL"] = keystoneInternalURL
953917
templateParameters["KeystonePublicURL"] = keystonePublicURL
954918
templateParameters["TransportURL"] = string(transportURLSecret.Data["transport_url"])
955-
templateParameters["DatabaseConnection"] = fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s",
919+
templateParameters["DatabaseConnection"] = fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s?read_default_file=/etc/my.cnf",
956920
instance.Spec.DatabaseUser,
957921
string(ospSecret.Data[instance.Spec.PasswordSelectors.Database]),
958922
instance.Status.DatabaseHostname,
@@ -1272,3 +1236,70 @@ func (r *CinderReconciler) volumeCleanupDeployments(ctx context.Context, instanc
12721236

12731237
return nil
12741238
}
1239+
1240+
func (r *CinderReconciler) ensureDB(
1241+
ctx context.Context,
1242+
h *helper.Helper,
1243+
instance *cinderv1beta1.Cinder,
1244+
) (*mariadbv1.Database, ctrl.Result, error) {
1245+
//
1246+
// create service DB instance
1247+
//
1248+
db := mariadbv1.NewDatabase(
1249+
instance.Name,
1250+
instance.Spec.DatabaseUser,
1251+
instance.Spec.Secret,
1252+
map[string]string{
1253+
"dbName": instance.Spec.DatabaseInstance,
1254+
},
1255+
)
1256+
1257+
// create or patch the DB
1258+
ctrlResult, err := db.CreateOrPatchDBByName(
1259+
ctx,
1260+
h,
1261+
instance.Spec.DatabaseInstance,
1262+
)
1263+
if err != nil {
1264+
instance.Status.Conditions.Set(condition.FalseCondition(
1265+
condition.DBReadyCondition,
1266+
condition.ErrorReason,
1267+
condition.SeverityWarning,
1268+
condition.DBReadyErrorMessage,
1269+
err.Error()))
1270+
return db, ctrl.Result{}, err
1271+
}
1272+
if (ctrlResult != ctrl.Result{}) {
1273+
instance.Status.Conditions.Set(condition.FalseCondition(
1274+
condition.DBReadyCondition,
1275+
condition.RequestedReason,
1276+
condition.SeverityInfo,
1277+
condition.DBReadyRunningMessage))
1278+
return db, ctrlResult, nil
1279+
}
1280+
// wait for the DB to be setup
1281+
// (ksambor) should we use WaitForDBCreatedWithTimeout instead?
1282+
ctrlResult, err = db.WaitForDBCreated(ctx, h)
1283+
if err != nil {
1284+
instance.Status.Conditions.Set(condition.FalseCondition(
1285+
condition.DBReadyCondition,
1286+
condition.ErrorReason,
1287+
condition.SeverityWarning,
1288+
condition.DBReadyErrorMessage,
1289+
err.Error()))
1290+
return db, ctrlResult, err
1291+
}
1292+
if (ctrlResult != ctrl.Result{}) {
1293+
instance.Status.Conditions.Set(condition.FalseCondition(
1294+
condition.DBReadyCondition,
1295+
condition.RequestedReason,
1296+
condition.SeverityInfo,
1297+
condition.DBReadyRunningMessage))
1298+
return db, ctrlResult, nil
1299+
}
1300+
1301+
// update Status.DatabaseHostname, used to config the service
1302+
instance.Status.DatabaseHostname = db.GetDatabaseHostname()
1303+
instance.Status.Conditions.MarkTrue(condition.DBReadyCondition, condition.DBReadyMessage)
1304+
return db, ctrlResult, nil
1305+
}

pkg/cinder/const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const (
4141
CustomServiceConfigFileName = "03-service-custom.conf"
4242
// CustomServiceConfigSecretsFileName -
4343
CustomServiceConfigSecretsFileName = "04-service-custom-secrets.conf"
44+
// MyCnfFileName -
45+
MyCnfFileName = "my.cnf"
4446

4547
// CinderPublicPort -
4648
CinderPublicPort int32 = 8776

pkg/cinder/cronjob.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cinder
1717

1818
import (
1919
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
20+
cinderv1beta1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
2021

2122
"fmt"
2223

@@ -75,6 +76,12 @@ func CronJob(
7576
MountPath: "/etc/cinder/cinder.conf.d",
7677
ReadOnly: true,
7778
},
79+
{
80+
Name: "config-data",
81+
MountPath: "/etc/my.cnf",
82+
SubPath: MyCnfFileName,
83+
ReadOnly: true,
84+
},
7885
}
7986

8087
// add CA cert if defined
@@ -83,6 +90,8 @@ func CronJob(
8390
cronJobVolumeMounts = append(cronJobVolumeMounts, instance.Spec.CinderAPI.TLS.CreateVolumeMounts(nil)...)
8491
}
8592

93+
cronJobExtraMounts := []cinderv1beta1.CinderExtraVolMounts{}
94+
8695
cronjob := &batchv1.CronJob{
8796
ObjectMeta: metav1.ObjectMeta{
8897
Name: fmt.Sprintf("%s-db-purge", ServiceName),
@@ -122,7 +131,7 @@ func CronJob(
122131
},
123132
},
124133
},
125-
Volumes: cronJobVolumes,
134+
Volumes: append(GetVolumes(instance.Name, false, cronJobExtraMounts, DbsyncPropagation), cronJobVolumes...),
126135
RestartPolicy: corev1.RestartPolicyNever,
127136
ServiceAccountName: instance.RbacResourceName(),
128137
},

pkg/cinder/volumes.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ func GetVolumeMounts(storageSvc bool, extraVol []cinderv1beta1.CinderExtraVolMou
172172
MountPath: "/var/lib/config-data/merged",
173173
ReadOnly: true,
174174
},
175+
{
176+
Name: "config-data",
177+
MountPath: "/etc/my.cnf",
178+
SubPath: MyCnfFileName,
179+
ReadOnly: true,
180+
},
175181
}
176182

177183
// Volume and backup services require extra directories

test/functional/cinder_controller_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/utils/ptr"
2626

2727
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
28+
"github.com/openstack-k8s-operators/cinder-operator/pkg/cinder"
2829
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
2930
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
3031
util "github.com/openstack-k8s-operators/lib-common/modules/common/util"
@@ -218,13 +219,17 @@ var _ = Describe("Cinder controller", func() {
218219
infra.SimulateTransportURLReady(cinderTest.CinderTransportURL)
219220
DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, "memcached", memcachedSpec))
220221
infra.SimulateMemcachedReady(cinderTest.CinderMemcached)
222+
mariadb.SimulateMariaDBAccountCompleted(cinderTest.Instance)
223+
mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Instance)
221224
})
222225
It("should create config-data and scripts ConfigMaps", func() {
223226
keystoneAPI := keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)
224227
DeferCleanup(keystone.DeleteKeystoneAPI, keystoneAPI)
225-
Eventually(func() corev1.Secret {
226-
return th.GetSecret(cinderTest.CinderConfigSecret)
227-
}, timeout, interval).ShouldNot(BeNil())
228+
cf := th.GetSecret(cinderTest.CinderConfigSecret)
229+
Expect(cf).ShouldNot(BeNil())
230+
conf := cf.Data[cinder.MyCnfFileName]
231+
Expect(conf).To(
232+
ContainSubstring("[client]\nssl=0"))
228233
Eventually(func() corev1.Secret {
229234
return th.GetSecret(cinderTest.CinderConfigScripts)
230235
}, timeout, interval).ShouldNot(BeNil())
@@ -461,7 +466,7 @@ var _ = Describe("Cinder controller", func() {
461466
infra.SimulateMemcachedReady(cinderTest.CinderMemcached)
462467
DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace))
463468
mariadb.SimulateMariaDBAccountCompleted(cinderTest.Instance)
464-
mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Instance)
469+
mariadb.SimulateMariaDBTLSDatabaseCompleted(cinderTest.Instance)
465470
th.SimulateJobSuccess(cinderTest.CinderDBSync)
466471
})
467472

@@ -510,6 +515,19 @@ var _ = Describe("Cinder controller", func() {
510515
)
511516
})
512517

518+
It("should create config-data and scripts ConfigMaps", func() {
519+
keystoneAPI := keystone.CreateKeystoneAPI(cinderTest.Instance.Namespace)
520+
DeferCleanup(keystone.DeleteKeystoneAPI, keystoneAPI)
521+
cf := th.GetSecret(cinderTest.CinderConfigSecret)
522+
Expect(cf).ShouldNot(BeNil())
523+
conf := cf.Data[cinder.MyCnfFileName]
524+
Expect(conf).To(
525+
ContainSubstring("[client]\nssl-ca=/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem\nssl=1"))
526+
Eventually(func() corev1.Secret {
527+
return th.GetSecret(cinderTest.CinderConfigScripts)
528+
}, timeout, interval).ShouldNot(BeNil())
529+
})
530+
513531
It("Creates CinderAPI", func() {
514532
DeferCleanup(k8sClient.Delete, ctx, th.CreateCABundleSecret(cinderTest.CABundleSecret))
515533
DeferCleanup(k8sClient.Delete, ctx, th.CreateCertSecret(cinderTest.InternalCertSecret))

test/kuttl/common/assert_sample_deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ spec:
121121
- mountPath: /var/lib/config-data/merged
122122
name: config-data
123123
readOnly: true
124+
- mountPath: /etc/my.cnf
125+
name: config-data
126+
readOnly: true
127+
subPath: my.cnf
124128
- mountPath: /etc/cinder/cinder.conf.d
125129
name: config-data-custom
126130
readOnly: true

test/kuttl/common/assert_tls_sample_deployment.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ spec:
8383
- mountPath: /var/lib/config-data/merged
8484
name: config-data
8585
readOnly: true
86+
- mountPath: /etc/my.cnf
87+
name: config-data
88+
readOnly: true
89+
subPath: my.cnf
8690
- mountPath: /etc/cinder/cinder.conf.d
8791
name: config-data-custom
8892
readOnly: true
@@ -175,6 +179,10 @@ spec:
175179
- mountPath: /var/lib/config-data/merged
176180
name: config-data
177181
readOnly: true
182+
- mountPath: /etc/my.cnf
183+
name: config-data
184+
readOnly: true
185+
subPath: my.cnf
178186
- mountPath: /etc/cinder/cinder.conf.d
179187
name: config-data-custom
180188
readOnly: true
@@ -203,6 +211,10 @@ spec:
203211
- mountPath: /var/lib/config-data/merged
204212
name: config-data
205213
readOnly: true
214+
- mountPath: /etc/my.cnf
215+
name: config-data
216+
readOnly: true
217+
subPath: my.cnf
206218
- mountPath: /etc/cinder/cinder.conf.d
207219
name: config-data-custom
208220
readOnly: true
@@ -267,6 +279,10 @@ spec:
267279
- mountPath: /var/lib/config-data/merged
268280
name: config-data
269281
readOnly: true
282+
- mountPath: /etc/my.cnf
283+
name: config-data
284+
readOnly: true
285+
subPath: my.cnf
270286
- mountPath: /var/lib/cinder
271287
name: var-lib-cinder
272288
- mountPath: /etc/nvme
@@ -315,6 +331,10 @@ spec:
315331
- mountPath: /var/lib/config-data/merged
316332
name: config-data
317333
readOnly: true
334+
- mountPath: /etc/my.cnf
335+
name: config-data
336+
readOnly: true
337+
subPath: my.cnf
318338
- mountPath: /var/lib/cinder
319339
name: var-lib-cinder
320340
- mountPath: /etc/nvme

0 commit comments

Comments
 (0)