Add team name to tpr object metadata name

This commit is contained in:
Murat Kabilov 2017-04-12 10:40:40 +02:00
parent db53134cbd
commit dd2ed5ff9d
6 changed files with 32 additions and 7 deletions

View File

@ -55,4 +55,6 @@ spec:
- name: PGOP_DOCKER_IMAGE - name: PGOP_DOCKER_IMAGE
value: "registry.opensource.zalan.do/acid/spilo-9.6:1.2-p12" value: "registry.opensource.zalan.do/acid/spilo-9.6:1.2-p12"
- name: PGOP_DB_HOSTED_ZONE - name: PGOP_DB_HOSTED_ZONE
value: "db.example.com" value: "db.example.com"
- name: PGOP_DNS_NAME_FORMAT
value: "%s.%s.staging.%s"

View File

@ -9,7 +9,6 @@ import (
"k8s.io/client-go/pkg/util/intstr" "k8s.io/client-go/pkg/util/intstr"
"github.bus.zalan.do/acid/postgres-operator/pkg/spec" "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" "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, Namespace: c.Metadata.Namespace,
Labels: c.labelsSet(), Labels: c.labelsSet(),
Annotations: map[string]string{ Annotations: map[string]string{
constants.ZalandoDnsNameAnnotation: util.ClusterDNSName(c.Metadata.Name, c.TeamName(), c.OpConfig.DbHostedZone), constants.ZalandoDnsNameAnnotation: c.dnsName(),
}, },
}, },
Spec: v1.ServiceSpec{ Spec: v1.ServiceSpec{

View File

@ -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 { func (c *Cluster) credentialSecretName(username string) string {
return fmt.Sprintf(constants.UserSecretTemplate, return fmt.Sprintf(constants.UserSecretTemplate,
username, username,

View File

@ -79,6 +79,7 @@ type PostgresSpec struct {
NumberOfInstances int32 `json:"numberOfInstances"` NumberOfInstances int32 `json:"numberOfInstances"`
Users map[string]UserFlags `json:"users"` Users map[string]UserFlags `json:"users"`
MaintenanceWindows []MaintenanceWindow `json:"maintenanceWindows,omitempty"` MaintenanceWindows []MaintenanceWindow `json:"maintenanceWindows,omitempty"`
ClusterName string `json:"-"`
} }
type PostgresqlList struct { type PostgresqlList struct {
@ -190,6 +191,18 @@ func (pl *PostgresqlList) GetListMeta() unversioned.List {
type PostgresqlListCopy PostgresqlList type PostgresqlListCopy PostgresqlList
type PostgresqlCopy Postgresql 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 { func (p *Postgresql) UnmarshalJSON(data []byte) error {
tmp := PostgresqlCopy{} tmp := PostgresqlCopy{}
err := json.Unmarshal(data, &tmp) err := json.Unmarshal(data, &tmp)
@ -197,6 +210,12 @@ func (p *Postgresql) UnmarshalJSON(data []byte) error {
return err return err
} }
tmp2 := Postgresql(tmp) tmp2 := Postgresql(tmp)
clusterName, err := clusterName(tmp2.Metadata.Name, tmp2.Spec.TeamId)
if err != nil {
return err
}
tmp2.Spec.ClusterName = clusterName
*p = tmp2 *p = tmp2
return nil return nil

View File

@ -44,6 +44,7 @@ type Config struct {
WALES3Bucket string `envconfig:"wal_s3_bucket"` WALES3Bucket string `envconfig:"wal_s3_bucket"`
KubeIAMRole string `envconfig:"kube_iam_role"` KubeIAMRole string `envconfig:"kube_iam_role"`
DebugLogging bool `split_words:"true" default:"false"` DebugLogging bool `split_words:"true" default:"false"`
DNSNameFormat string `envconfig:"dns_name_format" default:"%s.%s.%s"`
} }
func LoadFromEnv() *Config { func LoadFromEnv() *Config {

View File

@ -54,10 +54,6 @@ func PodSpiloRole(pod *v1.Pod) string {
return pod.Labels["spilo-role"] 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 { func PGUserPassword(user spec.PgUser) string {
s := md5.Sum([]byte(user.Password + user.Name)) s := md5.Sum([]byte(user.Password + user.Name))