Skip to content

Commit

Permalink
fix: handle null pointer when parsing metadata attributes (#759)
Browse files Browse the repository at this point in the history
* fix: handle null pointer when parsing metadata attributes

Fix behavior of getRegion and getZone methods when attributes aren't defined.
Align getNamespaceName return value with other methods to return null.

* 🦉 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
minherz and gcf-owl-bot[bot] committed Nov 24, 2021
1 parent 23f7fa5 commit e8cf6f9
Showing 1 changed file with 10 additions and 7 deletions.
Expand Up @@ -137,10 +137,10 @@ private String getModuleId() {
* K8S_POD_NAMESPACE_PATH} when available or read from a user defined environment variable
* "NAMESPACE_NAME"
*
* @return Namespace name or empty string if the name could not be discovered
* @return Namespace string or null if the name could not be discovered
*/
private String getNamespaceName() {
String value = "";
String value = null;
try {
value =
new String(
Expand All @@ -151,9 +151,6 @@ private String getNamespaceName() {
// if SA token is not shared the info about namespace is unavailable
// allow users to define the namespace name explicitly
value = getter.getEnv("NAMESPACE_NAME");
if (value == null) {
value = "";
}
}
return value;
}
Expand All @@ -178,7 +175,10 @@ private String getProjectId() {
*/
private String getRegion() {
String loc = getter.getAttribute("instance/region");
return loc.substring(loc.lastIndexOf('/') + 1);
if (loc != null) {
return loc.substring(loc.lastIndexOf('/') + 1);
}
return null;
}

private String getRevisionName() {
Expand All @@ -199,6 +199,9 @@ private String getVersionId() {
*/
private String getZone() {
String loc = getter.getAttribute("instance/zone");
return loc.substring(loc.lastIndexOf('/') + 1);
if (loc != null) {
return loc.substring(loc.lastIndexOf('/') + 1);
}
return null;
}
}

0 comments on commit e8cf6f9

Please sign in to comment.