Created
April 3, 2024 05:45
-
-
Save jstangroome/0cba92daf82713262f27b4ccd857ff0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getPodNamespace() string { | |
const namespaceEnvVar = "POD_NAMESPACE" | |
const namespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" | |
namespaceName := os.Getenv(namespaceEnvVar) | |
if namespaceName != "" { | |
return namespaceName | |
} | |
namespaceNameBytes, err := os.ReadFile(namespaceFile) | |
if err != nil { | |
log.Printf("[ERROR] '%s' environment variable not specified and could not read file '%s': '%#v'", | |
namespaceEnvVar, namespaceFile, err) | |
return "" | |
} | |
return string(namespaceNameBytes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment