Skip to content

Commit

Permalink
introduce eventLogf to HelmRepositoryOCIReconciler; fix formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Max Jonas Werner <[email protected]>
  • Loading branch information
Max Jonas Werner committed May 24, 2022
1 parent 29bd370 commit 5cadd3a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
9 changes: 2 additions & 7 deletions controllers/helmchart_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,8 @@ func (r *HelmChartReconciler) buildFromHelmRepository(ctx context.Context, obj *
if file != "" {
defer func() {
if err := os.Remove(file); err != nil {
r.eventLogf(
ctx,
obj,
corev1.EventTypeWarning,
meta.FailedReason,
"failed to delete temporary credentials file: %s",
err)
r.eventLogf(ctx, obj, corev1.EventTypeWarning, meta.FailedReason,
"failed to delete temporary credentials file: %s", err)
}
}()
}
Expand Down
29 changes: 20 additions & 9 deletions controllers/helmrepository_controller_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"errors"
"fmt"
"os"
"strings"
Expand All @@ -38,6 +39,7 @@ import (
helmgetter "helm.sh/helm/v3/pkg/getter"
helmreg "helm.sh/helm/v3/pkg/registry"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kuberecorder "k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -313,15 +315,8 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s
if file != "" {
defer func() {
if err := os.Remove(file); err != nil {
log := ctrl.LoggerFrom(ctx)
log.Error(err, "failed to delete temporary credentials file")
r.Eventf(
obj,
corev1.EventTypeWarning,
meta.FailedReason,
"failed to delete temporary credentials file: %s",
err,
)
r.eventLogf(ctx, obj, corev1.EventTypeWarning, meta.FailedReason,
"failed to delete temporary credentials file: %s", err)
}
}()
}
Expand Down Expand Up @@ -362,3 +357,19 @@ func (r *HelmRepositoryOCIReconciler) validateSource(ctx context.Context, obj *s

return sreconcile.ResultSuccess, nil
}

// eventLogf records events, and logs at the same time.
//
// This log is different from the debug log in the EventRecorder, in the sense
// that this is a simple log. While the debug log contains complete details
// about the event.
func (r *HelmRepositoryOCIReconciler) eventLogf(ctx context.Context, obj runtime.Object, eventType string, reason string, messageFmt string, args ...interface{}) {
msg := fmt.Sprintf(messageFmt, args...)
// Log and emit event.
if eventType == corev1.EventTypeWarning {
ctrl.LoggerFrom(ctx).Error(errors.New(reason), msg)
} else {
ctrl.LoggerFrom(ctx).Info(msg)
}
r.Eventf(obj, eventType, reason, msg)
}

0 comments on commit 5cadd3a

Please sign in to comment.