@@ -18,6 +18,7 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"os"
23
24
"strings"
@@ -38,6 +39,7 @@ import (
38
39
helmgetter "helm.sh/helm/v3/pkg/getter"
39
40
helmreg "helm.sh/helm/v3/pkg/registry"
40
41
corev1 "k8s.io/api/core/v1"
42
+ "k8s.io/apimachinery/pkg/runtime"
41
43
"k8s.io/apimachinery/pkg/types"
42
44
kuberecorder "k8s.io/client-go/tools/record"
43
45
ctrl "sigs.k8s.io/controller-runtime"
@@ -313,15 +315,8 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s
313
315
if file != "" {
314
316
defer func () {
315
317
if err := os .Remove (file ); err != nil {
316
- log := ctrl .LoggerFrom (ctx )
317
- log .Error (err , "failed to delete temporary credentials file" )
318
- r .Eventf (
319
- obj ,
320
- corev1 .EventTypeWarning ,
321
- meta .FailedReason ,
322
- "failed to delete temporary credentials file: %s" ,
323
- err ,
324
- )
318
+ r .eventLogf (ctx , obj , corev1 .EventTypeWarning , meta .FailedReason ,
319
+ "failed to delete temporary credentials file: %s" , err )
325
320
}
326
321
}()
327
322
}
@@ -362,3 +357,19 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s
362
357
363
358
return sreconcile .ResultSuccess , nil
364
359
}
360
+
361
+ // eventLogf records events, and logs at the same time.
362
+ //
363
+ // This log is different from the debug log in the EventRecorder, in the sense
364
+ // that this is a simple log. While the debug log contains complete details
365
+ // about the event.
366
+ func (r * HelmRepositoryOCIReconciler ) eventLogf (ctx context.Context , obj runtime.Object , eventType string , reason string , messageFmt string , args ... interface {}) {
367
+ msg := fmt .Sprintf (messageFmt , args ... )
368
+ // Log and emit event.
369
+ if eventType == corev1 .EventTypeWarning {
370
+ ctrl .LoggerFrom (ctx ).Error (errors .New (reason ), msg )
371
+ } else {
372
+ ctrl .LoggerFrom (ctx ).Info (msg )
373
+ }
374
+ r .Eventf (obj , eventType , reason , msg )
375
+ }
0 commit comments