birb-scheduler/birb_scheduler/k8s_buzerator.py

40 lines
1.6 KiB
Python

#!/usr/bin/env python3
import logging
import time
from kubernetes import config, client
from kubernetes.client.rest import ApiException
config.load_incluster_config()
def ensure_running_pod_on_site(site: str):
# Configs can be set in Configuration class directly or using helper utility
logger = logging.getLogger("k8s")
with client.ApiClient() as api_client:
# Create an instance of the API class
api_instance = client.CustomObjectsApi(api_client)
group = 'types.kubefed.io' # str | the custom resource's group
version = 'v1beta1' # str | the custom resource's version
namespace = 'birbnetes' # str | The custom resource's namespace
plural = 'federateddeployments' # str | the custom resource's plural name. For TPRs this would be lowercase plural kind.
name = f'svm-prefilter-service-{site}' # str | the custom object's name
try:
api_response = api_instance.get_namespaced_custom_object(group, version, namespace, plural, name)
except ApiException as e:
logger.error("Exception when calling CustomObjectsApi->get_namespaced_custom_object: %s\n" % e)
raise
if api_response['spec']['template']['spec']['replicas'] != 0:
logging.debug("It seems like that this site have working replicas")
return
api_response['spec']['template']['spec']['replicas'] = 1
api_instance.patch_namespaced_custom_object(group, version, namespace, plural, name, api_response)
# TODO: Wait for pod to start up
time.sleep(7) # close enough