Skip to content
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

Closed
wants to merge 7 commits into from

Conversation

wildhemp
Copy link
Contributor

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:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #494

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 wildhemp requested review from a team as code owners August 25, 2021 19:18
@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/java-logging API. label Aug 25, 2021
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Aug 25, 2021
@simonz130
Copy link

@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.

@simonz130 simonz130 added the automerge Merge the pull request once unit tests and other checks pass. label Sep 10, 2021
@gcf-merge-on-green
Copy link

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.

@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Sep 10, 2021
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.
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"));
Copy link
Contributor

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.

Comment on lines +39 to +47
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;
}
Copy link
Contributor

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");
}

@minherz
Copy link
Contributor

minherz commented Oct 12, 2021

Closing due to lack of feedback and the fact that the same topic is handled in #708

@minherz minherz closed this Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/java-logging API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with container name as reported by k8s_container resource type
3 participants