Skip to content

Commit

Permalink
Merge pull request kubernetes#2066 from lavalamp/eventing2
Browse files Browse the repository at this point in the history
Event fixes
  • Loading branch information
smarterclayton committed Oct 30, 2014
2 parents ff9befe + a7cc25f commit 8a2d778
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pkg/api/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
return nil, fmt.Errorf("unexpected self link format: %v", meta.SelfLink())
}
return &ObjectReference{
Kind: kind,
APIVersion: version[1],
// TODO: correct Name and UID when TypeMeta makes a distinction
Kind: kind,
APIVersion: version[1],
Name: meta.Name(),
Namespace: meta.Namespace(),
UID: meta.UID(),
ResourceVersion: meta.ResourceVersion(),
}, nil
Expand Down
24 changes: 24 additions & 0 deletions pkg/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,5 +624,29 @@ func init() {
}
return s.Convert(&in.Items, &out.Items, 0)
},

// Object ID <-> Name
// TODO: amend the conversion package to allow overriding specific fields.
func(in *ObjectReference, out *newer.ObjectReference, s conversion.Scope) error {
out.Kind = in.Kind
out.Namespace = in.Namespace
out.Name = in.ID
out.UID = in.UID
out.APIVersion = in.APIVersion
out.ResourceVersion = in.ResourceVersion
out.FieldPath = in.FieldPath
return nil
},
func(in *newer.ObjectReference, out *ObjectReference, s conversion.Scope) error {
out.ID = in.Name
out.Kind = in.Kind
out.Namespace = in.Namespace
out.ID = in.Name
out.UID = in.UID
out.APIVersion = in.APIVersion
out.ResourceVersion = in.ResourceVersion
out.FieldPath = in.FieldPath
return nil
},
)
}
2 changes: 1 addition & 1 deletion pkg/api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ type ServerOpList struct {
type ObjectReference struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
ID string `json:"name,omitempty" yaml:"name,omitempty"`
UID string `json:"uid,omitempty" yaml:"uid,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
Expand Down
24 changes: 24 additions & 0 deletions pkg/api/v1beta2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,5 +574,29 @@ func init() {
}
return s.Convert(&in.Items, &out.Items, 0)
},

// Object ID <-> Name
// TODO: amend the conversion package to allow overriding specific fields.
func(in *ObjectReference, out *newer.ObjectReference, s conversion.Scope) error {
out.Kind = in.Kind
out.Namespace = in.Namespace
out.Name = in.ID
out.UID = in.UID
out.APIVersion = in.APIVersion
out.ResourceVersion = in.ResourceVersion
out.FieldPath = in.FieldPath
return nil
},
func(in *newer.ObjectReference, out *ObjectReference, s conversion.Scope) error {
out.ID = in.Name
out.Kind = in.Kind
out.Namespace = in.Namespace
out.ID = in.Name
out.UID = in.UID
out.APIVersion = in.APIVersion
out.ResourceVersion = in.ResourceVersion
out.FieldPath = in.FieldPath
return nil
},
)
}
2 changes: 1 addition & 1 deletion pkg/api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ type ServerOpList struct {
type ObjectReference struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
ID string `json:"name,omitempty" yaml:"name,omitempty"`
UID string `json:"uid,omitempty" yaml:"uid,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
func (c *events) Create(event *api.Event) (*api.Event, error) {
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
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/client/record/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
)

// EventRecorder knows how to store events (client.Client implements it.)
// EventRecorder must respect the namespace that will be embedded in 'event'.
type EventRecorder interface {
Create(event *api.Event) (*api.Event, error)
}
Expand Down Expand Up @@ -96,19 +97,27 @@ var events = watch.NewMux(queueLen)
// handling of events, so imagine people writing switch statements to handle them. You want to
// make that easy.
// '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) {
ref, err := api.GetReference(object)
if err != nil {
glog.Errorf("Could not construct reference to: %#v due to: %v", object, err)
return
}
ref.FieldPath = fieldPath
t := util.Now()

e := &api.Event{
ObjectMeta: api.ObjectMeta{
Name: fmt.Sprintf("%v.%x", ref.Name, t.UnixNano()),
Namespace: ref.Namespace,
},
InvolvedObject: *ref,
Status: status,
Reason: reason,
Message: message,
Timestamp: util.Now(),
Timestamp: t,
}

events.Action(watch.Added, e)
Expand Down
20 changes: 16 additions & 4 deletions pkg/client/record/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package record_test
import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
Expand Down Expand Up @@ -55,9 +56,10 @@ func TestEventf(t *testing.T) {
{
obj: &api.Pod{
ObjectMeta: api.ObjectMeta{
SelfLink: "/api/v1beta1/pods/foo",
Name: "foo",
UID: "bar",
SelfLink: "/api/v1beta1/pods/foo",
Name: "foo",
Namespace: "baz",
UID: "bar",
},
},
fieldPath: "desiredState.manifest.containers[2]",
Expand All @@ -66,9 +68,14 @@ func TestEventf(t *testing.T) {
messageFmt: "some verbose message: %v",
elements: []interface{}{1},
expect: &api.Event{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: "baz",
},
InvolvedObject: api.ObjectReference{
Kind: "Pod",
Name: "foo",
Namespace: "baz",
UID: "bar",
APIVersion: "v1beta1",
FieldPath: "desiredState.manifest.containers[2]",
Expand All @@ -78,7 +85,7 @@ func TestEventf(t *testing.T) {
Message: "some verbose message: 1",
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`,
},
}

Expand All @@ -92,6 +99,11 @@ func TestEventf(t *testing.T) {
t.Errorf("timestamp wasn't set")
}
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) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
}
Expand Down

0 comments on commit 8a2d778

Please sign in to comment.