Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for custom DB names #257

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions controllers/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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}"))

})

Expand Down Expand Up @@ -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}"))

})

Expand Down
9 changes: 8 additions & 1 deletion controllers/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to have a default fallback to trustyai_database to be backward compatible?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree

}

for _, key := range mandatoryKeys {
value, exists := secret.Data[key]
Expand Down
3 changes: 2 additions & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
9 changes: 7 additions & 2 deletions controllers/templates/service/deployment.tmpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading