Beispiel #1
0
class KubeClientTestCase(unittest.TestCase):
    def setUp(self):
        self.client = KubeClient("http://192.168.0.10:8080/api/v1/")

    def tearDown(self):
        self.client = None

    def test_add_slash(self):
        url = "http://192.168.0.10:8080"
        self.assertEqual(KubeClient.add_slash(url),
                         "http://192.168.0.10:8080/")

    def test_create_instance(self):
        url = "http://192.168.0.10:8080"
        client = KubeClient(url)
        self.assertEqual(client.base_url, "http://192.168.0.10:8080/")

    def test_send_request(self):
        res = self.client.send_request("get",
                                       "namespaces",
                                       labels={
                                           'a': 1,
                                           'name': 'wangtao'
                                       })
        self.assertEqual(isinstance(res, dict), True)

    def test_list_namespaces(self):
        namespaces = self.client.list_namespces()
        print(namespaces)
        self.assertEqual(True, True)

    def test_list_nodes(self):
        nodes = self.client.list_nodes()
        print(nodes)
        self.assertEqual(True, True)

    def test_create_namespace(self):
        self.client.create_namespace('user')

    def test_delete_namespace(self):
        self.client.delete_namespace('abcd')

    def test_list_controllers(self):
        controllers = self.client.list_controllers('user')
        print(controllers)

    def test_create_controller_1(self):
        image_name = '192.168.0.15:5000/user/nginx:1.9.9'
        res = self.client.create_controller('user',
                                            'test-nginx',
                                            image_name,
                                            replicas=2,
                                            tcp_ports={"http": 80})
        print(res)

    def test_create_controller_2(self):
        image_name = '192.168.0.15:5000/admin/ubuntu:14.04'
        self.client.create_controller('test-space',
                                      'test-nginx',
                                      image_name,
                                      replicas=1,
                                      commands=['sleep', '3600'],
                                      envs={"MYSQL": "192.168.0.100"})

    def test_create_controller_3(self):
        image_name = '192.168.0.15:5000/admin/nginx:1.9.9'
        self.client.create_controller('test-space',
                                      'test-nginx',
                                      image_name,
                                      replicas=1,
                                      tcp_ports={
                                          "http": 80,
                                          "https": 443
                                      })

    def test_create_controller_volume(self):
        image_name = '192.168.0.15:5000/user/nginx:1.9.9'
        self.client.create_controller(
            'user',
            'test-nginx',
            image_name,
            cpu="100m",
            memory="64Mi",
            replicas=1,
            tcp_ports={"http": 80},
            volumes={"project0-volume0": "/var/www/html"})

    def test_delete_controller(self):
        self.client.delete_controller('test-space', 'test-nginx')

    def test_list_services(self):
        services = self.client.list_services('test-space')
        print(services)

    def test_create_service_internal(self):
        res = self.client.create_service('user',
                                         'test-nginx',
                                         tcp_ports={"http": 80},
                                         is_public=False)
        print(res)

    def test_create_service_external(self):
        res = self.client.create_service('test-space',
                                         'test-nginx',
                                         tcp_ports={"http": 80},
                                         is_public=True)
        print(res)

    def test_create_service_session(self):
        self.client.create_service('test-space',
                                   'nginx',
                                   tcp_ports={"http": 80},
                                   is_public=True,
                                   session_affinity=True)

    def test_delete_service(self):
        self.client.delete_service('test-space', 'nginx')

    def test_get_service_details(self):
        res = self.client.get_service_details('test-space', 'test-nginx')
        print(res)

    def test_create_persistentvolume(self):
        res = self.client.create_persistentvolume(
            'default', 'project0-volume0', '10Mi',
            '/hummer/user/project0/volume0', '192.168.0.15')
        print(res)

    def test_delete_persistentvolume(self):
        res = self.client.delete_persistentvolume('default',
                                                  'project0-volume0')
        print(res)

    def test_create_persistentvolumeclaim(self):
        res = self.client.create_persistentvolumeclaim('default',
                                                       'project0-volume0',
                                                       '10Mi')
        print(res)

    def test_delete_persistentvolumeclaim(self):
        res = self.client.delete_persistentvolumeclaim('default',
                                                       'project0-volume0')
        print(res)

    def test_list_pods(self):
        res = self.client.list_pods('user', label="app=project0-nginx-test")
        print(res)

    def test_get_logs_of_pod(self):
        res = self.client.get_logs_of_pod('user', 'project0-nginx-test-3uhej',
                                          20)
        lines = res.split('\n')
        print(lines)

    def test_create_autoscaler(self):
        beta_client = KubeClient(
            "http://192.168.0.10:8080/apis/extensions/v1beta1/")
        res = beta_client.create_autoscaler('user', 'project0-nginx-test', 1,
                                            5, 50)
        print(res)

    def test_delete_autoscaler(self):
        beta_client = KubeClient(
            "http://192.168.0.10:8080/apis/extensions/v1beta1/")
        res = beta_client.delete_autoscaler('user', 'project0-nginx-test')
        print(res)

    def test_list_host_ips(self):
        hosts = self.client.list_host_ips('user', "app=project0-2048")
        print(hosts)
