예제 #1
0
    def test_node_affinity_with_required(self):
        model = _constants.pod_with_node_affinity()
        pod = Pod(model)
        config = _utils.create_config()
        k8s = K8sPod(config=config, name="yo")
        k8s.model = pod

        if _utils.is_reachable(config):
            # ensure we have enough nodes
            nodes = K8sNode(config=config, name="yo").list()
            self.assertGreaterEqual(len(nodes), 3)
            # tag the nodes
            for i in range(len(nodes)):
                node = nodes[i]
                if i == 0:
                    node.labels.update(
                        {'kubernetes.io/e2e-az-name': 'e2e-az1'})
                if i == 1:
                    node.labels.update(
                        {'kubernetes.io/e2e-az-name': 'e2e-az2'})
                if i == 2:
                    node.labels.update(
                        {'kubernetes.io/e2e-az-name': 'e2e-az3'})
                node.update()
            nodes = K8sNode(config=config, name="yo").list()
            for node in nodes:
                self.assertIn('kubernetes.io/e2e-az-name', node.labels)
            # create the pod
            k8s.create()
            # untag the nodes
            for node in nodes:
                node.labels.pop('kubernetes.io/e2e-az-name', None)
                node.update()
        pass
예제 #2
0
 def list(self, pattern=None, labels=None):
     ls = super(K8sPod, self).list(labels=labels)
     pods = list(map(lambda pod: Pod(pod), ls))
     if pattern is not None:
         pods = list(filter(lambda pod: pattern in pod.name, pods))
     k8s = list()
     for x in pods:
         p = K8sPod(config=self.config, name=x.name).from_model(m=x)
         k8s.append(p)
     return k8s
예제 #3
0
 def list(self, pattern=None):
     ls = super(K8sPod, self).list()
     pods = map(lambda x: Pod(x), ls)
     if pattern is not None:
         pods = filter(lambda x: pattern in x.name, pods)
     k8s = []
     for x in pods:
         p = K8sPod(config=self.config, name=x.name)
         p.model = x
         k8s.append(p)
     return k8s
예제 #4
0
    def test_pod_affinity_no_good_nodes(self):
        model = _constants.pod_with_pod_affinity()
        pod = Pod(model)
        config = _utils.create_config()
        k8s = K8sPod(config=config, name="yo")
        k8s.model = pod

        if _utils.is_reachable(config):
            nodes = K8sNode(config=config, name="yo").list()
            # untag all nodes
            for node in nodes:
                node.labels.pop('kubernetes.io/e2e-az-name', None)
                node.update()
            with self.assertRaises(TimedOutException):
                # timeout because of required
                k8s.create()
예제 #5
0
    def test_pod_affinities(self):
        config = _utils.create_config()
        container = _utils.create_container(name="nginx", image="nginx:latest")

        pod_s1 = _utils.create_pod(config=config, name="s1")
        pod_s1.labels.update({'security': 'S1'})
        pod_s1.node_selector = {'kubernetes.io/e2e-az-name': 'e2e-az1'}
        pod_s1.add_container(container)

        pod_s2 = _utils.create_pod(config=config, name="s2")
        pod_s2.labels.update({'security': 'S2'})
        pod_s2.node_selector = {'kubernetes.io/e2e-az-name': 'e2e-az2'}
        pod_s2.add_container(container)

        model = _constants.pod_with_pod_affinity()
        pod = Pod(model)
        config = _utils.create_config()
        k8s = K8sPod(config=config, name="yo")
        k8s.model = pod

        if _utils.is_reachable(config):
            # ensure we have enough nodes
            nodes = K8sNode(config=config, name="yo").list()
            if len(nodes) < 3:
                self.skipTest(reason='Not enough nodes to perform test.')
            else:
                self.assertGreaterEqual(len(nodes), 3)
            # tag the nodes
            for i in range(len(nodes)):
                node = nodes[i]
                if i == 0:
                    node.labels.update(
                        {'kubernetes.io/e2e-az-name': 'e2e-az1'})
                if i == 1:
                    node.labels.update(
                        {'kubernetes.io/e2e-az-name': 'e2e-az2'})
                if i == 2:
                    node.labels.update(
                        {'kubernetes.io/e2e-az-name': 'e2e-az3'})
                node.update()
            # create the pods for affinity lookup
            pod_s1.create()
            pod_s2.create()
            # create the pod with affinities
            k8s.create()
            pass
