@@ -18,6 +18,7 @@ package controllers
1818
1919import (
2020 "context"
21+ "errors"
2122 "fmt"
2223 "os"
2324 "strings"
@@ -38,6 +39,7 @@ import (
3839 helmgetter "helm.sh/helm/v3/pkg/getter"
3940 helmreg "helm.sh/helm/v3/pkg/registry"
4041 corev1 "k8s.io/api/core/v1"
42+ "k8s.io/apimachinery/pkg/runtime"
4143 "k8s.io/apimachinery/pkg/types"
4244 kuberecorder "k8s.io/client-go/tools/record"
4345 ctrl "sigs.k8s.io/controller-runtime"
@@ -313,15 +315,8 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s
313315 if file != "" {
314316 defer func () {
315317 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 )
325320 }
326321 }()
327322 }
@@ -362,3 +357,19 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s
362357
363358 return sreconcile .ResultSuccess , nil
364359}
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