Skip to content

Commit 31f92a1

Browse files
authored
extend inherited annotations unit test to include logical backup cron job (#2723)
* extend inherited annotations test to logical backup cron job * sync on updated when enabled, not only on schedule changes
1 parent a87307e commit 31f92a1

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

pkg/cluster/cluster.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,11 +1067,7 @@ func (c *Cluster) Update(oldSpec, newSpec *acidv1.Postgresql) error {
10671067

10681068
}
10691069

1070-
// apply schedule changes
1071-
// this is the only parameter of logical backups a user can overwrite in the cluster manifest
1072-
if (oldSpec.Spec.EnableLogicalBackup && newSpec.Spec.EnableLogicalBackup) &&
1073-
(newSpec.Spec.LogicalBackupSchedule != oldSpec.Spec.LogicalBackupSchedule) {
1074-
c.logger.Debugf("updating schedule of the backup cron job")
1070+
if oldSpec.Spec.EnableLogicalBackup && newSpec.Spec.EnableLogicalBackup {
10751071
if err := c.syncLogicalBackupJob(); err != nil {
10761072
c.logger.Errorf("could not sync logical backup jobs: %v", err)
10771073
updateFailed = true

pkg/cluster/util_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func newFakeK8sAnnotationsClient() (k8sutil.KubernetesClient, *k8sFake.Clientset
5151
EndpointsGetter: clientSet.CoreV1(),
5252
PodsGetter: clientSet.CoreV1(),
5353
DeploymentsGetter: clientSet.AppsV1(),
54+
CronJobsGetter: clientSet.BatchV1(),
5455
}, clientSet
5556
}
5657

@@ -176,6 +177,22 @@ func checkResourcesInheritedAnnotations(cluster *Cluster, resultAnnotations map[
176177
return nil
177178
}
178179

180+
checkCronJob := func(annotations map[string]string) error {
181+
cronJobList, err := cluster.KubeClient.CronJobs(namespace).List(context.TODO(), clusterOptions)
182+
if err != nil {
183+
return err
184+
}
185+
for _, cronJob := range cronJobList.Items {
186+
if err := containsAnnotations(updateAnnotations(annotations), cronJob.Annotations, cronJob.ObjectMeta.Name, "Logical backup cron job"); err != nil {
187+
return err
188+
}
189+
if err := containsAnnotations(updateAnnotations(annotations), cronJob.Spec.JobTemplate.Spec.Template.Annotations, cronJob.Name, "Logical backup cron job pod template"); err != nil {
190+
return err
191+
}
192+
}
193+
return nil
194+
}
195+
179196
checkSecrets := func(annotations map[string]string) error {
180197
secretList, err := cluster.KubeClient.Secrets(namespace).List(context.TODO(), clusterOptions)
181198
if err != nil {
@@ -203,7 +220,7 @@ func checkResourcesInheritedAnnotations(cluster *Cluster, resultAnnotations map[
203220
}
204221

205222
checkFuncs := []func(map[string]string) error{
206-
checkSts, checkPods, checkSvc, checkPdb, checkPooler, checkPvc, checkSecrets, checkEndpoints,
223+
checkSts, checkPods, checkSvc, checkPdb, checkPooler, checkCronJob, checkPvc, checkSecrets, checkEndpoints,
207224
}
208225
for _, f := range checkFuncs {
209226
if err := f(resultAnnotations); err != nil {
@@ -251,6 +268,7 @@ func newInheritedAnnotationsCluster(client k8sutil.KubernetesClient) (*Cluster,
251268
Spec: acidv1.PostgresSpec{
252269
EnableConnectionPooler: boolToPointer(true),
253270
EnableReplicaConnectionPooler: boolToPointer(true),
271+
EnableLogicalBackup: true,
254272
Volume: acidv1.Volume{
255273
Size: "1Gi",
256274
},
@@ -306,6 +324,10 @@ func newInheritedAnnotationsCluster(client k8sutil.KubernetesClient) (*Cluster,
306324
if err != nil {
307325
return nil, err
308326
}
327+
err = cluster.createLogicalBackupJob()
328+
if err != nil {
329+
return nil, err
330+
}
309331
pvcList := CreatePVCs(namespace, clusterName, cluster.labelsSet(false), 2, "1Gi")
310332
for _, pvc := range pvcList.Items {
311333
_, err = cluster.KubeClient.PersistentVolumeClaims(namespace).Create(context.TODO(), &pvc, metav1.CreateOptions{})

0 commit comments

Comments
 (0)