예제 #6
0
    def get_by_name(config=None, name=None):
        if config is None:
            config = K8sConfig()
        if not is_valid_string(name):
            raise SyntaxError(
                'K8sPod.get_by_name(): name: [ {0} ] is invalid.'.format(name))

        pod_list = []
        data = {'labelSelector': 'name={0}'.format(name)}
        pods = K8sPod(config=config, name=name).get_with_params(data=data)

        for pod in pods:
            try:
                p = Pod(pod)
                k8s_pod = K8sPod(config=config, name=p.metadata.name).get()
                pod_list.append(k8s_pod)
            except NotFoundException:
                pass

        return pod_list
예제 #7
0
    def get_by_labels(config=None, labels=None):
        if config is None:
            config = K8sConfig()
        if not is_valid_dict(labels):
            raise SyntaxError(
                'K8sPod.get_by_labels(): labels: [ {} ] is invalid.'.format(
                    labels))

        pod_list = []
        selector = ",".join(
            ['%s=%s' % (key, value) for (key, value) in labels.items()])
        data = {'labelSelector': selector}
        p = K8sPod(config=config, name=labels['name'])
        pods = p.get_with_params(data=data)

        for pod in pods:
            try:
                p = Pod(pod)
                k8s_pod = K8sPod(config=config, name=p.metadata.name).get()
                pod_list.append(k8s_pod)
            except NotFoundException:
                pass

        return pod_list
예제 #8
0
 def get(self):
     self.model = Pod(model=self.get_model())
     return self
예제 #9
0
    def __init__(self, config=None, name=None):
        K8sPodBasedObject.__init__(self, config=config, obj_type='Pod', name=name)
        self.model = Pod(name=name, namespace=self.config.namespace)

        if self.config.pull_secret is not None:
            self.model.add_image_pull_secrets(name=self.config.pull_secret)
예제 #10
0
class K8sPod(K8sPodBasedObject):

    def __init__(self, config=None, name=None):
        K8sPodBasedObject.__init__(self, config=config, obj_type='Pod', name=name)
        self.model = Pod(name=name, namespace=self.config.namespace)

        if self.config.pull_secret is not None:
            self.model.add_image_pull_secrets(name=self.config.pull_secret)

    # -------------------------------------------------------------------------------------  add

    def add_annotation(self, k=None, v=None):
        self.model.add_pod_annotation(k=k, v=v)
        return self

    def add_label(self, k=None, v=None):
        self.model.add_pod_label(k=k, v=v)
        return self

    # ------------------------------------------------------------------------------------- delete

    def del_annotation(self, k=None):
        self.model.del_pod_annotation(k=k)
        return self

    def del_label(self, k=None):
        self.model.del_pod_label(k=k)
        return self

    # ------------------------------------------------------------------------------------- get

    def get(self):
        self.model = Pod(model=self.get_model())
        return self

    def get_annotation(self, k=None):
        return self.model.get_pod_annotation(k=k)

    def get_annotations(self):
        return self.model.get_pod_annotations()

    def get_label(self, k=None):
        return self.model.get_pod_label(k=k)

    def get_labels(self):
        return self.model.get_pod_labels()

    def get_namespace(self):
        return self.model.get_pod_namespace()

    def get_status(self):
        return self.model.get_pod_status()

    # ------------------------------------------------------------------------------------- polling readiness

    def is_ready(self):
        ready = False
        status = self.get_status()
        if status is not None:
            assert isinstance(status, PodStatus)
            pod_phase = status.get_pod_phase()
            conditions = status.get_pod_conditions()
            conditions_ok = 0
            for cond in conditions:
                assert isinstance(cond, dict)
                cond_type = cond.get('type', '')
                cond_status = cond.get('status', 'False')
                if cond_status == 'True' and cond_type == 'Ready':
                    conditions_ok += 1
            if pod_phase == 'Running' and len(conditions) == conditions_ok:
                ready = True
        return ready

    # ------------------------------------------------------------------------------------- set

    def set_annotations(self, dico=None):
        self.model.set_pod_annotations(new_dict=dico)
        return self

    def set_labels(self, dico=None):
        self.model.set_pod_labels(dico=dico)
        return self

    def set_namespace(self, name=None):
        self.model.set_pod_namespace(name=name)
        return self

    # ------------------------------------------------------------------------------------- filtering

    @staticmethod
    def get_by_name(config=None, name=None):
        if name is None:
            raise SyntaxError('K8sPod: name: [ {0} ] cannot be None.'.format(name))
        if not isinstance(name, str):
            raise SyntaxError('K8sPod: name: [ {0} ] must be a string.'.format(name))

        pod_list = list()
        data = {'labelSelector': 'name={0}'.format(name)}
        pods = K8sPod(config=config, name=name).get_with_params(data=data)

        for pod in pods:
            try:
                pod_name = Pod(model=pod).get_pod_name()
                pod_list.append(K8sPod(config=config, name=pod_name).get())
            except NotFoundException:
                pass

        return pod_list

    @staticmethod
    def get_by_labels(config=None, labels=None):
        if labels is None:
            raise SyntaxError('K8sPod: labels: [ {0} ] cannot be None.'.format(labels))
        if not isinstance(labels, dict):
            raise SyntaxError('K8sPod: labels: [ {0} ] must be a dict.'.format(labels))

        pod_list = list()
        my_labels = ",".join(['%s=%s' % (key, value) for (key, value) in labels.items()])
        data = dict(labelSelector="{labels}".format(labels=my_labels))
        pods = K8sPod(config=config, name=labels.get('name')).get_with_params(data=data)

        for pod in pods:
            try:
                pod_name = Pod(model=pod).get_pod_name()
                pod_list.append(K8sPod(config=config, name=pod_name).get())
            except NotFoundException:
                pass

        return pod_list
