Skip to content

Commit

Permalink
update grafana rendering
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Mange <[email protected]>
  • Loading branch information
thibaultmg committed Jan 9, 2025
1 parent f15ca59 commit b84bfbc
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resources:
- dash-acm-alert-analysis.yaml
- dash-acm-alerts-by-clusters.yaml
- dash-acm-cluster-by-alerts.yaml
- scrape-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resources:
- dash-acm-hcp-overview.yaml
- dash-acm-resources-hcp.yaml
- scrape-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resources:
- dash-acm-hcp-overview.yaml
- dash-acm-resources-hcp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resources:
- dash-acm-clusters-overview-ocp311.yaml
- dash-acm-optimization-overview-ocp311.yaml
- dash-k8s-capacity-planning-ocp311.yaml
- dash-k8s-compute-resources-namespace-pods-ocp311.yaml
- dash-k8s-compute-resources-pod-ocp311.yaml
- dash-k8s-namespaces-in-cluster-ocp311.yaml
- dash-k8s-pods-in-namespace-ocp311.yaml
- dash-k8s-summary-by-node-ocp311.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
resources:
- dash-acm-clusters-overview.yaml
- dash-acm-optimization-overview.yaml
- dash-cluster-rsrc-use.yaml
- dash-k8s-apiserver.yaml
- dash-k8s-compute-resources-cluster.yaml
- dash-k8s-compute-resources-namespace-pods.yaml
- dash-k8s-compute-resources-namespace-workloads.yaml
- dash-k8s-compute-resources-node-pods.yaml
- dash-k8s-compute-resources-pod.yaml
- dash-k8s-compute-resources-workload.yaml
- dash-k8s-etcd.yaml
- dash-k8s-networking-cluster.yaml
- dash-k8s-service-level-overview-api-server-cluster.yaml
- dash-k8s-service-level-overview-api-server.yaml
- dash-node-rsrc-use.yaml
- prometheus-rule.yaml
- scrape-config.yaml

Original file line number Diff line number Diff line change
Expand Up @@ -1155,19 +1155,12 @@ data:
},
"timezone": "browser",
"title": "Kubernetes / Networking / Cluster",
"uid": "efd9761bb5dd42a3800fdd6577a8430c",
"uid": "ff635a025bcfea7bc3dd4f508990a3e9",
"version": 1
}
kind: ConfigMap
metadata:
name: grafana-dashboard-k8s-networking-cluster-nexus
name: grafana-dashboard-k8s-networking-cluster
namespace: open-cluster-management-observability
annotations:
observability.open-cluster-management.io/dashboard-folder: Nexus
ownerReferences:
- apiVersion: observability.open-cluster-management.io/v1beta2
kind: MultiClusterObservability
name: observability
uid: 577e70b7-fe3d-44a0-a8ef-427bd7c073e5
controller: true
blockOwnerDeletion: true
labels:
general-folder: 'true'
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resources:
- dash-acm-clusters-overview-optimized.yaml
- dash-acm-clusters-overview.yaml
- dash-acm-optimization-overview.yaml
- dash-cluster-rsrc-use.yaml
- dash-k8s-apiserver.yaml
- dash-k8s-compute-resources-cluster.yaml
- dash-k8s-compute-resources-namespace-pods.yaml
- dash-k8s-compute-resources-namespace-workloads.yaml
- dash-k8s-compute-resources-node-pods.yaml
- dash-k8s-compute-resources-pod.yaml
- dash-k8s-compute-resources-workload.yaml
- dash-k8s-etcd.yaml
- dash-k8s-networking-cluster.yaml
- dash-k8s-service-level-overview-api-server-cluster.yaml
- dash-k8s-service-level-overview-api-server.yaml
- dash-node-rsrc-use.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resources:
- dash-acm-openshift-virtualization-overview.yaml
- dash-acm-openshift-virtualization-single-cluster-view.yaml
- dash-acm-openshift-virtualization-single-vm-view.yaml
- dash-acm-virtual-machines-by-time-in-status.yaml
- dash-acm-virtual-machines-inventory.yaml
- scrape-config.yaml

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package rendering