Beispiel #2
0
class VolumeDestroyer(object):
    """
    VolumeDestroyer is to destroy volume instance, including volume direction,
    persistentvolume and persistentvolumeclaim.
    """
    volume = None

    def __init__(self, volume):
        self.kubeclient = KubeClient("http://{}:{}{}".format(settings.MASTER_IP,
            settings.K8S_PORT, settings.K8S_API_PATH))

        self.volume = volume
        self.namespace = self.volume.project.name
        self.project_name = self.volume.project.name
        self.volume_name = self.volume.name
        self.capacity = str(self.volume.capacity) + self.volume.capacity_unit
        self.nfs_path = get_volume_nfs_dir(settings.NFS_BASE_DIR,
            self.namespace, self.volume.name)
        self.nfs_server = settings.NFS_IP

    def destroy_volume(self):
        """
        Destroy volume instance by multiple threading.
        """
        deleting_thread = Thread(target=self._destroy_volume_instance)
        deleting_thread.start()

    def _destroy_volume_instance(self):
        logger.info('User {} delete volume {} in project {}.'.format(
            self.volume.user.username, self.volume.name, self.project_name))

        self._update_volume_status(status='deleting')

        if not self._remove_volume_dir_on_nfs():
            logger.debug("remove direction {} on nfs server failed.".format(
                self.nfs_path))
        else:
            logger.info("remove direction {} on nfs server successfully."
                .format(self.nfs_path))

        if not self._delete_persistentvolumeclaim():
            logger.debug("delete persistentvolumeclaim {} failed.".format(
                self.volume_name))
        else:
            logger.debug("delete persistentvolumeclaim {} successfully."
                .format(self.volume_name))

        if not self._delete_persistentvolume():
            logger.debug("delete persistentvolume {}-{} failed.".format(
                self.namespace, self.volume_name))
        else:
            logger.debug("delete persistentvolume {}-{} successfully.".format(
                self.namespace, self.volume_name))

        # self._update_volume_status(status='deleted')
        self._delete_volume_metadata()

    def _remove_volume_dir_on_nfs(self):
        """
        Remove direction on nfs server.
        """
        client = NFSLocalClient()
        client.removedir(self.nfs_path)
        return True

    def _delete_persistentvolumeclaim(self):
        return self.kubeclient.delete_persistentvolumeclaim(
            namespace=self.namespace, name=self.volume_name)

    def _delete_persistentvolume(self):
        return self.kubeclient.delete_persistentvolume(
            namespace=self.namespace, name=self.volume_name)

    def _update_volume_status(self, status):
        self.volume.status = status
        self.volume.save()

    def _delete_volume_metadata(self):
        logger.debug("delete volume {} metadata.".format(self.volume_name))
        self.volume.delete()
