Пример #1
0
    def test_resource_by_node_faked(self, cluster, monkeypatch):
        def fake_list_pods(self):
            with open('kqueen/fixtures/testdata_list_pods_by_node.yml',
                      'r') as stream:
                data_loaded = yaml.load(stream)
            return data_loaded

        monkeypatch.setattr(KubernetesAPI, 'list_pods_by_node', fake_list_pods)

        api = KubernetesAPI(cluster=cluster)
        resources = api.resources_by_node()

        req = {
            'minion1': {
                'limits': {
                    'cpu': 5.0,
                    'memory': 2147483648.0
                },
                'requests': {
                    'cpu': 1.1,
                    'memory': 512102400.0
                }
            }
        }
        print(resources)

        assert resources == req
Пример #2
0
    def test_version(self, cluster):
        api = KubernetesAPI(cluster=cluster)

        version = api.get_version()
        print(version)

        assert isinstance(version, dict)
        assert 'git_version' in version
        assert 'platform' in version
Пример #3
0
 def cluster_get(self):
     """
     Implementation of :func:`~kqueen.engines.base.BaseEngine.cluster_get`
     """
     try:
         client = KubernetesAPI(cluster=self.cluster)
         client.get_version()
     except Exception as e:
         msg = 'Fetching data from backend for cluster {} failed with following reason:'.format(self.cluster.id)
         logger.exception(msg)
         return {'state': config.get('CLUSTER_ERROR_STATE')}
     return {'state': config.get('CLUSTER_OK_STATE')}
Пример #4
0
    def test_extrace_service_addon(self, cluster):
        service = {
            'metadata': {
                'annotations': {
                    'kqueen/name': 'Addon name',
                    'kqueen/icon': 'http://icon',
                    'kqueen/link': 'http://link',
                    'other': 'other annotation',
                }
            }
        }

        api = KubernetesAPI(cluster=cluster)
        extracted = api._extract_annotation(service)

        assert extracted['name'] == 'Addon name'
        assert extracted['icon'] == 'http://icon'
        assert 'other' not in extracted
Пример #5
0
    def test_raise_apiexception(self, cluster, monkeypatch, method_name):
        # monkeypatch all kubernetes-client resources used
        monkeypatch.setattr(kubernetes.client.CoreV1Api, 'list_node', fake_raise(ApiException))
        monkeypatch.setattr(kubernetes.client.CoreV1Api, 'list_pod_for_all_namespaces', fake_raise(ApiException))
        monkeypatch.setattr(kubernetes.client.CoreV1Api, 'list_service_for_all_namespaces', fake_raise(ApiException))
        monkeypatch.setattr(kubernetes.client.ExtensionsV1beta1Api, 'list_deployment_for_all_namespaces', fake_raise(ApiException))

        api = KubernetesAPI(cluster=cluster)
        method = getattr(api, method_name)

        with pytest.raises(ApiException):
            method()
