Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion api/v1/postgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func (p *Postgres) ToPeripheralResourceLookupKey() types.NamespacedName {
}
}

func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *corev1.ConfigMap, sc string, pgParamBlockList map[string]bool, rbs *BackupConfig, srcDB *Postgres, patroniTTL, patroniLoopWait, patroniRetryTimeout uint32, dboIsSuperuser bool, enableTlsCert bool, image string, cpuRequestsPercentage int) (*unstructured.Unstructured, error) {
func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *corev1.ConfigMap, sc string, pgParamBlockList map[string]bool, rbs *BackupConfig, srcDB *Postgres, patroniTTL, patroniLoopWait, patroniRetryTimeout uint32, dboIsSuperuser bool, enableTlsCert bool, image string, cpuRequestsPercentage int, tscEnable bool, tscKey string, tscMaxSkew, tscMinDomains int32) (*unstructured.Unstructured, error) {
if z == nil {
z = &zalando.Postgresql{}
}
Expand Down Expand Up @@ -829,6 +829,35 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor
z.Spec.TLS = nil
}

if tscEnable {
tsc := corev1.TopologySpreadConstraint{
MaxSkew: tscMaxSkew,
WhenUnsatisfiable: corev1.ScheduleAnyway,
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"application": "spilo",
"cluster-name": z.Name,
NameLabelName: p.Name,
PartitionIDLabelName: p.Spec.PartitionID,
ProjectIDLabelName: p.Spec.ProjectID,
TenantLabelName: p.Spec.Tenant,
UIDLabelName: string(p.UID),
"team": p.generateTeamID(),
},
},
TopologyKey: tscKey,
}

// if defined, set the minDomains (and corresponding whenUnsatisfied) field as well
if tscMinDomains > 0 {
tsc.MinDomains = &tscMinDomains
tsc.WhenUnsatisfiable = corev1.DoNotSchedule
}

// override topology spread constraints
z.Spec.TopologySpreadConstraints = []corev1.TopologySpreadConstraint{tsc}
}

jsonZ, err := runtime.DefaultUnstructuredConverter.ToUnstructured(z)
if err != nil {
return nil, fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion api/v1/postgres_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
p := &Postgres{
Spec: tt.spec,
}
got, _ := p.ToUnstructuredZalandoPostgresql(nil, tt.c, tt.sc, tt.pgParamBlockList, tt.rbs, tt.srcDB, 130, 10, 60, false, false, "dockerImage", 66)
got, _ := p.ToUnstructuredZalandoPostgresql(nil, tt.c, tt.sc, tt.pgParamBlockList, tt.rbs, tt.srcDB,
130, 10, 60, false, false, "dockerImage", 66, false, "", 0, 0)

jsonZ, err := runtime.DefaultUnstructuredConverter.ToUnstructured(got)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions controllers/postgres_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ type PostgresReconciler struct {
WalGExporterCPULimit string
WalGExporterMemoryLimit string
SpiloCpuRequestsPercentage int
EnableTopologySpreadConstraints bool
TscMinDomains, TscMaxSkew int32
TscKey string
}

type PatroniStandbyCluster struct {
Expand Down Expand Up @@ -480,7 +483,7 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
return fmt.Errorf("failed to fetch zalando postgresql: %w", err)
}

u, err := instance.ToUnstructuredZalandoPostgresql(nil, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert, r.PostgresImage, r.SpiloCpuRequestsPercentage)
u, err := instance.ToUnstructuredZalandoPostgresql(nil, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert, r.PostgresImage, r.SpiloCpuRequestsPercentage, r.EnableTopologySpreadConstraints, r.TscKey, r.TscMaxSkew, r.TscMinDomains)
if err != nil {
return fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err)
}
Expand All @@ -496,7 +499,7 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
// Update zalando postgresql
mergeFrom := client.MergeFrom(rawZ.DeepCopy())