예제 #11
0
 def get(self):
     self.model = Pod(self.get_model())
     return self
예제 #12
0
    def __init__(self, config=None, name=None):
        super(K8sPod, self).__init__(config=config, obj_type="Pod", name=name)

        self.model = Pod(name=name, namespace=self.config.namespace)
        if self.config.pull_secret is not None:
            self.model.add_image_pull_secrets(name=self.config.pull_secret)
예제 #13
0
class K8sPod(K8sPodBasedObject):
    def __init__(self, config=None, name=None):
        super(K8sPod, self).__init__(config=config, obj_type="Pod", name=name)

        self.model = Pod(name=name, namespace=self.config.namespace)
        if self.config.pull_secret is not None:
            self.model.add_image_pull_secrets(name=self.config.pull_secret)

    # -------------------------------------------------------------------------------------  override

    def create(self):
        super(K8sPod, self).create()
        self.get()
        self._wait_for_readiness()
        return self

    def update(self):
        super(K8sPod, self).update()
        self.get()
        self._wait_for_readiness()
        return self

    def _wait_for_readiness(self):
        start_time = time.time()
        while not self.is_ready():
            time.sleep(0.2)
            elapsed_time = time.time() - start_time
            if elapsed_time >= POD_READY_TIMEOUT_SECONDS:
                raise TimedOutException("Timed out on Pod readiness: [ {0} ]".format(self.name))

    # -------------------------------------------------------------------------------------  add

    def add_annotation(self, k=None, v=None):
        self.model.add_pod_annotation(k=k, v=v)
        return self

    def add_label(self, k=None, v=None):
        self.model.add_pod_label(k=k, v=v)
        return self

    # ------------------------------------------------------------------------------------- delete

    def del_annotation(self, k=None):
        self.model.del_pod_annotation(k=k)
        return self

    def del_label(self, k=None):
        self.model.del_pod_label(k=k)
        return self

    # ------------------------------------------------------------------------------------- get

    def get(self):
        self.model = Pod(model=self.get_model())
        return self

    def get_annotation(self, k=None):
        return self.model.get_pod_annotation(k=k)

    def get_annotations(self):
        return self.model.get_pod_annotations()

    def get_label(self, k=None):
        return self.model.get_pod_label(k=k)

    def get_labels(self):
        return self.model.get_pod_labels()

    def get_namespace(self):
        return self.model.get_pod_namespace()

    def get_status(self):
        self.get()
        return self.model.get_pod_status()

    # ------------------------------------------------------------------------------------- polling readiness

    def is_ready(self):
        status = self.get_status()
        if status is not None and isinstance(status, PodStatus):
            pod_phase = status.get_pod_phase()
            conditions = status.get_pod_conditions()
            conditions_ok = 0
            for cond in conditions:
                if cond.get("status", "False") == "True":
                    conditions_ok += 1
            if pod_phase == "Running" and len(conditions) == conditions_ok:
                return True
        return False

    # ------------------------------------------------------------------------------------- set

    def set_annotations(self, annotations=None):
        self.model.set_pod_annotations(annotations)
        return self

    def set_labels(self, labels=None):
        self.model.set_pod_labels(labels)
        return self

    def set_namespace(self, namespace=None):
        self.model.set_pod_namespace(namespace)
        return self

    # ------------------------------------------------------------------------------------- filtering

    @staticmethod
    def get_by_name(config=None, name=None):
        if name is None:
            raise SyntaxError("K8sPod: name: [ {0} ] cannot be None.".format(name))
        if not isinstance(name, str):
            raise SyntaxError("K8sPod: name: [ {0} ] must be a string.".format(name))
        if config is None:
            config = K8sConfig()

        pod_list = list()
        data = {"labelSelector": "name={0}".format(name)}
        pods = K8sPod(config=config, name=name).get_with_params(data=data)

        for pod in pods:
            try:
                pod_name = Pod(model=pod).get_pod_name()
                pod_list.append(K8sPod(config=config, name=pod_name).get())
            except NotFoundException:
                pass

        return pod_list

    @staticmethod
    def get_by_labels(config=None, labels=None):
        if labels is None:
            raise SyntaxError("K8sPod: labels: [ {0} ] cannot be None.".format(labels))
        if not isinstance(labels, dict):
            raise SyntaxError("K8sPod: labels: [ {0} ] must be a dict.".format(labels))
        if config is None:
            config = K8sConfig()

        pod_list = list()
        my_labels = ",".join(["%s=%s" % (key, value) for (key, value) in labels.items()])
        data = dict(labelSelector="{labels}".format(labels=my_labels))
        pods = K8sPod(config=config, name=labels.get("name")).get_with_params(data=data)

        for pod in pods:
            try:
                pod_name = Pod(model=pod).get_pod_name()
                pod_list.append(K8sPod(config=config, name=pod_name).get())
            except NotFoundException:
                pass

        return pod_list
