Skip to main content

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)

Metadata

Release files for kg-grafana 0.8.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kg-grafana 0.8.4
File Size Uploaded Provenance
kg_grafana-0.8.4.tar.gz 13.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for kg-grafana 0.8.4
File Interpreter ABI Platform
kg_grafana-0.8.4-py3-none-any.whl Python 3 none any Details

Total release size: 26.8 kB

Release files / kg_grafana-0.8.4.tar.gz

Download URL kg_grafana-0.8.4.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cc6a05c7bf2887379097eedd3cc80b13ebeb5b3d2202b428a0bbfae4b6b58c1d
BLAKE2b-256 checksum
How to use checksums
76100cfb40e6f783c09031a70f4c0e4d4c1db49d93026f06d49a6c4211aed186
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / kg_grafana-0.8.4-py3-none-any.whl

Download URL kg_grafana-0.8.4-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6799aaeaceac13ae2a4f71ed4c4ffe94cd615073b7b6ad36f1fbd03eaad46533
BLAKE2b-256 checksum
How to use checksums
ea670cf6a10e21cb089387f578f172c5c1c364a08e1e1ca2cbcdc33512af7b04
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for kg-grafana 0.8.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page