Kubernetes integration
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import logging
|
||||
from uuid import uuid4
|
||||
|
||||
import connexion
|
||||
import six
|
||||
|
||||
from openapi_server import util
|
||||
from pymongo import MongoClient
|
||||
from flask import Response
|
||||
|
||||
from kubernetes import client, config
|
||||
|
||||
logging.basicConfig(format='[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s')
|
||||
logger = logging.getLogger("API")
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.info("Hello there")
|
||||
|
||||
config.load_incluster_config() # or config.load_kube_config()
|
||||
collection = MongoClient("mongo").get_database("stadtmg").get_collection("predictions")
|
||||
|
||||
with client.ApiClient() as api_client:
|
||||
app_api_instance = client.AppsV1Api(api_client)
|
||||
core_api_instance = client.CoreV1Api(api_client)
|
||||
|
||||
|
||||
def detect_post(body=None): # noqa: E501
|
||||
"""detect_post
|
||||
|
||||
# noqa: E501
|
||||
|
||||
:param body:
|
||||
:type body: str
|
||||
|
||||
:rtype: str
|
||||
"""
|
||||
image_id = str(uuid4())
|
||||
logger.debug(f"Processing image '{image_id}'")
|
||||
while collection.find_one({"id": image_id}) is not None:
|
||||
image_id = str(uuid4())
|
||||
|
||||
collection.insert_one({
|
||||
"id": image_id,
|
||||
"input": body,
|
||||
})
|
||||
|
||||
job_name = f"bodenerkennung-{image_id}"
|
||||
metadata = client.V1ObjectMeta(
|
||||
name=job_name,
|
||||
labels={
|
||||
"io.kompose.service": job_name,
|
||||
},
|
||||
namespace="stadtmg",
|
||||
)
|
||||
spec = client.V1JobSpec(
|
||||
backoff_limit=0,
|
||||
ttl_seconds_after_finished=500,
|
||||
template=dict(
|
||||
spec=dict(
|
||||
containers=[
|
||||
dict(
|
||||
name="bodenerkennung",
|
||||
image="masasana.azurecr.io/stadt_mg_bodenerkennung:1.1.1",
|
||||
imagePullPolicy="Always",
|
||||
command=["python", "predict.py"],
|
||||
args=[
|
||||
"--source", "mongo://mongo",
|
||||
"--image_id", image_id,
|
||||
"--category_json", "",
|
||||
]
|
||||
)
|
||||
],
|
||||
imagePullSecrets=[{"name": "acr-secret"}],
|
||||
restartPolicy="Never",
|
||||
)
|
||||
),
|
||||
)
|
||||
logger.debug(metadata)
|
||||
logger.debug(spec)
|
||||
|
||||
job = client.V1Job(
|
||||
api_version="batch/v1",
|
||||
kind="Job",
|
||||
metadata=metadata,
|
||||
spec=spec,
|
||||
)
|
||||
logger.debug(job)
|
||||
|
||||
batch_api = client.BatchV1Api()
|
||||
batch_api.create_namespaced_job("stadtmg", job)
|
||||
|
||||
return Response(image_id, status=200)
|
||||
|
||||
|
||||
def image_image_id_get(image_id): # noqa: E501
|
||||
"""image_image_id_get
|
||||
|
||||
# noqa: E501
|
||||
|
||||
:param image_id:
|
||||
:type image_id:
|
||||
|
||||
:rtype: file
|
||||
"""
|
||||
db_object = collection.find_one({"id": image_id})
|
||||
if db_object is None:
|
||||
return Response(f"Image with id '{image_id}' not found", status=404)
|
||||
|
||||
image = db_object.get("output")
|
||||
if image is None:
|
||||
return Response(status=204)
|
||||
|
||||
return Response(image, status=200, mimetype="image/png")
|
||||
@@ -0,0 +1,3 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
Reference in New Issue
Block a user