Skip to content

Commit 1dc85de

Browse files
committed
chore: dependencies and test
Signed-off-by: Armando Ruocco <[email protected]>
1 parent 496ef84 commit 1dc85de

File tree

5 files changed

+173
-23
lines changed

5 files changed

+173
-23
lines changed

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/cloudnative-pg/cnpg-i-machinery v0.4.0
1212
github.com/cloudnative-pg/machinery v0.3.0
1313
github.com/onsi/ginkgo/v2 v2.23.4
14-
github.com/onsi/gomega v1.37.0
14+
github.com/onsi/gomega v1.38.0
1515
github.com/spf13/cobra v1.9.1
1616
github.com/spf13/viper v1.20.1
1717
google.golang.org/grpc v1.74.2
@@ -20,7 +20,6 @@ require (
2020
k8s.io/apiextensions-apiserver v0.33.2
2121
k8s.io/apimachinery v0.33.2
2222
k8s.io/client-go v0.33.2
23-
k8s.io/component-base v0.33.2
2423
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397
2524
sigs.k8s.io/controller-runtime v0.21.0
2625
sigs.k8s.io/kustomize/api v0.20.0
@@ -105,21 +104,22 @@ require (
105104
go.yaml.in/yaml/v2 v2.4.2 // indirect
106105
go.yaml.in/yaml/v3 v3.0.3 // indirect
107106
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 // indirect
108-
golang.org/x/net v0.40.0 // indirect
107+
golang.org/x/net v0.41.0 // indirect
109108
golang.org/x/oauth2 v0.30.0 // indirect
110-
golang.org/x/sync v0.14.0 // indirect
109+
golang.org/x/sync v0.15.0 // indirect
111110
golang.org/x/sys v0.33.0 // indirect
112111
golang.org/x/term v0.32.0 // indirect
113-
golang.org/x/text v0.25.0 // indirect
112+
golang.org/x/text v0.26.0 // indirect
114113
golang.org/x/time v0.9.0 // indirect
115-
golang.org/x/tools v0.31.0 // indirect
114+
golang.org/x/tools v0.33.0 // indirect
116115
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
117116
google.golang.org/genproto/googleapis/api v0.0.0-20250528174236-200df99c418a // indirect
118117
google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a // indirect
119118
google.golang.org/protobuf v1.36.6 // indirect
120119
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
121120
gopkg.in/inf.v0 v0.9.1 // indirect
122121
k8s.io/apiserver v0.33.2 // indirect
122+
k8s.io/component-base v0.33.2 // indirect
123123
k8s.io/klog/v2 v2.130.1 // indirect
124124
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
125125
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.2 // indirect

go.sum

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J
131131
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
132132
github.com/onsi/ginkgo/v2 v2.23.4 h1:ktYTpKJAVZnDT4VjxSbiBenUjmlL/5QkBEocaWXiQus=
133133
github.com/onsi/ginkgo/v2 v2.23.4/go.mod h1:Bt66ApGPBFzHyR+JO10Zbt0Gsp4uWxu5mIOTusL46e8=
134-
github.com/onsi/gomega v1.37.0 h1:CdEG8g0S133B4OswTDC/5XPSzE1OeP29QOioj2PID2Y=
135-
github.com/onsi/gomega v1.37.0/go.mod h1:8D9+Txp43QWKhM24yyOBEdpkzN8FvJyAwecBgsU4KU0=
134+
github.com/onsi/gomega v1.38.0 h1:c/WX+w8SLAinvuKKQFh77WEucCnPk4j2OTUr7lt7BeY=
135+
github.com/onsi/gomega v1.38.0/go.mod h1:OcXcwId0b9QsE7Y49u+BTrL4IdKOBOKnD6VQNTJEB6o=
136136
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
137137
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
138138
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -241,15 +241,15 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
241241
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
242242
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
243243
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
244-
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
245-
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
244+
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
245+
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
246246
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
247247
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
248248
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
249249
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
250250
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
251-
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
252-
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
251+
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
252+
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
253253
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
254254
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
255255
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -259,16 +259,16 @@ golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
259259
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
260260
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
261261
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
262-
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
263-
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
262+
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
263+
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
264264
golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY=
265265
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
266266
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
267267
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
268268
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
269269
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
270-
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
271-
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=
270+
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
271+
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
272272
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
273273
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
274274
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/cnpgi/instance/metrics.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ func (m metricsImpl) Define(
7272
ValueType: &metrics.MetricType{Type: metrics.MetricType_TYPE_GAUGE},
7373
},
7474
{
75-
FqName: firstRecoverabilityPointMetricName,
76-
Help: "The first point of recoverability for the cluster as a unix timestamp",
75+
FqName: firstRecoverabilityPointMetricName,
76+
Help: "The first point of recoverability for the cluster as a unix timestamp",
77+
ValueType: &metrics.MetricType{Type: metrics.MetricType_TYPE_GAUGE},
7778
},
7879
{
79-
FqName: lastAvailableBackupTimestampMetricName,
80-
Help: "The last available backup as a unix timestamp",
80+
FqName: lastAvailableBackupTimestampMetricName,
81+
Help: "The last available backup as a unix timestamp",
82+
ValueType: &metrics.MetricType{Type: metrics.MetricType_TYPE_GAUGE},
8183
},
8284
},
8385
}, nil
@@ -102,7 +104,19 @@ func (m metricsImpl) Collect(
102104
return nil, err
103105
}
104106

