Пример #1
0
    def run_in_pod(self, namespace="default"):
        """
        run image inside Kubernetes Pod
        :param namespace: str, name of namespace where pod will be created
        :return: Pod instance
        """

        core_api = get_core_api()

        image_data = self.get_metadata()

        pod = Pod.create(image_data)

        try:
            pod_instance = core_api.create_namespaced_pod(namespace=namespace,
                                                          body=pod)
        except ApiException as e:
            raise ConuException(
                "Exception when calling CoreV1Api->create_namespaced_pod: %s\n"
                % e)

        logger.info("Starting Pod %s in namespace %s" %
                    (pod_instance.metadata.name, namespace))

        return Pod(name=pod_instance.metadata.name,
                   namespace=pod_instance.metadata.namespace,
                   spec=pod_instance.spec)
Пример #2
0
    def __init__(self,
                 name=None,
                 selector=None,
                 labels=None,
                 image_metadata=None,
                 namespace='default',
                 create_in_cluster=False,
                 from_template=None):
        """
        Utility functions for kubernetes deployments.

        :param name: str, name of the deployment
        :param selector: Label selector for pods. Existing ReplicaSets whose pods are selected by
         this will be the ones affected by this deployment. It must match the pod template's labels
        :param labels: dict, dict of labels
        :param image_metadata: ImageMetadata
        :param namespace: str, name of the namespace
        :param create_in_cluster: bool, if True deployment is created in Kubernetes cluster
        :param from_template: str, deployment template, example:
               - https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

        """

        self.namespace = namespace

        if (from_template is not None) and (name is not None or selector
                                            is not None or labels is not None
                                            or image_metadata is not None):
            raise ConuException(
                'from_template cannot be passed to constructor at the same time with'
                ' name, selector, labels or image_metadata')
        elif from_template is not None:
            self.body = yaml.load(from_template)

            self.name = self.body['metadata']['name']

        elif (name is not None and selector is not None and labels is not None
              and image_metadata is not None):
            self.name = name
            self.pod = Pod.create(image_metadata)

            self.spec = client.V1DeploymentSpec(
                selector=client.V1LabelSelector(match_labels=selector),
                template=client.V1PodTemplateSpec(
                    metadata=client.V1ObjectMeta(labels=selector),
                    spec=self.pod.spec))

            self.metadata = client.V1ObjectMeta(name=self.name,
                                                namespace=self.namespace,
                                                labels=labels)

            self.body = client.V1Deployment(spec=self.spec,
                                            metadata=self.metadata)
        else:
            raise ConuException(
                'to create deployment you need to specify template or'
                ' properties: name, selector, labels, image_metadata')

        self.api = get_apps_api()

        if create_in_cluster:
            self.create_in_cluster()