From ecfe88cfc766de7b509dbfac645ab00d26f3263c Mon Sep 17 00:00:00 2001 From: Eran Raichstein Date: Mon, 21 Feb 2022 15:28:02 +0200 Subject: [PATCH] add hostip for k8s --- README.md | 2 +- pkg/pipeline/transform/kubernetes/kubernetes.go | 2 ++ pkg/pipeline/transform/kubernetes/kubernetes_test.go | 3 ++- pkg/pipeline/transform/transform_network.go | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 878b76522..29208d200 100644 --- a/README.md +++ b/README.md @@ -307,7 +307,7 @@ The fifth rule `add_kubernetes` generates new fields with kubernetes information matching the `input` value (`srcIP` in the example above) with kubernetes `nodes`, `pods` and `services` IPs. All the kubernetes fields will be named by appending `output` value (`srcK8S` in the example above) to the kubernetes metadata field names -(e.g., `Namespace`, `Name`, `Type`, `OwnerName`, `OwnerType` ) +(e.g., `Namespace`, `Name`, `Type`, `HostIP`, `OwnerName`, `OwnerType` ) In addition, if the `parameters` value is not empty, fields with kubernetes labels will be generated, and named by appending `parameters` value to the label keys. diff --git a/pkg/pipeline/transform/kubernetes/kubernetes.go b/pkg/pipeline/transform/kubernetes/kubernetes.go index f798f4e82..8a69d7c49 100644 --- a/pkg/pipeline/transform/kubernetes/kubernetes.go +++ b/pkg/pipeline/transform/kubernetes/kubernetes.go @@ -62,6 +62,7 @@ type Info struct { Labels map[string]string OwnerReferences []metav1.OwnerReference Owner Owner + HostIP string } func (k *KubeData) GetInfo(ip string) (*Info, error) { @@ -78,6 +79,7 @@ func (k *KubeData) GetInfo(ip string) (*Info, error) { Namespace: pod.Namespace, Labels: pod.Labels, OwnerReferences: pod.OwnerReferences, + HostIP: pod.Status.HostIP, } case typeNode: node := objs[0].(*v1.Node) diff --git a/pkg/pipeline/transform/kubernetes/kubernetes_test.go b/pkg/pipeline/transform/kubernetes/kubernetes_test.go index 7729468c8..68b4197da 100644 --- a/pkg/pipeline/transform/kubernetes/kubernetes_test.go +++ b/pkg/pipeline/transform/kubernetes/kubernetes_test.go @@ -43,7 +43,7 @@ type InformerInterface interface { } func (indexMock *IndexerMock) ByIndex(indexName, indexedValue string) ([]interface{}, error) { - pod := fakePod("podName", "podNamespace", "podHost") + pod := fakePod("podName", "podNamespace", "podHostIP") podInterface := interface{}(pod) return []interface{}{podInterface}, nil } @@ -77,6 +77,7 @@ func TestKubeData_getInfo(t *testing.T) { Type: "Pod", Name: "podName", Namespace: "podNamespace", + HostIP: "podHostIP", Owner: Owner{Name: "podName", Type: "Pod"}, } kubeData = KubeData{ipInformers: map[string]cache.SharedIndexInformer{}} diff --git a/pkg/pipeline/transform/transform_network.go b/pkg/pipeline/transform/transform_network.go index c6ada8593..d65b8ae1a 100644 --- a/pkg/pipeline/transform/transform_network.go +++ b/pkg/pipeline/transform/transform_network.go @@ -140,6 +140,9 @@ func (n *Network) Transform(inputEntry config.GenericMap) config.GenericMap { outputEntries[rule.Parameters+"_"+labelKey] = labelValue } } + if kubeInfo.HostIP != "" { + outputEntries[rule.Output+"_HostIP"] = kubeInfo.HostIP + } default: log.Panicf("unknown type %s for transform.Network rule: %v", rule.Type, rule) }