Created
December 10, 2018 17:24
-
-
Save DazWilkin/44d20ffc859c56aec6420b3aeac7c52c to your computer and use it in GitHub Desktop.
Obtain GCP Project ID from metadata service
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
FROM golang:1.11 as build | |
ADD . /go/src/github.com/dazwilkin/metadata | |
WORKDIR /go/src/github.com/dazwilkin/metadata | |
ARG GOFLAGS="" | |
RUN go get ${GOFLAGS} ./... | |
FROM gcr.io/distroless/base | |
COPY --from=build /go/bin/metadata / | |
ENTRYPOINT ["/metadata"] |
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
package main | |
import ( | |
"log" | |
"cloud.google.com/go/compute/metadata" | |
) | |
func main() { | |
instanceID, err := metadata.InstanceID() | |
if err != nil { | |
log.Println("Error getting instance ID:", err) | |
instanceID = "unknown" | |
} | |
zone, err := metadata.Zone() | |
if err != nil { | |
log.Println("Error getting zone:", err) | |
zone = "unknown" | |
} | |
projectID, err := metadata.ProjectID() | |
if err != nil { | |
log.Println("Error getting Project ID: ", err) | |
projectID = "unknown" | |
} | |
log.Printf("[%s] %s/%s", projectID, zone, instanceID) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment