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
89 changes: 89 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.createDashboardsIfAbsent(reqLogger)
if err != nil {
return reconcile.Result{}, err
}

return reconcile.Result{}, nil
}

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

func (r *ArgoCDMetricsReconciler) createDashboardsIfAbsent(reqLogger logr.Logger) error {
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: dashboardNamespace}, &corev1.Namespace{})
if err != nil {
reqLogger.Info("OpenShift dashboards are not installed, 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 := newDashboard(entry.Name(), dashboardNamespace)
if err != nil {
reqLogger.Info("There was an error creating dashboard ", "Namespace", dashboardNamespace, "Name", entry.Name())
return err
}

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)
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)
// Should we return an error or continue with other dashboards?
return err
Copy link
Contributor

@jaideepr97 jaideepr97 Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should continue with other dashboards here, unless they only make sense in context of each other.
If that is not that case, and there is value in viewing each dashboard independently, we should not return error here

same for line 332 as well

}
}
}
}
return nil
}

func newDashboard(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
45 changes: 45 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 Down Expand Up @@ -287,3 +289,46 @@ 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) {

ns := corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: dashboardNamespace,
},
}

testCases := []struct {
instanceName string
namespace string
}{
{
instanceName: argoCDInstanceName,
namespace: "openshift-gitops",
},
}
for _, tc := range testCases {
r := newMetricsReconciler(t, tc.namespace, tc.instanceName)
// Need namespace for dashboards to be available before reconciling
err := r.Client.Create(context.TODO(), &ns)
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