|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package openshift |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + _ "embed" |
| 20 | + |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/client-go/kubernetes" |
| 25 | +) |
| 26 | + |
| 27 | +// The dashboard is created manually following the syntax from Grafana 5. For development purposes, this dashboard can be created just by loading the JSON file |
| 28 | +// in a ConfigMap from the openshift-config-managed and adding the console.openshift.io/dashboard=true label. |
| 29 | +// |
| 30 | +//go:embed metrics-dashboard.json |
| 31 | +var dashboardContent string |
| 32 | + |
| 33 | +const ( |
| 34 | + openshiftDashboardsNamespace = "openshift-config-managed" |
| 35 | + configMapName = "opentelemetry-collector" |
| 36 | +) |
| 37 | + |
| 38 | +type DashboardManagement struct { |
| 39 | + clientset kubernetes.Interface |
| 40 | +} |
| 41 | + |
| 42 | +func NewDashboardManagement(clientset kubernetes.Interface) DashboardManagement { |
| 43 | + return DashboardManagement{ |
| 44 | + clientset: clientset, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (d DashboardManagement) Start(ctx context.Context) error { |
| 49 | + cm := corev1.ConfigMap{ |
| 50 | + ObjectMeta: v1.ObjectMeta{ |
| 51 | + Name: configMapName, |
| 52 | + Namespace: openshiftDashboardsNamespace, |
| 53 | + Labels: map[string]string{ |
| 54 | + "console.openshift.io/dashboard": "true", |
| 55 | + }, |
| 56 | + }, |
| 57 | + Data: map[string]string{ |
| 58 | + "otel.json": dashboardContent, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + _, err := d.clientset.CoreV1().ConfigMaps(openshiftDashboardsNamespace).Create(ctx, &cm, metav1.CreateOptions{}) |
| 63 | + if err != nil { |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + <-ctx.Done() |
| 68 | + |
| 69 | + return d.clientset.CoreV1().ConfigMaps(openshiftDashboardsNamespace).Delete(ctx, configMapName, metav1.DeleteOptions{}) |
| 70 | +} |
| 71 | + |
| 72 | +func (d DashboardManagement) NeedLeaderElection() bool { |
| 73 | + return true |
| 74 | +} |
0 commit comments