in src/routingmanager/RoutingManagerApp.cs [751:811]
private Task<bool> ReplaceServiceNamedPortsAsync(V1Service service, IEnumerable<V1Pod> pods, CancellationToken cancellationToken)
{
return Task.Run(() =>
{
V1Pod selectedPod = null;
foreach (var triggerServicePort in service.Spec.Ports)
{
int port_int = -1;
if (!int.TryParse(triggerServicePort.TargetPort.Value, out port_int))
{
if (selectedPod == null)
{
// try to find a pod for this service by matching label selector
selectedPod =
pods.FirstOrDefault(pod =>
service.Spec?.Selector != null
&& service.Spec.Selector.All(selector => pod.Metadata.Labels != null
&& pod.Metadata.Labels.ContainsKey(selector.Key)
&& StringComparer.OrdinalIgnoreCase.Equals(pod.Metadata.Labels[selector.Key], selector.Value)));
if (selectedPod == null)
{
return false;
}
}
bool found = false;
foreach (var container in selectedPod.Spec.Containers)
{
// Container ports could be null in case it is a sidecar container which doesn't need to be accessed through a service
if (container.Ports == null)
{
continue;
}
foreach (var containerPort in container.Ports)
{
if (StringComparer.OrdinalIgnoreCase.Equals(containerPort.Name, triggerServicePort.TargetPort))
{
_log.Info("Identified named port '{0}' in service '{1}'", new PII(containerPort.Name), new PII(service.Metadata.Name));
triggerServicePort.TargetPort = containerPort.ContainerPort;
found = true;
break;
}
}
if (found)
{
break;
}
}
if (!found)
{
return false;
}
}
}
return true;
}, cancellationToken);
}