Skip to content

Commit

Permalink
NETOBSERV-1705: do not send zone/cluster labels config... (#677)
Browse files Browse the repository at this point in the history
do not send zone/cluster labels config to the console plugin when the
related features are disabled. Else, the console plugin assumes that
they are available, regardless of the features knobs
  • Loading branch information
jotak committed Jul 16, 2024
1 parent 4316b01 commit 044d197
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
3 changes: 1 addition & 2 deletions controllers/consoleplugin/consoleplugin_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions pkg/dashboards/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ 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"
)

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"]))
Expand Down Expand Up @@ -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"]))
Expand All @@ -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"]))
Expand Down
27 changes: 19 additions & 8 deletions pkg/metrics/predefined_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
}
Expand Down Expand Up @@ -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...)
}
9 changes: 7 additions & 2 deletions pkg/metrics/predefined_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions pkg/test/util/utils.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 044d197

Please sign in to comment.