From c597377617cead6ae2897b6987ddda86796f2720 Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Tue, 20 Feb 2018 15:36:43 +0100 Subject: [PATCH] Use cluster UID as a suffix to the WAL bucket. (#211) Avoid reusing WAL S3 buckets of the older cluster with the same name as the existing one. For the new cluster, the S3 bucket name will include a suffix that is equal to the UID of the PostgreSQL object describing the cluster. That way, the bucket name will stay the same for all members iff they correspond to the same PostgreSQL cluster object. When "clone: uid:" key is present in the cluster manifest and the cluster is cloned from an S3 bucket (currently that happens if the endTimestamp is present in the clone description) the S3 bucket to clone from is suffixed with the -uid value. --- pkg/cluster/k8sres.go | 13 ++++++++++++- pkg/spec/postgresql.go | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index 1bc18422e..3fb8d050a 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -7,6 +7,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/client-go/pkg/api/v1" "k8s.io/client-go/pkg/apis/apps/v1beta1" @@ -280,6 +281,7 @@ func (c *Cluster) tolerations(tolerationsSpec *[]v1.Toleration) []v1.Toleration } func (c *Cluster) generatePodTemplate( + uid types.UID, resourceRequirements *v1.ResourceRequirements, resourceRequirementsScalyrSidecar *v1.ResourceRequirements, tolerationsSpec *[]v1.Toleration, @@ -358,6 +360,7 @@ func (c *Cluster) generatePodTemplate( } if c.OpConfig.WALES3Bucket != "" { envVars = append(envVars, v1.EnvVar{Name: "WAL_S3_BUCKET", Value: c.OpConfig.WALES3Bucket}) + envVars = append(envVars, v1.EnvVar{Name: "WAL_BUCKET_SCOPE_SUFFIX", Value: getWALBucketScopeSuffix(string(uid))}) } if c.OpConfig.EtcdHost == "" { @@ -500,6 +503,13 @@ func (c *Cluster) generatePodTemplate( return &template } +func getWALBucketScopeSuffix(uid string) string { + if uid != "" { + return fmt.Sprintf("-%s", uid) + } + return "" +} + func makeResources(cpuRequest, memoryRequest, cpuLimit, memoryLimit string) spec.Resources { return spec.Resources{ ResourceRequest: spec.ResourceDescription{ @@ -537,7 +547,7 @@ func (c *Cluster) generateStatefulSet(spec *spec.PostgresSpec) (*v1beta1.Statefu customPodEnvVars = cm.Data } } - podTemplate := c.generatePodTemplate(resourceRequirements, resourceRequirementsScalyrSidecar, &spec.Tolerations, &spec.PostgresqlParam, &spec.Patroni, &spec.Clone, &spec.DockerImage, customPodEnvVars) + podTemplate := c.generatePodTemplate(c.Postgresql.GetUID(), resourceRequirements, resourceRequirementsScalyrSidecar, &spec.Tolerations, &spec.PostgresqlParam, &spec.Patroni, &spec.Clone, &spec.DockerImage, customPodEnvVars) volumeClaimTemplate, err := generatePersistentVolumeClaimTemplate(spec.Volume.Size, spec.Volume.StorageClass) if err != nil { return nil, fmt.Errorf("could not generate volume claim template: %v", err) @@ -757,6 +767,7 @@ func (c *Cluster) generateCloneEnvironment(description *spec.CloneDescription) [ result = append(result, v1.EnvVar{Name: "CLONE_METHOD", Value: "CLONE_WITH_WALE"}) result = append(result, v1.EnvVar{Name: "CLONE_WAL_S3_BUCKET", Value: c.OpConfig.WALES3Bucket}) result = append(result, v1.EnvVar{Name: "CLONE_TARGET_TIME", Value: description.EndTimestamp}) + result = append(result, v1.EnvVar{Name: "CLONE_WAL_BUCKET_SCOPE_SUFFIX", Value: getWALBucketScopeSuffix(description.Uid)}) } return result diff --git a/pkg/spec/postgresql.go b/pkg/spec/postgresql.go index a08d260d6..e124c10c1 100644 --- a/pkg/spec/postgresql.go +++ b/pkg/spec/postgresql.go @@ -55,6 +55,7 @@ type Patroni struct { // CloneDescription describes which cluster the new should clone and up to which point in time type CloneDescription struct { ClusterName string `json:"cluster,omitempty"` + Uid string `json:"uid,omitempty"` EndTimestamp string `json:"timestamp,omitempty"` }