Skip to content

Commit f9f80c3

Browse files
committed
Add test, fix duplicated namespaces filters
1 parent ddee483 commit f9f80c3

File tree

3 files changed

+224
-16
lines changed

3 files changed

+224
-16
lines changed

internal/controller/flp/flp_monolith_objects.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (b *monolithBuilder) deployment(annotations map[string]string) *appsv1.Depl
119119
}
120120

121121
func (b *monolithBuilder) configMaps() (*corev1.ConfigMap, string, *corev1.ConfigMap, error) {
122-
kafkaStage := newGRPCPipeline(b.desired)
122+
grpcStage := newGRPCPipeline(b.desired)
123123
pipeline := newPipelineBuilder(
124124
b.desired,
125125
b.flowMetrics,
@@ -128,7 +128,7 @@ func (b *monolithBuilder) configMaps() (*corev1.ConfigMap, string, *corev1.Confi
128128
b.info.Loki,
129129
b.info.ClusterInfo.GetID(),
130130
&b.volumes,
131-
&kafkaStage,
131+
&grpcStage,
132132
)
133133
err := pipeline.AddProcessorStages()
134134
if err != nil {

internal/controller/flp/slices.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,33 @@ func slicesToFilters(fc *flowslatest.FlowCollectorSpec, fcSlices []sliceslatest.
1616
if fc.Processor.SlicesConfig.CollectionMode == flowslatest.CollectionAlwaysCollect {
1717
return nil
1818
}
19+
processed := make(map[string]any)
1920
var rules []api.TransformFilterRule
2021
for _, ns := range fc.Processor.SlicesConfig.NamespacesAllowList {
21-
var query string
2222
if len(ns) >= 2 && strings.HasPrefix(ns, "/") && strings.HasSuffix(ns, "/") {
2323
// Regex
2424
pattern := strings.TrimPrefix(strings.TrimSuffix(ns, "/"), "/")
25-
query = fmt.Sprintf(`SrcK8S_Namespace=~"%s" or DstK8S_Namespace=~"%s"`, pattern, pattern)
26-
} else {
27-
query = fmt.Sprintf(`SrcK8S_Namespace="%s" or DstK8S_Namespace="%s"`, ns, ns)
25+
rules = append(rules, api.TransformFilterRule{
26+
Type: api.KeepEntryQuery,
27+
KeepEntryQuery: fmt.Sprintf(`SrcK8S_Namespace=~"%s" or DstK8S_Namespace=~"%s"`, pattern, pattern),
28+
})
29+
} else if _, found := processed[ns]; !found {
30+
rules = append(rules, api.TransformFilterRule{
31+
Type: api.KeepEntryQuery,
32+
KeepEntryQuery: fmt.Sprintf(`SrcK8S_Namespace="%s" or DstK8S_Namespace="%s"`, ns, ns),
33+
})
34+
processed[ns] = nil
2835
}
29-
rules = append(rules, api.TransformFilterRule{
30-
Type: api.KeepEntryQuery,
31-
KeepEntryQuery: query,
32-
})
3336
}
3437
for i := range fcSlices {
35-
query := fmt.Sprintf(`SrcK8S_Namespace="%s" or DstK8S_Namespace="%s"`, fcSlices[i].Namespace, fcSlices[i].Namespace)
36-
rules = append(rules, api.TransformFilterRule{
37-
Type: api.KeepEntryQuery,
38-
KeepEntryQuery: query,
39-
KeepEntrySampling: uint16(fcSlices[i].Spec.Sampling),
40-
})
38+
if _, found := processed[fcSlices[i].Namespace]; !found {
39+
rules = append(rules, api.TransformFilterRule{
40+
Type: api.KeepEntryQuery,
41+
KeepEntryQuery: fmt.Sprintf(`SrcK8S_Namespace="%s" or DstK8S_Namespace="%s"`, fcSlices[i].Namespace, fcSlices[i].Namespace),
42+
KeepEntrySampling: uint16(fcSlices[i].Spec.Sampling),
43+
})
44+
processed[fcSlices[i].Namespace] = nil
45+
}
4146
}
4247
return rules
4348
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package flp
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/netobserv/flowlogs-pipeline/pkg/api"
8+
"github.com/netobserv/flowlogs-pipeline/pkg/config"
9+
"github.com/stretchr/testify/assert"
10+
corev1 "k8s.io/api/core/v1"
11+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
13+
flowslatest "github.com/netobserv/network-observability-operator/api/flowcollector/v1beta2"
14+
sliceslatest "github.com/netobserv/network-observability-operator/api/flowcollectorslice/v1alpha1"
15+
"github.com/netobserv/network-observability-operator/api/flowmetrics/v1alpha1"
16+
"github.com/netobserv/network-observability-operator/internal/controller/flp/fmstatus"
17+
"github.com/netobserv/network-observability-operator/internal/controller/reconcilers"
18+
"github.com/netobserv/network-observability-operator/internal/pkg/cluster"
19+
"github.com/netobserv/network-observability-operator/internal/pkg/helper"
20+
"github.com/netobserv/network-observability-operator/internal/pkg/manager/status"
21+
)
22+
23+
var (
24+
adminSubnets = []flowslatest.SubnetLabel{
25+
{
26+
Name: "admin",
27+
CIDRs: []string{"10.0.0.0/16"},
28+
},
29+
}
30+
autoSubnets = []flowslatest.SubnetLabel{
31+
{
32+
Name: "test",
33+
CIDRs: []string{"1.2.3.4/32", "10.0.0.0/8"},
34+
},
35+
}
36+
slicez = []sliceslatest.FlowCollectorSlice{
37+
{
38+
ObjectMeta: v1.ObjectMeta{Name: "a", Namespace: "ns-a"},
39+
},
40+
{
41+
ObjectMeta: v1.ObjectMeta{Name: "b1", Namespace: "ns-b"},
42+
Spec: sliceslatest.FlowCollectorSliceSpec{
43+
SubnetLabels: []sliceslatest.SubnetLabel{
44+
{
45+
Name: "my-override",
46+
CIDRs: []string{"10.10.0.0/16"},
47+
},
48+
{
49+
Name: "my-label",
50+
CIDRs: []string{"100.0.0.0/24"},
51+
},
52+
},
53+
},
54+
},
55+
{
56+
ObjectMeta: v1.ObjectMeta{Name: "b2", Namespace: "ns-b"},
57+
Spec: sliceslatest.FlowCollectorSliceSpec{
58+
SubnetLabels: []sliceslatest.SubnetLabel{
59+
{
60+
Name: "my-label-2",
61+
CIDRs: []string{"100.51.0.0/24"},
62+
},
63+
},
64+
},
65+
},
66+
}
67+
)
68+
69+
func getConfiguredFiltersAndSubnets(cm *corev1.ConfigMap) ([]api.TransformFilterRule, []api.NetworkTransformSubnetLabel) {
70+
var cfs config.Root
71+
err := json.Unmarshal([]byte(cm.Data[configFile]), &cfs)
72+
if err != nil {
73+
return nil, nil
74+
}
75+
var filters []api.TransformFilterRule
76+
var subnetLabels []api.NetworkTransformSubnetLabel
77+
for _, stage := range cfs.Parameters {
78+
if stage.Transform != nil && stage.Name == "enrich" {
79+
subnetLabels = stage.Transform.Network.SubnetLabels
80+
}
81+
if stage.Transform != nil && stage.Name == "filters" {
82+
filters = stage.Transform.Filter.Rules
83+
}
84+
}
85+
return filters, subnetLabels
86+
}
87+
88+
func defaultBuilderWithSlices(cfg *flowslatest.SlicesConfig) (monolithBuilder, error) {
89+
fc := getConfig()
90+
fc.Processor.SlicesConfig = cfg
91+
fc.Processor.SubnetLabels.CustomLabels = adminSubnets
92+
info := reconcilers.Common{Namespace: "namespace", Loki: &helper.LokiConfig{}, ClusterInfo: &cluster.Info{}}
93+
return newMonolithBuilder(info.NewInstance(image, status.Instance{}), &fc, &v1alpha1.FlowMetricList{}, slicez, autoSubnets)
94+
}
95+
96+
func TestSlicesDisabled(t *testing.T) {
97+
fmstatus.Reset()
98+
b, err := defaultBuilderWithSlices(&flowslatest.SlicesConfig{Enable: false})
99+
assert.NoError(t, err)
100+
cm, _, _, err := b.configMaps()
101+
assert.NoError(t, err)
102+
filters, subnets := getConfiguredFiltersAndSubnets(cm)
103+
assert.Nil(t, filters)
104+
assert.Equal(t, []api.NetworkTransformSubnetLabel{
105+
{
106+
Name: "admin",
107+
CIDRs: []string{"10.0.0.0/16"},
108+
},
109+
{
110+
Name: "test",
111+
CIDRs: []string{"1.2.3.4/32", "10.0.0.0/8"},
112+
},
113+
}, subnets)
114+
}
115+
116+
func TestSlicesEnablesCollectAll(t *testing.T) {
117+
fmstatus.Reset()
118+
b, err := defaultBuilderWithSlices(&flowslatest.SlicesConfig{
119+
Enable: true,
120+
CollectionMode: flowslatest.CollectionAlwaysCollect,
121+
NamespacesAllowList: []string{"should-be-ignored"},
122+
})
123+
assert.NoError(t, err)
124+
cm, _, _, err := b.configMaps()
125+
assert.NoError(t, err)
126+
filters, subnets := getConfiguredFiltersAndSubnets(cm)
127+
assert.Nil(t, filters)
128+
assert.Equal(t, []api.NetworkTransformSubnetLabel{
129+
{
130+
Name: "admin",
131+
CIDRs: []string{"10.0.0.0/16"},
132+
},
133+
{
134+
Name: "my-override",
135+
CIDRs: []string{"10.10.0.0/16"},
136+
},
137+
{
138+
Name: "my-label",
139+
CIDRs: []string{"100.0.0.0/24"},
140+
},
141+
{
142+
Name: "my-label-2",
143+
CIDRs: []string{"100.51.0.0/24"},
144+
},
145+
{
146+
Name: "test",
147+
CIDRs: []string{"1.2.3.4/32", "10.0.0.0/8"},
148+
},
149+
}, subnets)
150+
}
151+
152+
func TestSlicesEnablesWhitelist(t *testing.T) {
153+
fmstatus.Reset()
154+
b, err := defaultBuilderWithSlices(&flowslatest.SlicesConfig{
155+
Enable: true,
156+
CollectionMode: flowslatest.CollectionAllowList,
157+
NamespacesAllowList: []string{"should-be-filtered", "/should-.*/"},
158+
})
159+
assert.NoError(t, err)
160+
cm, _, _, err := b.configMaps()
161+
assert.NoError(t, err)
162+
filters, subnets := getConfiguredFiltersAndSubnets(cm)
163+
assert.Equal(t, []api.TransformFilterRule{
164+
{
165+
Type: api.KeepEntryQuery,
166+
KeepEntryQuery: `SrcK8S_Namespace="should-be-filtered" or DstK8S_Namespace="should-be-filtered"`,
167+
},
168+
{
169+
Type: api.KeepEntryQuery,
170+
KeepEntryQuery: `SrcK8S_Namespace=~"should-.*" or DstK8S_Namespace=~"should-.*"`,
171+
},
172+
{
173+
Type: api.KeepEntryQuery,
174+
KeepEntryQuery: `SrcK8S_Namespace="ns-a" or DstK8S_Namespace="ns-a"`,
175+
},
176+
{
177+
Type: api.KeepEntryQuery,
178+
KeepEntryQuery: `SrcK8S_Namespace="ns-b" or DstK8S_Namespace="ns-b"`,
179+
},
180+
}, filters)
181+
assert.Equal(t, []api.NetworkTransformSubnetLabel{
182+
{
183+
Name: "admin",
184+
CIDRs: []string{"10.0.0.0/16"},
185+
},
186+
{
187+
Name: "my-override",
188+
CIDRs: []string{"10.10.0.0/16"},
189+
},
190+
{
191+
Name: "my-label",
192+
CIDRs: []string{"100.0.0.0/24"},
193+
},
194+
{
195+
Name: "my-label-2",
196+
CIDRs: []string{"100.51.0.0/24"},
197+
},
198+
{
199+
Name: "test",
200+
CIDRs: []string{"1.2.3.4/32", "10.0.0.0/8"},
201+
},
202+
}, subnets)
203+
}

0 commit comments

Comments
 (0)