Пример #1
0
    def test_cleanup(self):

        api = get_core_api()

        # take just namespaces that are not in terminating state
        number_of_namespaces = len(
            [item for item in api.list_namespace().items if item.status.phase != "Terminating"])

        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key, cleanup=[K8sCleanupPolicy.NAMESPACES]) as k8s_backend:

            # create two namespaces
            k8s_backend.create_namespace()
            k8s_backend.create_namespace()

        # cleanup should delete two namespaces created with k8s backend
        assert len(
            [item for item in api.list_namespace().items
             if item.status.phase != "Terminating"]) == number_of_namespaces

        with K8sBackend(api_key=api_key) as k8s_backend:

            # create two namespaces
            k8s_backend.create_namespace()
            k8s_backend.create_namespace()

        # no cleanup - namespaces are not deleted after work with backend is finished
        assert len(
            [item for item in api.list_namespace().items
             if item.status.phase != "Terminating"]) == number_of_namespaces + 2
Пример #2
0
    def __init__(self,
                 api_key=None,
                 logging_level=logging.INFO,
                 logging_kwargs=None,
                 cleanup=None):
        """
        This method serves as a configuration interface for conu.

        :param api_key: str, Bearer API token
        :param logging_level: int, control logger verbosity: see logging.{DEBUG,INFO,ERROR}
        :param logging_kwargs: dict, additional keyword arguments for logger set up, for more info
                                see docstring of set_logging function
        :param cleanup: list, list of k8s cleanup policy values, examples:
            - [CleanupPolicy.EVERYTHING]
            - [CleanupPolicy.PODS, CleanupPolicy.SERVICES]
            - [CleanupPolicy.NOTHING]
        """
        super(K8sBackend, self).__init__(logging_level=logging_level,
                                         logging_kwargs=logging_kwargs)

        k8s_client.API_KEY = api_key
        self.core_api = k8s_client.get_core_api()
        self.apps_api = k8s_client.get_apps_api()

        self.managed_namespaces = []

        self.cleanup = cleanup or [K8sCleanupPolicy.NOTHING]

        if K8sCleanupPolicy.NOTHING in self.cleanup and len(self.cleanup) != 1:
            raise ConuException(
                "Cleanup policy NOTHING cannot be combined with other values")
Пример #3
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)
Пример #4
0
    def __init__(self, name, ports, namespace='default', labels=None, selector=None,
                 create_in_cluster=False, spec=None):
        """
        Utility functions for kubernetes services.

        :param name: str, name of the service
        :param namespace: str, name of the namespace
        :param ports: list of str, list of exposed ports, example:
                - ['1234/tcp', '8080/udp']
        :param labels: dict, dict of labels
        :param selector: dict, route service traffic to pods with label keys and
         values matching this selector
        """
        self.name = name
        self.namespace = namespace
        self.ports = ports

        exposed_ports = metadata_ports_to_k8s_ports(self.ports)

        self.metadata = client.V1ObjectMeta(name=self.name, namespace=self.namespace, labels=labels)
        self.spec = spec or client.V1ServiceSpec(ports=exposed_ports, selector=selector)

        self.body = client.V1Service(spec=self.spec, metadata=self.metadata)

        self.api = get_core_api()

        if create_in_cluster:
            self.create_in_cluster()
Пример #5
0
    def __init__(self, name, namespace, spec):
        """
        Utility functions for kubernetes pods.

        :param name: name of pod
        :param namespace: str, namespace in which is pod created
        :param spec: pod spec
        https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1PodSpec.md
        """

        self.name = name
        self.namespace = namespace
        self.spec = spec
        self.phase = None
        self.core_api = get_core_api()
Пример #6
0
    def __init__(self, logging_level=logging.INFO, logging_kwargs=None):
        """
        This method serves as a configuration interface for conu.

        :param logging_level: int, control logger verbosity: see logging.{DEBUG,INFO,ERROR}
        :param logging_kwargs: dict, additional keyword arguments for logger set up, for more info
                                see docstring of set_logging function
        :param cleanup: list, list of cleanup policy values, examples:
            - [CleanupPolicy.EVERYTHING]
            - [CleanupPolicy.VOLUMES, CleanupPolicy.TMP_DIRS]
            - [CleanupPolicy.NOTHING]
        """
        super(K8sBackend, self).__init__(logging_level=logging_level,
                                         logging_kwargs=logging_kwargs)

        self.core_api = get_core_api()
        self.apps_api = get_apps_api()
Пример #7
0
    def __init__(self, namespace, name=None, spec=None, from_template=None):
        """
        Utility functions for kubernetes pods.

        :param namespace: str, namespace in which is pod created
        :param name: name of pod
        :param spec: pod spec

            * https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1PodSpec.md
        :param from_template: str, pod template, example:

            * https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/#pod-templates
        """
        self.core_api = get_core_api()
        self.namespace = namespace
        self.phase = None

        if (from_template is not None) and (name is not None
                                            or spec is not None):
            raise ConuException(
                'from_template cannot be passed to constructor at the same time'
                ' with name or spec')
        elif from_template is not None:  # create Pod from template
            try:
                pod_instance = self.core_api.create_namespaced_pod(
                    namespace=namespace, body=from_template)
            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))

            self.name = pod_instance.metadata.name
            self.spec = pod_instance.spec

        elif name is not None or spec is not None:
            self.name = name
            self.spec = spec
        else:
            raise ConuException(
                'to create pod you need to specify pod template or'
                ' properties: name and spec.')