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-918 dashboards follow-up #294

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
with:
files: ./cover.out
flags: unittests
fail_ci_if_error: true
fail_ci_if_error: false

bundle-check:
runs-on: ubuntu-latest
Expand Down
5 changes: 3 additions & 2 deletions api/v1alpha1/flowcollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ type FLPMetrics struct {
// +optional
Server MetricsServerConfig `json:"server,omitempty"`

// ignoreTags is a list of tags to specify which metrics to ignore
//+kubebuilder:default:={"egress","packets"}
// ignoreTags is a list of tags to specify which metrics to ignore. Each metric is associated with a list of tags. More details in https://github.com/netobserv/network-observability-operator/tree/main/controllers/flowlogspipeline/metrics_definitions .
// Available tags are: egress, ingress, flows, bytes, packets, namespaces, nodes, workloads
//+kubebuilder:default:={"flows","bytes","packets"}
IgnoreTags []string `json:"ignoreTags,omitempty"`
}

Expand Down
10 changes: 7 additions & 3 deletions bundle/manifests/flows.netobserv.io_flowcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1951,10 +1951,14 @@ spec:
properties:
ignoreTags:
default:
- egress
- flows
- bytes
- packets
description: ignoreTags is a list of tags to specify which
metrics to ignore
description: 'ignoreTags is a list of tags to specify which
metrics to ignore. Each metric is associated with a list
of tags. More details in https://github.com/netobserv/network-observability-operator/tree/main/controllers/flowlogspipeline/metrics_definitions
. Available tags are: egress, ingress, flows, bytes, packets,
namespaces, nodes, workloads'
items:
type: string
type: array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ metadata:
"logLevel": "info",
"metrics": {
"ignoreTags": [
"egress",
"flows",
"bytes",
"packets"
],
"server": {
Expand Down
10 changes: 7 additions & 3 deletions config/crd/bases/flows.netobserv.io_flowcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1938,10 +1938,14 @@ spec:
properties:
ignoreTags:
default:
- egress
- flows
- bytes
- packets
description: ignoreTags is a list of tags to specify which
metrics to ignore
description: 'ignoreTags is a list of tags to specify which
metrics to ignore. Each metric is associated with a list
of tags. More details in https://github.com/netobserv/network-observability-operator/tree/main/controllers/flowlogspipeline/metrics_definitions
. Available tags are: egress, ingress, flows, bytes, packets,
namespaces, nodes, workloads'
items:
type: string
type: array
Expand Down
3 changes: 2 additions & 1 deletion config/samples/flows_v1alpha1_flowcollector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ spec:
server:
port: 9102
ignoreTags:
- egress
- flows
- bytes
- packets
dropUnusedFields: true
resources:
Expand Down
3 changes: 2 additions & 1 deletion config/samples/flows_v1alpha1_flowcollector_versioned.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ spec:
server:
port: 9102
ignoreTags:
- egress
- flows
- bytes
- packets
dropUnusedFields: true
resources:
Expand Down
3 changes: 2 additions & 1 deletion controllers/flowcollector_controller_console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func flowCollectorConsolePluginSpecs() {
}
return svc.Spec.Ports[0].Port
}, timeout, interval).Should(Equal(int32(9001)))
By("Creating the ovn-flows-configmap with the configuration from the FlowCollector")

By("Creating the console plugin configmap")
Eventually(func() interface{} {
ofc := v1.ConfigMap{}
if err := k8sClient.Get(ctx, configKey, &ofc); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions controllers/flowcollector_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func flowCollectorControllerSpecs() {
ConnectionEndTimeout: &metav1.Duration{
Duration: conntrackEndTimeout,
},
Metrics: flowslatest.FLPMetrics{
IgnoreTags: []string{"flows"},
},
},
Agent: flowslatest.FlowCollectorAgent{
Type: "IPFIX",
Expand Down Expand Up @@ -203,6 +206,18 @@ func flowCollectorControllerSpecs() {
Namespace: operatorNamespace,
}, &cm)
}, timeout, interval).Should(Succeed())

