mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
Add namespaces to event creation path.
This commit is contained in:
parent
ab9346d107
commit
a7cc25f7ff
@ -49,10 +49,10 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
|
|||||||
return nil, fmt.Errorf("unexpected self link format: %v", meta.SelfLink())
|
return nil, fmt.Errorf("unexpected self link format: %v", meta.SelfLink())
|
||||||
}
|
}
|
||||||
return &ObjectReference{
|
return &ObjectReference{
|
||||||
Kind: kind,
|
Kind: kind,
|
||||||
APIVersion: version[1],
|
APIVersion: version[1],
|
||||||
// TODO: correct Name and UID when TypeMeta makes a distinction
|
|
||||||
Name: meta.Name(),
|
Name: meta.Name(),
|
||||||
|
Namespace: meta.Namespace(),
|
||||||
UID: meta.UID(),
|
UID: meta.UID(),
|
||||||
ResourceVersion: meta.ResourceVersion(),
|
ResourceVersion: meta.ResourceVersion(),
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -50,7 +50,7 @@ func newEvents(c *Client) *events {
|
|||||||
// Create makes a new event. Returns the copy of the event the server returns, or an error.
|
// Create makes a new event. Returns the copy of the event the server returns, or an error.
|
||||||
func (c *events) Create(event *api.Event) (*api.Event, error) {
|
func (c *events) Create(event *api.Event) (*api.Event, error) {
|
||||||
result := &api.Event{}
|
result := &api.Event{}
|
||||||
err := c.r.Post().Path("events").Body(event).Do().Into(result)
|
err := c.r.Post().Path("events").Namespace(event.Namespace).Body(event).Do().Into(result)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// EventRecorder knows how to store events (client.Client implements it.)
|
// EventRecorder knows how to store events (client.Client implements it.)
|
||||||
|
// EventRecorder must respect the namespace that will be embedded in 'event'.
|
||||||
type EventRecorder interface {
|
type EventRecorder interface {
|
||||||
Create(event *api.Event) (*api.Event, error)
|
Create(event *api.Event) (*api.Event, error)
|
||||||
}
|
}
|
||||||
@ -96,6 +97,8 @@ var events = watch.NewMux(queueLen)
|
|||||||
// handling of events, so imagine people writing switch statements to handle them. You want to
|
// handling of events, so imagine people writing switch statements to handle them. You want to
|
||||||
// make that easy.
|
// make that easy.
|
||||||
// 'message' is intended to be human readable.
|
// 'message' is intended to be human readable.
|
||||||
|
//
|
||||||
|
// The resulting event will be created in the same namespace as the reference object.
|
||||||
func Event(object runtime.Object, fieldPath, status, reason, message string) {
|
func Event(object runtime.Object, fieldPath, status, reason, message string) {
|
||||||
ref, err := api.GetReference(object)
|
ref, err := api.GetReference(object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -103,12 +106,18 @@ func Event(object runtime.Object, fieldPath, status, reason, message string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ref.FieldPath = fieldPath
|
ref.FieldPath = fieldPath
|
||||||
|
t := util.Now()
|
||||||
|
|
||||||
e := &api.Event{
|
e := &api.Event{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: fmt.Sprintf("%v.%x", ref.Name, t.UnixNano()),
|
||||||
|
Namespace: ref.Namespace,
|
||||||
|
},
|
||||||
InvolvedObject: *ref,
|
InvolvedObject: *ref,
|
||||||
Status: status,
|
Status: status,
|
||||||
Reason: reason,
|
Reason: reason,
|
||||||
Message: message,
|
Message: message,
|
||||||
Timestamp: util.Now(),
|
Timestamp: t,
|
||||||
}
|
}
|
||||||
|
|
||||||
events.Action(watch.Added, e)
|
events.Action(watch.Added, e)
|
||||||
|
@ -19,6 +19,7 @@ package record_test
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
@ -55,9 +56,10 @@ func TestEventf(t *testing.T) {
|
|||||||
{
|
{
|
||||||
obj: &api.Pod{
|
obj: &api.Pod{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
SelfLink: "/api/v1beta1/pods/foo",
|
SelfLink: "/api/v1beta1/pods/foo",
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
UID: "bar",
|
Namespace: "baz",
|
||||||
|
UID: "bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
fieldPath: "desiredState.manifest.containers[2]",
|
fieldPath: "desiredState.manifest.containers[2]",
|
||||||
@ -66,9 +68,14 @@ func TestEventf(t *testing.T) {
|
|||||||
messageFmt: "some verbose message: %v",
|
messageFmt: "some verbose message: %v",
|
||||||
elements: []interface{}{1},
|
elements: []interface{}{1},
|
||||||
expect: &api.Event{
|
expect: &api.Event{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
Namespace: "baz",
|
||||||
|
},
|
||||||
InvolvedObject: api.ObjectReference{
|
InvolvedObject: api.ObjectReference{
|
||||||
Kind: "Pod",
|
Kind: "Pod",
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
|
Namespace: "baz",
|
||||||
UID: "bar",
|
UID: "bar",
|
||||||
APIVersion: "v1beta1",
|
APIVersion: "v1beta1",
|
||||||
FieldPath: "desiredState.manifest.containers[2]",
|
FieldPath: "desiredState.manifest.containers[2]",
|
||||||
@ -78,7 +85,7 @@ func TestEventf(t *testing.T) {
|
|||||||
Message: "some verbose message: 1",
|
Message: "some verbose message: 1",
|
||||||
Source: "eventTest",
|
Source: "eventTest",
|
||||||
},
|
},
|
||||||
expectLog: `Event(api.ObjectReference{Kind:"Pod", Namespace:"", Name:"foo", UID:"bar", APIVersion:"v1beta1", ResourceVersion:"", FieldPath:"desiredState.manifest.containers[2]"}): status: 'running', reason: 'started' some verbose message: 1`,
|
expectLog: `Event(api.ObjectReference{Kind:"Pod", Namespace:"baz", Name:"foo", UID:"bar", APIVersion:"v1beta1", ResourceVersion:"", FieldPath:"desiredState.manifest.containers[2]"}): status: 'running', reason: 'started' some verbose message: 1`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,6 +99,11 @@ func TestEventf(t *testing.T) {
|
|||||||
t.Errorf("timestamp wasn't set")
|
t.Errorf("timestamp wasn't set")
|
||||||
}
|
}
|
||||||
a.Timestamp = item.expect.Timestamp
|
a.Timestamp = item.expect.Timestamp
|
||||||
|
// Check that name has the right prefix.
|
||||||
|
if n, en := a.Name, item.expect.Name; !strings.HasPrefix(n, en) {
|
||||||
|
t.Errorf("Name '%v' does not contain prefix '%v'", n, en)
|
||||||
|
}
|
||||||
|
a.Name = item.expect.Name
|
||||||
if e, a := item.expect, &a; !reflect.DeepEqual(e, a) {
|
if e, a := item.expect, &a; !reflect.DeepEqual(e, a) {
|
||||||
t.Errorf("diff: %s", util.ObjectDiff(e, a))
|
t.Errorf("diff: %s", util.ObjectDiff(e, a))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user