Closed damfleu closed 1 month ago
I'm not sure why I used Contains
originally instead of HasPrefix
, as that would have been more appropriate. Nonetheless, rather than use a regex, why not simply append a dot to the needle? Wouldn't that have the same effect here or are there cases the regex would more completely cover?
The goal is to find a label like traefik.http.routers.hello.[attribute]=[value]
without looking for a specific attribute which may not be there in all cases. I don't think we'd ever have the case of a bare label like traefik.http.routers.hello=[value]
, but even in this case we could also check for an exact match (without the dot).
I'm not sure why I used
Contains
originally instead ofHasPrefix
, as that would have been more appropriate.
Though in this case HasPrefix
would have had the same issue!
Nonetheless, rather than use a regex, why not simply append a dot to the needle? Wouldn't that have the same effect here or are there cases the regex would more completely cover?
The goal is to find a label like
traefik.http.routers.hello.[attribute]=[value]
without looking for a specific attribute which may not be there in all cases. I don't think we'd ever have the case of a bare label liketraefik.http.routers.hello=[value]
, but even in this case we could also check for an exact match (without the dot).
Right, I went with that at first and then used a regex to handle exactly the case where the label ends there. I just didn't want to assume that wasn't be a valid case. Using HasPrefix()
and adding a dot to the needle sounds good, it should cover all cases where an attribute is expected.
Though in this case HasPrefix would have had the same issue!
Yup, you are totally correct, I was just commenting on my odd choice there 😖. Started out as a quick hack. It worked, I shipped!
Thanks for the comments, code has been updated, let me know what you think!
There is an issue with
findContainerByServiceName
where it incorrectly matches containers with similar names, specifically when one service name is a prefix of the other.The check that the label contains the string
traefik.<svcType>.services.<svcName>
will match for both the service name<svcName>
but also services such as<svcName>-suffix
. If the docker container for the longer service is looked up first, then it will incorrectly retrieve the port binding from that container.The labels should be in the format
traefik.<svcType>.services.<svcName>.<attribute>
so just add a dot at the end of the needle and useHasPrefix()
to ensure an exact match. The added test verifies the fix for this issue wherehello
would end up with"http://192.168.100.100:5566"
instead of5555
. Unfortunately, since it depends in which order the containers will be iterated over, it doesn't fail (without the fix) all the time.Here is the output of a non-passing run: