From b0bb378a3c118dbb8a19d57c7e56758f91b91bd2 Mon Sep 17 00:00:00 2001 From: Radhika Pai Date: Tue, 28 Jan 2020 15:51:51 -0600 Subject: [PATCH] Grafana: Provision to add customized HomePage This code will help to add any customized dashboard as a Home Page for Grafana. The add_home_dashboard script will be executed after the Grafana is deployed which sets a new Dashboard(OSH Home) as a landing Page for a specific Organization. Change-Id: I32b6b9cad4eaefe7d153cae797d3b3143be5c49b --- .../templates/bin/_add-home-dashboard.sh.tpl | 32 +++++++ grafana/templates/configmap-bin.yaml | 2 + grafana/templates/job-add-home-dashboard.yaml | 74 +++++++++++++++ grafana/values.yaml | 20 ++++ grafana/values_overrides/home_dashboard.yaml | 94 +++++++++++++++++++ .../osh-infra-monitoring/110-grafana.sh | 2 +- 6 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 grafana/templates/bin/_add-home-dashboard.sh.tpl create mode 100644 grafana/templates/job-add-home-dashboard.yaml create mode 100644 grafana/values_overrides/home_dashboard.yaml diff --git a/grafana/templates/bin/_add-home-dashboard.sh.tpl b/grafana/templates/bin/_add-home-dashboard.sh.tpl new file mode 100644 index 000000000..a4ce099cb --- /dev/null +++ b/grafana/templates/bin/_add-home-dashboard.sh.tpl @@ -0,0 +1,32 @@ +#!/bin/bash + +# Copyright 2020 The Openstack-Helm Authors. +# +# 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. + +set -xe + +home_dashboard_id=$(curl -K- <<< "--user ${GF_SECURITY_ADMIN_USER}:${GF_SECURITY_ADMIN_PASSWORD}" -XGET "${GRAFANA_URI}api/search?query=OSH%20Home" | sed 's/\[{.id":"*\([0-9a-zA-Z]*\)*,*.*}[]]/\1/') + +echo $home_dashboard_id + +if [ $home_dashboard_id == "[]" ] +then + echo "Failed. Verify Home Dashboard is present in Grafana" +else +#Set Customized Home Dashboard id as Org preference + curl -K- <<< "--user ${GF_SECURITY_ADMIN_USER}:${GF_SECURITY_ADMIN_PASSWORD}" \ + -XPUT "${GRAFANA_URI}api/org/preferences" -H "Content-Type: application/json" \ + -d "{\"homeDashboardId\": $home_dashboard_id}" + echo "Successful" +fi \ No newline at end of file diff --git a/grafana/templates/configmap-bin.yaml b/grafana/templates/configmap-bin.yaml index 775f406c2..0c7322940 100644 --- a/grafana/templates/configmap-bin.yaml +++ b/grafana/templates/configmap-bin.yaml @@ -32,6 +32,8 @@ data: {{ tuple "bin/_grafana.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} selenium-tests.py: | {{ tuple "bin/_selenium-tests.py.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} + add-home-dashboard.sh: | +{{ tuple "bin/_add-home-dashboard.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} set-admin-password.sh: | {{ tuple "bin/_set-admin-password.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} {{- end }} diff --git a/grafana/templates/job-add-home-dashboard.yaml b/grafana/templates/job-add-home-dashboard.yaml new file mode 100644 index 000000000..e874b7c47 --- /dev/null +++ b/grafana/templates/job-add-home-dashboard.yaml @@ -0,0 +1,74 @@ +{{/* +Copyright 2020 The Openstack-Helm Authors. + +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. +*/}} + +{{- if .Values.manifests.job_add_home_dashboard }} +{{- $envAll := . }} + +{{- $serviceAccountName := "add-home-dashboard" }} +{{ tuple $envAll "add_home_dashboard" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }} +--- +apiVersion: batch/v1 +kind: Job +metadata: + name: grafana-add-home-dashboard + annotations: + {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }} +spec: + template: + metadata: + labels: +{{ tuple $envAll "grafana" "add_home_dashboard" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }} + spec: + serviceAccountName: {{ $serviceAccountName }} + restartPolicy: OnFailure + nodeSelector: + {{ .Values.labels.job.node_selector_key }}: {{ .Values.labels.job.node_selector_value | quote }} + initContainers: +{{ tuple $envAll "add_home_dashboard" list | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 8 }} + containers: + - name: add-home-dashboard +{{ tuple $envAll "add_home_dashboard" | include "helm-toolkit.snippets.image" | indent 10 }} +{{ tuple $envAll $envAll.Values.pod.resources.jobs.add_home_dashboard | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }} + env: + - name: GF_SECURITY_ADMIN_USER + valueFrom: + secretKeyRef: + name: grafana-admin-creds + key: GRAFANA_ADMIN_USERNAME + - name: GF_SECURITY_ADMIN_PASSWORD + valueFrom: + secretKeyRef: + name: grafana-admin-creds + key: GRAFANA_ADMIN_PASSWORD + - name: GRAFANA_URI + value: {{ tuple "grafana" "internal" "grafana" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup" }} + command: + - /tmp/add-home-dashboard.sh + volumeMounts: + - name: pod-tmp + mountPath: /tmp + - name: grafana-bin + mountPath: /tmp/add-home-dashboard.sh + subPath: add-home-dashboard.sh + readOnly: true + volumes: + - name: pod-tmp + emptyDir: {} + - name: grafana-bin + configMap: + name: grafana-bin + defaultMode: 0555 +{{- end }} \ No newline at end of file diff --git a/grafana/values.yaml b/grafana/values.yaml index 6118bb556..6ae93a934 100644 --- a/grafana/values.yaml +++ b/grafana/values.yaml @@ -23,6 +23,7 @@ images: db_init: docker.io/openstackhelm/heat:newton-ubuntu_xenial grafana_db_session_sync: docker.io/openstackhelm/heat:newton-ubuntu_xenial selenium_tests: docker.io/openstackhelm/osh-selenium:ubuntu_bionic-20191017 + add_home_dashboard: docker.io/openstackhelm/heat:stein-ubuntu_bionic image_repo_sync: docker.io/docker:17.07.0 pull_policy: IfNotPresent local_registry: @@ -77,6 +78,13 @@ pod: grafana_set_admin_password: allowPrivilegeEscalation: false readOnlyRootFilesystem: true + add_home_dashboard: + pod: + runAsUser: 104 + container: + grafana_set_admin_password: + allowPrivilegeEscalation: false + readOnlyRootFilesystem: true test: pod: runAsUser: 104 @@ -154,6 +162,13 @@ pod: limits: memory: "1024Mi" cpu: "2000m" + add_home_dashboard: + requests: + memory: "128Mi" + cpu: "100m" + limits: + memory: "1024Mi" + cpu: "2000m" tests: requests: memory: "128Mi" @@ -329,6 +344,10 @@ dependencies: services: - endpoint: internal service: grafana + add_home_dashboard: + services: + - endpoint: internal + service: grafana network: grafana: @@ -375,6 +394,7 @@ manifests: job_db_session_sync: true job_image_repo_sync: true job_set_admin_user: true + job_add_home_dashboard: false network_policy: false secret_db: true secret_db_session: true diff --git a/grafana/values_overrides/home_dashboard.yaml b/grafana/values_overrides/home_dashboard.yaml new file mode 100644 index 000000000..b66c463cf --- /dev/null +++ b/grafana/values_overrides/home_dashboard.yaml @@ -0,0 +1,94 @@ +# This overrides file provides a reference for dashboards for +# customized OSH Welcome Page +conf: + dashboards: + home_dashboard: + annotations: + list: + - builtIn: 1 + datasource: "-- Grafana --" + enable: true + hide: true + iconColor: rgba(0, 211, 255, 1) + name: Annotations & Alerts + type: dashboard + editable: false + gnetId: + graphTooltip: 0 + id: 51 + links: [] + panels: + - content: |- +
+ OSH Home Dashboard +
+ editable: true + gridPos: + h: 3 + w: 24 + x: 0 + 'y': 0 + id: 1 + links: [] + mode: html + options: {} + style: {} + title: '' + transparent: true + type: text + - folderId: 0 + gridPos: + h: 10 + w: 13 + x: 6 + 'y': 3 + headings: true + id: 3 + limit: 30 + links: [] + options: {} + query: '' + recent: true + search: false + starred: true + tags: [] + title: '' + type: dashlist + schemaVersion: 18 + style: dark + tags: [] + templating: + list: [] + time: + from: now-1h + to: now + timepicker: + hidden: true + refresh_intervals: + - 5s + - 10s + - 30s + - 1m + - 5m + - 15m + - 30m + - 1h + - 2h + - 1d + time_options: + - 5m + - 15m + - 1h + - 6h + - 12h + - 24h + - 2d + - 7d + - 30d + type: timepicker + timezone: browser + title: OSH Home + version: 3 + +manifests: + job_add_home_dashboard: true \ No newline at end of file diff --git a/tools/deployment/osh-infra-monitoring/110-grafana.sh b/tools/deployment/osh-infra-monitoring/110-grafana.sh index 9036ffb5b..8e91458b0 100755 --- a/tools/deployment/osh-infra-monitoring/110-grafana.sh +++ b/tools/deployment/osh-infra-monitoring/110-grafana.sh @@ -19,7 +19,7 @@ set -xe #NOTE: Lint and package chart make grafana -FEATURE_GATES="calico,ceph,containers,coredns,elasticsearch,kubernetes,nginx,nodes,openstack,prometheus,apparmor" +FEATURE_GATES="calico,ceph,containers,coredns,elasticsearch,kubernetes,nginx,nodes,openstack,prometheus,home_dashboard,apparmor" : ${OSH_INFRA_EXTRA_HELM_ARGS_GRAFANA:="$({ ./tools/deployment/common/get-values-overrides.sh grafana;} 2> /dev/null)"} #NOTE: Deploy command