From dd2ed5ff9d283382355b7e9421be45f4c70b1f3a Mon Sep 17 00:00:00 2001 From: Murat Kabilov Date: Wed, 12 Apr 2017 10:40:40 +0200 Subject: [PATCH] Add team name to tpr object metadata name --- manifests/postgres-operator.yaml | 4 +++- pkg/cluster/k8sres.go | 3 +-- pkg/cluster/util.go | 8 ++++++++ pkg/spec/postgresql.go | 19 +++++++++++++++++++ pkg/util/config/config.go | 1 + pkg/util/util.go | 4 ---- 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/manifests/postgres-operator.yaml b/manifests/postgres-operator.yaml index cd73586b0..6daf66efd 100644 --- a/manifests/postgres-operator.yaml +++ b/manifests/postgres-operator.yaml @@ -55,4 +55,6 @@ spec: - name: PGOP_DOCKER_IMAGE value: "registry.opensource.zalan.do/acid/spilo-9.6:1.2-p12" - name: PGOP_DB_HOSTED_ZONE - value: "db.example.com" \ No newline at end of file + value: "db.example.com" + - name: PGOP_DNS_NAME_FORMAT + value: "%s.%s.staging.%s" \ No newline at end of file diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index cc11c3298..dc18d7043 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -9,7 +9,6 @@ import ( "k8s.io/client-go/pkg/util/intstr" "github.bus.zalan.do/acid/postgres-operator/pkg/spec" - "github.bus.zalan.do/acid/postgres-operator/pkg/util" "github.bus.zalan.do/acid/postgres-operator/pkg/util/constants" ) @@ -243,7 +242,7 @@ func (c *Cluster) genService(allowedSourceRanges []string) *v1.Service { Namespace: c.Metadata.Namespace, Labels: c.labelsSet(), Annotations: map[string]string{ - constants.ZalandoDnsNameAnnotation: util.ClusterDNSName(c.Metadata.Name, c.TeamName(), c.OpConfig.DbHostedZone), + constants.ZalandoDnsNameAnnotation: c.dnsName(), }, }, Spec: v1.ServiceSpec{ diff --git a/pkg/cluster/util.go b/pkg/cluster/util.go index c37edca0d..931b6585d 100644 --- a/pkg/cluster/util.go +++ b/pkg/cluster/util.go @@ -209,6 +209,14 @@ func (c *Cluster) labelsSet() labels.Set { } } +func (c *Cluster) dnsName() string { + return strings.ToLower(fmt.Sprintf( + c.OpConfig.DNSFormat, + c.Spec.ClusterName, + c.TeamName(), + c.OpConfig.DbHostedZone)) +} + func (c *Cluster) credentialSecretName(username string) string { return fmt.Sprintf(constants.UserSecretTemplate, username, diff --git a/pkg/spec/postgresql.go b/pkg/spec/postgresql.go index 1f56ca92d..de06ce77d 100644 --- a/pkg/spec/postgresql.go +++ b/pkg/spec/postgresql.go @@ -79,6 +79,7 @@ type PostgresSpec struct { NumberOfInstances int32 `json:"numberOfInstances"` Users map[string]UserFlags `json:"users"` MaintenanceWindows []MaintenanceWindow `json:"maintenanceWindows,omitempty"` + ClusterName string `json:"-"` } type PostgresqlList struct { @@ -190,6 +191,18 @@ func (pl *PostgresqlList) GetListMeta() unversioned.List { type PostgresqlListCopy PostgresqlList type PostgresqlCopy Postgresql +func clusterName(clusterName string, teamName string) (string, error) { + teamNameLen := len(teamName) + if len(clusterName) < teamNameLen + 2 { + return "", fmt.Errorf("Name is too short") + } + if strings.ToLower(clusterName[:teamNameLen]) != strings.ToLower(teamName) { + return "", fmt.Errorf("Name must start with the team name") + } + + return clusterName[teamNameLen+1:], nil +} + func (p *Postgresql) UnmarshalJSON(data []byte) error { tmp := PostgresqlCopy{} err := json.Unmarshal(data, &tmp) @@ -197,6 +210,12 @@ func (p *Postgresql) UnmarshalJSON(data []byte) error { return err } tmp2 := Postgresql(tmp) + + clusterName, err := clusterName(tmp2.Metadata.Name, tmp2.Spec.TeamId) + if err != nil { + return err + } + tmp2.Spec.ClusterName = clusterName *p = tmp2 return nil diff --git a/pkg/util/config/config.go b/pkg/util/config/config.go index eda40742e..63821bcc5 100644 --- a/pkg/util/config/config.go +++ b/pkg/util/config/config.go @@ -44,6 +44,7 @@ type Config struct { WALES3Bucket string `envconfig:"wal_s3_bucket"` KubeIAMRole string `envconfig:"kube_iam_role"` DebugLogging bool `split_words:"true" default:"false"` + DNSNameFormat string `envconfig:"dns_name_format" default:"%s.%s.%s"` } func LoadFromEnv() *Config { diff --git a/pkg/util/util.go b/pkg/util/util.go index f41618015..c20d2b651 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -54,10 +54,6 @@ func PodSpiloRole(pod *v1.Pod) string { return pod.Labels["spilo-role"] } -func ClusterDNSName(clusterName, teamName, hostedZone string) string { - return fmt.Sprintf("%s.%s.%s", clusterName, teamName, hostedZone) -} - func PGUserPassword(user spec.PgUser) string { s := md5.Sum([]byte(user.Password + user.Name))