Skip to main content

KubraGen Builder: Grafana

Project description

KubraGen Builder: Grafana

PyPI version Supported Python versions

kg_grafana is a builder for KubraGen that deploys a Grafana server in Kubernetes.

KubraGen is a Kubernetes YAML generator library that makes it possible to generate configurations using the full power of the Python programming language.

Example

from kubragen import KubraGen
from kubragen.consts import PROVIDER_GOOGLE, PROVIDERSVC_GOOGLE_GKE
from kubragen.object import Object
from kubragen.option import OptionRoot
from kubragen.options import Options
from kubragen.output import OutputProject, OD_FileTemplate, OutputFile_ShellScript, OutputFile_Kubernetes, \
    OutputDriver_Print
from kubragen.provider import Provider

from kg_grafana import GrafanaBuilder, GrafanaOptions, GrafanaDashboardSource_GNet, GrafanaDashboardSource_Url

kg = KubraGen(provider=Provider(PROVIDER_GOOGLE, PROVIDERSVC_GOOGLE_GKE), options=Options({
    'namespaces': {
        'mon': 'app-monitoring',
    },
}))

out = OutputProject(kg)

shell_script = OutputFile_ShellScript('create_gke.sh')
out.append(shell_script)

shell_script.append('set -e')

#
# OUTPUTFILE: app-namespace.yaml
#
file = OutputFile_Kubernetes('app-namespace.yaml')

file.append([
    Object({
        'apiVersion': 'v1',
        'kind': 'Namespace',
        'metadata': {
            'name': 'app-monitoring',
        },
    }, name='ns-monitoring', source='app', instance='app')
])

out.append(file)
shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

shell_script.append(f'kubectl config set-context --current --namespace=app-monitoring')

#
# SETUP: grafana
#
grafana_config = GrafanaBuilder(kubragen=kg, options=GrafanaOptions({
    'namespace': OptionRoot('namespaces.mon'),
    'basename': 'mygrafana',
    'config': {
        'service_port': 80,
        'admin': {
            'user': 'myuser',
            'password': 'mypassword',
        },
        'provisioning': {
            'datasources': [
                {
                    'name': 'Prometheus',
                    'type': 'prometheus',
                    'access': 'proxy',
                    'url': 'http://prometheus:9090',
                },
                {
                    'name': 'Loki',
                    'type': 'loki',
                    'access': 'proxy',
                    'url': 'http://loki:3100',
                },
            ],
            'dashboards': [
                {
                    'name': 'default',
                    'type': 'file',
                },
                {
                    'name': 'second',
                    'type': 'file',
                },
            ],
        },
        'dashboards': [
            GrafanaDashboardSource_GNet(provider='default', name='prometheus', gnetId=2, revision=2,
                                        datasource='Prometheus'),
            GrafanaDashboardSource_Url(provider='default', name='kubernetes',
                                       url='https://raw.githubusercontent.com/zaneclaes/grafana-dashboards/master/kubernetes.json'),
            # GrafanaDashboardSource_KData(provider='second', kdata=KData_ConfigMapManual(
            #     configmapName='grafana-default-config-map')),
        ],
    },
    'kubernetes': {
        'volumes': {
            'data': {
                'persistentVolumeClaim': {
                    'claimName': 'grafana-storage-claim'
                }
            }
        },
        'resources': {
            'deployment': {
                'requests': {
                    'cpu': '150m',
                    'memory': '300Mi'
                },
                'limits': {
                    'cpu': '300m',
                    'memory': '450Mi'
                },
            },
        },
    }
}))

grafana_config.ensure_build_names(grafana_config.BUILD_CONFIG, grafana_config.BUILD_SERVICE)


if grafana_config.BUILD_CONFIG in grafana_config.build_names_required():
    #
    # OUTPUTFILE: grafana-config.yaml
    #
    file = OutputFile_Kubernetes('grafana-config.yaml')
    out.append(file)

    file.append(grafana_config.build(grafana_config.BUILD_CONFIG))

    shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

#
# OUTPUTFILE: grafana.yaml
#
file = OutputFile_Kubernetes('grafana.yaml')
out.append(file)

file.append(grafana_config.build(grafana_config.BUILD_SERVICE))

shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

#
# Write files
#
out.output(OutputDriver_Print())
# out.output(OutputDriver_Directory('/tmp/build-gke'))

Output:

****** BEGIN FILE: 001-app-namespace.yaml ********
apiVersion: v1
kind: Namespace
metadata:
  name: app-monitoring

****** END FILE: 001-app-namespace.yaml ********
****** BEGIN FILE: 002-grafana.yaml ********
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mygrafana
  namespace: app-monitoring
  labels:
    app: mygrafana
spec:
  selector:
    matchLabels:
      app: mygrafana
  replicas: 1
  template:
    metadata:
      labels:
        app: mygrafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana:7.2.0
        ports:
        - containerPort: 3000
          protocol: TCP
        env:
        - name: GF_INSTALL_PLUGINS
          value: ''
        volumeMounts:
        - mountPath: /var/lib/grafana
          name: data
        resources:
          requests:
            cpu: 150m
            memory: 300Mi
          limits:
            cpu: 300m
            memory: 450Mi
      restartPolicy: Always
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: grafana-storage-claim
---
apiVersion: v1
kind: Service
metadata:
  name: mygrafana
  namespace: app-monitoring
spec:
  selector:
    app: mygrafana
  ports:
  - port: 80
    protocol: TCP
    targetPort: 3000

****** END FILE: 002-grafana.yaml ********
****** BEGIN FILE: create_gke.sh ********
#!/bin/bash

set -e
kubectl apply -f 001-app-namespace.yaml
kubectl config set-context --current --namespace=app-monitoring
kubectl apply -f 002-grafana.yaml

****** END FILE: create_gke.sh ********

Credits

Author

Rangel Reale (rangelreale@gmail.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for kg-grafana, version 0.8.4
Filename, size File type Python version Upload date Hashes
Filename, size kg_grafana-0.8.4-py3-none-any.whl (13.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size kg_grafana-0.8.4.tar.gz (13.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page