diff --git a/controllers/deployment_test.go b/controllers/deployment_test.go index de80a1c4..0aa0f925 100644 --- a/controllers/deployment_test.go +++ b/controllers/deployment_test.go @@ -359,7 +359,7 @@ var _ = Describe("TrustyAI operator", func() { namespace := "trusty-ns-a-1-db" instance = createDefaultDBCustomResource(namespace) WaitFor(func() error { - secret := createDatabaseConfiguration(namespace, defaultDatabaseConfigurationName, "mysql") + secret := createDatabaseConfiguration(namespace, defaultDatabaseConfigurationName, "mysql", "trustyai_service") return k8sClient.Create(ctx, secret) }, "failed to create ConfigMap") setupAndTestDeploymentDefault(instance, namespace) @@ -368,7 +368,7 @@ var _ = Describe("TrustyAI operator", func() { namespace := "trusty-ns-a-1-db" instance = createDefaultDBCustomResource(namespace) WaitFor(func() error { - secret := createDatabaseConfiguration(namespace, defaultDatabaseConfigurationName, "mariadb") + secret := createDatabaseConfiguration(namespace, defaultDatabaseConfigurationName, "mariadb", "trustyai_service") return k8sClient.Create(ctx, secret) }, "failed to create ConfigMap") setupAndTestDeploymentDefault(instance, namespace) @@ -584,9 +584,16 @@ var _ = Describe("TrustyAI operator", func() { Expect(envVar.ValueFrom.SecretKeyRef.Name).To(Equal(defaultDatabaseConfigurationName), "Secret name does not match") Expect(envVar.ValueFrom.SecretKeyRef.Key).To(Equal("databasePort"), "Secret key does not match") + envVar = foundEnvVar(trustyaiServiceContainer.Env, "DATABASE_NAME") + Expect(envVar).NotTo(BeNil(), "Env var DATABASE_NAME not found") + Expect(envVar.ValueFrom).NotTo(BeNil(), "Env var DATABASE_NAME does not have ValueFrom set") + Expect(envVar.ValueFrom.SecretKeyRef).NotTo(BeNil(), "Env var DATABASE_NAME is not using SecretKeyRef") + Expect(envVar.ValueFrom.SecretKeyRef.Name).To(Equal(defaultDatabaseConfigurationName), "Secret name does not match") + Expect(envVar.ValueFrom.SecretKeyRef.Key).To(Equal("databaseName"), "Secret key does not match") + envVar = foundEnvVar(trustyaiServiceContainer.Env, "QUARKUS_DATASOURCE_JDBC_URL") Expect(envVar).NotTo(BeNil(), "Env var QUARKUS_DATASOURCE_JDBC_URL not found") - Expect(envVar.Value).To(Equal("jdbc:${QUARKUS_DATASOURCE_DB_KIND}://${DATABASE_SERVICE}:${DATABASE_PORT}/trustyai_database")) + Expect(envVar.Value).To(Equal("jdbc:${QUARKUS_DATASOURCE_DB_KIND}://${DATABASE_SERVICE}:${DATABASE_PORT}/${DATABASE_NAME}")) }) @@ -695,9 +702,16 @@ var _ = Describe("TrustyAI operator", func() { Expect(envVar.ValueFrom.SecretKeyRef.Name).To(Equal(defaultDatabaseConfigurationName), "Secret name does not match") Expect(envVar.ValueFrom.SecretKeyRef.Key).To(Equal("databasePort"), "Secret key does not match") + envVar = foundEnvVar(trustyaiServiceContainer.Env, "DATABASE_NAME") + Expect(envVar).NotTo(BeNil(), "Env var DATABASE_NAME not found") + Expect(envVar.ValueFrom).NotTo(BeNil(), "Env var DATABASE_NAME does not have ValueFrom set") + Expect(envVar.ValueFrom.SecretKeyRef).NotTo(BeNil(), "Env var DATABASE_NAME is not using SecretKeyRef") + Expect(envVar.ValueFrom.SecretKeyRef.Name).To(Equal(defaultDatabaseConfigurationName), "Secret name does not match") + Expect(envVar.ValueFrom.SecretKeyRef.Key).To(Equal("databaseName"), "Secret key does not match") + envVar = foundEnvVar(trustyaiServiceContainer.Env, "QUARKUS_DATASOURCE_JDBC_URL") Expect(envVar).NotTo(BeNil(), "Env var QUARKUS_DATASOURCE_JDBC_URL not found") - Expect(envVar.Value).To(Equal("jdbc:${QUARKUS_DATASOURCE_DB_KIND}://${DATABASE_SERVICE}:${DATABASE_PORT}/trustyai_database")) + Expect(envVar.Value).To(Equal("jdbc:${QUARKUS_DATASOURCE_DB_KIND}://${DATABASE_SERVICE}:${DATABASE_PORT}/${DATABASE_NAME}")) }) diff --git a/controllers/secrets.go b/controllers/secrets.go index 090ab58d..499d768b 100644 --- a/controllers/secrets.go +++ b/controllers/secrets.go @@ -53,7 +53,14 @@ func (r *TrustyAIServiceReconciler) findDatabaseSecret(ctx context.Context, inst // validateDatabaseSecret validates the DB configuration secret func (r *TrustyAIServiceReconciler) validateDatabaseSecret(secret *corev1.Secret) error { - mandatoryKeys := []string{"databaseKind", "databaseUsername", "databasePassword", "databaseService", "databasePort"} + mandatoryKeys := []string{ + "databaseKind", + "databaseUsername", + "databasePassword", + "databaseService", + "databasePort", + "databaseName", + } for _, key := range mandatoryKeys { value, exists := secret.Data[key] diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 7ee75a93..e61f4ac4 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -215,13 +215,14 @@ func createSecret(namespace string, secretName string, data map[string]string) * } } -func createDatabaseConfiguration(namespace string, name string, dbKind string) *corev1.Secret { +func createDatabaseConfiguration(namespace string, name string, dbKind string, databaseName string) *corev1.Secret { return createSecret(namespace, name, map[string]string{ "databaseKind": dbKind, "databaseUsername": "foo", "databasePassword": "bar", "databaseService": "mariadb-service", "databasePort": "3306", + "databaseName": databaseName, }) } diff --git a/controllers/templates/service/deployment.tmpl.yaml b/controllers/templates/service/deployment.tmpl.yaml index da69b24e..f1e429f1 100644 --- a/controllers/templates/service/deployment.tmpl.yaml +++ b/controllers/templates/service/deployment.tmpl.yaml @@ -93,11 +93,16 @@ spec: secretKeyRef: name: {{ .Instance.Spec.Storage.DatabaseConfigurations }} key: databasePort + - name: DATABASE_NAME + valueFrom: + secretKeyRef: + name: {{ .Instance.Spec.Storage.DatabaseConfigurations }} + key: databaseName - name: QUARKUS_DATASOURCE_JDBC_URL {{ if .UseDBTLSCerts }} - value: "jdbc:${QUARKUS_DATASOURCE_DB_KIND}://${DATABASE_SERVICE}:${DATABASE_PORT}/trustyai_database?sslMode=verify-ca&serverSslCert=/etc/tls/db/tls.crt" + value: "jdbc:${QUARKUS_DATASOURCE_DB_KIND}://${DATABASE_SERVICE}:${DATABASE_PORT}/${DATABASE_NAME}?sslMode=verify-ca&serverSslCert=/etc/tls/db/tls.crt" {{ else }} - value: "jdbc:${QUARKUS_DATASOURCE_DB_KIND}://${DATABASE_SERVICE}:${DATABASE_PORT}/trustyai_database" + value: "jdbc:${QUARKUS_DATASOURCE_DB_KIND}://${DATABASE_SERVICE}:${DATABASE_PORT}/${DATABASE_NAME}" {{ end }} - name: SERVICE_DATA_FORMAT value: "HIBERNATE"