@@ -7,11 +7,14 @@ import (
77 "slices"
88 "time"
99
10+ "github.com/cloudnative-pg/barman-cloud/pkg/catalog"
1011 barmanCommand "github.com/cloudnative-pg/barman-cloud/pkg/command"
1112 barmanCredentials "github.com/cloudnative-pg/barman-cloud/pkg/credentials"
1213 cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
14+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1315 "k8s.io/apimachinery/pkg/types"
1416 "k8s.io/client-go/tools/record"
17+ "k8s.io/utils/ptr"
1518 "sigs.k8s.io/controller-runtime/pkg/client"
1619 "sigs.k8s.io/controller-runtime/pkg/log"
1720
@@ -75,8 +78,6 @@ func (c *RetentionPolicyRunnable) Start(ctx context.Context) error {
7578// cycle enforces the retention policies. On success, it returns the amount
7679// of time to wait to the next check.
7780func (c * RetentionPolicyRunnable ) cycle (ctx context.Context ) (time.Duration , error ) {
78- contextLogger := log .FromContext (ctx )
79-
8081 var cluster cnpgv1.Cluster
8182 var barmanObjectStore barmancloudv1.ObjectStore
8283
@@ -89,67 +90,118 @@ func (c *RetentionPolicyRunnable) cycle(ctx context.Context) (time.Duration, err
8990 return 0 , err
9091 }
9192
92- retentionPolicy := barmanObjectStore .Spec .RetentionPolicy
93+ if err := c .applyRetentionPolicy (ctx , & cluster , & barmanObjectStore ); err != nil {
94+ return 0 , err
95+ }
96+
9397 nextCheckInterval := time .Second * time .Duration (
9498 barmanObjectStore .Spec .InstanceSidecarConfiguration .RetentionPolicyIntervalSeconds )
99+ return nextCheckInterval , nil
100+ }
95101
102+ // applyRetentionPolicy applies the retention policy to the object
103+ // store and deletes the stale Kubernetes backup objects.
104+ func (c * RetentionPolicyRunnable ) applyRetentionPolicy (
105+ ctx context.Context ,
106+ cluster * cnpgv1.Cluster ,
107+ objectStore * barmancloudv1.ObjectStore ,
108+ ) error {
109+ contextLogger := log .FromContext (ctx )
110+
111+ configuration := config .NewFromCluster (cluster )
112+
113+ retentionPolicy := objectStore .Spec .RetentionPolicy
96114 if len (retentionPolicy ) == 0 {
97115 contextLogger .Info ("Skipping retention policy enforcement, no retention policy specified" )
98- return nextCheckInterval , nil
116+ return nil
99117 }
100118 if cluster .Status .CurrentPrimary != c .PodName {
101119 contextLogger .Info (
102- "Skipping retention policy enforcement, detected standby " ,
120+ "Skipping retention policy enforcement, not the current primary " ,
103121 "currentPrimary" , cluster .Status .CurrentPrimary , "podName" , c .PodName )
104- return nextCheckInterval , nil
122+ return nil
105123 }
106124
107- contextLogger .Info ("Enforcing retention policies " ,
125+ contextLogger .Info ("Applying backup retention policy " ,
108126 "retentionPolicy" , retentionPolicy )
109127
110128 osEnvironment := os .Environ ()
111- caBundleEnvironment := common .GetRestoreCABundleEnv (& barmanObjectStore .Spec .Configuration )
129+ caBundleEnvironment := common .GetRestoreCABundleEnv (& objectStore .Spec .Configuration )
112130 env , err := barmanCredentials .EnvSetBackupCloudCredentials (
113131 ctx ,
114132 c .Client ,
115- barmanObjectStore .Namespace ,
116- & barmanObjectStore .Spec .Configuration ,
133+ objectStore .Namespace ,
134+ & objectStore .Spec .Configuration ,
117135 common .MergeEnv (osEnvironment , caBundleEnvironment ))
118136 if err != nil {
119137 contextLogger .Error (err , "while setting backup cloud credentials" )
120- return 0 , err
138+ return err
121139 }
122140
123141 if err := barmanCommand .DeleteBackupsByPolicy (
124142 ctx ,
125- & barmanObjectStore .Spec .Configuration ,
143+ & objectStore .Spec .Configuration ,
126144 configuration .ServerName ,
127145 env ,
128146 retentionPolicy ,
129147 ); err != nil {
130148 contextLogger .Error (err , "while enforcing retention policies" )
131- c .Recorder .Event (& cluster , "Warning" , "RetentionPolicyFailed" , "Retention policy failed" )
132- return 0 , err
149+ c .Recorder .Event (cluster , "Warning" , "RetentionPolicyFailed" , "Retention policy failed" )
150+ return err
133151 }
134152
135153 backupList , err := barmanCommand .GetBackupList (
136154 ctx ,
137- & barmanObjectStore .Spec .Configuration ,
155+ & objectStore .Spec .Configuration ,
138156 configuration .ServerName ,
139157 env ,
140158 )
141159 if err != nil {
142160 contextLogger .Error (err , "while reading the backup list" )
143- return 0 , err
161+ return err
144162 }
145163
146- if err := deleteBackupsNotInCatalog (ctx , c .Client , & cluster , backupList .GetBackupIDs ()); err != nil {
164+ if err := deleteBackupsNotInCatalog (ctx , c .Client , cluster , backupList .GetBackupIDs ()); err != nil {
147165 contextLogger .Error (err , "while deleting Backups not present in the catalog" )
148- return 0 , err
166+ return err
149167 }
150168
151- // TODO(leonardoce): update first point of recoverability
152- return nextCheckInterval , nil
169+ return c .updateRecoveryWindow (ctx , backupList , objectStore , configuration .ServerName )
170+ }
171+
172+ // updateRecoveryWindow updates the recovery window inside the object
173+ // store status subresource
174+ func (c * RetentionPolicyRunnable ) updateRecoveryWindow (
175+ ctx context.Context ,
176+ backupList * catalog.Catalog ,
177+ objectStore * barmancloudv1.ObjectStore ,
178+ serverName string ,
179+ ) error {
180+ // Set the recovery window inside the barman object store object
181+ convertTime := func (t * time.Time ) * metav1.Time {
182+ if t == nil {
183+ return nil
184+ }
185+
186+ return ptr .To (metav1 .NewTime (* t ))
187+ }
188+
189+ firstRecoverabilityPoint := backupList .GetFirstRecoverabilityPoint ()
190+ lastSuccessfulBackupTime := backupList .GetLastSuccessfulBackupTime ()
191+ recoveryWindow := barmancloudv1.RecoveryWindow {
192+ FirstRecoverabilityPoint : convertTime (firstRecoverabilityPoint ),
193+ LastSuccessfulBackupTime : convertTime (lastSuccessfulBackupTime ),
194+ }
195+
196+ if objectStore .Status .ServerRecoveryWindow == nil {
197+ objectStore .Status .ServerRecoveryWindow = make (map [string ]barmancloudv1.RecoveryWindow )
198+ }
199+ objectStore .Status .ServerRecoveryWindow [serverName ] = recoveryWindow
200+ if err := c .Client .Status ().Update (ctx , objectStore ); err != nil {
201+ return err
202+ }
203+
204+ return nil
153205}
154206
155207// deleteBackupsNotInCatalog deletes all Backup objects pointing to the given cluster that are not
0 commit comments