コード例 #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)
コード例 #2
0
ファイル: volume.py プロジェクト: wangtaoking1/hummer
class VolumeBuilder(object):
    """
    VolumeBuilder is a builder to create an volume for persistent data storagy.

    """
    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 create_volume(self):
        """
        Create volume by multiple threading.
        """
        creating_thread = Thread(target=self._create_volume)
        creating_thread.start()

    def _create_volume(self):
        """
        First create a persistentvolume, then create a persistentvolumeclaim.
        """
        logger.info('User {} create a volume {} in project {}.'.format(
            self.volume.user.username, self.volume.name, self.project_name))

        # create volume dir on nfs server
        if self._create_volume_dir_on_nfs():
            logger.info("Create dir {} on nfs server {} successfully.".format(
                self.nfs_path, self.nfs_server))
        else:
            logger.info("Create dir {} on nfs server {} failed.".format(
                self.nfs_path, self.nfs_server))
            self._update_volume_status(status='error')
            return None

        # create persistentvolume
        if self._create_persistentvolume():
            logger.info("Create persistentvolume {}-{} successfully.".format(
                self.namespace, self.volume_name))
        else:
            logger.info("Create persistentvolume {}-{} failed.".format(
                self.namespace, self.volume_name))
            self._update_volume_status(status='error')
            return None

        # create persistentvolumeclaim
        if self._create_persistentvolumeclaim():
            logger.info("Create persistentvolumeclaim {} successfully.".format(
                self.volume_name))
        else:
            logger.info("Create persistentvolumeclaim {} failed.".format(
                self.volume_name))
            self._update_volume_status(status='error')
            return None

        self._update_volume_status(status='active')

    def _create_volume_dir_on_nfs(self):
        """
        Create direction on nfs server to store volume data.
        """
        client = NFSLocalClient()
        client.removedir(self.nfs_path)
        client.makedir(self.nfs_path)
        return True

    def _create_persistentvolume(self):
        return self.kubeclient.create_persistentvolume(
            namespace=self.namespace,
            name=self.volume_name,
            capacity=self.capacity,
            nfs_path=self.nfs_path,
            nfs_server=self.nfs_server
            )

    def _create_persistentvolumeclaim(self):
        return self.kubeclient.create_persistentvolumeclaim(
            namespace=self.namespace,
            name=self.volume_name,
            capacity=self.capacity
            )

    def _update_volume_status(self, status):
        self.volume.status = status
        self.volume.save()
コード例 #3
0
ファイル: test_k8sclient.py プロジェクト: ljb-2000/hummer
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)