Skip to content

Commit 2e51626

Browse files
author
Mario Macias
authored
NETOBSERV-666: K8s decoration not adding namespace if empty (#335)
1 parent 1d6ef70 commit 2e51626

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

pkg/pipeline/transform/transform_network.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ func (n *Network) Transform(inputEntry config.GenericMap) (config.GenericMap, bo
119119
log.Debugf("Can't find kubernetes info for IP %v err %v", outputEntry[rule.Input], err)
120120
continue
121121
}
122-
outputEntry[rule.Output+"_Namespace"] = kubeInfo.Namespace
122+
// NETOBSERV-666: avoid putting empty namespaces or Loki aggregation queries will
123+
// differentiate between empty and nil namespaces.
124+
if kubeInfo.Namespace != "" {
125+
outputEntry[rule.Output+"_Namespace"] = kubeInfo.Namespace
126+
}
123127
outputEntry[rule.Output+"_Name"] = kubeInfo.Name
124128
outputEntry[rule.Output+"_Type"] = kubeInfo.Type
125129
outputEntry[rule.Output+"_OwnerName"] = kubeInfo.Owner.Name

pkg/pipeline/transform/transform_network_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
package transform
1919

2020
import (
21+
"errors"
2122
"os"
2223
"path"
2324
"testing"
2425

2526
"github.com/netobserv/flowlogs-pipeline/pkg/api"
2627
"github.com/netobserv/flowlogs-pipeline/pkg/config"
28+
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/kubernetes"
2729
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/location"
2830
netdb "github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/netdb"
2931
"github.com/netobserv/flowlogs-pipeline/pkg/test"
32+
"github.com/stretchr/testify/assert"
3033
"github.com/stretchr/testify/require"
3134
)
3235

@@ -325,3 +328,41 @@ func Test_Transform_AddIfScientificNotation(t *testing.T) {
325328
require.Equal(t, true, output["dir_Evaluate"])
326329
require.Equal(t, "out", output["dir"])
327330
}
331+
332+
func TestTransform_K8sEmptyNamespace(t *testing.T) {
333+
kubernetes.Data = &fakeKubeData{}
334+
nt := Network{
335+
TransformNetwork: api.TransformNetwork{
336+
Rules: api.NetworkTransformRules{{
337+
Type: api.OpAddKubernetes,
338+
Input: "SrcAddr",
339+
Output: "SrcK8s",
340+
}, {
341+
Type: api.OpAddKubernetes,
342+
Input: "DstAddr",
343+
Output: "DstK8s",
344+
}},
345+
},
346+
}
347+
// We need to check that, whether it returns NotFound or just an empty namespace,
348+
// there is no map entry for that namespace (an empty-valued map entry is not valid)
349+
out, _ := nt.Transform(config.GenericMap{
350+
"SrcAddr": "1.2.3.4", // would return an empty namespace
351+
"DstAddr": "3.2.1.0", // would return NotFound
352+
})
353+
assert.NotContains(t, out, "SrcK8s_Namespace")
354+
assert.NotContains(t, out, "DstK8s_Namespace")
355+
}
356+
357+
type fakeKubeData struct{}
358+
359+
func (d *fakeKubeData) InitFromConfig(_ string) error {
360+
return nil
361+
}
362+
func (*fakeKubeData) GetInfo(n string) (*kubernetes.Info, error) {
363+
// If found, returns an empty info (empty namespace)
364+
if n == "1.2.3.4" {
365+
return &kubernetes.Info{}, nil
366+
}
367+
return nil, errors.New("notFound")
368+
}

0 commit comments

Comments
 (0)