Skip to content

Commit 3ce542d

Browse files
authored
Improve Readability of Names (#245)
* Attempt at using human readable names * More work on proper human readable names * Add UID suffix to make database names unique even if descriptions are identical * Add required UID in test data * Add required UID in test data * Use the visible ID, not the internal one, when generating the databsae name * godocs * Update comment * Refactoring
1 parent 74991b2 commit 3ce542d

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

api/v1/postgres_types.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"reflect"
1212
"strconv"
13+
"strings"
1314

1415
"regexp"
1516

@@ -55,7 +56,7 @@ const (
5556
// SharedBufferParameterKey defines the key under which the shared buffer size is stored in the parameters map. Defined by the postgres-operator/patroni
5657
SharedBufferParameterKey = "shared_buffers"
5758

58-
teamIDPrefix = "db"
59+
teamIDPrefix = "pg"
5960
)
6061

6162
var (
@@ -360,11 +361,15 @@ func (p *Postgres) ToUserPasswordSecretMatchingLabels() map[string]string {
360361

361362
func (p *Postgres) generateTeamID() string {
362363
// We only want letters and numbers
363-
generatedTeamID := alphaNumericRegExp.ReplaceAllString(p.Spec.ProjectID, "")
364+
generatedTeamID := alphaNumericRegExp.ReplaceAllString(p.Spec.Tenant, "")
364365

365-
// Prefix `db` to make sure the string is a valid dns entry (aka does not start with a number)
366+
// Add prefix to make sure the string is a valid dns entry (aka does not start with a number).
367+
// Also acts as minimal teamID in case the Tenant does not contain any alphanumeric characters.
366368
generatedTeamID = teamIDPrefix + generatedTeamID
367369

370+
// and only lower case
371+
generatedTeamID = strings.ToLower(generatedTeamID)
372+
368373
// Limit size
369374
maxLen := 16
370375
if len(generatedTeamID) > maxLen {
@@ -376,10 +381,23 @@ func (p *Postgres) generateTeamID() string {
376381

377382
func (p *Postgres) generateDatabaseName() string {
378383
// We only want letters and numbers
379-
generatedDatabaseName := alphaNumericRegExp.ReplaceAllString(string(p.Name), "")
384+
generatedDatabaseName := alphaNumericRegExp.ReplaceAllString(string(p.Spec.Description), "")
380385

381-
// Limit size
382-
maxLen := 20
386+
// and only lower case
387+
generatedDatabaseName = strings.ToLower(generatedDatabaseName)
388+
389+
// Limit the length of the description part of the name
390+
maxLen := 15
391+
if len(generatedDatabaseName) > maxLen {
392+
generatedDatabaseName = generatedDatabaseName[:maxLen]
393+
}
394+
395+
// Add UID in the mix
396+
generatedDatabaseName += alphaNumericRegExp.ReplaceAllString(string(p.Name), "")
397+
398+
// Limit to final size
399+
// This way, we have at least 5 chars of the uid as part of the database name.
400+
maxLen = 20
383401
if len(generatedDatabaseName) > maxLen {
384402
generatedDatabaseName = generatedDatabaseName[:maxLen]
385403
}
@@ -388,8 +406,25 @@ func (p *Postgres) generateDatabaseName() string {
388406
}
389407

390408
func (p *Postgres) ToPeripheralResourceNamespace() string {
391-
// as we have one namespace per database, we simplify things by also using the database name as namespace name
392-
return p.ToPeripheralResourceName()
409+
// We only want letters and numbers
410+
projectID := alphaNumericRegExp.ReplaceAllString(p.Spec.ProjectID, "")
411+
412+
// Limit size
413+
maxLen := 16
414+
if len(projectID) > maxLen {
415+
projectID = projectID[:maxLen]
416+
}
417+
418+
// We only want letters and numbers
419+
name := alphaNumericRegExp.ReplaceAllString(string(p.Name), "")
420+
421+
// Limit size
422+
maxLen = 20
423+
if len(name) > maxLen {
424+
name = name[:maxLen]
425+
}
426+
427+
return projectID + "-" + name
393428
}
394429

395430
func (p *Postgres) ToPeripheralResourceLookupKey() types.NamespacedName {

api/v1/postgres_types_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"regexp"
1111
"testing"
1212

13+
"github.com/google/uuid"
1314
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/apimachinery/pkg/types"
1416
)
1517

1618
func Test_setSharedBufferSize(t *testing.T) {
@@ -195,6 +197,7 @@ func TestPostgres_ToPeripheralResourceName(t *testing.T) {
195197
p := &Postgres{
196198
ObjectMeta: v1.ObjectMeta{
197199
Name: tt.postgresName,
200+
UID: types.UID(uuid.NewString()),
198201
},
199202
Spec: PostgresSpec{
200203
ProjectID: tt.projectID,

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.16
44

55
require (
66
github.com/go-logr/logr v0.4.0
7+
github.com/google/uuid v1.2.0 // indirect
78
github.com/metal-stack/firewall-controller v1.0.9
89
github.com/metal-stack/v v1.0.3
910
github.com/onsi/ginkgo v1.16.4

pkg/operatormanager/operatormanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ func (m *OperatorManager) createOrUpdateExporterSidecarService(ctx context.Conte
642642
return nil
643643
}
644644

645-
// createOrUpdateExporterSidecarService ensures the neccessary services to acces the sidecars exist
645+
// createOrUpdateExporterSidecarServiceMonitor ensures the servicemonitors for the sidecars exist
646646
func (m *OperatorManager) createOrUpdateExporterSidecarServiceMonitor(ctx context.Context, namespace string) error {
647647

648648
pesm := &coreosv1.ServiceMonitor{}

0 commit comments

Comments
 (0)