Example #1
0
 def get_service_configs(self, namespace, service_name):
     """ Returns the list of istio config pages for particular service
     Args:
         namespace: Namespace where service is located
         service_name: Name of service
     """
     istio_configs = []
     _all_vs_list = self._resource_list(
         attribute_name=CONFIG_TYPES[IstioConfigObjectType.VIRTUAL_SERVICE.text],
         resource_type=CONFIG_TYPES[IstioConfigObjectType.VIRTUAL_SERVICE.text],
         namespaces=[namespace])
     for _vs_item in _all_vs_list:
         if self._is_host_in_config(namespace, service_name, to_linear_string(
             self.istio_config_details(
                 namespace=namespace,
                 object_name=_vs_item.name,
                 object_type=IstioConfigObjectType.VIRTUAL_SERVICE.text).text)):
             istio_configs.append(_vs_item)
     _all_dr_list = self._resource_list(
         attribute_name=CONFIG_TYPES[IstioConfigObjectType.DESTINATION_RULE.text],
         resource_type=CONFIG_TYPES[IstioConfigObjectType.DESTINATION_RULE.text],
         namespaces=[namespace])
     for _dr_item in _all_dr_list:
         if self._is_host_in_config(namespace, service_name, to_linear_string(
             self.istio_config_details(
                 namespace=namespace,
                 object_name=_dr_item.name,
                 object_type=IstioConfigObjectType.DESTINATION_RULE.text).text)):
             istio_configs.append(_dr_item)
     return istio_configs
Example #2
0
 def get_workload_configs(self, namespace, workload):
     """ Returns the list of istio config pages for particular workload
     Args:
         namespace: Namespace where service is located
         workload: Workload object
     """
     istio_configs = []
     _all_peer_auth_list = self._resource_list(
         attribute_name=CONFIG_TYPES[IstioConfigObjectType.PEER_AUTHENTICATION.text],
         resource_type=CONFIG_TYPES[IstioConfigObjectType.PEER_AUTHENTICATION.text],
         namespaces=[namespace])
     for _peer_auth_item in _all_peer_auth_list:
         if 'app {}'.format(self._get_app_name(workload)) in to_linear_string(
             self.istio_config_details(
                 namespace=namespace,
                 object_name=_peer_auth_item.name,
                 object_type=IstioConfigObjectType.PEER_AUTHENTICATION.text).text):
             istio_configs.append(_peer_auth_item)
     return istio_configs
Example #3
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