Skip to content

Commit da0de8c

Browse files
Make sure the statefulset that is deleted manually gets re-created. (zalando#191)
* Make sure the statefulset that is deleted manually gets re-created. Per report and analysis by Manuel Gomez. * Move the existence checks for other objects out of the Create functions. create{Object} for services, endpoints and PDBs refused to continue if there is a cached definition in the cluster, however, the only place where it makes sense is when creating a new cluster. Note that contrary to the statefulset this doesn't fix any issues, since those definitions were nullified correspondingly when the sync code detected there is no object present in the Kubernetes cluster.
1 parent 6c57334 commit da0de8c

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

pkg/cluster/cluster.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,18 @@ func (c *Cluster) Create() error {
216216
if role == Replica && !c.Spec.ReplicaLoadBalancer {
217217
continue
218218
}
219+
if c.Endpoints[role] != nil {
220+
return fmt.Errorf("%s endpoint already exists in the cluster", role)
221+
}
219222
ep, err = c.createEndpoint(role)
220223
if err != nil {
221224
return fmt.Errorf("could not create %s endpoint: %v", role, err)
222225
}
223226
c.logger.Infof("endpoint %q has been successfully created", util.NameFromMeta(ep.ObjectMeta))
224227

228+
if c.Services[role] != nil {
229+
return fmt.Errorf("service already exists in the cluster")
230+
}
225231
service, err = c.createService(role)
226232
if err != nil {
227233
return fmt.Errorf("could not create %s service: %v", role, err)
@@ -239,12 +245,18 @@ func (c *Cluster) Create() error {
239245
}
240246
c.logger.Infof("secrets have been successfully created")
241247

248+
if c.PodDisruptionBudget != nil {
249+
return fmt.Errorf("pod disruption budget already exists in the cluster")
250+
}
242251
pdb, err := c.createPodDisruptionBudget()
243252
if err != nil {
244253
return fmt.Errorf("could not create pod disruption budget: %v", err)
245254
}
246255
c.logger.Infof("pod disruption budget %q has been successfully created", util.NameFromMeta(pdb.ObjectMeta))
247256

257+
if c.Statefulset != nil {
258+
return fmt.Errorf("statefulset already exists in the cluster")
259+
}
248260
ss, err = c.createStatefulSet()
249261
if err != nil {
250262
return fmt.Errorf("could not create statefulset: %v", err)

pkg/cluster/resources.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ func (c *Cluster) listResources() error {
6161

6262
func (c *Cluster) createStatefulSet() (*v1beta1.StatefulSet, error) {
6363
c.setProcessName("creating statefulset")
64-
if c.Statefulset != nil {
65-
return nil, fmt.Errorf("statefulset already exists in the cluster")
66-
}
6764
statefulSetSpec, err := c.generateStatefulSet(&c.Spec)
6865
if err != nil {
6966
return nil, fmt.Errorf("could not generate statefulset: %v", err)
@@ -236,11 +233,7 @@ func (c *Cluster) deleteStatefulSet() error {
236233
func (c *Cluster) createService(role PostgresRole) (*v1.Service, error) {
237234
c.setProcessName("creating %v service", role)
238235

239-
if c.Services[role] != nil {
240-
return nil, fmt.Errorf("service already exists in the cluster")
241-
}
242236
serviceSpec := c.generateService(role, &c.Spec)
243-
244237
service, err := c.KubeClient.Services(serviceSpec.Namespace).Create(serviceSpec)
245238
if err != nil {
246239
return nil, err
@@ -349,9 +342,6 @@ func (c *Cluster) deleteService(role PostgresRole) error {
349342

350343
func (c *Cluster) createEndpoint(role PostgresRole) (*v1.Endpoints, error) {
351344
c.setProcessName("creating endpoint")
352-
if c.Endpoints[role] != nil {
353-
return nil, fmt.Errorf("%s endpoint already exists in the cluster", role)
354-
}
355345
subsets := make([]v1.EndpointSubset, 0)
356346
if role == Master {
357347
//TODO: set subsets to the master
@@ -369,9 +359,6 @@ func (c *Cluster) createEndpoint(role PostgresRole) (*v1.Endpoints, error) {
369359
}
370360

371361
func (c *Cluster) createPodDisruptionBudget() (*policybeta1.PodDisruptionBudget, error) {
372-
if c.PodDisruptionBudget != nil {
373-
return nil, fmt.Errorf("pod disruption budget already exists in the cluster")
374-
}
375362
podDisruptionBudgetSpec := c.generatePodDisruptionBudget()
376363
podDisruptionBudget, err := c.KubeClient.
377364
PodDisruptionBudgets(podDisruptionBudgetSpec.Namespace).

pkg/cluster/sync.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ func (c *Cluster) syncStatefulSet() error {
245245
return fmt.Errorf("could not get statefulset: %v", err)
246246
}
247247
// statefulset does not exist, try to re-create it
248+
c.Statefulset = nil
248249
c.logger.Infof("could not find the cluster's statefulset")
249250
pods, err := c.listPods()
250251
if err != nil {

0 commit comments

Comments
 (0)