Skip to content

Commit f225d0f

Browse files
authored
Merge pull request #3097 from CecileRobertMichon/describe-pods
Add pod describe to e2e logger
2 parents e19d61b + d37e8a2 commit f225d0f

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ require (
9696
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
9797
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
9898
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
99+
github.com/fatih/camelcase v1.0.0 // indirect
99100
github.com/fatih/color v1.13.0 // indirect
100101
github.com/felixge/httpsnoop v1.0.3 // indirect
101102
github.com/fsnotify/fsnotify v1.6.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJ
227227
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
228228
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d h1:105gxyaGwCFad8crR9dcMQWvV9Hvulu6hwUh4tWPJnM=
229229
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d/go.mod h1:ZZMPRZwes7CROmyNKgQzC3XPs6L/G2EJLHddWejkmf4=
230+
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
231+
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
230232
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
231233
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
232234
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=

test/e2e/azure_clusterproxy.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4444
"k8s.io/apimachinery/pkg/runtime"
4545
"k8s.io/apimachinery/pkg/runtime/schema"
46+
"k8s.io/kubectl/pkg/describe"
4647
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
4748
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
4849
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
@@ -111,26 +112,21 @@ func (acp *AzureClusterProxy) collectPodLogs(ctx context.Context, namespace stri
111112

112113
Expect(workload.GetClient().List(ctx, pods)).To(Succeed())
113114

114-
var events = make(map[string]*corev1.EventList)
115115
var err error
116+
var podDescribe string
117+
118+
podDescriber, ok := describe.DescriberFor(schema.GroupKind{Group: corev1.GroupName, Kind: "Pod"}, workload.GetRESTConfig())
119+
if !ok {
120+
Logf("failed to get pod describer")
121+
}
116122

117123
for _, pod := range pods.Items {
118124
podNamespace := pod.GetNamespace()
119125

120-
// Collect events for Pod.
121-
if _, ok := events[podNamespace]; !ok {
122-
events[podNamespace], err = workload.GetClientSet().CoreV1().Events(podNamespace).List(ctx, metav1.ListOptions{})
123-
if err != nil {
124-
Logf("failed to get events in %s namespace: %v", podNamespace, err)
125-
}
126-
}
127-
128-
var eventMsgs string
129-
130-
for _, event := range events[podNamespace].Items {
131-
if event.InvolvedObject.Kind == "Pod" && event.InvolvedObject.Name == pod.GetName() {
132-
eventMsgs += fmt.Sprintf("%s\n", event.Message)
133-
}
126+
// Describe the pod.
127+
podDescribe, err = podDescriber.Describe(podNamespace, pod.GetName(), describe.DescriberSettings{ShowEvents: true})
128+
if err != nil {
129+
Logf("failed to describe pod %s/%s: %v", podNamespace, pod.GetName(), err)
134130
}
135131

136132
for _, container := range pod.Spec.Containers {
@@ -180,28 +176,28 @@ func (acp *AzureClusterProxy) collectPodLogs(ctx context.Context, namespace stri
180176
go func(pod corev1.Pod) {
181177
defer GinkgoRecover()
182178

183-
Logf("Collecting events for Pod %s/%s", podNamespace, pod.Name)
184-
eventFile := path.Join(aboveMachinesPath, podNamespace, pod.Name, "pod-events.txt")
185-
if err := os.MkdirAll(filepath.Dir(eventFile), 0o755); err != nil {
179+
Logf("Describing Pod %s/%s", podNamespace, pod.Name)
180+
describeFile := path.Join(aboveMachinesPath, podNamespace, pod.Name, "pod-describe.txt")
181+
if err := os.MkdirAll(filepath.Dir(describeFile), 0o755); err != nil {
186182
// Failing to mkdir should not cause the test to fail
187183
Logf("Error mkdir: %v", err)
188184
return
189185
}
190186

191-
f, err := os.OpenFile(eventFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
187+
f, err := os.OpenFile(describeFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
192188
if err != nil {
193189
// Failing to open the file should not cause the test to fail
194-
Logf("Error opening file to write Pod events: %v", err)
190+
Logf("Error opening file to write Pod describe: %v", err)
195191
return
196192
}
197193
defer f.Close()
198194

199195
out := bufio.NewWriter(f)
200196
defer out.Flush()
201-
_, err = out.WriteString(eventMsgs)
197+
_, err = out.WriteString(podDescribe)
202198
if errors.Is(err, io.ErrUnexpectedEOF) {
203-
// Failing to collect event message should not cause the test to fail
204-
Logf("failed to collect event message of pod %s/%s: %v", podNamespace, pod.Name, err)
199+
// Failing to describe pod should not cause the test to fail
200+
Logf("failed to describe pod %s/%s: %v", podNamespace, pod.Name, err)
205201
}
206202
}(pod)
207203
}

0 commit comments

Comments
 (0)