Skip to content

Commit

Permalink
[stable/grafana] Utilize 5.x datasource and dashboard import tooling …
Browse files Browse the repository at this point in the history
…and general refactor (helm#4713)

* Refactor grafana

- rename manifest to align with helm create
- remove unnecessary manifests

* Grafana refactor

- utilize new features of grafana 5.x to configure datasources and dashboards
  - remove all jobs
- update ingress manifest to align with helm create
- consolidate configmaps
- cleanup and refactor values.yaml
- remove unnecessary nesting from values.yaml
- add initContainer to download dashboards
- update labels and selectors to align with helm best practices
- general cleanup to align with helm best practices/patterns observed in `helm create`
- update values, NOTES, README and _helpers accordingly
- cleanup service manifest to allow more flexibility
- add rtluckie to maintainers
- bump chart version

Tested on:
k8s 1.8.10, 1.9.6 and 1.10.0

Limitations:
- Imported dashboards only support a single datasource

* Changes from comments

- remove deprecated grafana.ini option
- rework .Values.dashboards to make triggering download initContainer easier
- make initContainer image configurable
- add note to values.yaml regarding dashboards' dependency on dashboardProviders
- bump chart version to 1.0.0

* NOTES fixes

- remove unnecessary echo
- fix service.port references

* More changes from comments

- update name to gh username in Charts.yaml
- cleanup plugins install
- cleanup dashboards install -- don't rely on `defaults`
- cleanup reusing pv claim -- don't reply on `defaults`
- cleanup secrets -- don't rely on `defaults`
- cleanup service manifest -- don't rely `defaults`
- use storageClassName instead of storageClass (don't rely on `defaults`)
- remove redundant defaults from grafana.ini configmap

* Fix small regression

- use default when using templated value for pv name when existingClaim
  is not defined

Signed-off-by: voron <[email protected]>
  • Loading branch information
rtluckie authored and voron committed Sep 5, 2018
1 parent afa52ee commit 286c680
Show file tree
Hide file tree
Showing 18 changed files with 446 additions and 886 deletions.
4 changes: 3 additions & 1 deletion stable/grafana/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: grafana
version: 0.8.5
version: 1.0.0
appVersion: 5.0.4
description: The leading tool for querying and visualizing time series and metrics.
home: https://grafana.net
Expand All @@ -9,4 +9,6 @@ sources:
maintainers:
- name: Ming Hsieh
email: [email protected]
- name: rtluckie
email: [email protected]
engine: gotpl
58 changes: 30 additions & 28 deletions stable/grafana/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,33 @@ The command removes all the Kubernetes components associated with the chart and

## Configuration

| Parameter | Description | Default |
|-------------------------------------------|-------------------------------------|---------------------------------------------------|
| `server.image` | Container image to run | grafana/grafana:5.0.4 |
| `server.adminUser` | Admin user username | admin |
| `server.adminPassword` | Admin user password | Randomly generated |
| `server.antiAffinity.enabled` | Enable anti affinity | false |
| `server.antiAffinity.type` | Hard or soft anti affinity | hard |
| `server.persistentVolume.enabled` | Create a volume to store data | true |
| `server.persistentVolume.size` | Size of persistent volume claim | 1Gi RW |
| `server.persistentVolume.storageClass` | Type of persistent volume claim | `nil` (uses alpha storage class annotation) |
| `server.persistentVolume.accessMode` | ReadWriteOnce or ReadOnly | [ReadWriteOnce] |
| `server.persistentVolume.existingClaim` | Existing persistent volume claim | null |
| `server.persistentVolume.subPath` | Subdirectory of pvc to mount | null |
| `server.readinessProbe` | Server readiness probe | httpGet: {path: /api/health, port: 3000}, initialDelaySeconds: 30, timeoutSeconds: 30 |
| `server.replicaCount` | Desired number of grafana pods | 1 |
| `server.resources` | Server resource requests and limits | requests: {cpu: 100m, memory: 100Mi} |
| `server.tolerations` | node taints to tolerate (requires Kubernetes >=1.6) | null |
| `server.service.annotations` | Service annotations | null |
| `server.service.httpPort` | Service port | 80 |
| `server.service.loadBalancerIP` | IP to assign to load balancer | null |
| `server.service.loadBalancerSourceRanges` | List of IP CIDRs allowed access | null |
| `server.service.nodePort` | For service type "NodePort" | null |
| `server.service.externalIPs` | External IP addresses | null |
| `server.service.clusterIP` | Custom clusterIP to use for service | null |
| `server.service.type` | ClusterIP, NodePort, or LoadBalancer| ClusterIP |
| `server.setDatasource.enabled` | Creates grafana datasource with job | false |
| `server.extraEnv` | Extra environment variables to set in the server container. List of [EnvVar](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/#envvar-v1-core) values | [] |
| `dashboardImports.enabled` | Creates grafana dashboards with job | false |

| Parameter | Description | Default |
|----------------------------|-------------------------------------|---------------------------------------------------------|
| `replicas` | Number of nodes | `1` |
| `image.repository` | Image repository | `grafana/grafana` |
| `image.tag` | Image tag. (`Must be >= 5.0.0`) Possible values listed [here](https://hub.docker.com/r/grafana/grafana/tags/).| `5.0.4`|
| `image.pullPolicy` | Image pull policy | `IfNotPresent` |
| `service.type` | Kubernetes service type | `ClusterIP` |
| `service.port` | Kubernetes port where service is exposed| `9000` |
| `service.annotations` | Service annotations | `80` |
| `ingress.enabled` | Enables Ingress | `false` |
| `ingress.annotations` | Ingress annotations | `{}` |
| `ingress.hosts` | Ingress accepted hostnames | `[]` |
| `ingress.tls` | Ingress TLS configuration | `[]` |
| `resources` | CPU/Memory resource requests/limits | `{}` |
| `nodeSelector` | Node labels for pod assignment | `{}` |
| `tolerations` | Toleration labels for pod assignment | `[]` |
| `affinity` | Affinity settings for pod assignment | `{}` |
| `persistence.enabled` | Use persistent volume to store data | `false` |
| `persistence.size` | Size of persistent volume claim | `10Gi` |
| `persistence.existingClaim`| Use an existing PVC to persist data | `nil` |
| `persistence.storageClass` | Type of persistent volume claim | `generic` |
| `persistence.accessModes` | Persistence access modes | `[]` |
| `persistence.subPath` | Mount a sub directory of the persistent volume if set | `""` |
| `env` | Extra environment variables passed to pods | `{}` |
| `datasource` | Configure grafana datasources | `{}` |
| `dashboardProviders` | Configure grafana dashboard providers | `{}` |
| `dashboards` | Dashboards to import | `{}` |
| `grafana.ini` | Grafana's primary configuration | `{}` |

33 changes: 16 additions & 17 deletions stable/grafana/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
1. Get your '{{ .Values.server.adminUser }}' user password by running:
1. Get your '{{ .Values.adminUser }}' user password by running:

kubectl get secret --namespace {{ .Release.Namespace }} {{ template "grafana.server.fullname" . }} -o jsonpath="{.data.grafana-admin-password}" | base64 --decode ; echo
kubectl get secret --namespace {{ .Release.Namespace }} {{ template "grafana.fullname" . }} -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

2. The Grafana server can be accessed via port {{ .Values.server.service.httpPort }} on the following DNS name from within your cluster:
2. The Grafana server can be accessed via port {{ .Values.service.port }} on the following DNS name from within your cluster:

{{ template "grafana.server.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local
{{ if .Values.server.ingress.enabled }}
{{ template "grafana.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local
{{ if .Values.ingress.enabled }}
From outside the cluster, the server URL(s) are:
{{- range .Values.server.ingress.hosts }}
{{- range .Values.ingress.hosts }}
http://{{ . }}
{{- end }}
{{ else }}
Get the Grafana URL to visit by running these commands in the same shell:
{{ if contains "NodePort" .Values.server.service.type -}}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "grafana.server.fullname" . }})
{{ if contains "NodePort" .Values.service.type -}}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "grafana.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{ else if contains "LoadBalancer" .Values.server.service.type -}}
{{ else if contains "LoadBalancer" .Values.service.type -}}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get svc --namespace {{ .Release.Namespace }} -w {{ template "grafana.server.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "grafana.server.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo http://$SERVICE_IP:{{ .Values.server.httpPort -}}
{{ else if contains "ClusterIP" .Values.server.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "grafana.fullname" . }},component={{ .Values.server.name }}" -o jsonpath="{.items[0].metadata.name}")
You can watch the status of by running 'kubectl get svc --namespace {{ .Release.Namespace }} -w {{ template "grafana.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "grafana.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
http://$SERVICE_IP:{{ .Values.service.port -}}
{{ else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "grafana.fullname" . }},component={{ .Values.name }}" -o jsonpath="{.items[0].metadata.name}")
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 3000
{{- end }}
{{- end }}

3. Login with the password from step 1 and the username: {{ .Values.server.adminUser }}
3. Login with the password from step 1 and the username: {{ .Values.adminUser }}

{{- if .Values.server.persistentVolume.enabled }}
{{- else }}
{{- if not .Values.persistence.enabled }}
#################################################################################
###### WARNING: Persistence is disabled!!! You will lose your data when #####
###### the Grafana pod is terminated. #####
Expand Down
18 changes: 8 additions & 10 deletions stable/grafana/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@ Expand the name of the chart.
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "grafana.fullname" -}}
{{- $name := default "grafana" .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "grafana.server.fullname" -}}
{{- define "grafana.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
Expand All @@ -32,3 +23,10 @@ If release name contains chart name it will be used as a full name.
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "grafana.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
55 changes: 46 additions & 9 deletions stable/grafana/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "grafana.fullname" . }}
labels:
app: {{ template "grafana.fullname" . }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
component: "{{ .Values.server.name }}"
heritage: "{{ .Release.Service }}"
release: "{{ .Release.Name }}"
name: {{ template "grafana.server.fullname" . }}-config
app: {{ template "grafana.name" . }}
chart: {{ template "grafana.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
data:
{{- if .Values.server.installPlugins }}
grafana-install-plugins: {{ .Values.server.installPlugins | quote }}
{{- with .Values.plugins }}
plugins: {{ . | quote }}
{{- end }}
grafana.ini: |
{{- range $key, $value := index .Values "grafana.ini" }}
[{{ $key }}]
{{- range $elem, $elemVal := $value }}
{{ $elem }} = {{ $elemVal }}
{{- end }}
{{- end }}

{{- if .Values.datasources }}
{{- range $key, $value := .Values.datasources }}
{{ $key }}: |
{{ toYaml $value | indent 4 }}
{{- end -}}
{{- end -}}

{{- if .Values.dashboardProviders }}
{{- range $key, $value := .Values.dashboardProviders }}
{{ $key }}: |
{{ toYaml $value | indent 4 }}
{{- end -}}
{{- end -}}

{{- if .Values.dashboards }}
download_dashboards.sh: |
#!/usr/bin/env sh
set -euf
mkdir -p /var/lib/grafana/dashboards
{{- range $key, $value := .Values.dashboards }}
{{ if hasKey $value "gnetId" }}
curl -sk \
--connect-timeout 60 \
--max-time 60 \
-H "Accept: application/json" \
-H "Content-Type: application/json;charset=UTF-8" \
https://grafana.com/api/dashboards/{{ $value.gnetId }}/revisions/{{- if $value.revision -}}{{ $value.revision }}{{- else -}}1{{- end -}}/download {{ if $value.datasource }}| sed 's|\"datasource\":[^,]*|\"datasource\": \"{{ $value.datasource }}\"|g'{{ end }} \
> /var/lib/grafana/dashboards/{{ $key }}.json
{{- end }}
{{- end }}
{{ toYaml .Values.serverConfigFile | indent 2 }}
{{- end }}
12 changes: 0 additions & 12 deletions stable/grafana/templates/dashboards-configmap.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions stable/grafana/templates/dashboards-import-configmap.yaml

This file was deleted.

32 changes: 0 additions & 32 deletions stable/grafana/templates/dashboards-import-job-configmap.yaml

This file was deleted.

52 changes: 0 additions & 52 deletions stable/grafana/templates/dashboards-import-job.yaml

This file was deleted.

18 changes: 18 additions & 0 deletions stable/grafana/templates/dashboards-json-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "grafana.fullname" . }}-dashboards-json
labels:
app: {{ template "grafana.name" . }}
chart: {{ template "grafana.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
data:
{{- if .Values.dashboards }}
{{- range $key, $value := .Values.dashboards }}
{{ if hasKey $value "json" }}
{{ $key }}.json: |
{{ $value.json }}
{{- end }}
{{- end }}
{{- end -}}
Loading

0 comments on commit 286c680

Please sign in to comment.