@@ -18,14 +18,14 @@ package integration
18
18
19
19
import (
20
20
"context"
21
- "encoding/json"
22
21
"fmt"
23
22
"strings"
24
23
"testing"
25
24
"time"
26
25
27
26
v1 "k8s.io/api/core/v1"
28
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
+ "k8s.io/apimachinery/pkg/fields"
29
29
"k8s.io/apimachinery/pkg/util/uuid"
30
30
"k8s.io/apimachinery/pkg/util/wait"
31
31
"k8s.io/client-go/dynamic"
@@ -1994,21 +1994,26 @@ func TestTopologyMatchPlugin(t *testing.T) {
1994
1994
// if tt.expectedNodes == 0 we don't expect the pod to get scheduled
1995
1995
} else {
1996
1996
// wait for the pod scheduling to failed.
1997
+ var err error
1998
+ var events []v1.Event
1997
1999
if err := wait .Poll (5 * time .Second , 20 * time .Second , func () (bool , error ) {
1998
- events , err := podFailedScheduling (cs , ns , p .Name )
2000
+ events , err = getPodEvents (cs , ns , p .Name )
1999
2001
if err != nil {
2000
2002
// This could be a connection error, so we want to retry.
2001
2003
klog .ErrorS (err , "Failed check pod scheduling status for pod" , "pod" , klog .KRef (ns , p .Name ))
2002
2004
return false , nil
2003
2005
}
2004
- for _ , e := range events {
2005
- if strings .Contains (e .Message , tt .errMsg ) {
2006
+ candidateEvents := filterPodFailedSchedulingEvents (events )
2007
+ for _ , ce := range candidateEvents {
2008
+ if strings .Contains (ce .Message , tt .errMsg ) {
2006
2009
return true , nil
2007
2010
}
2008
- klog .Warningf ("Pod failed but error message does not contain substring: %q; got %q instead" , tt .errMsg , e .Message )
2011
+ klog .Warningf ("Pod failed but error message does not contain substring: %q; got %q instead" , tt .errMsg , ce .Message )
2009
2012
}
2010
2013
return false , nil
2011
2014
}); err != nil {
2015
+ // we need more context to troubleshoot, but let's not clutter the actual error
2016
+ t .Logf ("pod %q scheduling should failed with error: %v got %v events:\n %s" , p .Name , tt .errMsg , err , formatEvents (events ))
2012
2017
t .Errorf ("pod %q scheduling should failed, error: %v" , p .Name , err )
2013
2018
}
2014
2019
}
@@ -2146,29 +2151,42 @@ func parseTestUserEntry(entries []nrtTestUserEntry, ns string) []nrtTestEntry {
2146
2151
return teList
2147
2152
}
2148
2153
2149
- func podFailedScheduling (c clientset.Interface , podNamespace , podName string ) ([]v1.Event , error ) {
2150
- var failedSchedulingEvents []v1.Event
2151
- opt := metav1.ListOptions {
2152
- FieldSelector : fmt .Sprintf ("involvedObject.name=%s" , podName ),
2153
- TypeMeta : metav1.TypeMeta {Kind : "Pod" },
2154
+ func getPodEvents (c clientset.Interface , podNamespace , podName string ) ([]v1.Event , error ) {
2155
+ opts := metav1.ListOptions {
2156
+ FieldSelector : fields .SelectorFromSet (map [string ]string {
2157
+ "involvedObject.name" : podName ,
2158
+ "involvedObject.namespace" : podNamespace ,
2159
+ // TODO: use uid
2160
+ }).String (),
2161
+ TypeMeta : metav1.TypeMeta {Kind : "Pod" },
2154
2162
}
2155
- events , err := c .CoreV1 ().Events (podNamespace ).List (context .TODO (), opt )
2163
+ evs , err := c .CoreV1 ().Events (podNamespace ).List (context .TODO (), opts )
2156
2164
if err != nil {
2157
- return failedSchedulingEvents , err
2165
+ return nil , err
2158
2166
}
2167
+ return evs .Items , nil
2168
+ }
2159
2169
2160
- for _ , e := range events .Items {
2161
- if e .Reason == "FailedScheduling" {
2162
- failedSchedulingEvents = append (failedSchedulingEvents , e )
2170
+ func filterPodFailedSchedulingEvents (events []v1.Event ) []v1.Event {
2171
+ var failedSchedulingEvents []v1.Event
2172
+ for _ , ev := range events {
2173
+ if ev .Reason == "FailedScheduling" {
2174
+ failedSchedulingEvents = append (failedSchedulingEvents , ev )
2163
2175
}
2164
2176
}
2165
- return failedSchedulingEvents , nil
2177
+ return failedSchedulingEvents
2166
2178
}
2167
2179
2168
- func formatObject ( obj interface {} ) string {
2169
- bytes , err := json . Marshal ( obj )
2170
- if err != nil {
2171
- return fmt .Sprintf ( "<ERROR: %s> " , err )
2180
+ func formatEvents ( events []v1. Event ) string {
2181
+ var sb strings. Builder
2182
+ for idx , ev := range events {
2183
+ fmt .Fprintf ( & sb , "%02d - %s \n " , idx , eventToString ( ev ) )
2172
2184
}
2173
- return string (bytes )
2185
+ return sb .String ()
2186
+ }
2187
+
2188
+ func eventToString (ev v1.Event ) string {
2189
+ return fmt .Sprintf ("type=%q action=%q message=%q reason=%q reportedBy={%s/%s}" ,
2190
+ ev .Type , ev .Action , ev .Message , ev .Reason , ev .ReportingController , ev .ReportingInstance ,
2191
+ )
2174
2192
}
0 commit comments