<commit_before>import json

from typing import Any, ClassVar, Dict, List, Optional, Tuple, Union, TYPE_CHECKING

from ..config import Config

from .irresource import IRResource

if TYPE_CHECKING:
    from .ir import IR

def qualify_service_name(ir: 'IR', service: str, namespace: Optional[str]) -> str:
    fully_qualified = "." in service or "localhost" == service

    if namespace != ir.ambassador_namespace and namespace and not fully_qualified and not ir.ambassador_module.use_ambassador_namespace_for_service_resolution:
        # The target service name is not fully qualified.
        # We are most likely targeting a simple k8s svc with kube-dns resolution.
        # Make sure we actually resolve the service it's namespace, not the Ambassador process namespace.
        service = f"{service}.{namespace}"
        ir.logger.debug("KubernetesServiceResolver use_ambassador_namespace_for_service_resolution %s, fully qualified %s, upstream hostname %s" % (
            ir.ambassador_module.use_ambassador_namespace_for_service_resolution,
            fully_qualified,
            service
        ))
    
    return service

class IRBaseMapping (IRResource):
    group_id: str
    host: str
    route_weight: List[Union[str, int]]
    sni: bool
    cached_status: Optional[Dict[str, str]]
    status_update: Optional[Dict[str, str]]
<commit_msg>(from AES) Hopefully fix handling of services outside of our namespace with port numbers.<commit_after>import json

from typing import Any, ClassVar, Dict, List, Optional, Tuple, Union, TYPE_CHECKING

from ..config import Config

from .irresource import IRResource

if TYPE_CHECKING:
    from .ir import IR

def qualify_service_name(ir: 'IR', service: str, namespace: Optional[str]) -> str:
    fully_qualified = "." in service or "localhost" == service

    if namespace != ir.ambassador_namespace and namespace and not fully_qualified and not ir.ambassador_module.use_ambassador_namespace_for_service_resolution:
        # The target service name is not fully qualified.
        # We are most likely targeting a simple k8s svc with kube-dns resolution.
        # Make sure we actually resolve the service it's namespace, not the Ambassador process namespace.
        # 
        # Note well! The "unqualified" service here might contain a port number, so just appending 
        # the namespace won't end well. So start by checking for a port number...

        fields = service.split(":", 1)

        hostname = fields[0]
        port: Optional[str] = None

        if len(fields) > 1:
            port = fields[1]

        service = f"{hostname}.{namespace}"

        if port is not None:
            service += f":{port}"

        ir.logger.debug("KubernetesServiceResolver use_ambassador_namespace_for_service_resolution %s, fully qualified %s, upstream hostname %s" % (
            ir.ambassador_module.use_ambassador_namespace_for_service_resolution,
            fully_qualified,
            service
        ))
    
    return service

class IRBaseMapping (IRResource):
    group_id: str
    host: str
    route_weight: List[Union[str, int]]
    sni: bool
    cached_status: Optional[Dict[str, str]]
    status_update: Optional[Dict[str, str]]

{'fraction_non_alphanumeric': 0.0777396191070871, 'fraction_numerical': 0.0018732438339057135, 'mean_word_length': 3.306451612903226, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3089298', 'n_tokens_mistral': 897, 'n_tokens_neox': 859, 'n_words': 300}