@@ -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