@@ -12,6 +12,7 @@ import (
12
12
"fmt"
13
13
"io"
14
14
"os"
15
+ "path/filepath"
15
16
"testing"
16
17
"time"
17
18
@@ -26,6 +27,8 @@ import (
26
27
"k8s.io/apimachinery/pkg/runtime"
27
28
"k8s.io/apimachinery/pkg/runtime/serializer"
28
29
"k8s.io/apimachinery/pkg/util/yaml"
30
+ "k8s.io/client-go/kubernetes"
31
+ "sigs.k8s.io/e2e-framework/klient"
29
32
"sigs.k8s.io/e2e-framework/klient/k8s"
30
33
"sigs.k8s.io/kustomize/api/krusty"
31
34
"sigs.k8s.io/kustomize/kyaml/filesys"
@@ -51,6 +54,9 @@ func TestKubernetesAgentStandalone(t *testing.T) {
51
54
require .NoError (t , err )
52
55
require .NotNil (t , client )
53
56
57
+ testLogsBasePath := os .Getenv ("K8S_TESTS_POD_LOGS_BASE" )
58
+ require .NotEmpty (t , testLogsBasePath )
59
+
54
60
ctx := context .Background ()
55
61
56
62
namespace := info .Namespace
@@ -79,6 +85,9 @@ func TestKubernetesAgentStandalone(t *testing.T) {
79
85
},
80
86
}
81
87
t .Cleanup (func () {
88
+ if t .Failed () {
89
+ dumpLogs (t , ctx , client , namespace , testLogsBasePath )
90
+ }
82
91
_ = client .Resources ().Delete (ctx , k8sNamespaceObj )
83
92
for _ , obj := range objects {
84
93
_ = client .Resources (namespace ).Delete (ctx , obj )
@@ -129,6 +138,12 @@ func TestKubernetesAgentStandalone(t *testing.T) {
129
138
require .NoError (t , err )
130
139
131
140
for _ , pod := range podList .Items {
141
+ for _ , containerStatus := range pod .Status .ContainerStatuses {
142
+ if containerStatus .RestartCount > 0 {
143
+ return false
144
+ }
145
+ }
146
+
132
147
for _ , cond := range pod .Status .Conditions {
133
148
if cond .Type != corev1 .PodReady {
134
149
continue
@@ -141,8 +156,63 @@ func TestKubernetesAgentStandalone(t *testing.T) {
141
156
}
142
157
143
158
return true
144
- }, time .Second * 100 , time .Second * 1 )
145
- require .NoError (t , err )
159
+ }, time .Second * 100 , time .Second * 1 , "Timed out waiting for pods to be ready" )
160
+ }
161
+
162
+ func dumpLogs (t * testing.T , ctx context.Context , client klient.Client , namespace string , targetDir string ) {
163
+
164
+ podList := & corev1.PodList {}
165
+
166
+ clientset , err := kubernetes .NewForConfig (client .RESTConfig ())
167
+ if err != nil {
168
+ t .Logf ("Error creating clientset: %v\n " , err )
169
+ return
170
+ }
171
+
172
+ err = client .Resources (namespace ).List (ctx , podList )
173
+ if err != nil {
174
+ t .Logf ("Error listing pods: %v\n " , err )
175
+ return
176
+ }
177
+
178
+ for _ , pod := range podList .Items {
179
+
180
+ previous := false
181
+ for _ , containerStatus := range pod .Status .ContainerStatuses {
182
+ if containerStatus .RestartCount > 0 {
183
+ previous = true
184
+ break
185
+ }
186
+ }
187
+
188
+ for _ , container := range pod .Spec .Containers {
189
+ logFilePath := filepath .Join (targetDir , fmt .Sprintf ("%s-%s-%s.log" , t .Name (), pod .Name , container .Name ))
190
+ logFile , err := os .Create (logFilePath )
191
+ if err != nil {
192
+ t .Logf ("Error creating log file: %v\n " , err )
193
+ continue
194
+ }
195
+
196
+ req := clientset .CoreV1 ().Pods (namespace ).GetLogs (pod .Name , & corev1.PodLogOptions {
197
+ Container : container .Name ,
198
+ Previous : previous ,
199
+ })
200
+ podLogsStream , err := req .Stream (context .TODO ())
201
+ if err != nil {
202
+ t .Logf ("Error getting container %s of pod %s logs: %v\n " , container .Name , pod .Name , err )
203
+ continue
204
+ }
205
+
206
+ _ , err = io .Copy (logFile , podLogsStream )
207
+ if err != nil {
208
+ t .Logf ("Error writing container %s of pod %s logs: %v\n " , container .Name , pod .Name , err )
209
+ } else {
210
+ t .Logf ("Wrote container %s of pod %s logs to %s\n " , container .Name , pod .Name , logFilePath )
211
+ }
212
+
213
+ _ = podLogsStream .Close ()
214
+ }
215
+ }
146
216
}
147
217
148
218
// YAMLDecoder converts YAML bytes into test.Builder instances.
0 commit comments