|
| 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