'Extract a port from a container_inspect or the k8s API given a template variable.'

def _get_port(self, state, c_id, tpl_var):

    container_inspect = state.inspect_container(c_id)
    try:
        ports = map((lambda x: x.split('/')[0]), container_inspect['NetworkSettings']['Ports'].keys())
        if (len(ports) == 0):
            raise IndexError
    except (IndexError, KeyError, AttributeError):
        ports = map((lambda x: x.split('/')[0]), container_inspect['Config'].get('ExposedPorts', {}).keys())
        if ((not ports) and Platform.is_k8s()):
            log.debug(("Didn't    find    the    port    for    container    %s    (%s),    trying    the    kubernetes    way." % (c_id[:12], container_inspect.get('Config', {}).get('Image', ''))))
            spec = state.get_kube_container_spec(c_id)
            if spec:
                ports = [str(x.get('containerPort')) for x in spec.get('ports', [])]
    ports = sorted(ports, key=int)
    return self._extract_port_from_list(ports, tpl_var)
