From 7a76be7d3e9318b1636aa94cc0f5bb21dc7fe39c Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Mon, 23 Oct 2017 12:26:59 +0200 Subject: [PATCH] Minor fixes around PDB (pod-distruption-budget) syncing: (#147) - Call comparison function in the case of the sync as well as for update - Include full cluster name in PDB name - Assign cluster labels to the PDB object --- pkg/cluster/cluster.go | 10 +++------- pkg/cluster/k8sres.go | 3 ++- pkg/cluster/sync.go | 18 ++++++++++-------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 9bc236763..d4af4318f 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -536,13 +536,9 @@ func (c *Cluster) Update(newSpec *spec.Postgresql) error { c.logger.Infof("volumes have been updated successfully") } - newPDB := c.generatePodDisruptionBudget() - if match, reason := c.samePDBWith(newPDB); !match { - c.logPDBChanges(c.PodDisruptionBudget, newPDB, true, reason) - if err := c.updatePodDisruptionBudget(newPDB); err != nil { - c.setStatus(spec.ClusterStatusUpdateFailed) - return fmt.Errorf("could not update pod disruption budget: %v", err) - } + if err := c.syncPodDisruptionBudget(true); err != nil { + c.setStatus(spec.ClusterStatusUpdateFailed) + return fmt.Errorf("could not update pod disruption budget: %v", err) } c.setStatus(spec.ClusterStatusRunning) diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index 8b79eb454..49cf28cbb 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -69,7 +69,7 @@ func (c *Cluster) serviceName(role PostgresRole) string { } func (c *Cluster) podDisruptionBudgetName() string { - return c.OpConfig.PDBNameFormat.Format("cluster", c.Spec.ClusterName) + return c.OpConfig.PDBNameFormat.Format("cluster", c.Name) } func (c *Cluster) resourceRequirements(resources spec.Resources) (*v1.ResourceRequirements, error) { @@ -605,6 +605,7 @@ func (c *Cluster) generatePodDisruptionBudget() *policybeta1.PodDisruptionBudget ObjectMeta: metav1.ObjectMeta{ Name: c.podDisruptionBudgetName(), Namespace: c.Namespace, + Labels: c.labelsSet(), }, Spec: policybeta1.PodDisruptionBudgetSpec{ MinAvailable: &minAvailable, diff --git a/pkg/cluster/sync.go b/pkg/cluster/sync.go index be5760c74..97a539e8f 100644 --- a/pkg/cluster/sync.go +++ b/pkg/cluster/sync.go @@ -99,7 +99,7 @@ func (c *Cluster) Sync(newSpec *spec.Postgresql) (err error) { } c.logger.Debug("syncing pod disruption budgets") - if err = c.syncPodDisruptionBudget(); err != nil { + if err = c.syncPodDisruptionBudget(false); err != nil { err = fmt.Errorf("could not sync pod disruption budget: %v", err) return } @@ -149,7 +149,7 @@ func (c *Cluster) syncEndpoint() error { return nil } -func (c *Cluster) syncPodDisruptionBudget() error { +func (c *Cluster) syncPodDisruptionBudget(isUpdate bool) error { if c.PodDisruptionBudget == nil { c.logger.Infof("could not find the cluster's pod disruption budget") pdb, err := c.createPodDisruptionBudget() @@ -158,8 +158,15 @@ func (c *Cluster) syncPodDisruptionBudget() error { } c.logger.Infof("created missing pod disruption budget %q", util.NameFromMeta(pdb.ObjectMeta)) return nil + } else { + newPDB := c.generatePodDisruptionBudget() + if match, reason := c.samePDBWith(newPDB); !match { + c.logPDBChanges(c.PodDisruptionBudget, newPDB, isUpdate, reason) + if err := c.updatePodDisruptionBudget(newPDB); err != nil { + return err + } + } } - return nil } @@ -278,11 +285,6 @@ func (c *Cluster) syncVolumes() error { } func (c *Cluster) samePDBWith(pdb *policybeta1.PodDisruptionBudget) (match bool, reason string) { - if c.PodDisruptionBudget == nil { - match = false - reason = "pdb does not exist" - return - } match = reflect.DeepEqual(pdb.Spec, c.PodDisruptionBudget.Spec) if !match { reason = "new service spec doesn't match the current one"