From 47e3e29a566428ab47b0b7296e342ccf2c090636 Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Tue, 18 Apr 2017 17:16:27 +0200 Subject: [PATCH] Add version label to the cluster. (#96) * Add version label to the cluster. According to the STUPS team the daemon that exports logs to scalyr stops the export if the version label is missing. * Move label names to constants. * Run go fmt --- pkg/cluster/util.go | 8 ++++---- pkg/spec/postgresql.go | 2 +- pkg/util/constants/constants.go | 28 ++++++++++++++++------------ pkg/util/util.go | 9 +++++---- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/pkg/cluster/util.go b/pkg/cluster/util.go index 17dca2cd1..4035bc34f 100644 --- a/pkg/cluster/util.go +++ b/pkg/cluster/util.go @@ -156,10 +156,10 @@ func (c *Cluster) waitPodLabelsReady() error { LabelSelector: ls.String(), } masterListOption := v1.ListOptions{ - LabelSelector: labels.Merge(ls, labels.Set{"spilo-role": constants.PodRoleMaster}).String(), + LabelSelector: labels.Merge(ls, labels.Set{constants.SpiloRoleLabel: constants.PodRoleMaster}).String(), } replicaListOption := v1.ListOptions{ - LabelSelector: labels.Merge(ls, labels.Set{"spilo-role": constants.PodRoleReplica}).String(), + LabelSelector: labels.Merge(ls, labels.Set{constants.SpiloRoleLabel: constants.PodRoleReplica}).String(), } pods, err := c.KubeClient.Pods(namespace).List(listOptions) if err != nil { @@ -204,8 +204,8 @@ func (c *Cluster) waitStatefulsetPodsReady() error { func (c *Cluster) labelsSet() labels.Set { return labels.Set{ - "application": "spilo", - "spilo-cluster": c.Metadata.Name, + constants.ApplicationNameLabel: constants.ApplicationNameLabelValue, + constants.ClusterNameLabel: c.Metadata.Name, } } diff --git a/pkg/spec/postgresql.go b/pkg/spec/postgresql.go index fc6148f26..4d8e4b534 100644 --- a/pkg/spec/postgresql.go +++ b/pkg/spec/postgresql.go @@ -196,7 +196,7 @@ func clusterName(clusterName string, teamName string) (string, error) { if len(clusterName) < teamNameLen+2 { return "", fmt.Errorf("Name is too short") } - if strings.ToLower(clusterName[:teamNameLen+1]) != strings.ToLower(teamName) + "-" { + if strings.ToLower(clusterName[:teamNameLen+1]) != strings.ToLower(teamName)+"-" { return "", fmt.Errorf("Name must start with the team name and dash") } diff --git a/pkg/util/constants/constants.go b/pkg/util/constants/constants.go index c9c193b7a..11a685cde 100644 --- a/pkg/util/constants/constants.go +++ b/pkg/util/constants/constants.go @@ -2,16 +2,20 @@ package constants const ( //Constants - TPRName = "postgresql" - TPRVendor = "acid.zalan.do" - TPRDescription = "Managed PostgreSQL clusters" - TPRApiVersion = "v1" - DataVolumeName = "pgdata" - PasswordLength = 64 - UserSecretTemplate = "%s.%s.credentials.%s.%s" // Username, ClusterName, TPRName, TPRVendor - ZalandoDnsNameAnnotation = "zalando.org/dnsname" - KubeIAmAnnotation = "iam.amazonaws.com/role" - ResourceName = TPRName + "s" - PodRoleMaster = "master" - PodRoleReplica = "replica" + TPRName = "postgresql" + TPRVendor = "acid.zalan.do" + TPRDescription = "Managed PostgreSQL clusters" + TPRApiVersion = "v1" + DataVolumeName = "pgdata" + PasswordLength = 64 + UserSecretTemplate = "%s.%s.credentials.%s.%s" // Username, ClusterName, TPRName, TPRVendor + ZalandoDnsNameAnnotation = "zalando.org/dnsname" + KubeIAmAnnotation = "iam.amazonaws.com/role" + ResourceName = TPRName + "s" + PodRoleMaster = "master" + PodRoleReplica = "replica" + ApplicationNameLabel = "application" + ApplicationNameLabelValue = "spilo" + SpiloRoleLabel = "spilo-role" + ClusterNameLabel = "version" ) diff --git a/pkg/util/util.go b/pkg/util/util.go index 6d1a0c1e2..6c2ee1ba7 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -5,13 +5,14 @@ import ( "encoding/hex" "fmt" "math/rand" + "strings" "time" "github.com/motomux/pretty" + "k8s.io/client-go/pkg/api/v1" "github.bus.zalan.do/acid/postgres-operator/pkg/spec" - "k8s.io/client-go/pkg/api/v1" - "strings" + "github.bus.zalan.do/acid/postgres-operator/pkg/util/constants" ) var passwordChars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") @@ -37,7 +38,7 @@ func NameFromMeta(meta v1.ObjectMeta) spec.NamespacedName { } func PodClusterName(pod *v1.Pod) spec.NamespacedName { - if name, ok := pod.Labels["spilo-cluster"]; ok { + if name, ok := pod.Labels[constants.ClusterNameLabel]; ok { return spec.NamespacedName{ Namespace: pod.Namespace, Name: name, @@ -48,7 +49,7 @@ func PodClusterName(pod *v1.Pod) spec.NamespacedName { } func PodSpiloRole(pod *v1.Pod) string { - return pod.Labels["spilo-role"] + return pod.Labels[constants.SpiloRoleLabel] } func PGUserPassword(user spec.PgUser) string {