Skip to main content

KubraGen Builder: Traefik 2

PyPI version Supported Python versions

kg_traefik2 is a builder for KubraGen that deploys a Traefik 2 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_traefik2 import Traefik2Builder, Traefik2Options, Traefik2ConfigFile, Traefik2OptionsPort

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: traefik2
#
traefik2_config_file = Traefik2ConfigFile()

traefik2_config = Traefik2Builder(kubragen=kg, options=Traefik2Options({
    'namespace': OptionRoot('namespaces.mon'),
    'basename': 'mytraefik2',
    'config': {
        'traefik_config': traefik2_config_file,
        'traefik_args': [
            '--entrypoints.web.Address=:80',
            '--entryPoints.metrics.address=:9090',
            '--metrics.prometheus=true',
            '--metrics.prometheus.entryPoint=metrics',
        ],
        'ports': [
            Traefik2OptionsPort(name='web', port_container=80, port_service=80),
            Traefik2OptionsPort(name='metrics', port_container=9090, in_service=False),
        ],
    },
    'kubernetes': {
        'resources': {
            'deployment': {
                'requests': {
                    'cpu': '150m',
                    'memory': '300Mi'
                },
                'limits': {
                    'cpu': '300m',
                    'memory': '450Mi'
                },
            },
        },
    }
}))

traefik2_config.ensure_build_names(traefik2_config.BUILD_CRD, traefik2_config.BUILD_ACCESSCONTROL,
                                   traefik2_config.BUILD_CONFIG, traefik2_config.BUILD_SERVICE)

#
# OUTPUTFILE: traefik2-crd.yaml
#
file = OutputFile_Kubernetes('traefik2-crd.yaml')
out.append(file)

file.append(traefik2_config.build(traefik2_config.BUILD_CRD))

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


#
# OUTPUTFILE: traefik2-config.yaml
#
file = OutputFile_Kubernetes('traefik2-config.yaml')
out.append(file)

file.append(traefik2_config.build(traefik2_config.BUILD_ACCESSCONTROL, traefik2_config.BUILD_CONFIG))

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

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

file.append(traefik2_config.build(traefik2_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-traefik2-crd.yaml ********
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: ingressroutes.traefik.containo.us
spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: IngressRoute
    plural: ingressroutes
    singular: ingressroute
  scope: Namespaced
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: middlewares.traefik.containo.us
spec:
<...more...>
****** END FILE: 002-traefik2-crd.yaml ********
****** BEGIN FILE: 003-traefik2-config.yaml ********
apiVersion: v1
kind: ServiceAccount
metadata:
  name: mytraefik2
  namespace: app-monitoring
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: mytraefik2
rules:
- apiGroups: ['']
  resources: [services, endpoints, secrets]
  verbs: [get, list, watch]
- apiGroups: [extensions, networking.k8s.io]
  resources: [ingresses, ingressclasses]
  verbs: [get, list, watch]
- apiGroups: [extensions]
  resources: [ingresses/status]
  verbs: [update]
- apiGroups: [traefik.containo.us]
  resources: [middlewares, ingressroutes, traefikservices, ingressroutetcps, ingressrouteudps,
    tlsoptions, tlsstores]
  verbs: [get, list, watch]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: mytraefik2
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: mytraefik2
subjects:
- kind: ServiceAccount
  name: mytraefik2
  namespace: app-monitoring
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mytraefik2-config
  namespace: app-monitoring
data:
  prometheus.toml: |
    [global]
    checkNewVersion = false
    sendAnonymousUsage = false

****** END FILE: 003-traefik2-config.yaml ********
****** BEGIN FILE: 004-traefik2.yaml ********
kind: Deployment
apiVersion: apps/v1
metadata:
  name: mytraefik2
  namespace: app-monitoring
  labels:
    app: mytraefik2
spec:
  selector:
    matchLabels:
      app: mytraefik2
  template:
    metadata:
      labels:
        app: mytraefik2
    spec:
      serviceAccountName: mytraefik2
      volumes:
      - name: traefik2-config
        configMap:
          name: mytraefik2-config
      containers:
      - name: traefik
<...more...>
****** END FILE: 004-traefik2.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-traefik2-crd.yaml
kubectl apply -f 003-traefik2-config.yaml
kubectl apply -f 004-traefik2.yaml

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

Credits

based on

Traefik & Kubernetes

Author

Rangel Reale (rangelreale@gmail.com)

Metadata

Release files for kg-traefik2 0.8.0

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-traefik2 0.8.0
File Size Uploaded Provenance
kg_traefik2-0.8.0.tar.gz 11.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for kg-traefik2 0.8.0
File Interpreter ABI Platform
kg_traefik2-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.7 kB

Release files / kg_traefik2-0.8.0.tar.gz

Download URL kg_traefik2-0.8.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ff33ef7718f046eb182560e615c80d7008c9dc7be678d189dc4f5ca785417057
BLAKE2b-256 checksum
How to use checksums
b6739bb3e86dfd9a8b3d0488b7bf3e83f9d18ed1a96761b1f6b1540a693830ea
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_traefik2-0.8.0-py3-none-any.whl

Download URL kg_traefik2-0.8.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
843bb471b76d95f4b044cc757a965b95210a1540bee6468ab315a50b012066f8
BLAKE2b-256 checksum
How to use checksums
5f2c282b3c6c20cb393e3275e0afa67ade691d3045f1d7a8ff2433536375ad66
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-traefik2 0.8.0

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