Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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()
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 8
0
 def get(self):
     self.model = Pod(self.get_model())
     return self