Added k8s adapter

This commit is contained in:
Pünkösd Marcell 2021-04-17 21:35:44 +02:00
parent be62a5330b
commit e8f5f26afa
2 changed files with 38 additions and 1 deletions

View File

@ -0,0 +1,36 @@
from flask import current_app, _app_ctx_stack
import kubernetes
class FlaskKubernetes:
def __init__(self, app=None):
self.app = app
if app:
self.init_app(app)
def init_app(self, app):
# app.config.setdefault('SOMETHING', None)
app.extensions = getattr(app, 'extensions', {})
app.extensions['kubernetes'] = self
def connect(self) -> kubernetes.client.ApiClient:
# This may be slow, but at least safe
config = kubernetes.client.configuration.Configuration()
kubernetes.config.load_incluster_config(config)
return kubernetes.client.ApiClient(configuration=config)
@property
def connection(self) -> kubernetes.client.ApiClient:
ctx = _app_ctx_stack.top
if ctx is not None:
if not hasattr(ctx, 'kubernetes'): # first time called during this request
ctx.kubernetes = self.connect()
return ctx.kubernetes
@property
def corev1api(self) -> kubernetes.client.CoreV1Api:
return kubernetes.client.CoreV1Api(self.connection)

View File

@ -4,4 +4,5 @@ blinker
Flask
marshmallow
Flask-Classful
sentry-sdk
sentry-sdk
kubernetes