import (
"strings"

v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -15,6 +17,8 @@ import (
"github.com/stolostron/multicluster-observability-operator/operators/pkg/util"
)

const dashboardFolderAnnotationKey = "observability.open-cluster-management.io/dashboard-folder"

func (r *MCORenderer) newGranfanaRenderer() {
r.renderGrafanaFns = map[string]rendererutil.RenderFn{
"Deployment": r.renderGrafanaDeployments,
Expand All @@ -28,6 +32,8 @@ func (r *MCORenderer) newGranfanaRenderer() {
"RoleBinding": r.renderer.RenderNamespace,
"Ingress": r.renderer.RenderNamespace,
"PersistentVolumeClaim": r.renderer.RenderNamespace,
"ScrapeConfig": r.renderer.RenderNamespace,
"PrometheusRule": r.renderer.RenderNamespace,
}
}

Expand Down Expand Up @@ -86,6 +92,13 @@ func (r *MCORenderer) renderGrafanaTemplates(templates []*resource.Resource,
namespace string, labels map[string]string) ([]*unstructured.Unstructured, error) {
uobjs := []*unstructured.Unstructured{}
for _, template := range templates {
// Avoid rendering resources that are specific to the MCOA and non MCOA setups
if !MCOAPlatformMetricsEnabled(r.cr) && isMCOASpecificResource(template) {
continue
} else if MCOAPlatformMetricsEnabled(r.cr) && isNonMCOASpecificResource(template) {
continue
}

render, ok := r.renderGrafanaFns[template.GetKind()]
if !ok {
m, err := template.Map()
Expand All @@ -108,3 +121,33 @@ func (r *MCORenderer) renderGrafanaTemplates(templates []*resource.Resource,

return uobjs, nil
}

func isMCOASpecificResource(res *resource.Resource) bool {
if res.GetKind() == "ScrapeConfig" {
return true
}

if res.GetKind() == "PrometheusRule" {
return true
}

if res.GetKind() == "ConfigMap" && strings.HasSuffix(res.GetName(), "nexus") {
return true
}

return false
}

func isNonMCOASpecificResource(res *resource.Resource) bool {
// Exclude all dashboards living in the default directory as they are all duplicated
// for MCOA with some expressions adaptations due to the different set of metrics
// being collected.
if res.GetKind() == "ConfigMap" {
annotations := res.GetAnnotations(dashboardFolderAnnotationKey)
if dir, ok := annotations[dashboardFolderAnnotationKey]; !ok || dir == "" {
return true
}
}

return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package rendering

import (
"fmt"
"os"
"path/filepath"
"testing"

obv1beta2 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta2"
"github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/pkg/rendering/templates"
rendererutil "github.com/stolostron/multicluster-observability-operator/operators/pkg/rendering"
templatesutil "github.com/stolostron/multicluster-observability-operator/operators/pkg/rendering/templates"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func TestRenderGrafana(t *testing.T) {
wd, err := os.Getwd()
assert.NoError(t, err)
templatesPath := filepath.Join(wd, "..", "..", "manifests")
tmplRenderer := templatesutil.NewTemplateRenderer(templatesPath)
grafanaTemplates, err := templates.GetOrLoadGrafanaTemplates(tmplRenderer)
require.NoError(t, err)

testCases := map[string]struct {
mco obv1beta2.MultiClusterObservability
expect func(*testing.T, []*unstructured.Unstructured)
}{
"MCOA for metrics is enabled": {
mco: obv1beta2.MultiClusterObservability{
Spec: obv1beta2.MultiClusterObservabilitySpec{
Capabilities: &obv1beta2.CapabilitiesSpec{
Platform: &obv1beta2.PlatformCapabilitiesSpec{
Metrics: obv1beta2.PlatformMetricsSpec{
Collection: obv1beta2.PlatformMetricsCollectionSpec{
Enabled: true,
},
},
},
},
},
},
expect: func(t *testing.T, res []*unstructured.Unstructured) {
assert.Len(t, res, 44)
},
},
"MCOA for metrics is disabled": {
mco: obv1beta2.MultiClusterObservability{},
expect: func(t *testing.T, res []*unstructured.Unstructured) {
assert.Len(t, res, 40)
for _, r := range res {
assert.NotContains(t, r.GetName(), "nexus")
}
},
},
}

for tcName, tc := range testCases {

t.Run(tcName, func(t *testing.T) {
mcoRenderer := &MCORenderer{
renderer: rendererutil.NewRenderer(),
cr: &tc.mco,
}

mcoRenderer.newGranfanaRenderer()
namespace := "myns"
grafanaResources, err := mcoRenderer.renderGrafanaTemplates(grafanaTemplates, namespace, nil)
require.NoError(t, err)
for _, r := range grafanaResources {
if r.GetKind() == "ClusterRole" {
continue
}
assert.Equal(t, namespace, r.GetNamespace(), fmt.Sprintf(" resource %s/%s", r.GetKind(), r.GetName()))
}

tc.expect(t, grafanaResources)
})
}
}
12 changes: 12 additions & 0 deletions operators/multiclusterobservability/pkg/rendering/renderer_mcoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,15 @@ func MCOAEnabled(cr *obv1beta2.MultiClusterObservability) bool {
}
return mcoaEnabled
}

func MCOAPlatformMetricsEnabled(cr *obv1beta2.MultiClusterObservability) bool {
if cr.Spec.Capabilities == nil {
return false
}

if cr.Spec.Capabilities.Platform != nil && cr.Spec.Capabilities.Platform.Metrics.Collection.Enabled {
return true
}

return false
}

0 comments on commit b84bfbc

Please sign in to comment.