Пример #6
0
    def topology_data(self):
        """
        Return information about Kubernetes cluster in format used in
        visual processing.
        """
        raw_data = []
        kubernetes = KubernetesAPI(cluster=self)

        nodes = kubernetes.list_nodes()
        for node in nodes:
            node['kind'] = 'Node'

        pods = kubernetes.list_pods(False)
        for pod in pods:
            pod['kind'] = 'Pod'

        namespaces = kubernetes.list_namespaces()
        for namespace in namespaces:
            namespace['kind'] = 'Namespace'

        services = kubernetes.list_services(False)
        for service in services:
            service['kind'] = 'Service'

        deployments = kubernetes.list_deployments(False)
        for deployment in deployments:
            deployment['kind'] = 'Deployment'

        replica_sets = kubernetes.list_replica_sets(False)
        replica_set_dict = {
            datum['metadata']['uid']: datum
            for datum in replica_sets
        }

        raw_data = nodes + pods + services + deployments + namespaces

        resources = {datum['metadata']['uid']: datum for datum in raw_data}
        relations = []

        namespace_name_2_uid = {}
        node_name_2_uid = {}
        service_select_run_2_uid = {}
        service_select_app_2_uid = {}

        for resource_id, resource in resources.items():
            # Add node name to uid mapping
            if resource['kind'] == 'Node':
                node_name_2_uid[resource['metadata']['name']] = resource_id

            # Add node name to uid mapping
            if resource['kind'] == 'Namespace':
                namespace_name_2_uid[resource['metadata']
                                     ['name']] = resource_id

            # Add service run selector to uid_mapping
            if resource['kind'] == 'Service' and resource['spec'].get(
                    'selector', {}) is not None:
                if resource['spec'].get('selector', {}).get('run', False):
                    service_select_run_2_uid[resource['spec']['selector']
                                             ['run']] = resource_id
                if resource['spec'].get('selector', {}).get('app', False):
                    service_select_app_2_uid[resource['spec']['selector']
                                             ['app']] = resource_id

            # Add Containers as top-level resource
            """
            if resource['kind'] == 'Pod':
                for container in resource['spec']['containers']:
                    container_id = "{1}-{2}".format(
                        resource['metadata']['uid'], container['name'])
                    resources[container_id] = {
                        'metadata': container,
                        'kind': 'Container'
                    }
                    relations.append({
                        'source': resource_id,
                        'target': container_id,
                    })
            """

        for resource_id, resource in resources.items():
            if resource['kind'] not in ('Node', 'Namespace'):
                relations.append({
                    'source':
                    resource_id,
                    'target':
                    namespace_name_2_uid[resource['metadata']['namespace']]
                })

            if resource['kind'] == 'Pod':

                # define relationship between pods and nodes
                if resource['spec']['node_name'] is not None:
                    relations.append({
                        'source':
                        resource_id,
                        'target':
                        node_name_2_uid[resource['spec']['node_name']]
                    })

                # define relationships between pods and rep sets and
                # replication controllers
                if resource['metadata'].get('owner_references', False):
                    if resource['metadata']['owner_references'][0][
                            'kind'] == 'ReplicaSet':
                        rep_set_id = resource['metadata']['owner_references'][
                            0]['uid']
                        deploy_id = replica_set_dict[rep_set_id]['metadata'][
                            'owner_references'][0]['uid']
                        relations.append({
                            'source': deploy_id,
                            'target': resource_id
                        })

                # rel'n between pods and services
                if resource.get('metadata', {}).get('labels',
                                                    {}).get('run', False):
                    relations.append({
                        'source':
                        resource_id,
                        'target':
                        service_select_run_2_uid[resource['metadata']['labels']
                                                 ['run']]
                    })

                if resource.get('metadata', {}).get('labels',
                                                    {}).get('app', False):
                    try:
                        relations.append({
                            'source':
                            resource_id,
                            'target':
                            service_select_app_2_uid[resource['metadata']
                                                     ['labels']['app']]
                        })
                    except Exception:
                        pass

        out = {
            'items': resources,
            'relations': relations,
            'kinds': {
                'Pod': '',
            }
        }
        #        except Exception:
        #            out = {
        #                'items': [],
        #                'relations': []
        #            }

        return out
Пример #7
0
    def test_list_services(self, cluster):
        api = KubernetesAPI(cluster=cluster)

        services = api.list_services()
        assert isinstance(services, list)
Пример #8
0
    def test_list_pods_by_node(self, cluster):
        api = KubernetesAPI(cluster=cluster)

        pods = api.list_pods_by_node()

        assert isinstance(pods, dict)
Пример #9
0
    def test_pod_list(self, cluster):
        api = KubernetesAPI(cluster=cluster)
        pods = api.list_pods()

        assert isinstance(pods, list)
Пример #10
0
    def test_list_deployments(self, cluster):
        api = KubernetesAPI(cluster=cluster)

        deployments = api.list_deployments()
        assert isinstance(deployments, list)
Пример #11
0
    def test_get_kubeconfig(self, cluster):
        """Test get_kubeconfig returns YAML"""

        cluster.save()
        api = KubernetesAPI(cluster=cluster)
        print(api.get_kubeconfig_file())
Пример #12
0
    def test_init(self, cluster):
        cluster.save()

        api = KubernetesAPI(cluster=cluster)

        assert hasattr(api, 'cluster')
Пример #13
0
    def test_get_api_client(self, cluster):
        api = KubernetesAPI(cluster=cluster)

        api_client = api.get_api_client()
        print(api_client)
Пример #14
0
 def test_missing_cluster_param(self):
     with pytest.raises(ValueError, match='Missing parameter cluster'):
         KubernetesAPI()
Пример #15
0
    def test_persistent_volume_claims(self, cluster):
        api = KubernetesAPI(cluster=cluster)
        resources = api.list_persistent_volume_claims()

        assert isinstance(resources, list)
Пример #16
0
    def test_resource_by_node(self, cluster):
        api = KubernetesAPI(cluster=cluster)

        resources = api.resources_by_node()
        assert isinstance(resources, dict)
Пример #17
0
    def status(self):
        """Return information about Kubernetes cluster."""
        try:
            kubernetes = KubernetesAPI(cluster=self)
            out = {
                'addons':
                kubernetes.list_services(filter_addons=True),
                'deployments':
                kubernetes.list_deployments(),
                'namespaces':
                kubernetes.list_namespaces(),
                'nodes':
                kubernetes.list_nodes(),
                'nodes_pods':
                kubernetes.count_pods_by_node(),
                'persistent_volumes':
                kubernetes.list_persistent_volumes(),
                'persistent_volume_claims':
                kubernetes.list_persistent_volume_claims(),
                'pods':
                kubernetes.list_pods(),
                'replica_sets':
                kubernetes.list_replica_sets(),
                'services':
                kubernetes.list_services(),
                'version':
                kubernetes.get_version(),
            }
        except Exception as e:
            logger.exception(e)
            out = {}

        return out
