Skip to content

Commit c597377

Browse files
Use cluster UID as a suffix to the WAL bucket. (zalando#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.
1 parent 0e3f941 commit c597377

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pkg/cluster/k8sres.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"k8s.io/apimachinery/pkg/api/resource"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/types"
1011
"k8s.io/apimachinery/pkg/util/intstr"
1112
"k8s.io/client-go/pkg/api/v1"
1213
"k8s.io/client-go/pkg/apis/apps/v1beta1"
@@ -280,6 +281,7 @@ func (c *Cluster) tolerations(tolerationsSpec *[]v1.Toleration) []v1.Toleration
280281
}
281282

282283
func (c *Cluster) generatePodTemplate(
284+
uid types.UID,
283285
resourceRequirements *v1.ResourceRequirements,
284286
resourceRequirementsScalyrSidecar *v1.ResourceRequirements,
285287
tolerationsSpec *[]v1.Toleration,
@@ -358,6 +360,7 @@ func (c *Cluster) generatePodTemplate(
358360
}
359361
if c.OpConfig.WALES3Bucket != "" {
360362
envVars = append(envVars, v1.EnvVar{Name: "WAL_S3_BUCKET", Value: c.OpConfig.WALES3Bucket})
363+
envVars = append(envVars, v1.EnvVar{Name: "WAL_BUCKET_SCOPE_SUFFIX", Value: getWALBucketScopeSuffix(string(uid))})
361364
}
362365

363366
if c.OpConfig.EtcdHost == "" {
@@ -500,6 +503,13 @@ func (c *Cluster) generatePodTemplate(
500503
return &template
501504
}
502505

506+
func getWALBucketScopeSuffix(uid string) string {
507+
if uid != "" {
508+
return fmt.Sprintf("-%s", uid)
509+
}
510+
return ""
511+
}
512+
503513
func makeResources(cpuRequest, memoryRequest, cpuLimit, memoryLimit string) spec.Resources {
504514
return spec.Resources{
505515
ResourceRequest: spec.ResourceDescription{
@@ -537,7 +547,7 @@ func (c *Cluster) generateStatefulSet(spec *spec.PostgresSpec) (*v1beta1.Statefu
537547
customPodEnvVars = cm.Data
538548
}
539549
}
540-
podTemplate := c.generatePodTemplate(resourceRequirements, resourceRequirementsScalyrSidecar, &spec.Tolerations, &spec.PostgresqlParam, &spec.Patroni, &spec.Clone, &spec.DockerImage, customPodEnvVars)
550+
podTemplate := c.generatePodTemplate(c.Postgresql.GetUID(), resourceRequirements, resourceRequirementsScalyrSidecar, &spec.Tolerations, &spec.PostgresqlParam, &spec.Patroni, &spec.Clone, &spec.DockerImage, customPodEnvVars)
541551
volumeClaimTemplate, err := generatePersistentVolumeClaimTemplate(spec.Volume.Size, spec.Volume.StorageClass)
542552
if err != nil {
543553
return nil, fmt.Errorf("could not generate volume claim template: %v", err)
@@ -757,6 +767,7 @@ func (c *Cluster) generateCloneEnvironment(description *spec.CloneDescription) [
757767
result = append(result, v1.EnvVar{Name: "CLONE_METHOD", Value: "CLONE_WITH_WALE"})
758768
result = append(result, v1.EnvVar{Name: "CLONE_WAL_S3_BUCKET", Value: c.OpConfig.WALES3Bucket})
759769
result = append(result, v1.EnvVar{Name: "CLONE_TARGET_TIME", Value: description.EndTimestamp})
770+
result = append(result, v1.EnvVar{Name: "CLONE_WAL_BUCKET_SCOPE_SUFFIX", Value: getWALBucketScopeSuffix(description.Uid)})
760771
}
761772

762773
return result

pkg/spec/postgresql.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Patroni struct {
5555
// CloneDescription describes which cluster the new should clone and up to which point in time
5656
type CloneDescription struct {
5757
ClusterName string `json:"cluster,omitempty"`
58+
Uid string `json:"uid,omitempty"`
5859
EndTimestamp string `json:"timestamp,omitempty"`
5960
}
6061

0 commit comments

Comments
 (0)