fix: default values for schema and username (#941)

Signed-off-by: Dario Tranchitella <dario@tranchitella.eu>
This commit is contained in:
Dario Tranchitella
2025-09-05 15:32:10 +02:00
committed by GitHub
parent 72f32aba19
commit e2a0648989
4 changed files with 33 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"net"
"strconv"
"strings"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
@@ -95,3 +96,17 @@ func getLoadBalancerAddress(ingress []corev1.LoadBalancerIngress) (string, error
return "", kamajierrors.MissingValidIPError{}
}
func (in *TenantControlPlane) normalizeNamespaceName() string {
// The dash character (-) must be replaced with an underscore, PostgreSQL is complaining about it:
// https://github.com/clastix/kamaji/issues/328
return strings.ReplaceAll(fmt.Sprintf("%s_%s", in.GetNamespace(), in.GetName()), "-", "_")
}
func (in *TenantControlPlane) GetDefaultDatastoreUsername() string {
return in.normalizeNamespaceName()
}
func (in *TenantControlPlane) GetDefaultDatastoreSchema() string {
return in.normalizeNamespaceName()
}

View File

@@ -5,7 +5,6 @@ package datastore
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/pkg/errors"
@@ -163,8 +162,7 @@ func (r *Config) mutate(ctx context.Context, tenantControlPlane *kamajiv1alpha1.
// or defaulted by the defaulting webhook
username = []byte(tenantControlPlane.Spec.DataStoreUsername)
default:
// this can only happen on TCP creations when the webhook is not installed
return fmt.Errorf("cannot build datastore storage config, username must either exist in Spec or Status")
username = []byte(tenantControlPlane.GetDefaultDatastoreUsername())
}
}
@@ -180,8 +178,7 @@ func (r *Config) mutate(ctx context.Context, tenantControlPlane *kamajiv1alpha1.
// or defaulted by the defaulting webhook
dataStoreSchema = tenantControlPlane.Spec.DataStoreSchema
default:
// this can only happen on TCP creations when the webhook is not installed
return fmt.Errorf("cannot build datastore storage config, schema name must either exist in Spec or Status")
dataStoreSchema = tenantControlPlane.GetDefaultDatastoreSchema()
}
r.resource.Data = map[string][]byte{

View File

@@ -5,6 +5,7 @@ package datastore_test
import (
"context"
"fmt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
@@ -60,10 +61,20 @@ var _ = Describe("DatastoreStorageConfig", func() {
}
})
When("TCP has neither dataStoreSchema nor dataStoreUsername defined", func() {
When("TCP has neither dataStoreSchema nor dataStoreUsername defined, fallback to default value", func() {
It("should return an error", func() {
_, err := resources.Handle(ctx, dsc, tcp)
Expect(err).To(HaveOccurred())
op, err := resources.Handle(ctx, dsc, tcp)
Expect(err).ToNot(HaveOccurred())
Expect(op).To(Equal(controllerutil.OperationResultCreated))
var secrets corev1.SecretList
Expect(fakeClient.List(ctx, &secrets)).To(Succeed())
Expect(secrets.Items).To(HaveLen(1))
expectedValue := []byte(fmt.Sprintf("%s_%s", tcp.Namespace, tcp.Name))
Expect(secrets.Items[0].Data["DB_SCHEMA"]).To(Equal(expectedValue))
Expect(secrets.Items[0].Data["DB_USER"]).To(Equal(expectedValue))
})
})

View File

@@ -5,9 +5,7 @@ package handlers
import (
"context"
"fmt"
"net"
"strings"
"github.com/pkg/errors"
"gomodules.xyz/jsonpatch/v2"
@@ -73,14 +71,10 @@ func (t TenantControlPlaneDefaults) defaultUnsetFields(tcp *kamajiv1alpha1.Tenan
}
if len(tcp.Spec.DataStoreSchema) == 0 {
dss := strings.ReplaceAll(fmt.Sprintf("%s_%s", tcp.GetNamespace(), tcp.GetName()), "-", "_")
tcp.Spec.DataStoreSchema = dss
tcp.Spec.DataStoreSchema = tcp.GetDefaultDatastoreSchema()
}
if len(tcp.Spec.DataStoreUsername) == 0 {
// The dash character (-) must be replaced with an underscore, PostgreSQL is complaining about it:
// https://github.com/clastix/kamaji/issues/328
username := strings.ReplaceAll(fmt.Sprintf("%s_%s", tcp.GetNamespace(), tcp.GetName()), "-", "_")
tcp.Spec.DataStoreUsername = username
tcp.Spec.DataStoreUsername = tcp.GetDefaultDatastoreUsername()
}
}