9
9
"io/ioutil"
10
10
11
11
"github.com/bakito/batch-job-controller/pkg/lifecycle"
12
- "github.com/go-logr/logr"
13
12
corev1 "k8s.io/api/core/v1"
14
13
k8serrors "k8s.io/apimachinery/pkg/api/errors"
15
14
"k8s.io/client-go/kubernetes"
@@ -29,6 +28,8 @@ const (
29
28
LabelExecutionID = "batch-job-controller.bakito.github.com/execution-id"
30
29
)
31
30
31
+ var clog = ctrl .Log .WithName ("pod-controller" )
32
+
32
33
// PodReconciler reconciler
33
34
type PodReconciler struct {
34
35
client.Client
@@ -72,7 +73,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
72
73
73
74
if pod .Status .Phase == corev1 .PodSucceeded || pod .Status .Phase == corev1 .PodFailed {
74
75
if r .Controller .Config ().SavePodLog && pod .DeletionTimestamp == nil {
75
- r .savePodLogs (ctx , pod , podLog , executionID )
76
+ r .savePodLogs (ctx , pod , executionID )
76
77
}
77
78
if err := r .Controller .PodTerminated (executionID , node , pod .Status .Phase ); err != nil {
78
79
if ! errors .Is (err , & lifecycle.ExecutionIDNotFound {}) {
@@ -85,16 +86,16 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
85
86
return reconcile.Result {}, nil
86
87
}
87
88
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 ) {
89
90
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 )
91
92
if l , err := r .getPodLog (ctx , pod .Namespace , pod .Name , c .Name ); err != nil {
92
93
clog .Error (err , "could not get log of container" )
93
94
} 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 {
95
96
clog .Error (err , "error saving container log file" )
96
97
} else {
97
- clog .WithValues ( "name" , fileName ). Info ("saved container log file" )
98
+ clog .Info ("saved container log file" )
98
99
}
99
100
}
100
101
}
@@ -120,10 +121,10 @@ func (r *PodReconciler) getPodLog(ctx context.Context, namespace string, name st
120
121
return str , nil
121
122
}
122
123
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 {
124
125
if err := r .Controller .Config ().MkReportDir (executionID ); err != nil {
125
- return "" , err
126
+ return err
126
127
}
127
128
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 )
129
130
}
0 commit comments