Skip to content

Commit

Permalink
Merge pull request #366 from csibbitt/csibbitt/OSPRH-6134_dashboard_p…
Browse files Browse the repository at this point in the history
…lugin

Manage ObservabilityUIPlugin for Dashboards plugin
  • Loading branch information
openshift-merge-bot[bot] authored Apr 22, 2024
2 parents 7b820da + bcdc6d4 commit 1c6ec34
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 15 deletions.
5 changes: 5 additions & 0 deletions api/v1beta1/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const (

DashboardPrometheusRuleReadyCondition condition.Type = "DashboardPrometheusRuleReady"

DashboardPluginReadyCondition condition.Type = "DashboardPluginReady"

DashboardDatasourceReadyCondition condition.Type = "DashboardDatasourceReady"

DashboardDefinitionReadyCondition condition.Type = "DashboardDefinitionReady"
Expand Down Expand Up @@ -203,6 +205,9 @@ const (
DashboardPrometheusRuleReadyInitMessage = "Dashboard PrometheusRule not started"
DashboardPrometheusRuleUnableToOwnMessage = "Error occured when trying to own %s"

DashboardPluginReadyInitMessage = "Dashboard Plugin not started"
DashboardPluginFailedMessage = "Error occured when trying to install the dashboard plugin: %s"

DashboardDatasourceReadyInitMessage = "Dashboard Datasource not started"
DashboardDatasourceFailedMessage = "Error occured when trying to install the dashboard datasource: %s"

Expand Down
10 changes: 10 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ rules:
- get
- list
- watch
- apiGroups:
- observability.openshift.io
resources:
- uiplugins
verbs:
- create
- get
- list
- patch
- watch
- apiGroups:
- rabbitmq.openstack.org
resources:
Expand Down
64 changes: 49 additions & 15 deletions controllers/metricstorage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func (r *MetricStorageReconciler) GetLogger(ctx context.Context) logr.Logger {
//+kubebuilder:rbac:groups=network.openstack.org,resources=ipsets,verbs=get;list;watch
//+kubebuilder:rbac:groups=dataplane.openstack.org,resources=openstackdataplanenodesets,verbs=get;list;watch
//+kubebuilder:rbac:groups=dataplane.openstack.org,resources=openstackdataplaneservices,verbs=get;list;watch
//+kubebuilder:rbac:groups=observability.openshift.io,resources=uiplugins,verbs=get;list;watch;create;patch
//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete

// Reconcile reconciles MetricStorage
func (r *MetricStorageReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
Expand Down Expand Up @@ -172,6 +174,7 @@ func (r *MetricStorageReconciler) Reconcile(ctx context.Context, req ctrl.Reques
condition.UnknownCondition(telemetryv1.ScrapeConfigReadyCondition, condition.InitReason, telemetryv1.ScrapeConfigReadyInitMessage),
condition.UnknownCondition(telemetryv1.NodeSetReadyCondition, condition.InitReason, telemetryv1.NodeSetReadyInitMessage),
condition.UnknownCondition(telemetryv1.DashboardPrometheusRuleReadyCondition, condition.InitReason, telemetryv1.DashboardPrometheusRuleReadyInitMessage),
condition.UnknownCondition(telemetryv1.DashboardPluginReadyCondition, condition.InitReason, telemetryv1.DashboardPluginReadyInitMessage),
condition.UnknownCondition(telemetryv1.DashboardDatasourceReadyCondition, condition.InitReason, telemetryv1.DashboardDatasourceReadyInitMessage),
condition.UnknownCondition(telemetryv1.DashboardDefinitionReadyCondition, condition.InitReason, telemetryv1.DashboardDefinitionReadyInitMessage),
condition.UnknownCondition(telemetryv1.PrometheusReadyCondition, condition.InitReason, telemetryv1.PrometheusReadyInitMessage),
Expand Down Expand Up @@ -489,7 +492,49 @@ func (r *MetricStorageReconciler) reconcileNormal(
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardPrometheusRuleReadyCondition, telemetryv1.DashboardsNotEnabledMessage)
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardDatasourceReadyCondition, telemetryv1.DashboardsNotEnabledMessage)
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardDefinitionReadyCondition, telemetryv1.DashboardsNotEnabledMessage)
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardPluginReadyCondition, telemetryv1.DashboardsNotEnabledMessage)
} else {

const dashboardArtifactsNamespace = "openshift-config-managed"

// Deploy dashboard UI plugin from OBO
// TODO: Use the following instead of Unstructured{} after COO 0.2.0
// =====
// uiPluginObj := &obsui.ObservabilityUIPlugin{
// ObjectMeta: metav1.ObjectMeta{
// Name: "ui-dashboards",
// },
// }
// =====
uiPluginObj := &unstructured.Unstructured{}
uiPluginObj.SetUnstructuredContent(map[string]interface{}{
"spec": map[string]interface{}{
"type": "Dashboards",
},
})
uiPluginObj.SetGroupVersionKind(schema.GroupVersionKind{
Group: "observability.openshift.io",
Version: "v1alpha1",
Kind: "UIPlugin",
})
uiPluginObj.SetName("ui-dashboards")
// =====
op, err = controllerutil.CreateOrPatch(ctx, r.Client, uiPluginObj, func() error {
// uiPluginObj.Spec.Type = "Dashboards" // After we update to COO 0.2.0 as dependency
return nil
})
if err != nil {
Log.Error(err, fmt.Sprintf("Failed to update Dashboard Plugin definition %s - operation: %s", uiPluginObj.GetName(), string(op)))
instance.Status.Conditions.MarkFalse(telemetryv1.DashboardPluginReadyCondition,
condition.Reason("Can't create Dashboard Plugin definition"),
condition.SeverityError,
telemetryv1.DashboardPluginFailedMessage, err)
} else {
instance.Status.Conditions.MarkTrue(telemetryv1.DashboardPluginReadyCondition, condition.ReadyMessage)
}
if op != controllerutil.OperationResultNone {
Log.Info(fmt.Sprintf("Dashboard Plugin definition %s successfully changed - operation: %s", uiPluginObj.GetName(), string(op)))
}
// Deploy PrometheusRule for dashboards
err = r.ensureWatches(ctx, "prometheusrules.monitoring.rhobs", &monv1.PrometheusRule{}, eventHandler)
if err != nil {
Expand Down Expand Up @@ -529,27 +574,16 @@ func (r *MetricStorageReconciler) reconcileNormal(
datasourceCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: datasourceName,
Namespace: "console-dashboards",
Namespace: dashboardArtifactsNamespace,
},
}
dataSourceSuccess := false
op, err = controllerutil.CreateOrPatch(ctx, r.Client, datasourceCM, func() error {
datasourceCM.ObjectMeta.Labels = map[string]string{
"console.openshift.io/dashboard-datasource": "true",
}
datasourceCM.Data = map[string]string{
// WARNING: The lines below MUST be indented with spaces instead of tabs
"dashboard-datasource.yaml": `
kind: "Datasource"
metadata:
name: "` + datasourceName + `"
spec:
plugin:
kind: "PrometheusDatasource"
spec:
direct_url: "http://prometheus-operated.` + instance.Namespace + ".svc.cluster.local:9090\"",
}
return nil
datasourceCM.Data, err = metricstorage.DashboardDatasourceData(ctx, r.Client, instance, datasourceName, dashboardArtifactsNamespace)
return err
})
if err != nil {
Log.Error(err, "Failed to update Console UI Datasource ConfigMap %s - operation: %s", datasourceCM.Name, string(op))
Expand Down Expand Up @@ -578,7 +612,7 @@ func (r *MetricStorageReconciler) reconcileNormal(
dashboardCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: dashboardName,
Namespace: "openshift-config-managed",
Namespace: dashboardArtifactsNamespace,
},
}
op, err = controllerutil.CreateOrPatch(ctx, r.Client, dashboardCM, func() error {
Expand Down
60 changes: 60 additions & 0 deletions pkg/metricstorage/dashboard_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metricstorage

import (
"context"

telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func DashboardDatasourceData(ctx context.Context, c client.Client, instance *telemetryv1.MetricStorage, datasourceName string, namespace string) (map[string]string, error) {

scheme := "http"
certText := ""
if instance.Spec.PrometheusTLS.Enabled() {
scheme = "https"
namespacedName := types.NamespacedName{
Name: *instance.Spec.PrometheusTLS.SecretName,
Namespace: instance.Namespace,
}
caSecret := &corev1.Secret{}
err := c.Get(ctx, namespacedName, caSecret)
if err != nil {
return nil, err
}
certText = string(caSecret.Data["ca.crt"])
}

return map[string]string{
// WARNING: The value lines below MUST be indented with spaces, not tabs (because they are YAML strings)
"dashboard-datasource-ca": certText,
"dashboard-datasource.yaml": `
kind: "Datasource"
metadata:
name: "` + datasourceName + `"
project: "` + namespace + `"
spec:
plugin:
kind: "PrometheusDatasource"
spec:
direct_url: "` + scheme + `://metric-storage-prometheus.` + instance.Namespace + `.svc.cluster.local:9090"
`}, nil
}

0 comments on commit 1c6ec34

Please sign in to comment.