Skip to content

Commit

Permalink
delete secrets the right way (zalando#1054)
Browse files Browse the repository at this point in the history
* delete secrets the right way

* make a one function

* continue deleting secrets even if one delete fails

Co-authored-by: Felix Kunde <felix.kunde@zalando.de>
  • Loading branch information
FxKu and Felix Kunde authored Jul 10, 2020
1 parent b80f976 commit 3759634
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
6 changes: 2 additions & 4 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,8 @@ func (c *Cluster) Delete() {
c.logger.Warningf("could not delete statefulset: %v", err)
}

for _, obj := range c.Secrets {
if err := c.deleteSecret(obj); err != nil {
c.logger.Warningf("could not delete secret: %v", err)
}
if err := c.deleteSecrets(); err != nil {
c.logger.Warningf("could not delete secrets: %v", err)
}

if err := c.deletePodDisruptionBudget(); err != nil {
Expand Down
27 changes: 18 additions & 9 deletions pkg/cluster/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,17 +725,26 @@ func (c *Cluster) deleteEndpoint(role PostgresRole) error {
return nil
}

func (c *Cluster) deleteSecret(secret *v1.Secret) error {
c.setProcessName("deleting secret %q", util.NameFromMeta(secret.ObjectMeta))
c.logger.Debugf("deleting secret %q", util.NameFromMeta(secret.ObjectMeta))
err := c.KubeClient.Secrets(secret.Namespace).Delete(context.TODO(), secret.Name, c.deleteOptions)
if err != nil {
return err
func (c *Cluster) deleteSecrets() error {
c.setProcessName("deleting secrets")
var errors []string
errorCount := 0
for uid, secret := range c.Secrets {
c.logger.Debugf("deleting secret %q", util.NameFromMeta(secret.ObjectMeta))
err := c.KubeClient.Secrets(secret.Namespace).Delete(context.TODO(), secret.Name, c.deleteOptions)
if err != nil {
errors = append(errors, fmt.Sprintf("could not delete secret %q: %v", util.NameFromMeta(secret.ObjectMeta), err))
errorCount++
}
c.logger.Infof("secret %q has been deleted", util.NameFromMeta(secret.ObjectMeta))
c.Secrets[uid] = nil
}
c.logger.Infof("secret %q has been deleted", util.NameFromMeta(secret.ObjectMeta))
delete(c.Secrets, secret.UID)

return err
if errorCount > 0 {
return fmt.Errorf("could not delete all secrets: %v", errors)
}

return nil
}

func (c *Cluster) createRoles() (err error) {
Expand Down

0 comments on commit 3759634

Please sign in to comment.