diff --git a/controllers/consoleplugin/consoleplugin_objects.go b/controllers/consoleplugin/consoleplugin_objects.go index f89403b16..e8f89f3b4 100644 --- a/controllers/consoleplugin/consoleplugin_objects.go +++ b/controllers/consoleplugin/consoleplugin_objects.go @@ -410,9 +410,8 @@ func (b *builder) getPromConfig(ctx context.Context) cfg.PrometheusConfig { config.TokenPath = b.volumes.AddToken(constants.PluginName) - allMetricNames := metrics.GetAllNames() includeList := metrics.GetIncludeList(b.desired) - allMetrics := metrics.GetDefinitions(allMetricNames, nil) + allMetrics := metrics.GetDefinitions(b.desired, true) for i := range allMetrics { mSpec := allMetrics[i].Spec enabled := slices.Contains(includeList, mSpec.MetricName) diff --git a/pkg/dashboards/dashboard_test.go b/pkg/dashboards/dashboard_test.go index f8280ef86..37dd1fcbd 100644 --- a/pkg/dashboards/dashboard_test.go +++ b/pkg/dashboards/dashboard_test.go @@ -5,6 +5,7 @@ import ( metricslatest "github.com/netobserv/network-observability-operator/apis/flowmetrics/v1alpha1" "github.com/netobserv/network-observability-operator/pkg/metrics" + "github.com/netobserv/network-observability-operator/pkg/test/util" "github.com/stretchr/testify/assert" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -12,7 +13,7 @@ import ( func TestCreateFlowMetricsDashboard_All(t *testing.T) { assert := assert.New(t) - defs := metrics.GetDefinitions(metrics.GetAllNames(), nil) + defs := metrics.GetDefinitions(util.SpecForMetrics(), true) js := CreateFlowMetricsDashboards(defs) d, err := FromBytes([]byte(js["Main"])) @@ -87,7 +88,7 @@ func TestCreateFlowMetricsDashboard_All(t *testing.T) { func TestCreateFlowMetricsDashboard_OnlyNodeIngressBytes(t *testing.T) { assert := assert.New(t) - defs := metrics.GetDefinitions([]string{"node_ingress_bytes_total"}, nil) + defs := metrics.GetDefinitions(util.SpecForMetrics("node_ingress_bytes_total"), false) js := CreateFlowMetricsDashboards(defs) d, err := FromBytes([]byte(js["Main"])) @@ -106,7 +107,7 @@ func TestCreateFlowMetricsDashboard_OnlyNodeIngressBytes(t *testing.T) { func TestCreateFlowMetricsDashboard_DefaultList(t *testing.T) { assert := assert.New(t) - defs := metrics.GetDefinitions(metrics.DefaultIncludeList, nil) + defs := metrics.GetDefinitions(util.SpecForMetrics(), false) js := CreateFlowMetricsDashboards(defs) d, err := FromBytes([]byte(js["Main"])) diff --git a/pkg/metrics/predefined_metrics.go b/pkg/metrics/predefined_metrics.go index 168ed1502..ca3bd1a2b 100644 --- a/pkg/metrics/predefined_metrics.go +++ b/pkg/metrics/predefined_metrics.go @@ -198,13 +198,13 @@ func GetAllNames() []string { return names } -func GetDefinitions(names []string, toRemove []string) []metricslatest.FlowMetric { +func getUpdatedDefsFromNames(names []string, labelsToRemove []string) []metricslatest.FlowMetric { ret := []metricslatest.FlowMetric{} for i := range predefinedMetrics { for _, name := range names { if predefinedMetrics[i].MetricName == name { spec := predefinedMetrics[i].FlowMetricSpec - spec.Labels = removeLabels(spec.Labels, toRemove) + spec.Labels = removeLabels(spec.Labels, labelsToRemove) ret = append(ret, metricslatest.FlowMetric{Spec: spec}) } } @@ -258,15 +258,26 @@ func removeMetricsByPattern(list []string, search string) []string { return filtered } -func MergePredefined(fm []metricslatest.FlowMetric, fc *flowslatest.FlowCollectorSpec) []metricslatest.FlowMetric { - names := GetIncludeList(fc) - var toRemove []string +func GetDefinitions(fc *flowslatest.FlowCollectorSpec, allMetrics bool) []metricslatest.FlowMetric { + var names []string + if allMetrics { + names = GetAllNames() + } else { + names = GetIncludeList(fc) + } + + var labelsToRemove []string if !helper.IsZoneEnabled(&fc.Processor) { - toRemove = append(toRemove, "SrcK8S_Zone", "DstK8S_Zone") + labelsToRemove = append(labelsToRemove, "SrcK8S_Zone", "DstK8S_Zone") } if !helper.IsMultiClusterEnabled(&fc.Processor) { - toRemove = append(toRemove, "K8S_ClusterName") + labelsToRemove = append(labelsToRemove, "K8S_ClusterName") } - predefined := GetDefinitions(names, toRemove) + + return getUpdatedDefsFromNames(names, labelsToRemove) +} + +func MergePredefined(fm []metricslatest.FlowMetric, fc *flowslatest.FlowCollectorSpec) []metricslatest.FlowMetric { + predefined := GetDefinitions(fc, false) return append(predefined, fm...) } diff --git a/pkg/metrics/predefined_metrics_test.go b/pkg/metrics/predefined_metrics_test.go index b7944e3ee..649edfa1e 100644 --- a/pkg/metrics/predefined_metrics_test.go +++ b/pkg/metrics/predefined_metrics_test.go @@ -4,7 +4,9 @@ import ( "testing" flowslatest "github.com/netobserv/network-observability-operator/apis/flowcollector/v1beta2" + "github.com/netobserv/network-observability-operator/pkg/test/util" "github.com/stretchr/testify/assert" + "k8s.io/utils/ptr" ) func TestIncludeExclude(t *testing.T) { @@ -43,7 +45,7 @@ func TestIncludeExclude(t *testing.T) { func TestGetDefinitions(t *testing.T) { assert := assert.New(t) - res := GetDefinitions([]string{"namespace_flows_total", "node_ingress_bytes_total", "workload_egress_packets_total"}, nil) + res := GetDefinitions(util.SpecForMetrics("namespace_flows_total", "node_ingress_bytes_total", "workload_egress_packets_total"), false) assert.Len(res, 3) assert.Equal("node_ingress_bytes_total", res[0].Spec.MetricName) assert.Equal("Bytes", res[0].Spec.ValueField) @@ -59,7 +61,10 @@ func TestGetDefinitions(t *testing.T) { func TestGetDefinitionsRemoveZoneCluster(t *testing.T) { assert := assert.New(t) - res := GetDefinitions([]string{"namespace_flows_total", "node_ingress_bytes_total", "workload_egress_packets_total"}, []string{"K8S_ClusterName", "SrcK8S_Zone", "DstK8S_Zone"}) + spec := util.SpecForMetrics("namespace_flows_total", "node_ingress_bytes_total", "workload_egress_packets_total") + spec.Processor.AddZone = ptr.To(false) + spec.Processor.MultiClusterDeployment = ptr.To(false) + res := GetDefinitions(spec, false) assert.Len(res, 3) assert.Equal("node_ingress_bytes_total", res[0].Spec.MetricName) assert.Equal("Bytes", res[0].Spec.ValueField) diff --git a/pkg/test/util/utils.go b/pkg/test/util/utils.go new file mode 100644 index 000000000..e6af5d72e --- /dev/null +++ b/pkg/test/util/utils.go @@ -0,0 +1,30 @@ +package util + +import ( + flowslatest "github.com/netobserv/network-observability-operator/apis/flowcollector/v1beta2" + "k8s.io/utils/ptr" +) + +func SpecForMetrics(metrics ...string) *flowslatest.FlowCollectorSpec { + fc := flowslatest.FlowCollectorSpec{ + Agent: flowslatest.FlowCollectorAgent{ + EBPF: flowslatest.FlowCollectorEBPF{ + Privileged: true, + Features: []flowslatest.AgentFeature{flowslatest.FlowRTT, flowslatest.DNSTracking, flowslatest.PacketDrop}, + }, + }, + Processor: flowslatest.FlowCollectorFLP{ + Metrics: flowslatest.FLPMetrics{}, + AddZone: ptr.To(true), + MultiClusterDeployment: ptr.To(true), + }, + } + if len(metrics) > 0 { + var conv []flowslatest.FLPMetric + for _, m := range metrics { + conv = append(conv, flowslatest.FLPMetric(m)) + } + fc.Processor.Metrics.IncludeList = &conv + } + return &fc +}