Skip to content

Commit 9a354b8

Browse files
armrujbattiato
andauthored
fix: emit event and log when ImageCatalog retrieval fails (cloudnative-pg#9266)
Previously we emitted error and logs only for the errors of NotFound category, for the other types the the controller would fail silently without emitting a Kubernetes event. This made troubleshooting difficult, especially when using tools like ArgoCD. This commit ensures that a Warning event and a log is emitted when the ImageCatalog cannot be retrieved for any reason, providing better visibility into configuration errors. Closes cloudnative-pg#9016 Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com> Signed-off-by: Jonathan Battiato <jonathan.battiato@enterprisedb.com> Co-authored-by: Jonathan Battiato <jonathan.battiato@enterprisedb.com>
1 parent c6f5f48 commit 9a354b8

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

internal/controller/cluster_image.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ func (r *ClusterReconciler) getRequestedImageInfo(
162162
return apiv1.ImageInfo{}, fmt.Errorf("catalog %s/%s not found", catalogKind, catalogName)
163163
}
164164

165+
r.Recorder.Eventf(cluster, "Warning", "DiscoverImage", "Error getting %v/%v: %v",
166+
catalogKind, catalogName, err)
167+
contextLogger.Error(err, "while getting imageCatalog",
168+
"catalogKind", catalogKind, "catalogName", catalogName)
165169
return apiv1.ImageInfo{}, err
166170
}
167171

internal/controller/cluster_image_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ package controller
2121

2222
import (
2323
"context"
24+
"fmt"
2425

2526
corev1 "k8s.io/api/core/v1"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/client-go/tools/record"
29+
ctrl "sigs.k8s.io/controller-runtime"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
2831
"sigs.k8s.io/controller-runtime/pkg/client/fake"
32+
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
2933

3034
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
3135
schemeBuilder "github.com/cloudnative-pg/cloudnative-pg/internal/scheme"
@@ -223,3 +227,58 @@ var _ = Describe("Cluster image detection", func() {
223227
Expect(cluster.Status.PGDataImageInfo.MajorVersion).To(Equal(16))
224228
})
225229
})
230+
231+
var _ = Describe("Cluster image detection with errors", func() {
232+
It("emits an event when ImageCatalog retrieval fails", func(ctx SpecContext) {
233+
cluster := &apiv1.Cluster{
234+
ObjectMeta: metav1.ObjectMeta{
235+
Name: "cluster-example",
236+
Namespace: "default",
237+
},
238+
Spec: apiv1.ClusterSpec{
239+
ImageCatalogRef: &apiv1.ImageCatalogRef{
240+
TypedLocalObjectReference: corev1.TypedLocalObjectReference{
241+
Name: "catalog",
242+
Kind: "ImageCatalog",
243+
APIGroup: &apiv1.SchemeGroupVersion.Group,
244+
},
245+
Major: 15,
246+
},
247+
},
248+
}
249+
250+
fakeClient := fake.NewClientBuilder().
251+
WithScheme(schemeBuilder.BuildWithAllKnownScheme()).
252+
WithRuntimeObjects(cluster).
253+
WithStatusSubresource(cluster).
254+
WithInterceptorFuncs(interceptor.Funcs{
255+
Get: func(ctx context.Context, cl client.WithWatch, key client.ObjectKey, obj client.Object,
256+
opts ...client.GetOption,
257+
) error {
258+
if _, ok := obj.(*apiv1.ImageCatalog); ok {
259+
return fmt.Errorf("simulated error: no kind match")
260+
}
261+
return cl.Get(ctx, key, obj, opts...)
262+
},
263+
}).
264+
Build()
265+
266+
recorder := record.NewFakeRecorder(10)
267+
r := &ClusterReconciler{
268+
Client: fakeClient,
269+
Recorder: recorder,
270+
}
271+
272+
result, err := r.reconcileImage(ctx, cluster)
273+
274+
// reconcileImage handles the error by updating the status, so it returns nil error
275+
Expect(err).ToNot(HaveOccurred())
276+
Expect(result).To(Equal(&ctrl.Result{}))
277+
278+
// Check if the event was recorded
279+
// The event string format is "Type Reason Message"
280+
// We expect: Warning DiscoverImage Error getting ImageCatalog/catalog: ...
281+
Eventually(recorder.Events).Should(Receive(ContainSubstring(
282+
"Warning DiscoverImage Error getting ImageCatalog/catalog")))
283+
})
284+
})

0 commit comments

Comments
 (0)