Streamline service lookup

pull/573/head
jackgr 10 years ago
parent 30f8db7fbf
commit 378a27e8b3

@ -65,42 +65,48 @@ type KubernetesSecret struct {
Data map[string]string `json:"data,omitempty"` Data map[string]string `json:"data,omitempty"`
} }
// GetServiceURL takes a default service URL, a service name and a service port, // GetServiceURL takes a service name, a service port, and a default service URL,
// and returns a URL for accessing the service. It first looks for an environment // and returns a URL for accessing the service. It first looks for an environment
// variable set by Kubernetes by transposing the service name. If it can't find // variable set by Kubernetes by transposing the service name. If it can't find
// one, it looks up the service name in DNS. If that fails, it returns the default // one, it looks up the service name in DNS. If that doesn't work, it returns the
// service URL. // default service URL. If that's empty, it returns an HTTP localhost URL for the
func GetServiceURL(serviceURL, serviceName, servicePort string) string { // service port. If service port is empty, it panics.
if serviceURL == "" { func GetServiceURL(serviceName, servicePort, serviceURL string) (string, error) {
serviceURL = MakeEnvVariableURL(serviceName) if serviceName != "" {
if serviceURL == "" { varBase := strings.Replace(serviceName, "-", "_", -1)
varName := strings.ToUpper(varBase) + "_PORT"
serviceURL := os.Getenv(varName)
if serviceURL != "" {
return strings.Replace(serviceURL, "tcp", "http", 1), nil
}
if servicePort != "" {
addrs, err := net.LookupHost(serviceName) addrs, err := net.LookupHost(serviceName)
if err != nil || len(addrs) < 1 { if err == nil && len(addrs) > 0 {
log.Fatalf("cannot resolve service:%v. environment:%v\n", serviceName, os.Environ()) return fmt.Sprintf("http://%s:%s", addrs[0], servicePort), nil
}
}
} }
serviceURL = fmt.Sprintf("http://%s:%s", addrs[0], servicePort) if serviceURL != "" {
return serviceURL, nil
} }
if servicePort != "" {
serviceURL = fmt.Sprintf("http://localhost:%s", servicePort)
return serviceURL, nil
} }
return serviceURL err := fmt.Errorf("cannot resolve service:%v in environment:%v\n", serviceName, os.Environ())
return "", err
} }
// MakeEnvVariableURL takes a service name and returns the value of the // GetServiceURLOrDie calls GetServiceURL and exits if it returns an error.
// environment variable that identifies its URL, if it exists, or the empty func GetServiceURLOrDie(serviceName, servicePort, serviceURL string) string {
// string, if it doesn't. URL, err := GetServiceURL(serviceName, servicePort, serviceURL)
func MakeEnvVariableURL(str string) string { if err != nil {
prefix := MakeEnvVariableName(str) log.Fatal(err)
url := os.Getenv(prefix + "_PORT") }
return strings.Replace(url, "tcp", "http", 1)
}
// MakeEnvVariableName is copied from the Kubernetes source, return URL
// which is referenced by the documentation for service environment variables.
func MakeEnvVariableName(str string) string {
// TODO: If we simplify to "all names are DNS1123Subdomains" this
// will need two tweaks:
// 1) Handle leading digits
// 2) Handle dots
return strings.ToUpper(strings.Replace(str, "-", "_", -1))
} }

Loading…
Cancel
Save