Пример #18
0
    def topology_data(self):
        """
        Return information about Kubernetes cluster in the format used in
        visual processing.
        """
        kubernetes = KubernetesAPI(cluster=self)

        def set_kind(objects, kind):
            for obj in objects:
                obj['kind'] = kind

        nodes = kubernetes.list_nodes()
        set_kind(nodes, 'Node')

        pods = kubernetes.list_pods(False)
        set_kind(pods, 'Pod')

        namespaces = kubernetes.list_namespaces()
        set_kind(namespaces, 'Namespace')

        services = kubernetes.list_services(False)
        set_kind(services, 'Service')

        deployments = kubernetes.list_deployments(False)
        set_kind(deployments, 'Deployment')

        replica_sets = kubernetes.list_replica_sets(False)
        replica_set_dict = {
            datum['metadata']['uid']: datum
            for datum in replica_sets
        }

        raw_data = nodes + pods + services + deployments + namespaces

        resources = {datum['metadata']['uid']: datum for datum in raw_data}
        relations = []

        namespace_name_2_uid = {}
        node_name_2_uid = {}
        service_select_run_2_uid = {}
        service_select_app_2_uid = {}

        for resource_id, resource in resources.items():
            # Add node name to uid mapping
            if resource['kind'] == 'Node':
                node_name_2_uid[resource['metadata']['name']] = resource_id

            # Add node name to uid mapping
            if resource['kind'] == 'Namespace':
                namespace_name_2_uid[resource['metadata']
                                     ['name']] = resource_id

            # Add service run selector to uid_mapping
            if resource['kind'] == 'Service' and resource['spec'].get(
                    'selector', {}) is not None:
                if resource['spec'].get('selector', {}).get('run', False):
                    service_select_run_2_uid[resource['spec']['selector']
                                             ['run']] = resource_id
                if resource['spec'].get('selector', {}).get('app', False):
                    service_select_app_2_uid[resource['spec']['selector']
                                             ['app']] = resource_id

            # Add Containers as top-level resource
            """
            if resource['kind'] == 'Pod':
                for container in resource['spec']['containers']:
                    container_id = "{1}-{2}".format(
                        resource['metadata']['uid'], container['name'])
                    resources[container_id] = {
                        'metadata': container,
                        'kind': 'Container'
                    }
                    relations.append({
                        'source': resource_id,
                        'target': container_id,
                    })
            """

        for resource_id, resource in resources.items():
            if resource['kind'] not in ('Node', 'Namespace'):
                relations.append({
                    'source':
                    resource_id,
                    'target':
                    namespace_name_2_uid[resource['metadata']['namespace']]
                })

            if resource['kind'] == 'Pod':
                # Define the relationship between pods and nodes
                if resource['spec']['node_name'] is not None:
                    relations.append({
                        'source':
                        resource_id,
                        'target':
                        node_name_2_uid[resource['spec']['node_name']]
                    })

                # Define relationships between pods and rep sets and
                # replication controllers
                if resource['metadata'].get('owner_references', False):
                    if resource['metadata']['owner_references'][0][
                            'kind'] == 'ReplicaSet':

                        def get_uid(x):
                            return x['metadata']['owner_references'][0]['uid']

                        rep_set_id = get_uid(resource)
                        deploy_id = get_uid(replica_set_dict[rep_set_id])
                        relations.append({
                            'source': deploy_id,
                            'target': resource_id
                        })

                # Relation between pods and services
                if resource.get('metadata', {}).get('labels',
                                                    {}).get('run', False):
                    relations.append({
                        'source':
                        resource_id,
                        'target':
                        service_select_run_2_uid[resource['metadata']['labels']
                                                 ['run']]
                    })

                if resource.get('metadata', {}).get('labels',
                                                    {}).get('app', False):
                    try:
                        app_id = service_select_app_2_uid[resource['metadata']
                                                          ['labels']['app']]
                        relations.append({
                            'source': resource_id,
                            'target': app_id
                        })
                    except Exception as e:
                        pass

        out = {
            'items': resources,
            'relations': relations,
            'kinds': {
                'Pod': '',
            }
        }
        return out
Пример #19
0
    def test_list_nodes(self, cluster):
        api = KubernetesAPI(cluster=cluster)
        nodes = api.list_nodes()

        assert isinstance(nodes, list)