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.

Source Distribution

kg_grafana-0.8.4.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

kg_grafana-0.8.4-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file kg_grafana-0.8.4.tar.gz.

File metadata

  • Download URL: kg_grafana-0.8.4.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3

File hashes

Hashes for kg_grafana-0.8.4.tar.gz
Algorithm Hash digest
SHA256 cc6a05c7bf2887379097eedd3cc80b13ebeb5b3d2202b428a0bbfae4b6b58c1d
MD5 6cf5aa73d4c8b84e9f7aecf4bffc3f49
BLAKE2b-256 76100cfb40e6f783c09031a70f4c0e4d4c1db49d93026f06d49a6c4211aed186

See more details on using hashes here.

File details

Details for the file kg_grafana-0.8.4-py3-none-any.whl.

File metadata

  • Download URL: kg_grafana-0.8.4-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3

File hashes

Hashes for kg_grafana-0.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6799aaeaceac13ae2a4f71ed4c4ffe94cd615073b7b6ad36f1fbd03eaad46533
MD5 a1bb9c7b24038fb978c787ff0ddee755
BLAKE2b-256 ea670cf6a10e21cb089387f578f172c5c1c364a08e1e1ca2cbcdc33512af7b04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page