-
Notifications
You must be signed in to change notification settings - Fork 431
/
event.go
61 lines (53 loc) · 1.7 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package framework
import (
"context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/wait"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
type EventClient struct {
f *Framework
typedcorev1.EventInterface
namespace string
}
func (f *Framework) EventClient() *EventClient {
return f.EventClientNS(f.Namespace.Name)
}
func (f *Framework) EventClientNS(namespace string) *EventClient {
return &EventClient{
f: f,
EventInterface: f.ClientSet.CoreV1().Events(namespace),
namespace: namespace,
}
}
// WaitToHaveEvent waits the provided resource to have the specified event(s)
func (c *EventClient) WaitToHaveEvent(kind, name, eventType, reason, sourceComponent, sourceHost string) []corev1.Event {
var result []corev1.Event
err := wait.PollUntilContextTimeout(context.Background(), poll, timeout, false, func(ctx context.Context) (bool, error) {
Logf("Waiting for %s %s/%s to have event %s/%s", kind, c.namespace, name, eventType, reason)
selector := fields.Set{
"involvedObject.kind": kind,
"involvedObject.name": name,
"type": eventType,
"reason": reason,
}
events, err := c.List(ctx, metav1.ListOptions{FieldSelector: selector.AsSelector().String()})
if err != nil {
return handleWaitingAPIError(err, true, "listing events")
}
for _, event := range events.Items {
if sourceComponent != "" && event.Source.Component != sourceComponent {
continue
}
if sourceHost != "" && event.Source.Host != sourceHost {
continue
}
result = append(result, event)
}
return len(result) != 0, nil
})
ExpectNoError(err)
return result
}