105-
x := objectStore.Status.ServerRecoveryWindow[configuration.ServerName]
107+
x, ok := objectStore.Status.ServerRecoveryWindow[configuration.ServerName]
108+
if !ok {
109+
return nil, fmt.Errorf("no recovery window found for server %s", configuration.ServerName)
110+
}
111+
112+
var firstRecoverabilityPoint float64
113+
var lastAvailableBackup float64
114+
if x.FirstRecoverabilityPoint != nil {
115+
firstRecoverabilityPoint = float64(x.FirstRecoverabilityPoint.Unix())
116+
}
117+
if x.LastSuccessfulBackupTime != nil {
118+
lastAvailableBackup = float64(x.LastSuccessfulBackupTime.Unix())
119+
}
106120

107121
return &metrics.CollectMetricsResult{
108122
Metrics: []*metrics.CollectMetric{
@@ -112,11 +126,11 @@ func (m metricsImpl) Collect(
112126
},
113127
{
114128
FqName: firstRecoverabilityPointMetricName,
115-
Value: float64(x.FirstRecoverabilityPoint.Unix()),
129+
Value: firstRecoverabilityPoint,
116130
},
117131
{
118132
FqName: lastAvailableBackupTimestampMetricName,
119-
Value: float64(x.LastSuccessfulBackupTime.Unix()),
133+
Value: lastAvailableBackup,
120134
},
121135
},
122136
}, nil
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package instance
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
7+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
8+
"k8s.io/utils/ptr"
9+
"time"
10+
11+
"github.com/cloudnative-pg/cnpg-i/pkg/metrics"
12+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
13+
. "github.com/onsi/ginkgo/v2"
14+
. "github.com/onsi/gomega"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
"k8s.io/apimachinery/pkg/runtime"
17+
"sigs.k8s.io/controller-runtime/pkg/client"
18+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
19+
)
20+
21+
var _ = Describe("Metrics Collect method", func() {
22+
const (
23+
clusterName = "test-cluster"
24+
namespace = "test-ns"
25+
objectStoreName = "test-object-store"
26+
)
27+
28+
var (
29+
fakeClient client.Client
30+
m metricsImpl
31+
ctx context.Context
32+
req *metrics.CollectMetricsRequest
33+
objectStore *barmancloudv1.ObjectStore
34+
)
35+
36+
BeforeEach(func() {
37+
ctx = context.Background()
38+
scheme := runtime.NewScheme()
39+
Expect(barmancloudv1.AddToScheme(scheme)).To(Succeed())
40+
41+
// Timestamps for the test
42+
firstRecoverabilityPoint := metav1.NewTime(time.Now().Add(-24 * time.Hour))
43+
lastSuccessfulBackupTime := metav1.NewTime(time.Now())
44+
45+
// Create a fake ObjectStore with a status
46+
objectStore = &barmancloudv1.ObjectStore{
47+
ObjectMeta: metav1.ObjectMeta{
48+
Name: objectStoreName,
49+
Namespace: namespace,
50+
},
51+
Status: barmancloudv1.ObjectStoreStatus{
52+
ServerRecoveryWindow: map[string]barmancloudv1.RecoveryWindow{
53+
clusterName: {
54+
FirstRecoverabilityPoint: &firstRecoverabilityPoint,
55+
LastSuccessfulBackupTime: &lastSuccessfulBackupTime,
56+
},
57+
},
58+
},
59+
}
60+
61+
// Create a fake client with the ObjectStore
62+
fakeClient = fake.NewClientBuilder().
63+
WithScheme(scheme).
64+
WithStatusSubresource(&barmancloudv1.ObjectStore{}).
65+
WithObjects(objectStore).
66+
Build()
67+
68+
m = metricsImpl{Client: fakeClient}
69+
70+
// Create a minimal cluster definition
71+
clusterDefinition := cnpgv1.Cluster{
72+
ObjectMeta: metav1.ObjectMeta{
73+
Name: clusterName,
74+
Namespace: namespace,
75+
},
76+
Spec: cnpgv1.ClusterSpec{
77+
Plugins: []cnpgv1.PluginConfiguration{
78+
{
79+
Name: metadata.PluginName,
80+
Enabled: ptr.To(true),
81+
Parameters: map[string]string{
82+
"serverName": clusterName,
83+
"barmanObjectName": objectStoreName,
84+
},
85+
},
86+
},
87+
},
88+
}
89+
clusterJSON, err := json.Marshal(clusterDefinition)
90+
Expect(err).ToNot(HaveOccurred())
91+
92+
req = &metrics.CollectMetricsRequest{
93+
ClusterDefinition: clusterJSON,
94+
}
95+
})
96+
97+
It("should collect metrics successfully", func() {
98+
res, err := m.Collect(ctx, req)
99+
Expect(err).ToNot(HaveOccurred())
100+
Expect(res).ToNot(BeNil())
101+
Expect(res.Metrics).To(HaveLen(3))
102+
103+
// Verify the metrics
104+
metricsMap := make(map[string]float64)
105+
for _, metric := range res.Metrics {
106+
metricsMap[metric.FqName] = metric.Value
107+
}
108+
109+
// Check timestamp metrics
110+
expectedFirstPoint, _ := metricsMap[firstRecoverabilityPointMetricName]
111+
Expect(expectedFirstPoint).To(BeNumerically("~", float64(objectStore.Status.ServerRecoveryWindow[clusterName].FirstRecoverabilityPoint.Unix()), 1))
112+
113+
expectedLastBackup, _ := metricsMap[lastAvailableBackupTimestampMetricName]
114+
Expect(expectedLastBackup).To(BeNumerically("~", float64(objectStore.Status.ServerRecoveryWindow[clusterName].LastSuccessfulBackupTime.Unix()), 1))
115+
})
116+
117+
It("should return an error if the object store is not found", func() {
118+
// Use a client without any objects
119+
m.Client = fake.NewClientBuilder().Build()
120+
_, err := m.Collect(ctx, req)
121+
Expect(err).To(HaveOccurred())
122+
})
123+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package instance_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestInstance(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Instance Suite")
13+
}

0 commit comments

Comments
 (0)