Skip to content

Add OpenShift Dashboards for GitOps #589

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

Merged
merged 11 commits into from
Aug 30, 2023
101 changes: 101 additions & 0 deletions controllers/argocd_metrics_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package controllers

import (
"context"
"embed"
"fmt"
"path/filepath"
"strings"

argoapp "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
Expand All @@ -41,6 +44,8 @@ const (
readRoleNameFormat = "%s-read"
readRoleBindingNameFormat = "%s-prometheus-k8s-read-binding"
alertRuleName = "gitops-operator-argocd-alerts"
dashboardNamespace = "openshift-config-managed"
dashboardFolder = "dashboards"
)

type ArgoCDMetricsReconciler struct {
Expand All @@ -50,6 +55,12 @@ type ArgoCDMetricsReconciler struct {
Scheme *runtime.Scheme
}

// embed json dashboards
var (
//go:embed dashboards
dashboards embed.FS
)

// blank assignment to verify that ReconcileArgoCDRoute implements reconcile.Reconciler
var _ reconcile.Reconciler = &ArgoCDMetricsReconciler{}

Expand Down Expand Up @@ -154,6 +165,11 @@ func (r *ArgoCDMetricsReconciler) Reconcile(ctx context.Context, request reconci
return reconcile.Result{}, err
}

err = r.reconcileDashboards(reqLogger)
if err != nil {
return reconcile.Result{}, err
}

return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -292,6 +308,91 @@ func (r *ArgoCDMetricsReconciler) createPrometheusRuleIfAbsent(namespace string,
return err
}

func (r *ArgoCDMetricsReconciler) reconcileDashboards(reqLogger logr.Logger) error {
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: dashboardNamespace}, &corev1.Namespace{})
if err != nil {
reqLogger.Info("Monitoring dashboards are not supported on this cluster, skipping dashboard installation",
"Namespace", dashboardNamespace)
return nil
}

entries, err := dashboards.ReadDir(dashboardFolder)
if err != nil {
reqLogger.Error(err, "Could not read list of embedded dashboards")
return err
}

for _, entry := range entries {
reqLogger.Info("Processing dashboard", "Namespace", dashboardNamespace, "Name", entry.Name())

if !entry.IsDir() {
dashboard, err := newDashboardConfigMap(entry.Name(), dashboardNamespace)
if err != nil {
reqLogger.Info("There was an error creating dashboard ", "Namespace", dashboardNamespace, "Name", entry.Name())
continue
}

existingDashboard := &corev1.ConfigMap{}

err = r.Client.Get(context.TODO(), types.NamespacedName{Name: dashboard.Name, Namespace: dashboardNamespace}, existingDashboard)
if err == nil {
reqLogger.Info("A dashboard instance already exists",
"Namespace", existingDashboard.Namespace, "Name", existingDashboard.Name)

// See if we need to reconcile based on dashboard data only to allow users
// to disable dashboard via label if so desired. Note that disabling it
// will be reset if dashboard changes in newer version of operator.
if existingDashboard.Data[entry.Name()] != dashboard.Data[entry.Name()] {
reqLogger.Info("Dashboard data does not match expectation, reconciling",
"Namespace", dashboard.Namespace, "Name", dashboard.Name)
err := r.Client.Update(context.TODO(), dashboard)
if err != nil {
reqLogger.Error(err, "Error updating dashboard",
"Namespace", dashboard.Namespace, "Name", dashboard.Name)
}
}
continue
}

if errors.IsNotFound(err) {
reqLogger.Info("Creating new dashboard",
"Namespace", dashboard.Namespace, "Name", dashboard.Name)
err := r.Client.Create(context.TODO(), dashboard)
if err != nil {
reqLogger.Error(err, "Error creating a new dashboard",
"Namespace", dashboard.Namespace, "Name", dashboard.Name)
}
}
}
}
return nil
}

func newDashboardConfigMap(filename string, namespace string) (*corev1.ConfigMap, error) {

name := strings.TrimSuffix(filename, filepath.Ext(filename))

objectMeta := metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"console.openshift.io/dashboard": "true",
},
}

content, err := dashboards.ReadFile(dashboardFolder + "/" + filename)
if err != nil {
return nil, err
}

return &corev1.ConfigMap{
ObjectMeta: objectMeta,
Data: map[string]string{
filename: string(content),
},
}, nil
}

func newReadRole(namespace string) *rbacv1.Role {
objectMeta := metav1.ObjectMeta{
Name: fmt.Sprintf(readRoleNameFormat, namespace),
Expand Down
59 changes: 59 additions & 0 deletions controllers/argocd_metrics_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package controllers
import (
"context"
"fmt"
"path/filepath"
"strings"
"testing"

argoapp "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
Expand All @@ -27,6 +29,7 @@ import (
is "gotest.tools/assert/cmp"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -287,3 +290,59 @@ func TestReconciler_add_prometheus_rule(t *testing.T) {
assert.Equal(t, rule.Spec.Groups[0].Rules[0].Expr.StrVal, expr)
}
}

func TestReconciler_add_dashboard(t *testing.T) {

// Need to create openshift-config-managed namespace for dashboards
ns := corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: dashboardNamespace,
},
}

// Need to create one configmap to test update existing versus create
cm := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "gitops-overview",
Namespace: dashboardNamespace,
},
}

testCases := []struct {
instanceName string
namespace string
}{
{
instanceName: argoCDInstanceName,
namespace: "openshift-gitops",
},
}
for _, tc := range testCases {
r := newMetricsReconciler(t, tc.namespace, tc.instanceName)
// Create dashboard namespace
err := r.Client.Create(context.TODO(), &ns)
assert.NilError(t, err)
// Create update test dashboard
err = r.Client.Create(context.TODO(), &cm)
assert.NilError(t, err)

_, err = r.Reconcile(context.TODO(), newRequest(tc.namespace, tc.instanceName))
assert.NilError(t, err)

entries, err := dashboards.ReadDir(dashboardFolder)
assert.NilError(t, err)

for _, entry := range entries {
name := strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name()))
content, err := dashboards.ReadFile(dashboardFolder + "/" + entry.Name())
assert.NilError(t, err)

dashboard := &corev1.ConfigMap{}
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: dashboardNamespace}, dashboard)
assert.NilError(t, err)

assert.Assert(t, dashboard.ObjectMeta.Labels["console.openshift.io/dashboard"] == "true")
assert.Assert(t, dashboard.Data[entry.Name()] == string(content))
}
}
}
Loading