def set_deployment_env(self, deployment_name, container_name, env_name, env_value): logger.info('Set {0} env: "name:{1}, value:{2}" ...'.format( deployment_name, env_name, env_value)) p_deployment_data = self.appsv1betaapi.read_namespaced_deployment( deployment_name, self.namespace) p_containers = p_deployment_data.spec.template.spec.containers p_env_list = [] for p_container in p_containers: if p_container.name == container_name: p_env_list = p_container.env # {'name': 'CAPT_SCORE', 'value': '0', 'value_from': None} for p_env in p_env_list: if p_env.name == env_name: p_index = p_env_list.index(p_env) p_env_list[p_index].value = env_value break else: p_env_list.insert(0, { 'name': env_name, 'value': str(env_value), 'value_from': None }) deployment_spec_template_spec_containers = [ client.V1Container(name=container_name, env=p_env_list) ] deployment_spec_template_spec = client.V1PodSpec( containers=deployment_spec_template_spec_containers) deployment_spec_template = client.V1PodTemplateSpec( spec=deployment_spec_template_spec) deployment_spec = client.AppsV1beta1DeploymentSpec( template=deployment_spec_template) deployment = client.ExtensionsV1beta1Deployment(spec=deployment_spec) self.appsv1betaapi.patch_namespaced_deployment( name=deployment_name, namespace=self.namespace, body=deployment)
def create_deployment_object(images, app_name, config_location, key): try: username = os.environ['DOCKERUSER'] except: username = '******' configuration = google_authenticate('big-keyword-275020', 'us-central1-c', 'launch-app', key=key) client.Configuration.set_default(configuration) containers = [] # Create a container for each image for image in images: logger.info("Adding container to deployment with image {}...".format( image[0])) containers.append( client.V1Container( name=image[0].replace("gcr.io/big-keyword-275020/", '').replace(":latest", '').replace( username, '').replace('/', ''), image=image[0], ports=[client.V1ContainerPort(container_port=int(image[1]))])) logger.info(containers) # Create metadata and spec template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={'app': app_name}), spec=client.V1PodSpec(containers=containers)) # Create the specification section spec = client.V1DeploymentSpec(replicas=1, selector={'matchLabels': { 'app': app_name }}, template=template) # Create and instantiate the deployment object logger.info("APP_NAME: {}".format(app_name)) deployment = client.V1Deployment( api_version="apps/v1", kind="Deployment", metadata=client.V1ObjectMeta(name=app_name), spec=spec) # Return our deployment object return deployment
def _AssumeInsideKfp(self, namespace='my-namespace', pod_name='my-pod-name', pod_uid='my-pod-uid', pod_service_account_name='my-service-account-name', with_pvc=False): pod = k8s_client.V1Pod(api_version='v1', kind='Pod', metadata=k8s_client.V1ObjectMeta( name=pod_name, uid=pod_uid, ), spec=k8s_client.V1PodSpec(containers=[ k8s_client.V1Container(name='main', volume_mounts=[]), ], volumes=[])) if with_pvc: pod.spec.volumes.append( k8s_client.V1Volume( name='my-volume', persistent_volume_claim=k8s_client. V1PersistentVolumeClaimVolumeSource(claim_name='my-pvc'))) pod.spec.containers[0].volume_mounts.append( k8s_client.V1VolumeMount(name='my-volume', mount_path=self._base_dir)) mock.patch.object(kube_utils, 'is_inside_kfp', return_value=True).start() pod.spec.service_account_name = pod_service_account_name mock.patch.object(kube_utils, 'get_current_kfp_pod', return_value=pod).start() mock.patch.object(kube_utils, 'get_kfp_namespace', return_value=namespace).start() if with_pvc: (self._mock_core_v1_api.read_namespaced_persistent_volume_claim. return_value) = k8s_client.V1PersistentVolumeClaim( metadata=k8s_client.V1ObjectMeta(name='my-pvc'), spec=k8s_client.V1PersistentVolumeClaimSpec( access_modes=['ReadWriteMany']))
def get_init_container(self, init_command, init_args, env_vars, context_mounts, persistence_outputs, persistence_data): """Pod init container for setting outputs path.""" env_vars = to_list(env_vars, check_none=True) if self.original_name is not None and self.cloning_strategy == CloningStrategy.RESUME: return [] if self.original_name is not None and self.cloning_strategy == CloningStrategy.COPY: command = InitCommands.COPY original_outputs_path = stores.get_experiment_outputs_path( persistence=persistence_outputs, experiment_name=self.original_name) else: command = InitCommands.CREATE original_outputs_path = None outputs_path = stores.get_experiment_outputs_path( persistence=persistence_outputs, experiment_name=self.experiment_name) _, outputs_volume_mount = get_pod_outputs_volume( persistence_outputs=persistence_outputs) volume_mounts = outputs_volume_mount + to_list(context_mounts, check_none=True) init_command = init_command or ["/bin/sh", "-c"] init_args = init_args or to_list( get_output_args(command=command, outputs_path=outputs_path, original_outputs_path=original_outputs_path)) init_args += to_list( get_auth_context_args(entity='experiment', entity_name=self.experiment_name)) return [ client.V1Container( name=self.init_container_name, image=self.init_docker_image, image_pull_policy=self.init_docker_image_pull_policy, command=init_command, args=[''.join(init_args)], env=env_vars, resources=get_init_resources(), volume_mounts=volume_mounts) ]
def get_project_pod_spec(volume_mounts, volumes, image, command, args, ports, env_vars=None, container_name=None, resources=None, node_selector=None, restart_policy=None, use_service_account=False): """Pod spec to be used to create pods for project: tensorboard, notebooks.""" volume_mounts = get_list(volume_mounts) volumes = get_list(volumes) gpu_volume_mounts, gpu_volumes = get_gpu_volumes_def(resources) volume_mounts += gpu_volume_mounts volumes += gpu_volumes ports = [client.V1ContainerPort(container_port=port) for port in ports] containers = [ client.V1Container(name=container_name, image=image, command=command, args=args, ports=ports, env=env_vars, resources=get_resources(resources), volume_mounts=volume_mounts) ] service_account_name = None if use_service_account and settings.K8S_RBAC_ENABLED: service_account_name = settings.K8S_SERVICE_ACCOUNT_NAME return client.V1PodSpec(restart_policy=restart_policy, service_account_name=service_account_name, containers=containers, volumes=volumes, node_selector=node_selector)
def create_pod_template_spec(self, job_name, container_image, args, envs, resources=None): # Configure a Pod template container try: container = client.V1Container(name=job_name, image=container_image, args=args, resources=resources, env=envs, image_pull_policy="Always") pod_template_spec = client.V1PodSpec(restart_policy="Never", containers=[container]) return pod_template_spec except: print('error create_pod_template_spec')
def generate_pod_spec(self, image=None): if not image: image = self.image_tag return client.V1PodSpec( containers=[client.V1Container( name='model', image=image, command=self.preprocessor.get_command(), security_context=client.V1SecurityContext( run_as_user=0, ), env=[client.V1EnvVar( name='FAIRING_RUNTIME', value='1', )], # Set the directory where the python files are built. # TODO(jlewi): Would it be better to set PYTHONPATH? working_dir=self.preprocessor.path_prefix, )], )
def createDeploymentConfig(name, namespace, replicas, image): container = client.V1Container( name="nginx", image="nginx:1.7.9", ports=[client.V1ContainerPort(container_port=80)]) template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"name": name}), spec=client.V1PodSpec(containers=[container])) spec = client.ExtensionsV1beta1DeploymentSpec(replicas=3, template=template) deployemnt = client.ExtensionsV1beta1Deployment( api_version="extensions/v1beta1", kind="Deployment", metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME), spec=spec) return deployemnt
def get_init_container(self, init_command, init_args, env_vars, context_mounts, persistence_outputs, persistence_data): """Pod init container for setting outputs path.""" env_vars = to_list(env_vars, check_none=True) volume_mounts = to_list(context_mounts, check_none=True) init_command = init_command or ["/bin/sh", "-c"] init_args = to_list(get_auth_context_args(entity='notebook', entity_name=self.job_name)) return client.V1Container( name=self.init_container_name, image=self.init_docker_image, image_pull_policy=self.init_docker_image_pull_policy, command=init_command, args=init_args, env=env_vars, volume_mounts=volume_mounts)
def create_job_object(): # Configureate Pod template container container = client.V1Container( name='busybox', image='busybox', args=['sleep', '6']) # Create and configurate a spec section template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={'name': 'simple-job'}), spec=client.V1PodSpec(restart_policy='OnFailure', containers=[container])) # Create the specification of deployment spec = client.V1JobSpec(template=template) # Instantiate the job object job = client.V1Job( api_version='batch/v1', kind='Job', metadata=client.V1ObjectMeta(name=JOB_NAME), spec=spec) return job
def create_deployment_object(): # Configureate Pod template container container = client.V1Container( name="nginx", image="nginx:1.15.4", ports=[client.V1ContainerPort(container_port=80)]) # Create and configurate a spec section template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"app": "nginx"}), spec=client.V1PodSpec(containers=[container])) # Create the specification of deployment spec = client.AppsV1beta1DeploymentSpec(replicas=3, template=template) # Instantiate the deployment object deployment = client.AppsV1beta1Deployment( api_version="apps/v1beta1", kind="Deployment", metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME), spec=spec) return deployment
def create_job_manifest(n_job, n_node): container = client.V1Container( name="pn-generator", image="gblee87/pn_generator:0.2", env=[ client.V1EnvVar(name="BROKER_URL", value="amqp://*****:*****@taskqueue:5672"), client.V1EnvVar(name="QUEUE", value="taskqueue") ]) template = client.V1PodTemplateSpec( spec=client.V1PodSpec(containers=[container], restart_policy="Never")) spec = client.V1JobSpec(backoff_limit=4, template=template, completions=n_job, parallelism=n_node) job = client.V1Job(api_version="batch/v1", kind="Job", metadata=client.V1ObjectMeta(name=OBJECT_NAME), spec=spec) return job
def customEndpointSpec(custom_model_spec): env = [ client.V1EnvVar(name=i['name'], value=i['value']) for i in custom_model_spec['env'] ] if custom_model_spec.get('env', '') else None ports = [ client.V1ContainerPort( container_port=int(custom_model_spec.get('port', ''))) ] if custom_model_spec.get('port', '') else None containerSpec = client.V1Container( name=custom_model_spec.get('name', 'custom-container'), image=custom_model_spec['image'], env=env, ports=ports, command=custom_model_spec.get('command', None), args=custom_model_spec.get('args', None), image_pull_policy=custom_model_spec.get('image_pull_policy', None), working_dir=custom_model_spec.get('working_dir', None)) return V1alpha2EndpointSpec(custom=V1alpha2CustomSpec( container=containerSpec))
def create_deployment_object(dname, dnamespace, dreplicas, dimage, denv_name, dcpu, dmemory): str_dname = str(dname) str_dnamespace = str(dnamespace) str_dreplicas = str(dreplicas) str_dimage = str(dimage) str_denv_name = str(denv_name) str_dcpu = str(dcpu) str_dmemory = str(dmemory) # Configureate Pod template container container = client.V1Container( name=str.lower(str_denv_name), image=str_dimage, # image_pull_policy="IfNotPresent", env=[client.V1EnvVar(name=str_denv_name, value=str_dreplicas)], resources={"limits": { "cpu": str_dcpu, "memory": str_dmemory }}) # ports=[client.V1ContainerPort(container_port=80)]) # Create and configurate a spec section # get nodename from horizon local_settings.py node_name_ = getattr(settings, "NODE_NAME", None) template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"clusterType": str_denv_name}), spec=client.V1PodSpec(containers=[container], node_name=node_name_)) # Create the specification of deployment spec = client.ExtensionsV1beta1DeploymentSpec(replicas=int(str_dreplicas), template=template) # Instantiate the deployment object deployment = client.ExtensionsV1beta1Deployment( api_version="extensions/v1beta1", kind="Deployment", metadata=client.V1ObjectMeta(name=str_dname, namespace=str_dnamespace), spec=spec) return deployment
def main(): # Fetching and loading Kubernetes Information config.load_kube_config() extension = client.ExtensionsV1beta1Api() # Container container = client.V1Container( name="nginx", image="nginx:1.7.9", image_pull_policy="IfNotPresent", ports=[client.V1ContainerPort(container_port=80)]) # Template template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={ "app": "nginx", "version": "v2.0" }), spec=client.V1PodSpec(containers=[container])) # Spec spec = client.ExtensionsV1beta1DeploymentSpec( replicas=3, selector=client.V1LabelSelector(match_labels={"app": "nginx"}), strategy=client.ExtensionsV1beta1DeploymentStrategy( rolling_update=client.ExtensionsV1beta1RollingUpdateDeployment( max_surge=1, max_unavailable=0)), template=template) #Deployment deployment = client.ExtensionsV1beta1Deployment( api_version="extensions/v1beta1", kind="Deployment", metadata=client.V1ObjectMeta(name="nginx-deployment"), spec=spec) # Creation of the Deployment in specified namespace extension.patch_namespaced_deployment(name="nginx-deployment", namespace="ratanb", body=deployment)
def __init__(self, image, experiment): from mrunner.utils.namesgenerator import get_random_name experiment_name = re.sub(r'[ ,.\-_:;]+', '-', experiment.name) name = '{}-{}'.format(experiment_name, get_random_name('-')) envs = experiment.env.copy() envs.update(experiment.cmd.env if experiment.cmd else {}) envs = {k: str(v) for k, v in envs.items()} resources = dict([ self._map_resources(name, qty) for name, qty in experiment.resources.items() ]) internal_volume_name = 'experiment-storage' vol = client.V1Volume( name=internal_volume_name, persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource( claim_name=KubernetesBackend.NFS_PVC_NAME)) ctr = client.V1Container( name=name, image=image, args=experiment.params, volume_mounts=[ client.V1VolumeMount(mount_path=experiment.storage_dir, name=internal_volume_name) ], resources=client.V1ResourceRequirements( limits={k: v for k, v in resources.items()}), env=[client.V1EnvVar(name=k, value=v) for k, v in envs.items()]) pod_spec = client.V1PodSpec(restart_policy='Never', containers=[ctr], volumes=[vol]) pod_template = client.V1PodTemplateSpec(spec=pod_spec) job_spec = client.V1JobSpec( template=pod_template, backoff_limit=0) # , active_deadline_seconds=100) super(Job, self).__init__(metadata=client.V1ObjectMeta(name=name), spec=job_spec)
def create_deployment(apps_v1_api, env_vars=None): if env_vars is None: env_vars = { "MINIO_ACCESS_KEY": MINIO_ACCESS_KEY, "MINIO_SECRET_KEY": MINIO_SECRET_KEY } env_list = [] for env_name, env_value in env_vars.items(): env_list.append(client.V1EnvVar(name=env_name, value=env_value)) volume = client.V1Volume(name="data") container = client.V1Container( name="minio", image="minio/minio", args=["server", "/data"], volume_mounts=[client.V1VolumeMount(name="data", mount_path="/data")], env=env_list, image_pull_policy="Always", ports=[client.V1ContainerPort(container_port=9000)], ) template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"app": "minio"}), spec=client.V1PodSpec(volumes=[volume], containers=[container])) spec = client.V1DeploymentSpec( selector=client.V1LabelSelector(match_labels={"app": "minio"}), replicas=1, template=template) deployment = client.V1Deployment(api_version="apps/v1", kind="Deployment", metadata=client.V1ObjectMeta( name="minio-deployment", labels={"app": "minio"}), spec=spec) apps_v1_api.create_namespaced_deployment(namespace="default", body=deployment)
def get_sidecar_container(job_name, job_container_name, sidecar_container_name, sidecar_docker_image, namespace, app_label, sidecar_config, sidecar_args, env_vars=None): """Return a pod sidecar container.""" env_vars = to_list(env_vars) if env_vars else [] env_vars += get_sidecar_env_vars(job_name=job_name, job_container_name=job_container_name) env_vars += get_service_env_vars(namespace=namespace) for k, v in sidecar_config.items(): env_vars.append(get_env_var(name=k, value=v)) return client.V1Container(name=sidecar_container_name, image=sidecar_docker_image, command=get_sidecar_command(app_label=app_label), env=env_vars, args=sidecar_args)
def create_deployment_object(image, name, ports=[], replicas=1): container_ports = [client.V1ContainerPort(container_port=p) for p in ports] # Configure Pod template container container = client.V1Container(name=name, image=image, ports=container_ports) # Create and configurate a spec section template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"app": name}), spec=client.V1PodSpec(containers=[container])) # Create the specification of deployment spec = client.ExtensionsV1beta1DeploymentSpec(replicas=replicas, template=template) # Instantiate the deployment object deployment = client.ExtensionsV1beta1Deployment( api_version="extensions/v1beta1", kind="Deployment", metadata=client.V1ObjectMeta(name='{}-deployment'.format(name)), spec=spec) return deployment
def create_job_object(self, name, image, labels, completions, backoff_limit, env): # Configureate Pod template container container = client.V1Container(name=name, image=image, env=env) # Create and configurate a spec section template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels=labels), spec=client.V1PodSpec( restart_policy="Never", service_account_name=self.service_account_name, containers=[container])) # Create the specification of deployment spec = client.V1JobSpec(template=template, completions=completions, backoff_limit=backoff_limit) # Instantiate the job object job = client.V1Job(api_version="batch/v1", kind="Job", metadata=client.V1ObjectMeta(name=name), spec=spec) return job
def func_to_pod(image, runtime, extra_env, command, args, workdir): container = client.V1Container( name="base", image=image, env=extra_env + runtime.spec.env, command=[command], args=args, working_dir=workdir, image_pull_policy=runtime.spec.image_pull_policy, volume_mounts=runtime.spec.volume_mounts, resources=runtime.spec.resources, ) pod_spec = kube_resource_spec_to_pod_spec(runtime.spec, container) if runtime.spec.image_pull_secret: pod_spec.image_pull_secrets = [ client.V1LocalObjectReference(name=runtime.spec.image_pull_secret) ] return pod_spec
def func_to_pod(image, runtime, extra_env, command, args, workdir): container = client.V1Container(name='base', image=image, env=extra_env + runtime.spec.env, command=[command], args=args, working_dir=workdir, image_pull_policy=runtime.spec.image_pull_policy, volume_mounts=runtime.spec.volume_mounts, resources=runtime.spec.resources) pod_spec = client.V1PodSpec(containers=[container], restart_policy='Never', volumes=runtime.spec.volumes, service_account=runtime.spec.service_account) if runtime.spec.image_pull_secret: pod_spec.image_pull_secrets = [ client.V1LocalObjectReference(name=runtime.spec.image_pull_secret)] return pod_spec
def simple_daemonset(): """Return the Kubernetes config matching the simple-daemonset.yaml manifest.""" return client.V1DaemonSet( api_version="apps/v1", kind="DaemonSet", metadata=client.V1ObjectMeta(name="canal-daemonset", labels={"app": "canal"}), spec=client.V1DaemonSetSpec( selector=client.V1LabelSelector(match_labels={"app": "canal"}), template=client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"app": "canal"}), spec=client.V1PodSpec(containers=[ client.V1Container( name="canal", image="canal:3.7.2", ports=[client.V1ContainerPort(container_port=9099)], ) ]), ), ), )
def create_nifi_ss_object(): container = client.V1Container( name="apache", image="apache/nifi:1.9.2", env=[client.V1EnvVar(name='NIFI_WEB_HTTP_HOST', value='0.0.0.0')], ports=[client.V1ContainerPort(container_port=8080)]) template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"app": "nifi"}), spec=client.V1PodSpec(containers=[container])) spec = client.V1StatefulSetSpec(replicas=1, service_name='nifi', template=template, selector={'matchLabels': { 'app': 'nifi' }}) ss = client.V1StatefulSet(api_version="apps/v1", kind="StatefulSet", metadata=client.V1ObjectMeta(name='nifi'), spec=spec) return ss
def construct_deployment(name, replicas=1): """Construct a fake deployment body""" return client.V1beta2Deployment( api_version='extensions/v1beta1', kind='Deployment', metadata=client.V1ObjectMeta(name=name), spec=client.V1beta2DeploymentSpec( selector=client.V1LabelSelector( match_labels={'deployment-name': name}), template=client.V1PodTemplateSpec( spec=client.V1PodSpec(containers=[ client.V1Container(image="busybox", name="main", command=["sleep", "3600"]) ]), metadata=client.V1ObjectMeta(labels={"deployment-name": name}, name=name), ), replicas=replicas, ), )
def create_deployment_object(): # Configureate Pod template container container = client.V1Container( name="lala", image= "gcr.io/spacemesh-198810/node:46f1ad099cdfefc65dda5f726d26ecea7b1f35fb", ports=[client.V1ContainerPort(container_port=9091)]) # Create and configurate a spec section template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"app": "nginx"}), spec=client.V1PodSpec(containers=[container])) # Create the specification of deployment spec = client.ExtensionsV1beta1DeploymentSpec(replicas=1, template=template) # Instantiate the deployment object deployment = client.ExtensionsV1beta1Deployment( api_version="extensions/v1beta1", kind="Deployment", metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME), spec=spec) return deployment
def create_init_container(job, volume_mount, job_container): from ai_flow.application_master.master import GLOBAL_MASTER_CONFIG logging.info( 'Kubernetes GLOBAL_MASTER_CONFIG {}'.format(GLOBAL_MASTER_CONFIG)) init_args_default = [ str(job.job_config.project_desc.project_config), str(job.job_context.workflow_execution_id), job.job_config.project_path, DEFAULT_PROJECT_PATH ] container \ = client.V1Container(name='init-container', image=GLOBAL_MASTER_CONFIG['ai_flow_base_init_image'], image_pull_policy='Always', command=["python", "/app/download.py"], args=init_args_default, volume_mounts=[volume_mount]) volume = client.V1Volume(name='download-volume') pod_spec = client.V1PodSpec(restart_policy='Never', containers=[job_container], init_containers=[container], volumes=[volume]) return pod_spec
def create_deployment_object(): """ Creates the LB service and exposes the external IP """ container = client.V1Container( name="my-nginx", image="nginx:1.15", ports=[client.V1ContainerPort(container_port=80)]) template = client.V1PodTemplateSpec( metadata=client.V1ObjectMeta(labels={"app": "nginx"}), spec=client.V1PodSpec(containers=[container])) spec = client.ExtensionsV1beta1DeploymentSpec(replicas=1, template=template) set_progress("Instantiate the deployment object") deployment = client.ExtensionsV1beta1Deployment( api_version="extensions/v1beta1", kind="Deployment", metadata=client.V1ObjectMeta(name="nginx-deployment"), spec=spec) time.sleep(60) return deployment
def _create_k8s_job(self, yaml_spec): """ _create_k8s_job creates a kubernetes job based on the yaml spec """ pod = k8s_client.V1Pod(metadata=k8s_client.V1ObjectMeta( generate_name=yaml_spec['metadata']['generateName'])) container = k8s_client.V1Container( name=yaml_spec['spec']['containers'][0]['name'], image=yaml_spec['spec']['containers'][0]['image'], args=yaml_spec['spec']['containers'][0]['args']) pod.spec = k8s_client.V1PodSpec( restart_policy=yaml_spec['spec']['restartPolicy'], containers=[container], service_account_name=yaml_spec['spec']['serviceAccountName']) try: api_response = self._corev1.create_namespaced_pod( yaml_spec['metadata']['namespace'], pod) return api_response.metadata.name, True except k8s_client.rest.ApiException as e: logging.exception( "Exception when calling CoreV1Api->create_namespaced_pod: {}\n" .format(str(e))) return '', False
def deployment(self, name, image, command, env, vmounts, volumes, lifecycle=None, args=None, ports=None, replicas=1, patch=False): """Create and upload deployment, patch option also available.""" deploy = client.V1Deployment(kind="Deployment", api_version="apps/v1") deploy.metadata = client.V1ObjectMeta(name=name) container = client.V1Container(name=name, image=image, image_pull_policy="IfNotPresent", volume_mounts=vmounts, command=command, env=env, args=args, lifecycle=lifecycle) if ports: container.ports = list(map(lambda x: client.V1ContainerPort(container_port=x), ports)) template = client.V1PodTemplateSpec(metadata=client.V1ObjectMeta(labels={"app": name}), spec=client.V1PodSpec(containers=[container], volumes=volumes, restart_policy="Always")) spec = client.V1DeploymentSpec(replicas=replicas, template=template, selector=client.V1LabelSelector(match_labels={"app": name})) deploy.spec = spec try: api_app.create_namespaced_deployment(namespace=self._namespace, body=deploy) LOG.info(f'Deployment: {name} created.') except ApiException as e: if e.status == 409 and patch: api_app.patch_namespaced_deployment(name=name, namespace=self._namespace, body=deploy) LOG.info(f'Deployment: {name} patched.') else: LOG.error(f'Exception message: {e}')