Skip to content

Commit 9c621d3

Browse files
committed
chore: review
Signed-off-by: Leonardo Cecchi <[email protected]>
1 parent 41f312e commit 9c621d3

File tree

7 files changed

+102
-58
lines changed

7 files changed

+102
-58
lines changed

hack/build-dev-image.sh

Lines changed: 0 additions & 14 deletions
This file was deleted.

internal/cnpgi/instance/backup.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
barmanBackup "github.com/cloudnative-pg/barman-cloud/pkg/backup"
10+
barmanCommand "github.com/cloudnative-pg/barman-cloud/pkg/command"
1011
barmanCredentials "github.com/cloudnative-pg/barman-cloud/pkg/credentials"
1112
"github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
1213
"github.com/cloudnative-pg/cnpg-i/pkg/backup"
@@ -114,6 +115,43 @@ func (b BackupServiceImplementation) Backup(
114115
}
115116

116117
contextLogger.Info("Backup completed", "backup", executedBackupInfo.ID)
118+
119+
// Refresh the recovery window
120+
contextLogger.Info(
121+
"Refreshing the recovery window",
122+
"backupName", executedBackupInfo.BackupName,
123+
)
124+
backupList, err := barmanCommand.GetBackupList(
125+
ctx,
126+
&objectStore.Spec.Configuration,
127+
configuration.ServerName,
128+
env,
129+
)
130+
if err != nil {
131+
contextLogger.Error(err, "while reading the backup list")
132+
return nil, err
133+
}
134+
135+
if err := updateRecoveryWindow(
136+
ctx,
137+
b.Client,
138+
backupList,
139+
&objectStore,
140+
configuration.ServerName,
141+
); err != nil {
142+
contextLogger.Error(
143+
err,
144+
"Error while updating the recovery window in the ObjectStore status stanza. Skipping.",
145+
"backupName", executedBackupInfo.BackupName,
146+
)
147+
} else {
148+
contextLogger.Debug(
149+
"backupName", executedBackupInfo.BackupName,
150+
"Updated the recovery window in the ObjectStore status stanza",
151+
"serverRecoveryWindow", objectStore.Status.ServerRecoveryWindow,
152+
)
153+
}
154+
117155
return &backup.BackupResult{
118156
BackupId: executedBackupInfo.ID,
119157
BackupName: executedBackupInfo.BackupName,

internal/cnpgi/instance/manager.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@ func Start(ctx context.Context) error {
3535
controllerOptions := ctrl.Options{
3636
Scheme: scheme,
3737
Client: client.Options{
38-
// NOTE WE WILL MODIFY THE GENERATE CLIENT WITH A CUSTOM CACHE
38+
// Important: the caching options below are used by
39+
// controller-runtime only.
40+
// The plugin code uses an enhanced client with a
41+
// custom caching strategy specifically for ObjectStores
42+
// and Clusters.
43+
//
44+
// This custom strategy is necessary because we lack
45+
// permission to list these resources at the namespace
46+
// level. Additionally, controller-runtime does not
47+
// support caching a closed (explicit) set of objects
48+
// within a namespace - it can only cache either individual
49+
// objects or all objects in a namespace.
3950
Cache: &client.CacheOptions{
4051
DisableFor: []client.Object{
4152
&corev1.Secret{},

internal/cnpgi/instance/metrics.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ package instance
33
import (
44
"context"
55
"fmt"
6-
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
7-
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
8-
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
9-
"sigs.k8s.io/controller-runtime/pkg/client"
106
"strings"
117

128
"github.com/cloudnative-pg/cnpg-i/pkg/metrics"
139
"github.com/cloudnative-pg/machinery/pkg/log"
14-
)
10+
"sigs.k8s.io/controller-runtime/pkg/client"
1511

16-
var (
17-
// Sanitize the plugin name to be a valid Prometheus metric namespace
18-
metricsDomain = strings.NewReplacer(".", "_", "-", "_").Replace(metadata.PluginName)
12+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
13+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
14+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
1915
)
2016

17+
// Sanitize the plugin name to be a valid Prometheus metric namespace
18+
var metricsDomain = strings.NewReplacer(".", "_", "-", "_").Replace(metadata.PluginName)
19+
2120
type metricsImpl struct {
2221
// important the client should be one with a underlying cache
2322
Client client.Client
@@ -32,7 +31,6 @@ func buildFqName(name string) string {
3231
var (
3332
firstRecoverabilityPointMetricName = buildFqName("first_recoverability_point")
3433
lastAvailableBackupTimestampMetricName = buildFqName("last_available_backup_timestamp")
35-
testMetricName = buildFqName("test_metric")
3634
)
3735

3836
func (m metricsImpl) GetCapabilities(

internal/cnpgi/instance/metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var _ = Describe("Metrics Collect method", func() {
9898
res, err := m.Collect(ctx, req)
9999
Expect(err).ToNot(HaveOccurred())
100100
Expect(res).ToNot(BeNil())
101-
Expect(res.Metrics).To(HaveLen(3))
101+
Expect(res.Metrics).To(HaveLen(2))
102102

103103
// Verify the metrics
104104
metricsMap := make(map[string]float64)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package instance
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/cloudnative-pg/barman-cloud/pkg/catalog"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/utils/ptr"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
13+
)
14+
15+
// updateRecoveryWindow updates the recovery window inside the object
16+
// store status subresource
17+
func updateRecoveryWindow(
18+
ctx context.Context,
19+
c client.Client,
20+
backupList *catalog.Catalog,
21+
objectStore *barmancloudv1.ObjectStore,
22+
serverName string,
23+
) error {
24+
// Set the recovery window inside the barman object store object
25+
convertTime := func(t *time.Time) *metav1.Time {
26+
if t == nil {
27+
return nil
28+
}
29+
return ptr.To(metav1.NewTime(*t))
30+
}
31+
32+
recoveryWindow := barmancloudv1.RecoveryWindow{
33+
FirstRecoverabilityPoint: convertTime(backupList.GetFirstRecoverabilityPoint()),
34+
LastSuccessfulBackupTime: convertTime(backupList.GetLastSuccessfulBackupTime()),
35+
}
36+
37+
if objectStore.Status.ServerRecoveryWindow == nil {
38+
objectStore.Status.ServerRecoveryWindow = make(map[string]barmancloudv1.RecoveryWindow)
39+
}
40+
objectStore.Status.ServerRecoveryWindow[serverName] = recoveryWindow
41+
42+
return c.Status().Update(ctx, objectStore)
43+
}

internal/cnpgi/instance/retention.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import (
77
"slices"
88
"time"
99

10-
"github.com/cloudnative-pg/barman-cloud/pkg/catalog"
1110
barmanCommand "github.com/cloudnative-pg/barman-cloud/pkg/command"
1211
barmanCredentials "github.com/cloudnative-pg/barman-cloud/pkg/credentials"
1312
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
1413
"github.com/cloudnative-pg/machinery/pkg/log"
15-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1614
"k8s.io/apimachinery/pkg/types"
1715
"k8s.io/client-go/tools/record"
18-
"k8s.io/utils/ptr"
1916
"sigs.k8s.io/controller-runtime/pkg/client"
2017

2118
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
@@ -157,36 +154,7 @@ func (c *CatalogMaintenanceRunnable) maintenance(
157154
return err
158155
}
159156

160-
return c.updateRecoveryWindow(ctx, backupList, objectStore, configuration.ServerName)
161-
}
162-
163-
// updateRecoveryWindow updates the recovery window inside the object
164-
// store status subresource
165-
func (c *CatalogMaintenanceRunnable) updateRecoveryWindow(
166-
ctx context.Context,
167-
backupList *catalog.Catalog,
168-
objectStore *barmancloudv1.ObjectStore,
169-
serverName string,
170-
) error {
171-
// Set the recovery window inside the barman object store object
172-
convertTime := func(t *time.Time) *metav1.Time {
173-
if t == nil {
174-
return nil
175-
}
176-
return ptr.To(metav1.NewTime(*t))
177-
}
178-
179-
recoveryWindow := barmancloudv1.RecoveryWindow{
180-
FirstRecoverabilityPoint: convertTime(backupList.GetFirstRecoverabilityPoint()),
181-
LastSuccessfulBackupTime: convertTime(backupList.GetLastSuccessfulBackupTime()),
182-
}
183-
184-
if objectStore.Status.ServerRecoveryWindow == nil {
185-
objectStore.Status.ServerRecoveryWindow = make(map[string]barmancloudv1.RecoveryWindow)
186-
}
187-
objectStore.Status.ServerRecoveryWindow[serverName] = recoveryWindow
188-
189-
return c.Client.Status().Update(ctx, objectStore)
157+
return updateRecoveryWindow(ctx, c.Client, backupList, objectStore, configuration.ServerName)
190158
}
191159

192160
// deleteBackupsNotInCatalog deletes all Backup objects pointing to the given cluster that are not

0 commit comments

Comments
 (0)