Skip to content

Commit

Permalink
Adding GetFieldSelector() to client.EventInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiljindal committed Mar 20, 2015
1 parent c0ff504 commit 1b87060
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
50 changes: 37 additions & 13 deletions pkg/client/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type EventInterface interface {
// Search finds events about the specified object
Search(objOrRef runtime.Object) (*api.EventList, error)
Delete(name string) error
// Returns the appropriate field selector based on the API version being used to communicate with the server.
// The returned field selector can be used with List and Watch to filter desired events.
GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector
}

// events implements Events interface
Expand Down Expand Up @@ -148,20 +151,13 @@ func (e *events) Search(objOrRef runtime.Object) (*api.EventList, error) {
if e.namespace != "" && ref.Namespace != e.namespace {
return nil, fmt.Errorf("won't be able to find any events of namespace '%v' in namespace '%v'", ref.Namespace, e.namespace)
}
fields := fields.Set{}
if ref.Kind != "" {
fields["involvedObject.kind"] = ref.Kind
stringRefUID := string(ref.UID)
var refUID *string
if stringRefUID != "" {
refUID = &stringRefUID
}
if ref.Namespace != "" {
fields["involvedObject.namespace"] = ref.Namespace
}
if ref.Name != "" {
fields["involvedObject.name"] = ref.Name
}
if ref.UID != "" {
fields["involvedObject.uid"] = string(ref.UID)
}
return e.List(labels.Everything(), fields.AsSelector())
fieldSelector := e.GetFieldSelector(&ref.Name, &ref.Namespace, &ref.Kind, refUID)
return e.List(labels.Everything(), fieldSelector)
}

// Delete deletes an existing event.
Expand All @@ -173,3 +169,31 @@ func (e *events) Delete(name string) error {
Do().
Error()
}

// Returns the appropriate field selector based on the API version being used to communicate with the server.
// The returned field selector can be used with List and Watch to filter desired events.
func (e *events) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {
apiVersion := e.client.APIVersion()
field := fields.Set{}
if involvedObjectName != nil {
field[getInvolvedObjectNameFieldLabel(apiVersion)] = *involvedObjectName
}
if involvedObjectNamespace != nil {
field["involvedObject.namespace"] = *involvedObjectNamespace
}
if involvedObjectKind != nil {
field["involvedObject.kind"] = *involvedObjectKind
}
if involvedObjectUID != nil {
field["involvedObject.uid"] = *involvedObjectUID
}
return field.AsSelector()
}

// Returns the appropriate field label to use for name of the involved object as per the given API version.
func getInvolvedObjectNameFieldLabel(version string) string {
if api.PreV1Beta3(version) {
return "involvedObject.id"
}
return "involvedObject.name"
}
6 changes: 5 additions & 1 deletion pkg/client/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ func TestEventSearch(t *testing.T) {
Method: "GET",
Path: "/events",
Query: url.Values{
"fields": []string{"involvedObject.kind=Pod,involvedObject.name=foo,involvedObject.namespace=baz"},
"fields": []string{
"involvedObject.kind=Pod,",
getInvolvedObjectNameFieldLabel(testapi.Version()) + "=foo,",
"involvedObject.namespace=baz",
},
"labels": []string{},
},
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/fake_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ func (c *FakeEvents) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-event", Value: name})
return nil
}

func (c *FakeEvents) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-field-selector"})
return fields.Everything()
}
10 changes: 3 additions & 7 deletions pkg/kubectl/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/golang/glog"
)
Expand Down Expand Up @@ -204,13 +203,10 @@ func (d *PodDescriber) Describe(namespace, name string) (string, error) {

pod, err := pc.Get(name)
if err != nil {
events, err2 := d.Events(namespace).List(
eventsInterface := d.Events(namespace)
events, err2 := eventsInterface.List(
labels.Everything(),
fields.Set{
"involvedObject.name": name,
"involvedObject.namespace": namespace,
}.AsSelector(),
)
eventsInterface.GetFieldSelector(&name, &namespace, nil, nil))
if err2 == nil && len(events.Items) > 0 {
return tabbedString(func(out io.Writer) error {
fmt.Fprintf(out, "Pod '%v': error '%v', but found events.\n", name, err)
Expand Down
1 change: 0 additions & 1 deletion pkg/registry/event/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func (rs *REST) getAttrs(obj runtime.Object) (objLabels labels.Set, objFields fi
if !ok {
return nil, nil, fmt.Errorf("invalid object type")
}
// TODO: internal version leaks through here. This should be versioned.
return labels.Set{}, fields.Set{
"involvedObject.kind": event.InvolvedObject.Kind,
"involvedObject.namespace": event.InvolvedObject.Namespace,
Expand Down

0 comments on commit 1b87060

Please sign in to comment.