-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve loading of deployment details * Hide missing deployment details labels * Load deployment details once via init() * Add comment about go loadDeploymentDetails() * Simplify deployment_details.go * Remove getDeploymentDetailsIfLoaded() function Co-authored-by: Shabir Mohamed Abdul Samadh <[email protected]>
- Loading branch information
1 parent
1c558c7
commit 6b25a1a
Showing
3 changed files
with
81 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"cloud.google.com/go/compute/metadata" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var deploymentDetailsMap map[string]string | ||
var log *logrus.Logger | ||
|
||
func init() { | ||
initializeLogger() | ||
// Use a goroutine to ensure loadDeploymentDetails()'s GCP API | ||
// calls don't block non-GCP deployments. See issue #685. | ||
go loadDeploymentDetails() | ||
} | ||
|
||
func initializeLogger() { | ||
log = logrus.New() | ||
log.Level = logrus.DebugLevel | ||
log.Formatter = &logrus.JSONFormatter{ | ||
FieldMap: logrus.FieldMap{ | ||
logrus.FieldKeyTime: "timestamp", | ||
logrus.FieldKeyLevel: "severity", | ||
logrus.FieldKeyMsg: "message", | ||
}, | ||
TimestampFormat: time.RFC3339Nano, | ||
} | ||
log.Out = os.Stdout | ||
} | ||
|
||
func loadDeploymentDetails() { | ||
deploymentDetailsMap = make(map[string]string) | ||
var metaServerClient = metadata.NewClient(&http.Client{}) | ||
|
||
podHostname, err := os.Hostname() | ||
if err != nil { | ||
log.Error("Failed to fetch the hostname for the Pod", err) | ||
} | ||
|
||
podCluster, err := metaServerClient.InstanceAttributeValue("cluster-name") | ||
if err != nil { | ||
log.Error("Failed to fetch the name of the cluster in which the pod is running", err) | ||
} | ||
|
||
podZone, err := metaServerClient.Zone() | ||
if err != nil { | ||
log.Error("Failed to fetch the Zone of the node where the pod is scheduled", err) | ||
} | ||
|
||
deploymentDetailsMap["HOSTNAME"] = podHostname | ||
deploymentDetailsMap["CLUSTERNAME"] = podCluster | ||
deploymentDetailsMap["ZONE"] = podZone | ||
|
||
log.WithFields(logrus.Fields{ | ||
"cluster": podCluster, | ||
"zone": podZone, | ||
"hostname": podHostname, | ||
}).Debug("Loaded deployment details") | ||
} |
This file contains 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
This file contains 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