88 "fmt"
99 sd "google.golang.org/api/logging/v2"
1010 corev1 "k8s.io/api/core/v1"
11+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112)
1213
1314func TestMonitoredResourceFromEvent (t * testing.T ) {
@@ -31,8 +32,11 @@ func TestMonitoredResourceFromEvent(t *testing.T) {
3132 },
3233 },
3334 {
35+ // Pod event whose involvedObject namespace matches the event's
36+ // own metadata namespace is attributed to the pod.
3437 config : newTypesConfig ,
3538 event : & corev1.Event {
39+ ObjectMeta : metav1.ObjectMeta {Namespace : "test_pod_namespace" },
3640 InvolvedObject : corev1.ObjectReference {Kind : pod , Name : "test_pod_name" , Namespace : "test_pod_namespace" },
3741 },
3842 wanted : & sd.MonitoredResource {
@@ -46,6 +50,40 @@ func TestMonitoredResourceFromEvent(t *testing.T) {
4650 },
4751 },
4852 },
53+ {
54+ // Pod event whose involvedObject namespace disagrees with the
55+ // event's own metadata namespace must not be attributed to the
56+ // claimed pod; fall back to the cluster resource.
57+ config : newTypesConfig ,
58+ event : & corev1.Event {
59+ ObjectMeta : metav1.ObjectMeta {Namespace : "user_namespace" },
60+ InvolvedObject : corev1.ObjectReference {Kind : pod , Name : "test_pod_name" , Namespace : "kube-system" },
61+ },
62+ wanted : & sd.MonitoredResource {
63+ Type : k8sCluster ,
64+ Labels : map [string ]string {
65+ clusterName : newTypesConfig .clusterName ,
66+ location : newTypesConfig .location ,
67+ projectID : newTypesConfig .projectID ,
68+ },
69+ },
70+ },
71+ {
72+ // Pod event with no event-level namespace cannot be attributed
73+ // to a pod; fall back to the cluster resource.
74+ config : newTypesConfig ,
75+ event : & corev1.Event {
76+ InvolvedObject : corev1.ObjectReference {Kind : pod , Name : "test_pod_name" , Namespace : "kube-system" },
77+ },
78+ wanted : & sd.MonitoredResource {
79+ Type : k8sCluster ,
80+ Labels : map [string ]string {
81+ clusterName : newTypesConfig .clusterName ,
82+ location : newTypesConfig .location ,
83+ projectID : newTypesConfig .projectID ,
84+ },
85+ },
86+ },
4987 {
5088 config : newTypesConfig ,
5189 event : & corev1.Event {
0 commit comments