From e8d30f019dc48f443dd0c618423172e7c6ad178a Mon Sep 17 00:00:00 2001 From: saadali Date: Mon, 29 Dec 2014 17:10:38 -0800 Subject: [PATCH] Modify "kubectl get events" to print FieldPath so BoundPod events for the same Pod but different containers can be differentiated --- pkg/api/types.go | 9 +++++---- pkg/api/v1beta1/types.go | 9 +++++---- pkg/api/v1beta2/types.go | 9 +++++---- pkg/api/v1beta3/types.go | 9 +++++---- pkg/kubectl/resource_printer.go | 5 +++-- pkg/kubelet/kubelet.go | 6 +++++- pkg/kubelet/kubelet_test.go | 8 +++++--- 7 files changed, 33 insertions(+), 22 deletions(-) diff --git a/pkg/api/types.go b/pkg/api/types.go index 89855d46a4..68e2d68eb9 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -975,10 +975,11 @@ type ObjectReference struct { ResourceVersion string `json:"resourceVersion,omitempty"` // Optional. If referring to a piece of an object instead of an entire object, this string - // should contain a valid field access statement. For example, - // if the object reference is to a container within a pod, this would take on a value like: - // "desiredState.manifest.containers[2]". Such statements are valid language constructs in - // both go and JavaScript. This is syntax is chosen only to have some well-defined way of + // should contain information to identify the sub-object. For example, if the object + // reference is to a container within a pod, this would take on a value like: + // "spec.containers{name}" (where "name" refers to the name of the container that triggered + // the event) or if no container name is specified "spec.containers[2]" (container with + // index 2 in this pod). This syntax is chosen only to have some well-defined way of // referencing a part of an object. // TODO: this design is not final and this field is subject to change in the future. FieldPath string `json:"fieldPath,omitempty"` diff --git a/pkg/api/v1beta1/types.go b/pkg/api/v1beta1/types.go index 4d768ad27f..472de098f9 100644 --- a/pkg/api/v1beta1/types.go +++ b/pkg/api/v1beta1/types.go @@ -777,10 +777,11 @@ type ObjectReference struct { ResourceVersion string `json:"resourceVersion,omitempty" description:"specific resourceVersion to which this reference is made, if any"` // Optional. If referring to a piece of an object instead of an entire object, this string - // should contain a valid field access statement. For example, - // if the object reference is to a container within a pod, this would take on a value like: - // "desiredState.manifest.containers[2]". Such statements are valid language constructs in - // both go and JavaScript. This is syntax is chosen only to have some well-defined way of + // should contain information to identify the sub-object. For example, if the object + // reference is to a container within a pod, this would take on a value like: + // "spec.containers{name}" (where "name" refers to the name of the container that triggered + // the event) or if no container name is specified "spec.containers[2]" (container with + // index 2 in this pod). This syntax is chosen only to have some well-defined way of // referencing a part of an object. // TODO: this design is not final and this field is subject to change in the future. FieldPath string `json:"fieldPath,omitempty" description:"if referring to a piece of an object instead of an entire object, this string should contain a valid JSON/Go field access statement, such as desiredState.manifest.containers[2]"` diff --git a/pkg/api/v1beta2/types.go b/pkg/api/v1beta2/types.go index b8438ec8b8..e9c1cb1341 100644 --- a/pkg/api/v1beta2/types.go +++ b/pkg/api/v1beta2/types.go @@ -750,10 +750,11 @@ type ObjectReference struct { ResourceVersion string `json:"resourceVersion,omitempty" description:"specific resourceVersion to which this reference is made, if any"` // Optional. If referring to a piece of an object instead of an entire object, this string - // should contain a valid field access statement. For example, - // if the object reference is to a container within a pod, this would take on a value like: - // "desiredState.manifest.containers[2]". Such statements are valid language constructs in - // both go and JavaScript. This is syntax is chosen only to have some well-defined way of + // should contain information to identify the sub-object. For example, if the object + // reference is to a container within a pod, this would take on a value like: + // "spec.containers{name}" (where "name" refers to the name of the container that triggered + // the event) or if no container name is specified "spec.containers[2]" (container with + // index 2 in this pod). This syntax is chosen only to have some well-defined way of // referencing a part of an object. // TODO: this design is not final and this field is subject to change in the future. FieldPath string `json:"fieldPath,omitempty" description:"if referring to a piece of an object instead of an entire object, this string should contain a valid JSON/Go field access statement, such as desiredState.manifest.containers[2]"` diff --git a/pkg/api/v1beta3/types.go b/pkg/api/v1beta3/types.go index 12d1cc3175..80b0c40cb0 100644 --- a/pkg/api/v1beta3/types.go +++ b/pkg/api/v1beta3/types.go @@ -993,10 +993,11 @@ type ObjectReference struct { ResourceVersion string `json:"resourceVersion,omitempty"` // Optional. If referring to a piece of an object instead of an entire object, this string - // should contain a valid field access statement. For example, - // if the object reference is to a container within a pod, this would take on a value like: - // "spec.containers[2]". Such statements are valid language constructs in - // both go and JavaScript. This is syntax is chosen only to have some well-defined way of + // should contain information to identify the sub-object. For example, if the object + // reference is to a container within a pod, this would take on a value like: + // "spec.containers{name}" (where "name" refers to the name of the container that triggered + // the event) or if no container name is specified "spec.containers[2]" (container with + // index 2 in this pod). This syntax is chosen only to have some well-defined way of // referencing a part of an object. // TODO: this design is not final and this field is subject to change in the future. FieldPath string `json:"fieldPath,omitempty"` diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 797c948b44..e05b2c42be 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -193,7 +193,7 @@ var replicationControllerColumns = []string{"CONTROLLER", "CONTAINER(S)", "IMAGE var serviceColumns = []string{"NAME", "LABELS", "SELECTOR", "IP", "PORT"} var minionColumns = []string{"NAME", "LABELS"} var statusColumns = []string{"STATUS"} -var eventColumns = []string{"TIME", "NAME", "KIND", "CONDITION", "REASON", "MESSAGE"} +var eventColumns = []string{"TIME", "NAME", "KIND", "SUBOBJECT", "CONDITION", "REASON", "MESSAGE"} // addDefaultHandlers adds print handlers for default Kubernetes types. func (h *HumanReadablePrinter) addDefaultHandlers() { @@ -339,10 +339,11 @@ func printStatus(status *api.Status, w io.Writer) error { func printEvent(event *api.Event, w io.Writer) error { _, err := fmt.Fprintf( - w, "%s\t%s\t%s\t%s\t%s\t%s\n", + w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", event.Timestamp.Time.Format(time.RFC1123Z), event.InvolvedObject.Name, event.InvolvedObject.Kind, + event.InvolvedObject.FieldPath, event.Condition, event.Reason, event.Message, diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 7fec0862f5..d5929c8844 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -463,7 +463,11 @@ func fieldPath(pod *api.BoundPod, container *api.Container) (string, error) { for i := range pod.Spec.Containers { here := &pod.Spec.Containers[i] if here.Name == container.Name { - return fmt.Sprintf("spec.containers[%d]", i), nil + if here.Name == "" { + return fmt.Sprintf("spec.containers[%d]", i), nil + } else { + return fmt.Sprintf("spec.containers{%s}", here.Name), nil + } } } return "", fmt.Errorf("container %#v not found in pod %#v", container, pod) diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 43246a7637..5880f3dba4 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -956,6 +956,7 @@ func TestFieldPath(t *testing.T) { pod := &api.BoundPod{Spec: api.PodSpec{Containers: []api.Container{ {Name: "foo"}, {Name: "bar"}, + {Name: ""}, {Name: "baz"}, }}} table := map[string]struct { @@ -964,9 +965,10 @@ func TestFieldPath(t *testing.T) { path string success bool }{ - "basic": {pod, &api.Container{Name: "foo"}, "spec.containers[0]", true}, - "basic2": {pod, &api.Container{Name: "baz"}, "spec.containers[2]", true}, - "basicSamePointer": {pod, &pod.Spec.Containers[0], "spec.containers[0]", true}, + "basic": {pod, &api.Container{Name: "foo"}, "spec.containers{foo}", true}, + "basic2": {pod, &api.Container{Name: "baz"}, "spec.containers{baz}", true}, + "emptyName": {pod, &api.Container{Name: ""}, "spec.containers[2]", true}, + "basicSamePointer": {pod, &pod.Spec.Containers[0], "spec.containers{foo}", true}, "missing": {pod, &api.Container{Name: "qux"}, "", false}, }