u, err := instance.ToUnstructuredZalandoPostgresql(rawZ, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert, r.PostgresImage, r.SpiloCpuRequestsPercentage)
u, err := instance.ToUnstructuredZalandoPostgresql(rawZ, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert, r.PostgresImage, r.SpiloCpuRequestsPercentage, r.EnableTopologySpreadConstraints, r.TscKey, r.TscMaxSkew, r.TscMinDomains)
if err != nil {
return fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err)
}
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const (
podAntiaffinityPreferredDuringSchedulingFlg = "pod-antiaffinity-preferred-during-scheduling"
podAntiaffinityTopologyKeyFlg = "pod-antiaffinity-topology-key"
enablePodTopologySpreadConstraintWebhookFlg = "enable-pod-topology-spread-constraint-webhook"
enablePodTopologySpreadConstraintFlg = "enable-pod-topology-spread-constraint"
podTopologySpreadConstraintTopologyKeyFlg = "pod-topology-spread-constraint-topology-key"
podTopologySpreadConstraintMaxSkewFlg = "pod-topology-spread-constraint-max-skew"
podTopologySpreadConstraintMinDomainsFlg = "pod-topology-spread-constraint-min-domains"
Expand Down Expand Up @@ -173,6 +174,7 @@ func main() {
enableWalGExporter bool
podAntiaffinityPreferredDuringScheduling bool
enablePodTopologySpreadConstraintWebhook bool
enablePodTopologySpreadConstraint bool
enableSpiloReadinessProbe bool
enableKubernetesUseConfigMaps bool

Expand Down Expand Up @@ -369,6 +371,15 @@ func main() {

viper.SetDefault(enablePodTopologySpreadConstraintWebhookFlg, false)
enablePodTopologySpreadConstraintWebhook = viper.GetBool(enablePodTopologySpreadConstraintWebhookFlg)

viper.SetDefault(enablePodTopologySpreadConstraintFlg, false)
enablePodTopologySpreadConstraint = viper.GetBool(enablePodTopologySpreadConstraintFlg)

if !strings.Contains(operatorImage, `:v2`) && enablePodTopologySpreadConstraint {
setupLog.Error(nil, fmt.Sprintf("Flag %s not supported with postgres operator v1, use %s instead, exiting.", enablePodTopologySpreadConstraintFlg, enablePodTopologySpreadConstraintWebhookFlg))
os.Exit(1)
}

viper.SetDefault(podTopologySpreadConstraintTopologyKeyFlg, "machine.metal-stack.io/rack")
podTopologySpreadConstraintTopologyKey = viper.GetString(podTopologySpreadConstraintTopologyKeyFlg)
viper.SetDefault(podTopologySpreadConstraintMaxSkewFlg, 1)
Expand Down Expand Up @@ -437,6 +448,7 @@ func main() {
walGExporterCPULimitFlg, walGExporterCPULimit,
walGExporterMemoryLimitFlg, walGExporterMemoryLimit,
enablePodTopologySpreadConstraintWebhookFlg, enablePodTopologySpreadConstraintWebhook,
enablePodTopologySpreadConstraintFlg, enablePodTopologySpreadConstraint,
podTopologySpreadConstraintTopologyKeyFlg, podTopologySpreadConstraintTopologyKey,
podTopologySpreadConstraintMaxSkewFlg, podTopologySpreadConstraintMaxSkew,
podTopologySpreadConstraintMinDomainsFlg, podTopologySpreadConstraintMinDomains,
Expand Down Expand Up @@ -570,6 +582,10 @@ func main() {
WalGExporterCPULimit: walGExporterCPULimit,
WalGExporterMemoryLimit: walGExporterMemoryLimit,
SpiloCpuRequestsPercentage: spiloCpuRequestsPercentage,
EnableTopologySpreadConstraints: enablePodTopologySpreadConstraint,
TscKey: podTopologySpreadConstraintTopologyKey,
TscMaxSkew: podTopologySpreadConstraintMaxSkew,
TscMinDomains: podTopologySpreadConstraintMinDomains,
}).SetupWithManager(ctrlPlaneClusterMgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Postgres")
os.Exit(1)
Expand Down
Loading