Skip to content
Snippets Groups Projects
Commit 2d5e01b1 authored by Björn Vonheiden's avatar Björn Vonheiden
Browse files

Use kubernetes api to apply uc aggregation kubernetes components

Use the kubernetes api in python to run the kubernetes creation
operations instead of the default platform commands.
parent a77ebcb4
No related branches found
No related tags found
2 merge requests!42Integerate theodolite and run uc python scripts,!24run UC as python implementation
from kubernetes import client, config # kubernetes api from kubernetes import client, config # kubernetes api
from kubernetes.stream import stream from kubernetes.stream import stream
# import confuse
import argparse # parse arguments from cli import argparse # parse arguments from cli
from os import path # path utilities
import subprocess # execute bash commands import subprocess # execute bash commands
import time # process sleep import time # process sleep
import yaml # convert from file to yaml object
v1 = None # acces kubernetes api coreApi = None # acces kubernetes core api
appsApi = None # acces kubernetes apps api
customApi = None # acces kubernetes custom object api
args = None # CLI arguments args = None # CLI arguments
...@@ -65,15 +68,17 @@ def load_variables(): ...@@ -65,15 +68,17 @@ def load_variables():
def initialize_kubernetes_api(): def initialize_kubernetes_api():
global v1 global coreApi, appsApi, customApi
print('Connect to kubernetes api') print('Connect to kubernetes api')
try: try:
config.load_kube_config() # try using local config config.load_kube_config() # try using local config
except e: except config.config_exception.ConfigException:
# load config from pod, if local config is not available # load config from pod, if local config is not available
config.load_incluster_config() config.load_incluster_config()
v1 = client.CoreV1Api() coreApi = client.CoreV1Api()
appsApi = client.AppsV1Api()
customApi = client.CustomObjectsApi()
def create_topics(topics): def create_topics(topics):
...@@ -140,44 +145,58 @@ spec:\n\ ...@@ -140,44 +145,58 @@ spec:\n\
def start_application(): def start_application():
print('Start use case applications') print('Start use case applications')
parameters_path = 'uc-application/overlay/uc' + args.uc_id + '-application' # Apply aggregation service
f = open(parameters_path + '/set_paramters.yaml', 'w') with open(path.join(path.dirname(__file__), "uc-application/base/aggregation-service.yaml")) as f:
f.write('\ dep = yaml.safe_load(f)
apiVersion: apps/v1\n\ try:
kind: Deployment\n\ resp = coreApi.create_namespaced_service(
metadata:\n\ namespace="default", body=dep)
name: titan-ccp-aggregation\n\ print("Service '%s' created." % resp.metadata.name)
spec:\n\ except:
template:\n\ print("Service creation error.")
spec:\n\
containers:\n\ # Apply jmx config map for aggregation service
- name: uc-application\n\ with open(path.join(path.dirname(__file__), "uc-application/base/jmx-configmap.yaml")) as f:
env:\n\ dep = yaml.safe_load(f)
- name: COMMIT_INTERVAL_MS\n\ try:
value: "' + str(args.commit_interval_ms) + '"\n\ resp = coreApi.create_namespaced_config_map(
resources:\n\ namespace="default", body=dep)
limits:\n\ print("ConfigMap '%s' created." % resp.metadata.name)
memory: ' + str(args.memory_limit) + '\n\ except:
cpu: ' + str(args.cpu_limit) + '\n') # cpu limit is already str print("ConfigMap creation error.")
f.close()
# Create custom object service monitor
exec_command = [ with open(path.join(path.dirname(__file__), "uc-application/base/service-monitor.yaml")) as f:
'kubectl', dep = yaml.safe_load(f)
'apply', try:
'-k', resp = customApi.create_namespaced_custom_object(
parameters_path group="monitoring.coreos.com",
] version="v1",
output = subprocess.run(exec_command, capture_output=True, text=True) namespace="default",
print(output) plural="servicemonitors", # From CustomResourceDefinition of ServiceMonitor
exec_command = [ body=dep,
'kubectl', )
'scale', print("ServiceMonitor '%s' created." % resp['metadata']['name'])
'deployment', except:
'uc' + args.uc_id + '-titan-ccp-aggregation', print("ServiceMonitor creation error")
'--replicas=' + str(args.instances)
] # Create deployment
output = subprocess.run(exec_command, capture_output=True, text=True) with open(path.join(path.dirname(__file__), "uc-application/base/aggregation-deployment.yaml")) as f:
print(output) dep = yaml.safe_load(f)
dep['spec']['replicas'] = args.instances
uc_container = dep['spec']['template']['spec']['containers'][0]
uc_container['image'] = 'soerenhenning/uc1-app:latest'
uc_container['env'][1]['value'] = str(args.commit_interval_ms)
uc_container['resources']['limits']['memory'] = str(args.memory_limit)
uc_container['resources']['limits']['cpu'] = str(args.cpu_limit) # cpu limit is already str
try:
resp = appsApi.create_namespaced_deployment(
namespace="default",
body=dep
)
print("Deployment '%s' created." % resp.metadata.name)
except client.rest.ApiException as e:
print("Deployment creation error: %s" % e.reason)
return return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment