Esempio n. 1
0
 def workload_details(self, namespace, workload_name, workload_type):
     """ Returns the details of Workload
     Args:
         namespace: Namespace of the service
         workload_name: Workload name
         workload_type: Type of workload
     """
     _response = getattr(self,
                         WORKLOAD_TYPES[workload_type]).get(
                             namespace=namespace,
                             name=workload_name)
     _workload = WorkloadDetails(
         workload_type=_response.kind,
         name=_response.metadata.name,
         created_at=from_rest_to_ui(
             _response.metadata.creationTimestamp),
         resource_version=_response.metadata.resourceVersion,
         istio_sidecar=None,
         labels=dict(_response.metadata.labels if _response.metadata.labels
                     else _response.spec.selector.matchLabels),
         pods=self.get_workload_pods(namespace, workload_name),
         health=None,
         workload_status=self._get_workload_status(_response))
     _workload.set_istio_configs(istio_configs=self.get_workload_configs(namespace, _workload))
     return _workload
Esempio n. 2
0
    def service_details(self, namespace, service_name, skip_workloads=True):
        """ Returns the details of service
        Args:
            namespace: Namespace of the service
            service_name: Service name
        """
        _response = self._service.get(namespace=namespace, name=service_name)
        _ports = ''
        for _port in _response.spec.ports:
            _ports += '{}{}/{} '.format(_port['name'] + ' ' if _port['name'] and _port['name'] != ''
                                        else '',
                                        _port['port'],
                                        _port['protocol'])
        _labels = dict(_response.metadata.labels if _response.metadata.labels else {})
        _service = ServiceDetails(
            namespace=_response.metadata.namespace,
            name=_response.metadata.name,
            istio_sidecar=None,
            created_at=from_rest_to_ui(
                _response.metadata.creationTimestamp),
            resource_version=_response.metadata.resourceVersion,
            service_type=_response.spec.type,
            ip=_response.spec.clusterIP,
            endpoints=([] if skip_workloads else self._get_service_endpoints(
                namespace,
                self._get_service_app(_response.metadata.name,
                                      _labels))),
            ports=_ports.strip(),
            labels=_labels,
            selectors=dict(_response.spec.selector if _response.spec.selector else {}),
            workloads=([] if skip_workloads else self._get_service_workloads(
                namespace,
                self._get_service_app(_response.metadata.name,
                                      _labels))),
            # TODO health
            health=None,
            istio_configs=self.get_service_configs(
                _response.metadata.namespace,
                service_name))

        return _service
Esempio n. 3
0
    def workload_details(self, namespace, workload_name, workload_type):
        """Returns details of Workload.
        Args:
            namespaces: namespace where Workload is located
            workload_name: name of Workload
        """

        _workload_data = self.get_response('workloadDetails',
                                           path={
                                               'namespace': namespace,
                                               'workload': workload_name
                                           })
        _workload = None
        if _workload_data:
            _workloads_rest = self.workload_list(namespaces=[namespace])
            _workload_rest = set([
                _w for _w in _workloads_rest if _w.name == workload_name
            ]).pop()
            _services = []
            if _workload_data['services']:
                for _ws_data in _workload_data['services']:
                    _services.append(_ws_data['name'])
            _destination_services = []
            # TODO find a better way to take Traffic
            if 'destinationServices' in _workload_data:
                for _ds_data in _workload_data['destinationServices']:
                    _destination_services.append(
                        DestinationService(_from=workload_name,
                                           name=_ds_data['name'],
                                           namespace=_ds_data['namespace']))
            _all_pods = []
            if _workload_data['pods']:
                for _pod_data in _workload_data['pods']:
                    ''' TODO read from tooltip in UI and then activate this code
                    _istio_init_containers = ''
                    _istio_containers = ''
                    if _pod_data['istioContainers']:
                        _istio_containers = _pod_data['istioContainers'][0]['image']
                    if _pod_data['istioInitContainers']:
                        _istio_init_containers = _pod_data['istioInitContainers'][0]['image']
                    _created_by = '{} ({})'.format(_pod_data['createdBy'][0]['name'],
                                                   _pod_data['createdBy'][0]['kind'])'''
                    _pod = WorkloadPod(name=str(_pod_data['name']),
                                       status=self.get_pod_status(
                                           _workload_data['istioSidecar'],
                                           _pod_data))
                    _all_pods.append(_pod)

            _workload_health = self.get_workload_health(
                namespace=namespace, workload_name=_workload_data['name'])

            _labels = self.get_labels(_workload_data)
            _applications = set([
                _a.name for _a in self.application_list(namespaces=[namespace])
                if _labels['app'] == _a.name
            ])
            _config_list = self.istio_config_list(
                namespaces=[namespace],
                config_names=[],
                params={'workloadSelector': dict_to_params(_labels)})

            _workload = WorkloadDetails(
                name=_workload_data['name'],
                istio_sidecar=_workload_rest.istio_sidecar,
                workload_type=_workload_data['type'],
                created_at=from_rest_to_ui(_workload_data['createdAt']),
                resource_version=_workload_data['resourceVersion'],
                health=_workload_health.is_healthy()
                if _workload_health else None,
                workload_status=_workload_health,
                icon=self.get_icon_type(_workload_data),
                labels=_labels,
                traffic=_destination_services,
                pods=_all_pods,
                services=_services,
                applications=_applications,
                istio_configs=_config_list)
        return _workload
Esempio n. 4
0
    def service_details(self, namespace, service_name):
        """Returns details of Service.
        Args:
            namespaces: namespace where Service is located
            service_name: name of Service
        """

        _service_data = self.get_response('serviceDetails',
                                          path={
                                              'namespace': namespace,
                                              'service': service_name
                                          },
                                          params={'validate': 'true'})
        _service = None
        if _service_data:
            _services_rest = self.service_list(namespaces=[namespace])
            _service_rest = set([
                _s for _s in _services_rest if _s.name == service_name
            ]).pop()
            workloads = []
            if _service_data['workloads']:
                for _wl_data in _service_data['workloads']:
                    workloads.append(
                        WorkloadDetails(
                            name=_wl_data['name'],
                            workload_type=_wl_data['type'],
                            labels=self.get_labels(_wl_data),
                            created_at=from_rest_to_ui(_wl_data['createdAt']),
                            resource_version=_wl_data['resourceVersion']))
            source_workloads = []
            # TODO better way to find Traffic
            if 'dependencies' in _service_data:
                for _wl_data in _service_data['dependencies']:
                    _wl_names = []
                    for _wl_name in _service_data['dependencies'][_wl_data]:
                        _wl_names.append(_wl_name['name'])
                    source_workloads.append(
                        SourceWorkload(to=_wl_data, workloads=_wl_names))
            istio_configs = []
            virtual_services = []
            if _service_data['virtualServices'] \
                    and len(_service_data['virtualServices']['items']) > 0:
                for _vs_data in _service_data['virtualServices']['items']:
                    _weights = []
                    if 'http' in _vs_data['spec']:
                        _protocol = _vs_data['spec']['http'][0]
                    else:
                        _protocol = _vs_data['spec']['tcp'][0]
                    for _route in _protocol['route']:
                        _weights.append(
                            VirtualServiceWeight(
                                host=_route['destination']['host'],
                                subset=_route['destination']['subset']
                                if 'subset' in _route['destination'] else None,
                                port=_route['destination']['port']['number']
                                if 'port' in _route['destination'] else None,
                                status=_route['destination']['status']
                                if 'status' in _route['destination'] else None,
                                weight=_route['weight'] if
                                ('weight' in _route
                                 and _route['weight'] != 0) else None))
                    if 'match' in _protocol:
                        _protocol_route = 'match ' + \
                            to_linear_string(_protocol['match'])
                    else:
                        _protocol_route = ''
                    _validation = self.get_istio_config_validation(
                        _vs_data['metadata']['namespace'], 'virtualservices',
                        _vs_data['metadata']['name'])
                    virtual_services.append(
                        VirtualService(
                            status=_validation,
                            name=_vs_data['metadata']['name'],
                            created_at=from_rest_to_ui(
                                _vs_data['metadata']['creationTimestamp']),
                            resource_version=_vs_data['metadata']
                            ['resourceVersion'],
                            protocol_route=_protocol_route,
                            hosts=_vs_data['spec']['hosts'],
                            weights=_weights))
                    # It also requires IstioConfig type of objects in several testcases
                    istio_configs.append(
                        IstioConfig(
                            name=_vs_data['metadata']['name'],
                            namespace=_vs_data['metadata']['namespace'],
                            object_type=OBJECT_TYPE.VIRTUAL_SERVICE.text,
                            validation=_validation))

            destination_rules = []
            if _service_data['destinationRules'] \
                    and len(_service_data['destinationRules']['items']) > 0:
                for _dr_data in _service_data['destinationRules']['items']:
                    if 'trafficPolicy' in _dr_data['spec']:
                        _traffic_policy = to_linear_string(
                            _dr_data['spec']['trafficPolicy'])
                    else:
                        _traffic_policy = None
                    _dr_subsets = []
                    if 'subsets' in _dr_data['spec']:
                        for _subset in _dr_data['spec']['subsets']:
                            _dr_subsets.append(
                                DestinationRuleSubset(
                                    status=None,
                                    name=_subset['name'],
                                    labels=_subset['labels']
                                    if 'labels' in _subset else {},
                                    traffic_policy=(to_linear_string(
                                        _subset['trafficPolicy'])
                                                    if 'trafficPolicy'
                                                    in _subset else None)))

                    _validation = self.get_istio_config_validation(
                        _dr_data['metadata']['namespace'], 'destinationrules',
                        _dr_data['metadata']['name'])
                    destination_rules.append(
                        DestinationRule(
                            status=_validation,
                            name=_dr_data['metadata']['name'],
                            host=_dr_data['spec']['host'],
                            traffic_policy=_traffic_policy
                            if _traffic_policy else '',
                            subsets=_dr_subsets,
                            created_at=from_rest_to_ui(
                                _dr_data['metadata']['creationTimestamp']),
                            resource_version=_dr_data['metadata']
                            ['resourceVersion']))

                    # It also requires IstioConfig type of objects in several testcases
                    istio_configs.append(
                        IstioConfig(
                            name=_dr_data['metadata']['name'],
                            namespace=_dr_data['metadata']['namespace'],
                            object_type=OBJECT_TYPE.DESTINATION_RULE.text,
                            validation=_validation))

            _ports = ''
            for _port in _service_data['service']['ports']:
                _ports += '{}{}/{} '.format(
                    _port['name'] + ' ' if _port['name'] != '' else '',
                    _port['port'], _port['protocol'])
            endpoints = []
            if _service_data['endpoints']:
                for _endpoint in _service_data['endpoints'][0]['addresses']:
                    endpoints.append(_endpoint['ip'])
            _labels = self.get_labels(_service_data['service'])
            _applications = set([
                _a.name for _a in self.application_list(namespaces=[namespace])
                if _labels['app'] == _a.name
            ])
            _validations = []
            if _service_data['validations'] \
                    and len(_service_data['validations']['service']) > 0:
                for _data in _service_data['validations']['service'][
                        service_name]['checks']:
                    _validations.append(_data['message'])
            _service_health = self.get_service_health(
                namespace=namespace,
                service_name=service_name,
                istioSidecar=_service_rest.istio_sidecar)
            _service = ServiceDetails(
                name=_service_data['service']['name'],
                istio_sidecar=_service_rest.istio_sidecar,
                created_at=from_rest_to_ui(
                    _service_data['service']['createdAt']),
                resource_version=_service_data['service']['resourceVersion'],
                service_type=_service_data['service']['type'],
                ip=_service_data['service']['ip'],
                endpoints=endpoints,
                validations=_validations,
                ports=_ports.strip(),
                labels=_labels,
                selectors=self.get_selectors(_service_data['service']),
                health=_service_health.is_healthy()
                if _service_health else None,
                service_status=_service_health,
                icon=self.get_icon_type(_service_data),
                workloads=workloads,
                applications=_applications,
                traffic=source_workloads,
                virtual_services=virtual_services,
                destination_rules=destination_rules,
                istio_configs=istio_configs,
                istio_configs_number=len(istio_configs))
        return _service