예제 #14
0
class K8sPod(K8sPodBasedObject):
    def __init__(self, config=None, name=None):
        K8sPodBasedObject.__init__(self, config=config, obj_type='Pod', name=name)
        self.model = Pod(name=name, namespace=self.config.get_namespace())
        if self.config.get_pull_secret() is not None:
            self.model.add_image_pull_secrets(name=self.config.get_pull_secret())

    def add_annotation(self, k, v):
        self.model.add_pod_annotation(k=k, v=v)
        return self

    def add_label(self, k, v):
        self.model.add_pod_label(k=k, v=v)
        return self

    def del_annotation(self, k):
        self.model.del_pod_annotation(k=k)
        return self

    def del_label(self, k):
        self.model.del_pod_label(k=k)
        return self

    def get(self):
        self.model = Pod(model=self.get_model())
        return self

    def get_annotation(self, k):
        return self.model.get_pod_annotation(k=k)

    def get_annotations(self):
        return self.model.get_pod_annotations()

    def get_label(self, k):
        return self.model.get_pod_label(k=k)

    def get_labels(self):
        return self.model.get_pod_labels()

    def get_status(self):
        return self.model.get_pod_status()

    def is_ready(self):
        ready = False
        status = self.get_status()
        if status is not None:
            assert isinstance(status, PodStatus)
            pod_phase = status.get_pod_phase()
            conditions = status.get_pod_conditions()
            conditions_ok = 0
            for cond in conditions:
                assert isinstance(cond, dict)
                cond_type = cond.get('type', '')
                cond_status = cond.get('status', 'False')
                if cond_status == 'True' and cond_type == 'Ready':
                    conditions_ok += 1
            if pod_phase == 'Running' and len(conditions) == conditions_ok:
                ready = True
        return ready

    def set_annotations(self, new_dict):
        self.model.set_pod_annotations(new_dict=new_dict)
        return self

    def set_labels(self, new_dict):
        self.model.set_pod_labels(new_dict=new_dict)
        return self

    def set_namespace(self, name):
        self.model.set_pod_namespace(name=name)
        return self

    @staticmethod
    def get_by_name(config, name):
        try:
            pod_list = list()
            data = dict(labelSelector="name={pod_name}".format(pod_name=name))
            pods = K8sPod(config=config, name=name).get_with_params(data=data)
            for pod in pods:
                try:
                    pod_name = Pod(model=pod).get_pod_name()
                    pod_list.append(K8sPod(config=config, name=pod_name).get())
                except NotFoundException:
                    pass
        except Exception as e:
            message = "Got an exception of type {my_type} with message {my_msg}"\
                .format(my_type=type(e), my_msg=e.message)
            raise Exception(message)
        return pod_list

    @staticmethod
    def get_by_labels(config, labels):
        assert isinstance(labels, dict)
        try:
            pod_list = list()
            my_labels = ",".join(['%s=%s' % (key, value) for (key, value) in labels.items()])
            data = dict(labelSelector="{labels}".format(labels=my_labels))
            pods = K8sPod(config=config, name=labels.get('name')).get_with_params(data=data)
            for pod in pods:
                try:
                    pod_name = Pod(model=pod).get_pod_name()
                    pod_list.append(K8sPod(config=config, name=pod_name).get())
                except NotFoundException:
                    pass
        except Exception as e:
            message = "Got an exception of type {my_type} with message {my_msg}"\
                .format(my_type=type(e), my_msg=e.message)
            raise Exception(message)
        return pod_list