def create_from_yaml(self, yaml_path, wait=True, max_threads=DEFAULT_MAX_THREADS): with open(yaml_path, "r") as f: for resource in yaml.load_all(f.read()): if resource.get("kind") is None: raise K8sInvalidResourceBody() if resource["kind"] == "Pod": self.pod.create(body=resource, wait=wait) if resource["kind"] == "Deployment": self.deployment.create(body=resource, wait=wait, max_threads=max_threads) if resource["kind"] == "DaemonSet": self.daemon_set.create(body=resource, wait=wait, max_threads=max_threads) if resource["kind"] == "Namespace": self.namespace.create(body=resource, wait=wait) if resource["kind"] == "Secret": self.pod.create(body=resource, wait=wait) if resource["kind"] == "Service": self.service.create(body=resource, wait=wait) else: raise K8sInvalidResourceBody("unsupported resource type")
def create(self, body, namespace=DEFAULT_NAMESPACE, wait=True, max_threads=DEFAULT_MAX_THREADS): """ Create deployment :param body: deployment's body :type body: dictionary or V1Deployment :param namespace: the namespace to create the deployment in if there is no namespace in the yaml (default value is 'default') :type namespace: str :param wait: to wait until the creation is over (default value is True) :type wait: bool :param max_threads: max number of threads to open during waiting (default value is DEFAULT_MAX_THREADS) :type max_threads: int :return: deployment name :rtype: str """ # check the type of the body and that it contains name # and raise exception if not try: if isinstance(body, V1Deployment): deployment_name = body.metadata.name if hasattr(body, "metadata") and hasattr( body.metadata, "namespace"): namespace = body.metadata.namespace elif isinstance(body, dict): deployment_name = body["metadata"]["name"] namespace = body.get("metadata", {}).get("namespace", namespace) else: raise K8sInvalidResourceBody() except (KeyError, AttributeError): raise K8sInvalidResourceBody() # create the deployment from the body self.client_app.create_namespaced_deployment(body=body, namespace=namespace) logger.info(f"Created the deployment {deployment_name} in {namespace} " "namespace") # wait to the deployment to run if wait: self.wait_for_deployment_to_run(deployment_name=deployment_name, namespace=namespace, max_threads=max_threads) return deployment_name
def create(self, body, namespace=DEFAULT_NAMESPACE, wait=True): """ Create pod :param body: pod's body :type body: dictionary or V1Pod :param namespace: the namespace to create the pod in if there is no namespace in the yaml (default value is 'default') :type namespace: str :param wait: to wait until the creation is over (default value is True) :type wait: bool :return: pod name :rtype: str """ # check the type of the body and that it contains name # and raise exception if not try: if isinstance(body, V1Pod): pod_name = body.metadata.name if hasattr(body, "metadata") and hasattr( body.metadata, "namespace"): namespace = body.metadata.namespace containers_counter = len(body.spec.containers) elif isinstance(body, dict): pod_name = body["metadata"]["name"] namespace = body.get("metadata", {}).get("namespace", namespace) containers_counter = len(body["spec"]["containers"]) else: raise K8sInvalidResourceBody() except (KeyError, AttributeError): raise K8sInvalidResourceBody() # create the pod from the body pod_obj = self.client_core.create_namespaced_pod(body=body, namespace=namespace) logger.info(f"Created the pod {pod_name} in {namespace} namespace") # wait to the containers to run if wait: self.wait_for_containers_to_run( pod_name=pod_name, pod_id=pod_obj.metadata.uid, containers_counter=containers_counter, namespace=namespace) return pod_name
def create(self, body, namespace=DEFAULT_NAMESPACE, wait=True): """ Create a secret :param body: secret's body :type body: dictionary or V1Secret :param namespace: the namespace to create the secret in if there is no namespace in the yaml (default value is 'default') :type namespace: str :param wait: to wait until the creation is over (default value is True) :type wait: bool :return: secret name :rtype: str """ try: if isinstance(body, V1Secret): secret_name = body.metadata.name if hasattr(body, "metadata") and hasattr( body.metadata, "namespace"): namespace = body.metadata.namespace elif isinstance(body, dict): secret_name = body["metadata"]["name"] namespace = body.get("metadata", {}).get("namespace", namespace) else: raise K8sInvalidResourceBody() except (KeyError, AttributeError): raise K8sInvalidResourceBody() # create the secret from the body self.client_core.create_namespaced_secret(namespace=namespace, body=body) logger.info( f"Created the secret {secret_name} in namespace {namespace}") # wait to secret creation if wait: self.wait_to_secret_creation(secret_name=secret_name, namespace=namespace) return secret_name
def __init__(self, kind="", name="", api_version="", body=None): body = body or {} super(K8sResource, self).__init__() self["metadata"] = {} self["spec"] = {} self.update(body) if not self.get("metadata", {}).get("name"): if name: self["metadata"]["name"] = name else: raise K8sInvalidResourceBody("Did not satisfy a name to the " "resource") if not self.get("kind"): if kind: self["kind"] = kind else: raise K8sInvalidResourceBody("Did not satisfy kind to the " "resource") if not self.get("apiVersion"): if kind: self["apiVersion"] = api_version else: raise K8sInvalidResourceBody("Did not satisfy an apiVersion " "to the resource")