By("Expecting the monitoring dashboards configmap to be created")
Eventually(func() interface{} {
ofc := v1.ConfigMap{}
if err := k8sClient.Get(ctx, types.NamespacedName{
Name: "grafana-dashboard-netobserv",
Namespace: "openshift-config-managed",
}, &ofc); err != nil {
return err
}
return ofc.Data["netobserv-metrics.json"]
}, timeout, interval).Should(ContainSubstring(`"panels": [`))
})

It("Should update successfully", func() {
Expand All @@ -225,6 +240,9 @@ func flowCollectorControllerSpecs() {
ConnectionEndTimeout: &metav1.Duration{
Duration: conntrackEndTimeout,
},
Metrics: flowslatest.FLPMetrics{
IgnoreTags: []string{"flows", "bytes", "packets"},
},
}
fc.Spec.Loki = flowslatest.FlowCollectorLoki{}
fc.Spec.Agent.IPFIX = flowslatest.FlowCollectorIPFIX{
Expand Down Expand Up @@ -292,6 +310,14 @@ func flowCollectorControllerSpecs() {
Expect(ds.Spec.Template.Spec.Tolerations).
To(ContainElement(v1.Toleration{Operator: v1.TolerationOpExists}))
})

By("Expecting the monitoring dashboards configmap to be deleted")
Eventually(func() interface{} {
return k8sClient.Get(ctx, types.NamespacedName{
Name: "grafana-dashboard-netobserv",
Namespace: "openshift-config-managed",
}, &v1.ConfigMap{})
}, timeout, interval).Should(MatchError(`configmaps "grafana-dashboard-netobserv" not found`))
})

