Skip to content

Commit

Permalink
Improve Readability of Names (#245)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
eberlep authored Aug 9, 2021
1 parent 74991b2 commit 3ce542d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
51 changes: 43 additions & 8 deletions api/v1/postgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"reflect"
"strconv"
"strings"

"regexp"

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

teamIDPrefix = "db"
teamIDPrefix = "pg"
)

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

func (p *Postgres) generateTeamID() string {
// We only want letters and numbers
generatedTeamID := alphaNumericRegExp.ReplaceAllString(p.Spec.ProjectID, "")
generatedTeamID := alphaNumericRegExp.ReplaceAllString(p.Spec.Tenant, "")

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

// and only lower case
generatedTeamID = strings.ToLower(generatedTeamID)

// Limit size
maxLen := 16
if len(generatedTeamID) > maxLen {
Expand All @@ -376,10 +381,23 @@ func (p *Postgres) generateTeamID() string {

func (p *Postgres) generateDatabaseName() string {
// We only want letters and numbers
generatedDatabaseName := alphaNumericRegExp.ReplaceAllString(string(p.Name), "")
generatedDatabaseName := alphaNumericRegExp.ReplaceAllString(string(p.Spec.Description), "")

// Limit size
maxLen := 20
// and only lower case
generatedDatabaseName = strings.ToLower(generatedDatabaseName)

// Limit the length of the description part of the name
maxLen := 15
if len(generatedDatabaseName) > maxLen {
generatedDatabaseName = generatedDatabaseName[:maxLen]
}

// Add UID in the mix
generatedDatabaseName += alphaNumericRegExp.ReplaceAllString(string(p.Name), "")

// Limit to final size
// This way, we have at least 5 chars of the uid as part of the database name.
maxLen = 20
if len(generatedDatabaseName) > maxLen {
generatedDatabaseName = generatedDatabaseName[:maxLen]
}
Expand All @@ -388,8 +406,25 @@ func (p *Postgres) generateDatabaseName() string {
}

func (p *Postgres) ToPeripheralResourceNamespace() string {
// as we have one namespace per database, we simplify things by also using the database name as namespace name
return p.ToPeripheralResourceName()
// We only want letters and numbers
projectID := alphaNumericRegExp.ReplaceAllString(p.Spec.ProjectID, "")

// Limit size
maxLen := 16
if len(projectID) > maxLen {
projectID = projectID[:maxLen]
}

// We only want letters and numbers
name := alphaNumericRegExp.ReplaceAllString(string(p.Name), "")

// Limit size
maxLen = 20
if len(name) > maxLen {
name = name[:maxLen]
}

return projectID + "-" + name
}

func (p *Postgres) ToPeripheralResourceLookupKey() types.NamespacedName {
Expand Down
3 changes: 3 additions & 0 deletions api/v1/postgres_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"regexp"
"testing"

"github.com/google/uuid"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func Test_setSharedBufferSize(t *testing.T) {
Expand Down Expand Up @@ -195,6 +197,7 @@ func TestPostgres_ToPeripheralResourceName(t *testing.T) {
p := &Postgres{
ObjectMeta: v1.ObjectMeta{
Name: tt.postgresName,
UID: types.UID(uuid.NewString()),
},
Spec: PostgresSpec{
ProjectID: tt.projectID,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/go-logr/logr v0.4.0
github.com/google/uuid v1.2.0 // indirect
github.com/metal-stack/firewall-controller v1.0.9
github.com/metal-stack/v v1.0.3
github.com/onsi/ginkgo v1.16.4
Expand Down
2 changes: 1 addition & 1 deletion pkg/operatormanager/operatormanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func (m *OperatorManager) createOrUpdateExporterSidecarService(ctx context.Conte
return nil
}

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

pesm := &coreosv1.ServiceMonitor{}
Expand Down

0 comments on commit 3ce542d

Please sign in to comment.