Skip to content

Commit 4a45bc1

Browse files
committed
Added configured cleanup function to remove the restored configmaps
1 parent fd1ff0c commit 4a45bc1

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

docs/reference/operator_parameters.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ Those are top-level keys, containing both leaf keys and groups.
171171
* **repair_period**
172172
period between consecutive repair requests. The default is `5m`.
173173

174+
* **pitr_backup_retention**
175+
retention time for PITR backup config maps. The operator will clean up config maps older than the configured retention. The value is a duration string, e.g. "168h". The default is `168h`.
176+
174177
* **set_memory_request_to_limit**
175178
Set `memory_request` to `memory_limit` for all Postgres clusters (the default
176179
value is also increased but configured `max_memory_request` can not be

manifests/operatorconfiguration.crd.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ spec:
113113
repair_period:
114114
type: string
115115
default: "5m"
116+
pitr_backup_retention:
117+
type: string
118+
default: "168h"
116119
set_memory_request_to_limit:
117120
type: boolean
118121
default: false

manifests/postgresql-operator-default-configuration.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ configuration:
1919
min_instances: -1
2020
resync_period: 30m
2121
repair_period: 5m
22+
pitr_backup_retention: 168h
2223
# set_memory_request_to_limit: false
2324
# sidecars:
2425
# - image: image:123

pkg/apis/acid.zalan.do/v1/operator_configuration_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ type OperatorConfigurationData struct {
266266
Workers uint32 `json:"workers,omitempty"`
267267
ResyncPeriod Duration `json:"resync_period,omitempty"`
268268
RepairPeriod Duration `json:"repair_period,omitempty"`
269+
PitrBackupRetention Duration `json:"pitr_backup_retention,omitempty"`
269270
SetMemoryRequestToLimit bool `json:"set_memory_request_to_limit,omitempty"`
270271
ShmVolume *bool `json:"enable_shm_volume,omitempty"`
271272
SidecarImages map[string]string `json:"sidecar_docker_images,omitempty"` // deprecated in favour of SidecarContainers

pkg/controller/postgresql.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"github.com/sirupsen/logrus"
1414

1515
v1 "k8s.io/api/core/v1"
16-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1716
"k8s.io/apimachinery/pkg/api/errors"
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1818
"k8s.io/apimachinery/pkg/types"
1919
"k8s.io/client-go/tools/cache"
2020

@@ -27,8 +27,8 @@ import (
2727
)
2828

2929
const (
30-
restoreAnnotationKey = "postgres-operator.zalando.org/action"
31-
restoreAnnotationValue = "restore-in-place"
30+
restoreAnnotationKey = "postgres-operator.zalando.org/action"
31+
restoreAnnotationValue = "restore-in-place"
3232
)
3333

3434
func (c *Controller) clusterResync(stopCh <-chan struct{}, wg *sync.WaitGroup) {
@@ -44,6 +44,9 @@ func (c *Controller) clusterResync(stopCh <-chan struct{}, wg *sync.WaitGroup) {
4444
if err := c.processPendingRestores(); err != nil {
4545
c.logger.Errorf("could not process pending restores: %v", err)
4646
}
47+
if err := c.cleanupRestores(); err != nil {
48+
c.logger.Errorf("could not cleanup restores: %v", err)
49+
}
4750
case <-stopCh:
4851
return
4952
}
@@ -611,8 +614,6 @@ func (c *Controller) validateRestoreInPlace(pgOld, pgNew *acidv1.Postgresql) err
611614
return nil
612615
}
613616

614-
615-
616617
// handlerRestoreInPlace starts an asynchronous point-in-time-restore.
617618
// It creates a ConfigMap to store the state and then deletes the old Postgresql CR.
618619
func (c *Controller) handlerRestoreInPlace(pgOld, pgNew *acidv1.Postgresql) {
@@ -780,6 +781,44 @@ func (c *Controller) processSingleInProgressCm(cm v1.ConfigMap) error {
780781
return nil
781782
}
782783

784+
func (c *Controller) cleanupRestores() error {
785+
c.logger.Debug("cleaning up old restore config maps")
786+
namespace := c.opConfig.WatchedNamespace
787+
if namespace == "" {
788+
namespace = v1.NamespaceAll
789+
}
790+
791+
cmList, err := c.KubeClient.ConfigMaps(namespace).List(context.TODO(), metav1.ListOptions{})
792+
if err != nil {
793+
return fmt.Errorf("could not list restore ConfigMaps: %v", err)
794+
}
795+
796+
retention := c.opConfig.PitrBackupRetention
797+
if retention <= 0 {
798+
c.logger.Debugf("Pitr backup retention is not set, skipping cleanup")
799+
return nil
800+
}
801+
c.logger.Debugf("Pitr backup retention is %s", retention.String())
802+
803+
for _, cm := range cmList.Items {
804+
if !strings.HasPrefix(cm.Name, "pitr-") {
805+
continue
806+
}
807+
808+
age := time.Since(cm.CreationTimestamp.Time)
809+
if age > retention {
810+
c.logger.Infof("deleting old restore config map %q, age: %s", cm.Name, age.String())
811+
err := c.KubeClient.ConfigMaps(cm.Namespace).Delete(context.TODO(), cm.Name, metav1.DeleteOptions{})
812+
if err != nil {
813+
c.logger.Errorf("could not delete config map %q: %v", cm.Name, err)
814+
// continue with next cm
815+
}
816+
}
817+
}
818+
819+
return nil
820+
}
821+
783822
/*
784823
Ensures the pod service account and role bindings exists in a namespace
785824
before a PG cluster is created there so that a user does not have to deploy

pkg/util/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type CRD struct {
1818
ReadyWaitTimeout time.Duration `name:"ready_wait_timeout" default:"30s"`
1919
ResyncPeriod time.Duration `name:"resync_period" default:"30m"`
2020
RepairPeriod time.Duration `name:"repair_period" default:"5m"`
21+
PitrBackupRetention time.Duration `name:"pitr_backup_retention" default:"168h"`
2122
EnableCRDRegistration *bool `name:"enable_crd_registration" default:"true"`
2223
EnableCRDValidation *bool `name:"enable_crd_validation" default:"true"`
2324
CRDCategories []string `name:"crd_categories" default:"all"`

0 commit comments

Comments
 (0)