From 2e516262a2e2cee26e2027dece923a6b6cba4936 Mon Sep 17 00:00:00 2001 From: Mario Macias Date: Fri, 25 Nov 2022 14:30:57 +0100 Subject: [PATCH] NETOBSERV-666: K8s decoration not adding namespace if empty (#335) --- pkg/pipeline/transform/transform_network.go | 6 ++- .../transform/transform_network_test.go | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pkg/pipeline/transform/transform_network.go b/pkg/pipeline/transform/transform_network.go index 22c2d76f6..06fe92723 100644 --- a/pkg/pipeline/transform/transform_network.go +++ b/pkg/pipeline/transform/transform_network.go @@ -119,7 +119,11 @@ func (n *Network) Transform(inputEntry config.GenericMap) (config.GenericMap, bo log.Debugf("Can't find kubernetes info for IP %v err %v", outputEntry[rule.Input], err) continue } - outputEntry[rule.Output+"_Namespace"] = kubeInfo.Namespace + // NETOBSERV-666: avoid putting empty namespaces or Loki aggregation queries will + // differentiate between empty and nil namespaces. + if kubeInfo.Namespace != "" { + outputEntry[rule.Output+"_Namespace"] = kubeInfo.Namespace + } outputEntry[rule.Output+"_Name"] = kubeInfo.Name outputEntry[rule.Output+"_Type"] = kubeInfo.Type outputEntry[rule.Output+"_OwnerName"] = kubeInfo.Owner.Name diff --git a/pkg/pipeline/transform/transform_network_test.go b/pkg/pipeline/transform/transform_network_test.go index 0eea53713..becca2681 100644 --- a/pkg/pipeline/transform/transform_network_test.go +++ b/pkg/pipeline/transform/transform_network_test.go @@ -18,15 +18,18 @@ package transform import ( + "errors" "os" "path" "testing" "github.com/netobserv/flowlogs-pipeline/pkg/api" "github.com/netobserv/flowlogs-pipeline/pkg/config" + "github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/kubernetes" "github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/location" netdb "github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/netdb" "github.com/netobserv/flowlogs-pipeline/pkg/test" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -325,3 +328,41 @@ func Test_Transform_AddIfScientificNotation(t *testing.T) { require.Equal(t, true, output["dir_Evaluate"]) require.Equal(t, "out", output["dir"]) } + +func TestTransform_K8sEmptyNamespace(t *testing.T) { + kubernetes.Data = &fakeKubeData{} + nt := Network{ + TransformNetwork: api.TransformNetwork{ + Rules: api.NetworkTransformRules{{ + Type: api.OpAddKubernetes, + Input: "SrcAddr", + Output: "SrcK8s", + }, { + Type: api.OpAddKubernetes, + Input: "DstAddr", + Output: "DstK8s", + }}, + }, + } + // We need to check that, whether it returns NotFound or just an empty namespace, + // there is no map entry for that namespace (an empty-valued map entry is not valid) + out, _ := nt.Transform(config.GenericMap{ + "SrcAddr": "1.2.3.4", // would return an empty namespace + "DstAddr": "3.2.1.0", // would return NotFound + }) + assert.NotContains(t, out, "SrcK8s_Namespace") + assert.NotContains(t, out, "DstK8s_Namespace") +} + +type fakeKubeData struct{} + +func (d *fakeKubeData) InitFromConfig(_ string) error { + return nil +} +func (*fakeKubeData) GetInfo(n string) (*kubernetes.Info, error) { + // If found, returns an empty info (empty namespace) + if n == "1.2.3.4" { + return &kubernetes.Info{}, nil + } + return nil, errors.New("notFound") +}