metadata server
parent
6c8fbd80b8
commit
311d4876ef
|
@ -1,15 +1,56 @@
|
||||||
package pubsub
|
package pubsub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MetadataEndpoint - default metadata server for gcloud pubsub
|
||||||
|
const MetadataEndpoint = "metadata/computeMetadata/v1/instance/attributes/cluster-name"
|
||||||
|
|
||||||
func containerRegistryURI(projectID, registry string) string {
|
func containerRegistryURI(projectID, registry string) string {
|
||||||
return registry + "%2F" + projectID
|
return registry + "%2F" + projectID
|
||||||
}
|
}
|
||||||
|
|
||||||
func containerRegistrySubName(projectID, topic string) string {
|
func containerRegistrySubName(projectID, topic string) string {
|
||||||
return "keel-" + projectID + "-" + topic
|
cluster := "unknown"
|
||||||
|
clusterName, err := clusterName(MetadataEndpoint)
|
||||||
|
if err != nil {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"error": err,
|
||||||
|
"metadata_endpoint": MetadataEndpoint,
|
||||||
|
}).Warn("trigger.pubsub.containerRegistrySubName: got error while retrieving cluster metadata, messages might be lost if more than one Keel instance is created")
|
||||||
|
} else {
|
||||||
|
cluster = clusterName
|
||||||
|
}
|
||||||
|
|
||||||
|
return "keel-" + cluster + "-" + projectID + "-" + topic
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cloud.google.com/compute/docs/storing-retrieving-metadata
|
||||||
|
func clusterName(metadataEndpoint string) (string, error) {
|
||||||
|
req, err := http.NewRequest(http.MethodGet, metadataEndpoint, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Add("Metadata-Flavor", "Google")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(body), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isGoogleContainerRegistry - we only care about gcr.io images,
|
// isGoogleContainerRegistry - we only care about gcr.io images,
|
||||||
|
|
Loading…
Reference in New Issue