Commit 378a27e8 authored by jackgr's avatar jackgr

Streamline service lookup

parent 30f8db7f
...@@ -65,42 +65,48 @@ type KubernetesSecret struct { ...@@ -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)
} }
} }
return serviceURL if serviceURL != "" {
} return serviceURL, nil
}
// MakeEnvVariableURL takes a service name and returns the value of the if servicePort != "" {
// environment variable that identifies its URL, if it exists, or the empty serviceURL = fmt.Sprintf("http://localhost:%s", servicePort)
// string, if it doesn't. return serviceURL, nil
func MakeEnvVariableURL(str string) string { }
prefix := MakeEnvVariableName(str)
url := os.Getenv(prefix + "_PORT") err := fmt.Errorf("cannot resolve service:%v in environment:%v\n", serviceName, os.Environ())
return strings.Replace(url, "tcp", "http", 1) return "", err
} }
// MakeEnvVariableName is copied from the Kubernetes source, // GetServiceURLOrDie calls GetServiceURL and exits if it returns an error.
// which is referenced by the documentation for service environment variables. func GetServiceURLOrDie(serviceName, servicePort, serviceURL string) string {
func MakeEnvVariableName(str string) string { URL, err := GetServiceURL(serviceName, servicePort, serviceURL)
// TODO: If we simplify to "all names are DNS1123Subdomains" this if err != nil {
// will need two tweaks: log.Fatal(err)
// 1) Handle leading digits }
// 2) Handle dots
return strings.ToUpper(strings.Replace(str, "-", "_", -1)) return URL
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment