New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Temporary "fix How containername is determined from hostname #642
Conversation
The container name can contain '-' character, so String.indexOf is not a good way to determine it from hostname. Instead it should be substring(0, <second to last '-'>), which is what this fix is doing.
@wildhemp - there were github changes to ubuntu image that affected our CI. As a result, your PR failed on CI. I mitigated it by updating the CI but now you need to sync your form with latest changes to our fork master and then your PR will pass the CI and I'll merge it. |
Merge-on-green attempted to merge your PR for 6 hours, but it was not mergeable because either one of your required status checks failed, one of your required reviews was not approved, or there is a do not merge label. Learn more about your required status checks here: https://help.github.com/en/github/administering-a-repository/enabling-required-status-checks. You can remove and reapply the label to re-run the bot. |
google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java
Outdated
Show resolved
Hide resolved
The container name can contain '-' character, so String.indexOf is not a good way to determine it from hostname. Instead it should be substring(0, <second to last '-'>), which is what this fix is doing. This also makes sure we only do this when the last '-' is 6 to last character. Otherwise we it just returns the host name because the original host naming can be overriden in which case we don't want to do this magic and instead just return the host name. Note, this is a temporary 'fix' and a proper solution would send the container name down to the pod rather then trying to deduce it from the host name. This simply makes things a bit better for when defaults are being used.
The container name can contain '-' character, so String.indexOf is not a good way to determine it from hostname. Instead it should be substring(0, <second to last '-'>), which is what this fix is doing. This also makes sure we only do this when the last '-' is 6 to last character. Otherwise we it just returns the host name because the original host naming can be overriden in which case we don't want to do this magic and instead just return the host name. Note, this is a temporary 'fix' and a proper solution would send the container name down to the pod rather then trying to deduce it from the host name. This simply makes things a bit better for when defaults are being used.
The container name can contain '-' character, so String.indexOf is not a good way to determine it from hostname. Instead it should be substring(0, <second to last '-'>), which is what this fix is doing. This also makes sure we only do this when the last '-' is 6 to last character. Otherwise we it just returns the host name because the original host naming can be overriden in which case we don't want to do this magic and instead just return the host name. Note, this is a temporary 'fix' and a proper solution would send the container name down to the pod rather then trying to deduce it from the host name. This simply makes things a bit better for when defaults are being used.
google-cloud-logging/src/main/java/com/google/cloud/logging/K8sContainerUtil.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/main/java/com/google/cloud/logging/K8sContainerUtil.java
Show resolved
Hide resolved
google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/K8sContainerUtilTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/K8sContainerUtilTest.java
Show resolved
Hide resolved
The container name can contain '-' character, so String.indexOf is not a good way to determine it from hostname. Instead it should be substring(0, <second to last '-'>), which is what this fix is doing. This also makes sure we only do this when the last '-' is 6 to last character. Otherwise we it just returns the host name because the original host naming can be overriden in which case we don't want to do this magic and instead just return the host name. Note, this is a temporary 'fix' and a proper solution would send the container name down to the pod rather then trying to deduce it from the host name. This simply makes things a bit better for when defaults are being used.
value = MetadataConfig.getContainerName(); | ||
value = MetadataConfig.getContainerName(); | ||
if (value == null && resourceType.equals("k8s_container")) { | ||
value = getContainerNameFromHostName(System.getenv("HOSTNAME")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code retrieves pod name and not container name. By default containers receive the same hostname which is a pod name if the pod is launched as standalone or the name is generated following the template REPLICASET-?????
where REPLICASET
is a name of the replicaset build as PODNAME-HASH
and ?????
are 5 alphanumeric identifier.
So, when you extract the first part of the hostname you get the pod name and not container name.
int lastDashIndex = hostName.length() - 6; | ||
if (lastDashIndex > 0 && hostName.charAt(lastDashIndex) == '-') { | ||
int secondToLastIndex = hostName.lastIndexOf("-", lastDashIndex - 1); | ||
if (secondToLastIndex > 0) { | ||
return hostName.substring(0, secondToLastIndex); | ||
} | ||
} | ||
return hostName; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not easy to understand the implementation logic. Would it be simpler to use a regexp? For example something like:
const String pattern = "([a-z0-9]*)-[a-z0-9]{9}-[a-z0-9]{5}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher("myservice-bc8d57467-tgcbj");
if (m.find( )) {
System.out.println("Pod name: " + m.group(0));
} else {
System.out.println("unknown");
}
Closing due to lack of feedback and the fact that the same topic is handled in #708 |
The container name can contain '-' character, so String.indexOf is not a good way to determine it from hostname. Instead it should be substring(0, <second to last '-'>), which is what this fix is doing.
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #494