Skip to content

Commit d5f3614

Browse files
committed
add custom logger for cotroller file operations
1 parent e091aaa commit d5f3614

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

pkg/controller/controller.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io/ioutil"
1010

1111
"github.com/bakito/batch-job-controller/pkg/lifecycle"
12-
"github.com/go-logr/logr"
1312
corev1 "k8s.io/api/core/v1"
1413
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1514
"k8s.io/client-go/kubernetes"
@@ -29,6 +28,8 @@ const (
2928
LabelExecutionID = "batch-job-controller.bakito.github.com/execution-id"
3029
)
3130

31+
var clog = ctrl.Log.WithName("pod-controller")
32+
3233
// PodReconciler reconciler
3334
type PodReconciler struct {
3435
client.Client
@@ -72,7 +73,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
7273

7374
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed {
7475
if r.Controller.Config().SavePodLog && pod.DeletionTimestamp == nil {
75-
r.savePodLogs(ctx, pod, podLog, executionID)
76+
r.savePodLogs(ctx, pod, executionID)
7677
}
7778
if err := r.Controller.PodTerminated(executionID, node, pod.Status.Phase); err != nil {
7879
if !errors.Is(err, &lifecycle.ExecutionIDNotFound{}) {
@@ -85,16 +86,16 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
8586
return reconcile.Result{}, nil
8687
}
8788

88-
func (r *PodReconciler) savePodLogs(ctx context.Context, pod *corev1.Pod, podLog logr.Logger, executionID string) {
89+
func (r *PodReconciler) savePodLogs(ctx context.Context, pod *corev1.Pod, executionID string) {
8990
for _, c := range pod.Spec.Containers {
90-
clog := podLog.WithValues("container", c.Name)
91+
clog := clog.WithValues("node", pod.Spec.NodeName, "id", executionID, "container", c.Name)
9192
if l, err := r.getPodLog(ctx, pod.Namespace, pod.Name, c.Name); err != nil {
9293
clog.Error(err, "could not get log of container")
9394
} else {
94-
if fileName, err := r.savePodLog(pod.Spec.NodeName, executionID, c.Name, l); err != nil {
95+
if err := r.savePodLog(pod.Spec.NodeName, executionID, c.Name, l); err != nil {
9596
clog.Error(err, "error saving container log file")
9697
} else {
97-
clog.WithValues("name", fileName).Info("saved container log file")
98+
clog.Info("saved container log file")
9899
}
99100
}
100101
}
@@ -120,10 +121,10 @@ func (r *PodReconciler) getPodLog(ctx context.Context, namespace string, name st
120121
return str, nil
121122
}
122123

123-
func (r *PodReconciler) savePodLog(node string, executionID string, name string, data string) (string, error) {
124+
func (r *PodReconciler) savePodLog(node string, executionID string, name string, data string) error {
124125
if err := r.Controller.Config().MkReportDir(executionID); err != nil {
125-
return "", err
126+
return err
126127
}
127128
fileName := r.Controller.Config().ReportFileName(executionID, fmt.Sprintf("%s-container-%s.log", node, name))
128-
return fileName, ioutil.WriteFile(fileName, []byte(data), 0o600)
129+
return ioutil.WriteFile(fileName, []byte(data), 0o600)
129130
}

0 commit comments

Comments
 (0)