Skip to content

Commit

Permalink
NETOBSERV-666: K8s decoration not adding namespace if empty (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Macias authored Nov 25, 2022
1 parent 1d6ef70 commit 2e51626
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/pipeline/transform/transform_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions pkg/pipeline/transform/transform_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}

0 comments on commit 2e51626

Please sign in to comment.