Beispiel #3
0
class KubeClientTestCase(unittest.TestCase):
    def setUp(self):
        self.client = KubeClient("http://192.168.0.10:8080/api/v1/")

    def tearDown(self):
        self.client = None

    def test_add_slash(self):
        url = "http://192.168.0.10:8080"
        self.assertEqual(KubeClient.add_slash(url), "http://192.168.0.10:8080/")

    def test_create_instance(self):
        url = "http://192.168.0.10:8080"
        client = KubeClient(url)
        self.assertEqual(client.base_url, "http://192.168.0.10:8080/")

    def test_send_request(self):
        res = self.client.send_request("get", "namespaces",
            labels={'a': 1, 'name': 'wangtao'})
        self.assertEqual(isinstance(res, dict), True)

    def test_list_namespaces(self):
        namespaces = self.client.list_namespces()
        print(namespaces)
        self.assertEqual(True, True)

    def test_list_nodes(self):
        nodes = self.client.list_nodes()
        print(nodes)
        self.assertEqual(True, True)

    def test_create_namespace(self):
        self.client.create_namespace('user')

    def test_delete_namespace(self):
        self.client.delete_namespace('abcd')

    def test_list_controllers(self):
        controllers = self.client.list_controllers('user')
        print(controllers)

    def test_create_controller_1(self):
        image_name = '192.168.0.15:5000/user/nginx:1.9.9'
        res = self.client.create_controller('user', 'test-nginx', image_name,
            replicas=2, tcp_ports={"http": 80})
        print(res)

    def test_create_controller_2(self):
        image_name = '192.168.0.15:5000/admin/ubuntu:14.04'
        self.client.create_controller('test-space', 'test-nginx', image_name,
            replicas=1,
            commands=['sleep', '3600'],
            envs={"MYSQL": "192.168.0.100"}
        )

    def test_create_controller_3(self):
        image_name = '192.168.0.15:5000/admin/nginx:1.9.9'
        self.client.create_controller('test-space', 'test-nginx', image_name,
            replicas=1, tcp_ports={"http": 80, "https": 443})

    def test_create_controller_volume(self):
        image_name = '192.168.0.15:5000/user/nginx:1.9.9'
        self.client.create_controller('user', 'test-nginx', image_name,
            cpu="100m", memory="64Mi", replicas=1, tcp_ports={"http": 80},
            volumes={"project0-volume0": "/var/www/html"})

    def test_delete_controller(self):
        self.client.delete_controller('test-space', 'test-nginx')

    def test_list_services(self):
        services = self.client.list_services('test-space')
        print(services)

    def test_create_service_internal(self):
        res = self.client.create_service('user', 'test-nginx',
            tcp_ports={"http": 80},
            is_public=False
        )
        print(res)

    def test_create_service_external(self):
        res = self.client.create_service('test-space', 'test-nginx',
            tcp_ports={"http": 80},
            is_public=True
        )
        print(res)

    def test_create_service_session(self):
        self.client.create_service('test-space', 'nginx',
            tcp_ports={"http": 80},
            is_public=True,
            session_affinity=True
        )

    def test_delete_service(self):
        self.client.delete_service('test-space', 'nginx')

    def test_get_service_details(self):
        res = self.client.get_service_details('test-space', 'test-nginx')
        print(res)

    def test_create_persistentvolume(self):
        res = self.client.create_persistentvolume('default', 'project0-volume0', '10Mi',
            '/hummer/user/project0/volume0', '192.168.0.15')
        print(res)

    def test_delete_persistentvolume(self):
        res = self.client.delete_persistentvolume('default', 'project0-volume0')
        print(res)

    def test_create_persistentvolumeclaim(self):
        res = self.client.create_persistentvolumeclaim('default', 'project0-volume0',
            '10Mi')
        print(res)

    def test_delete_persistentvolumeclaim(self):
        res = self.client.delete_persistentvolumeclaim('default', 'project0-volume0')
        print(res)

    def test_list_pods(self):
        res = self.client.list_pods('user', label="app=project0-nginx-test")
        print(res)

    def test_get_logs_of_pod(self):
        res = self.client.get_logs_of_pod('user', 'project0-nginx-test-3uhej', 20)
        lines = res.split('\n')
        print(lines)

    def test_create_autoscaler(self):
        beta_client = KubeClient("http://192.168.0.10:8080/apis/extensions/v1beta1/")
        res = beta_client.create_autoscaler('user', 'project0-nginx-test', 1, 5, 50)
        print(res)

    def test_delete_autoscaler(self):
        beta_client = KubeClient("http://192.168.0.10:8080/apis/extensions/v1beta1/")
        res = beta_client.delete_autoscaler('user', 'project0-nginx-test')
        print(res)

    def test_list_host_ips(self):
        hosts = self.client.list_host_ips('user', "app=project0-2048")
        print(hosts)