Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wait_to_statefulset_to_be_ready(statefulset_name, name_space, time_out=None):
start = datetime.now()
while True:
resp = client.AppsV1Api().read_namespaced_stateful_set(name=statefulset_name, namespace=name_space)
total_sleep_time = (datetime.now()-start).total_seconds()
if resp.status.replicas == resp.status.ready_replicas:
ready_replicas = resp.status.ready_replicas
print("Total time waiting for statefulset {0} [size: {1}]: {2} sec\n".format(statefulset_name,
ready_replicas,
total_sleep_time))
break
print("{0}/{1} pods ready {2} sec ".format(resp.status.ready_replicas, resp.status.replicas,
total_sleep_time), end="\r")
time.sleep(1)
if time_out and total_sleep_time > time_out:
raise Exception("Timeout waiting for statefulset to be ready")
if modified_file_path != os.path.join(dir_path, filename) and is_change:
# remove modified file
ut.delete_file(modified_file_path)
name = dep["metadata"]["name"]
if dep['kind'] == 'StatefulSet':
k8s_client = client.AppsV1Api()
if not delete:
k8s_client.create_namespaced_stateful_set(body=dep, namespace=namespace)
else:
k8s_client.delete_namespaced_stateful_set(name=name, namespace=namespace)
elif dep['kind'] == 'DaemonSet':
k8s_client = client.AppsV1Api()
k8s_client.create_namespaced_daemon_set(body=dep, namespace=namespace)
elif dep['kind'] == 'Deployment':
k8s_client = client.AppsV1Api()
k8s_client.create_namespaced_deployment(body=dep, namespace=namespace)
elif dep['kind'] == 'Service':
try:
k8s_client = client.CoreV1Api()
k8s_client.create_namespaced_service(body=dep, namespace=namespace)
except ApiException as e:
if e.status == 409:
print(f"Service exists: {dep['metadata']['name']}")
continue
raise e
elif dep['kind'] == 'PodDisruptionBudget':
k8s_client = client.PolicyV1beta1Api()
k8s_client.create_namespaced_pod_disruption_budget(body=dep, namespace=namespace)
elif dep["kind"] == 'Role':
k8s_client = client.RbacAuthorizationV1Api()
k8s_client.create_namespaced_role(body=dep, namespace=namespace)
import k8sUtils
from DataHandler import DataHandler
from job import Job, JobSchema
from pod_template import PodTemplate
from dist_pod_template import DistPodTemplate
from config import config
from cluster_manager import record
logger = logging.getLogger(__name__)
# The config will be loaded from default location.
k8s_config.load_kube_config()
k8s_CoreAPI = client.CoreV1Api()
k8s_AppsAPI = client.AppsV1Api()
class JobDeployer:
def __init__(self):
self.k8s_CoreAPI = k8s_CoreAPI
self.k8s_AppsAPI = k8s_AppsAPI
self.namespace = "default"
self.pretty = "pretty_example"
@record
def _create_pod(self, body):
api_response = self.k8s_CoreAPI.create_namespaced_pod(
namespace=self.namespace,
body=body,
pretty=self.pretty,
)
return api_response
Create instance of Apps V1 API of kubernetes:
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md
:return: instance of client
"""
global apps_api
if apps_api is None:
config.load_kube_config()
if API_KEY is not None:
# Configure API key authorization: BearerToken
configuration = client.Configuration()
configuration.api_key['authorization'] = API_KEY
configuration.api_key_prefix['authorization'] = 'Bearer'
apps_api = client.AppsV1Api(client.ApiClient(configuration))
else:
apps_api = client.AppsV1Api()
return apps_api
def get_deployment_replicas(self, deployment_name, namespace='default'):
"""
Get number of replicas for a specified deployment from Kubernetes API
:param deployment_name: name of a deployment
:type deployment_name: str
:param namespace: name of a namespace to look in
:type namespace: str
:return: number of replicas for a specified deployment
:rtype int
"""
apps_api = kubernetes.client.AppsV1Api(_build_client())
scale_data = apps_api.read_namespaced_deployment(deployment_name, namespace)
print("Scale data for {} in {} ns is {}".format(deployment_name, namespace, scale_data))
if scale_data is not None:
return 0 if not scale_data.status.available_replicas else scale_data.status.available_replicas
else:
return None
def undeploy(self):
"""
Undeploy the Redis Pub/Sub service in an orchestrator.
"""
from kubernetes import client
if self.params.deployed:
return
from kubernetes import client
api_client = client.AppsV1Api()
delete_options = client.V1DeleteOptions()
try:
api_client.delete_namespaced_deployment(self.redis_server_name, self.params.orchestrator_params['namespace'], delete_options)
except client.rest.ApiException as e:
print("Got exception: %s\n while deleting redis-server", e)
api_client = client.CoreV1Api()
try:
api_client.delete_namespaced_service(self.redis_service_name, self.params.orchestrator_params['namespace'], delete_options)
except client.rest.ApiException as e:
print("Got exception: %s\n while deleting redis-server", e)
auth_req = google.auth.transport.requests.Request()
credentials.refresh(auth_req)
configuration = client.Configuration()
configuration.host = f"https://{cluster.endpoint}:443"
configuration.verify_ssl = False
configuration.api_key = {"authorization": "Bearer " + credentials.token}
client.Configuration.set_default(configuration)
if num_gpus > 0:
with request.urlopen(GCLOUD_NVIDIA_DAEMONSET) as r:
dep = yaml.safe_load(r)
dep["spec"]["selector"] = {
"matchLabels": dep["spec"]["template"]["metadata"]["labels"]
}
dep = client.ApiClient()._ApiClient__deserialize(dep, "V1DaemonSet")
k8s_client = client.AppsV1Api()
k8s_client.create_namespaced_daemon_set("kube-system", body=dep)
# create tiller service account
client.CoreV1Api().create_namespaced_service_account(
"kube-system",
{
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": {
"name": "tiller",
"generateName": "tiller",
"namespace": "kube-system",
},
},
)
def find_model_deployment(namespace, model_id, model_version):
"""
Find one model deployment. Return None if there is no the deployment
:param namespace: namespace
:type namespace: str
:param model_id: model id
:type model_id: str
:param model_version: model version
:type model_version: str
:return: :py:class:`kubernetes.client.models.v1_deployment.V1Deployment`
"""
client = k8s_utils.build_client()
apps_api = kubernetes.client.AppsV1Api(client)
try:
return apps_api.read_namespaced_deployment(
normalize_k8s_name(model_id, model_version),
namespace
)
except kubernetes.client.rest.ApiException as e:
if e.status == 404:
return None
raise e
def daemonset():
try:
valus = defaultdict()
v1 = client.AppsV1Api()
tables = ('name','namespace', 'images', 'mount_path', 'cpu_limit','mem_limit','number_ready')
keys = ('namespace', 'images', 'mount_path', 'cpu_limit','mem_limit','number_ready')
ret = v1.list_daemon_set_for_all_namespaces()
for i in ret.items:
try:
mount_path = []
for path in i.spec.template.spec.containers[0].volume_mounts:
mount_path.append(path.mount_path)
number_ready = i.status.number_ready
if number_ready == 0:
number_ready = 1
cpu_limit = 'None'
mem_limit = 'None'
limits = i.spec.template.spec.containers[0].resources.limits
if limits:
if 'cpu' in limits: