Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NETOBSERV-666: K8s decoration not adding namespace if empty #335

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}