It("Should redeploy if the spec doesn't change but the external flowlogs-pipeline-config does", func() {
Expand Down
41 changes: 24 additions & 17 deletions controllers/flowlogspipeline/flp_common_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ const (
)

const (
dashboardName = "netobserv"
dashboardTitle = "Netobserv Metrics"
dashboardTags = "['netobserv','grafana','dashboard','flp']"
dashboardName = "netobserv"
dashboardTitle = "NetObserv"
dashboardTags = "['netobserv-mixin']"
dashboardCMNamespace = "openshift-config-managed"
dashboardCMAnnotation = "console.openshift.io/dashboard"
dashboardCMName = "grafana-dashboard-netobserv"
dashboardCMFile = "netobserv-metrics.json"
)

var FlpConfSuffix = map[ConfKind]string{
Expand Down Expand Up @@ -117,13 +121,11 @@ func RoleBindingName(ck ConfKind) string { return name(ck) + "-role" }
func RoleBindingMonoName(ck ConfKind) string { return name(ck) + "-role-mono" }
func promServiceName(ck ConfKind) string { return name(ck) + "-prom" }
func configMapName(ck ConfKind) string { return name(ck) + "-config" }
func dbConfigMapName(ck ConfKind) string { return name(ck) + "-metrics-dashboard" }
func serviceMonitorName(ck ConfKind) string { return name(ck) + "-monitor" }
func prometheusRuleName(ck ConfKind) string { return name(ck) + "-alert" }
func (b *builder) name() string { return name(b.confKind) }
func (b *builder) promServiceName() string { return promServiceName(b.confKind) }
func (b *builder) configMapName() string { return configMapName(b.confKind) }
func (b *builder) dbConfigMapName() string { return dbConfigMapName(b.confKind) }
func (b *builder) serviceMonitorName() string { return serviceMonitorName(b.confKind) }
func (b *builder) prometheusRuleName() string { return prometheusRuleName(b.confKind) }

Expand Down Expand Up @@ -321,7 +323,10 @@ func (b *builder) obtainMetricsConfiguration() (api.PromMetricsItems, string, er
if stages[0].Encode == nil || stages[0].Encode.Prom == nil {
return nil, "", fmt.Errorf("error generating truncated config, Encode expected in %v", stages)
}
jsonStr, _ := cg.GenerateGrafanaJson()
jsonStr, err := cg.GenerateGrafanaJson()
if err != nil {
return nil, "", fmt.Errorf("error generating grafana dashboard: %w", err)
}
return stages[0].Encode.Prom.Metrics, jsonStr, nil
}

Expand Down Expand Up @@ -500,30 +505,32 @@ func (b *builder) addTransformStages(stage *config.PipelineBuilderStage) (*corev
return nil, err
}

// prometheus stage (encode) configuration
promEncode := api.PromEncode{
Prefix: "netobserv_",
Metrics: promMetrics,
var dashboardConfigMap *corev1.ConfigMap
if len(promMetrics) > 0 {
// prometheus stage (encode) configuration
promEncode := api.PromEncode{
Prefix: "netobserv_",
Metrics: promMetrics,
}
enrichedStage.EncodePrometheus("prometheus", promEncode)
dashboardConfigMap = b.makeMetricsDashboardConfigMap(dashboard)
}

enrichedStage.EncodePrometheus("prometheus", promEncode)
b.addCustomExportStages(&enrichedStage)

dashboardConfigMap := b.makeMetricsDashboardConfigMap(dashboard)
return dashboardConfigMap, nil
}

func (b *builder) makeMetricsDashboardConfigMap(dashboard string) *corev1.ConfigMap {
configMap := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: b.dbConfigMapName(),
Namespace: "openshift-config-managed",
Name: dashboardCMName,
Namespace: dashboardCMNamespace,
Labels: map[string]string{
"console.openshift.io/dashboard": "true",
dashboardCMAnnotation: "true",
},
},
Data: map[string]string{
"netobserv-metrics.json": dashboard,
dashboardCMFile: dashboard,
},
}
return &configMap
Expand Down
20 changes: 18 additions & 2 deletions controllers/flowlogspipeline/flp_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package flowlogspipeline
import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

flowslatest "github.com/netobserv/network-observability-operator/api/v1beta1"
Expand Down Expand Up @@ -103,10 +105,24 @@ func (r *FLPReconciler) Reconcile(ctx context.Context, desired *flowslatest.Flow
}

func (r *reconcilersCommonInfo) reconcileDashboardConfig(ctx context.Context, dbConfigMap *corev1.ConfigMap) error {
if dbConfigMap == nil {
// Dashboard config not desired => delete if exists
if err := r.Delete(ctx, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: dashboardCMName,
Namespace: dashboardCMNamespace,
},
}); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("deleting %s ConfigMap: %w", dashboardCMName, err)
}
}
return nil
}
curr := &corev1.ConfigMap{}
if err := r.Get(ctx, types.NamespacedName{
Name: dbConfigMap.Name,
Namespace: dbConfigMap.Namespace,
Name: dashboardCMName,
Namespace: dashboardCMNamespace,
}, curr); err != nil {
if errors.IsNotFound(err) {
return r.CreateOwned(ctx, dbConfigMap)
Expand Down
32 changes: 30 additions & 2 deletions controllers/flowlogspipeline/flp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,9 @@ func TestMergeMetricsConfigurationNoIgnore(t *testing.T) {
cfg := getConfig()

b := newMonolithBuilder("namespace", image, &cfg, true, &certWatcher)
stages, parameters, _, err := b.buildPipelineConfig()
stages, parameters, cm, err := b.buildPipelineConfig()
assert.NoError(err)
assert.NotNil(cm)
assert.True(validatePipelineConfig(stages, parameters))
jsonStages, _ := json.Marshal(stages)
assert.Equal(`[{"name":"ipfix"},{"name":"extract_conntrack","follows":"ipfix"},{"name":"enrich","follows":"extract_conntrack"},{"name":"loki","follows":"enrich"},{"name":"stdout","follows":"enrich"},{"name":"prometheus","follows":"enrich"}]`, string(jsonStages))
Expand All @@ -755,6 +756,11 @@ func TestMergeMetricsConfigurationNoIgnore(t *testing.T) {
assert.Equal("workload_ingress_bytes_total", parameters[5].Encode.Prom.Metrics[5].Name)
assert.Equal("workload_ingress_packets_total", parameters[5].Encode.Prom.Metrics[6].Name)
assert.Equal("netobserv_", parameters[5].Encode.Prom.Prefix)

var grafanaDashboard map[string]interface{}
err = json.Unmarshal([]byte(cm.Data[dashboardCMFile]), &grafanaDashboard)
assert.NoError(err)
assert.Len(grafanaDashboard["panels"], 7)
}

func TestMergeMetricsConfigurationWithIgnore(t *testing.T) {
Expand All @@ -764,14 +770,36 @@ func TestMergeMetricsConfigurationWithIgnore(t *testing.T) {
cfg.Processor.Metrics.IgnoreTags = []string{"nodes"}

b := newMonolithBuilder("namespace", image, &cfg, true, &certWatcher)
stages, parameters, _, err := b.buildPipelineConfig()
stages, parameters, cm, err := b.buildPipelineConfig()
assert.NoError(err)
assert.NotNil(cm)
assert.True(validatePipelineConfig(stages, parameters))
jsonStages, _ := json.Marshal(stages)
assert.Equal(`[{"name":"ipfix"},{"name":"extract_conntrack","follows":"ipfix"},{"name":"enrich","follows":"extract_conntrack"},{"name":"loki","follows":"enrich"},{"name":"stdout","follows":"enrich"},{"name":"prometheus","follows":"enrich"}]`, string(jsonStages))
assert.Len(parameters[5].Encode.Prom.Metrics, 5)
assert.Equal("namespace_flows_total", parameters[5].Encode.Prom.Metrics[0].Name)
assert.Equal("netobserv_", parameters[5].Encode.Prom.Prefix)

var grafanaDashboard map[string]interface{}
err = json.Unmarshal([]byte(cm.Data[dashboardCMFile]), &grafanaDashboard)
assert.NoError(err)
assert.Len(grafanaDashboard["panels"], 5)
}

func TestMergeMetricsConfigurationIgnoreAll(t *testing.T) {
assert := assert.New(t)

cfg := getConfig()
cfg.Processor.Metrics.IgnoreTags = []string{"nodes", "namespaces", "workloads"}

b := newMonolithBuilder("namespace", image, &cfg, true, &certWatcher)
stages, parameters, cm, err := b.buildPipelineConfig()
assert.NoError(err)
assert.Nil(cm)
assert.True(validatePipelineConfig(stages, parameters))
jsonStages, _ := json.Marshal(stages)
assert.Equal(`[{"name":"ipfix"},{"name":"extract_conntrack","follows":"ipfix"},{"name":"enrich","follows":"extract_conntrack"},{"name":"loki","follows":"enrich"},{"name":"stdout","follows":"enrich"}]`, string(jsonStages))
assert.Len(parameters, 5)
}

func TestPipelineWithExporter(t *testing.T) {
Expand Down
21 changes: 17 additions & 4 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
configv1 "github.com/openshift/api/config/v1"
osv1alpha1 "github.com/openshift/api/console/v1alpha1"
operatorsv1 "github.com/openshift/api/operator/v1"
"github.com/stretchr/testify/mock"
Expand All @@ -40,10 +41,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/netobserv/network-observability-operator/controllers/operator"

flowsv1alpha1 "github.com/netobserv/network-observability-operator/api/v1alpha1"
flowsv1beta1 "github.com/netobserv/network-observability-operator/api/v1beta1"
"github.com/netobserv/network-observability-operator/controllers/operator"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -85,6 +85,7 @@ var _ = BeforeSuite(func() {
filepath.Join("..", "config", "crd", "bases"),
// We need to install the ConsolePlugin CRD to test setup of our Network Console Plugin
filepath.Join("..", "vendor", "github.com", "openshift", "api", "console", "v1alpha1"),
filepath.Join("..", "vendor", "github.com", "openshift", "api", "config", "v1"),
filepath.Join("..", "vendor", "github.com", "openshift", "api", "operator", "v1"),
},
ErrorIfCRDPathMissing: true,
Expand All @@ -106,6 +107,9 @@ var _ = BeforeSuite(func() {
err = osv1alpha1.Install(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = configv1.Install(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = apiregv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -150,10 +154,19 @@ var _ = AfterSuite(func() {
})

func prepareNamespaces() error {
return k8sClient.Create(ctx, &corev1.Namespace{
if err := k8sClient.Create(ctx, &corev1.Namespace{
TypeMeta: metav1.TypeMeta{Kind: "Namespace", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: testCnoNamespace},
})
}); err != nil {
return err
}
if err := k8sClient.Create(ctx, &corev1.Namespace{
TypeMeta: metav1.TypeMeta{Kind: "Namespace", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "openshift-config-managed"},
}); err != nil {
return err
}
return nil
}

// NewTestFlowCollectorReconciler allows mocking the IP resolutor of a
Expand Down
Loading