-
Notifications
You must be signed in to change notification settings - Fork 5
/
event.go
38 lines (34 loc) · 924 Bytes
/
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
package kubernetes
import (
"github.com/gophercloud/gophercloud"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
)
// SendErrorAsEvent sends an event for an error to the given objects and returns the error.
// It cast some openstack errors to send more information of the error in the event.
func SendErrorAsEvent(r record.EventRecorder, err error, objects ...runtime.Object) error {
if err == nil {
return nil
}
var body []byte
switch casted := err.(type) {
case gophercloud.ErrDefault401:
body = casted.Body
case gophercloud.ErrDefault403:
body = casted.Body
case gophercloud.ErrDefault404:
body = casted.Body
case gophercloud.ErrDefault409:
body = casted.Body
default:
body = []byte(err.Error())
}
if len(body) == 0 {
return err
}
for _, ob := range objects {
r.Event(ob, coreV1.EventTypeWarning, "Failed", string(body))
}
return err
}