Skip to content

Commit 400e1b3

Browse files
authored
Merge pull request #1186 from erain/event-resource-attribution-hardening
Tighten resource attribution in the Stackdriver event sink
2 parents 102ba58 + 208ca39 commit 400e1b3

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

event-exporter/sinks/stackdriver/log_entry_factory_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func TestFromEvent(t *testing.T) {
111111
{
112112
desc: "k8s pod event with pod labels",
113113
event: &corev1.Event{
114+
ObjectMeta: metav1.ObjectMeta{Namespace: "test_namespace"},
114115
Type: "Normal",
115116
InvolvedObject: involvedPodObject,
116117
LastTimestamp: lastTimestamp,

event-exporter/sinks/stackdriver/monitored_resource_factory.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ func (f *monitoredResourceFactory) resourceFromEvent(event *corev1.Event) *sd.Mo
7474

7575
switch event.InvolvedObject.Kind {
7676
case pod:
77-
monitoredResource = f.buildPodMonitoredResource(event)
77+
// The event's own metadata.namespace is the RBAC-enforced source
78+
// of truth for where the event was created. Only emit a pod-scoped
79+
// resource when the involved object's namespace agrees with it;
80+
// otherwise fall back to the default cluster-scoped resource.
81+
if event.Namespace != "" && event.Namespace == event.InvolvedObject.Namespace {
82+
monitoredResource = f.buildPodMonitoredResource(event)
83+
} else {
84+
monitoredResource = f.defaultResource
85+
}
7886
case node:
7987
monitoredResource = f.buildNodeMonitoredResource(event)
8088
default:
@@ -86,7 +94,7 @@ func (f *monitoredResourceFactory) resourceFromEvent(event *corev1.Event) *sd.Mo
8694
func (f *monitoredResourceFactory) buildPodMonitoredResource(event *corev1.Event) *sd.MonitoredResource {
8795
labels := copyMap(f.commonLabels)
8896
labels[podName] = event.InvolvedObject.Name
89-
labels[namespaceName] = event.InvolvedObject.Namespace
97+
labels[namespaceName] = event.Namespace
9098

9199
return &sd.MonitoredResource{
92100
Type: k8sPod,

event-exporter/sinks/stackdriver/monitored_resource_factory_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
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

1314
func 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

Comments
 (0)