Commit dc91ff26 authored by jackgr's avatar jackgr

Move service lookup to pkg/util

parent 105348bd
...@@ -20,9 +20,7 @@ import ( ...@@ -20,9 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"net"
"net/http" "net/http"
"os"
"strings" "strings"
"github.com/kubernetes/helm/cmd/manager/manager" "github.com/kubernetes/helm/cmd/manager/manager"
...@@ -93,9 +91,9 @@ func newManager(c *router.Context) manager.Manager { ...@@ -93,9 +91,9 @@ func newManager(c *router.Context) manager.Manager {
service := repo.NewInmemRepoService() service := repo.NewInmemRepoService()
cp := c.CredentialProvider cp := c.CredentialProvider
rp := repo.NewRepoProvider(service, repo.NewGCSRepoProvider(cp), cp) rp := repo.NewRepoProvider(service, repo.NewGCSRepoProvider(cp), cp)
expander := manager.NewExpander(getServiceURL(cfg.ExpanderURL, cfg.ExpanderName, expanderPort), rp) expander := manager.NewExpander(util.GetServiceURL(cfg.ExpanderURL, cfg.ExpanderName, expanderPort), rp)
deployer := manager.NewDeployer(getServiceURL(cfg.DeployerURL, cfg.DeployerName, deployerPort)) deployer := manager.NewDeployer(util.GetServiceURL(cfg.DeployerURL, cfg.DeployerName, deployerPort))
address := strings.TrimPrefix(getServiceURL(cfg.MongoAddress, cfg.MongoName, cfg.MongoPort), "http://") address := strings.TrimPrefix(util.GetServiceURL(cfg.MongoAddress, cfg.MongoName, cfg.MongoPort), "http://")
repository := createRepository(address) repository := createRepository(address)
return manager.NewManager(expander, deployer, repository, rp, service, c.CredentialProvider) return manager.NewManager(expander, deployer, repository, rp, service, c.CredentialProvider)
} }
...@@ -109,41 +107,6 @@ func createRepository(address string) repository.Repository { ...@@ -109,41 +107,6 @@ func createRepository(address string) repository.Repository {
return r return r
} }
func getServiceURL(serviceURL, serviceName, servicePort string) string {
if serviceURL == "" {
serviceURL = makeEnvVariableURL(serviceName)
if serviceURL == "" {
addrs, err := net.LookupHost(serviceName)
if err != nil || len(addrs) < 1 {
log.Fatalf("cannot resolve service:%v. environment:%v\n", serviceName, os.Environ())
}
serviceURL = fmt.Sprintf("http://%s:%s", addrs[0], servicePort)
}
}
return serviceURL
}
// makeEnvVariableURL takes a service name and returns the value of the
// environment variable that identifies its URL, if it exists, or the empty
// string, if it doesn't.
func makeEnvVariableURL(str string) string {
prefix := makeEnvVariableName(str)
url := os.Getenv(prefix + "_PORT")
return strings.Replace(url, "tcp", "http", 1)
}
// makeEnvVariableName is copied from the Kubernetes source,
// 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))
}
func listDeploymentsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { func listDeploymentsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error {
handler := "manager: list deployments" handler := "manager: list deployments"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
......
...@@ -16,6 +16,14 @@ limitations under the License. ...@@ -16,6 +16,14 @@ limitations under the License.
package util package util
import (
"fmt"
"log"
"net"
"os"
"strings"
)
// KubernetesConfig defines the configuration options for talking to Kubernetes master // KubernetesConfig defines the configuration options for talking to Kubernetes master
type KubernetesConfig struct { type KubernetesConfig struct {
KubePath string // The path to kubectl binary KubePath string // The path to kubectl binary
...@@ -56,3 +64,43 @@ type KubernetesSecret struct { ...@@ -56,3 +64,43 @@ type KubernetesSecret struct {
Metadata map[string]string `json:"metadata"` Metadata map[string]string `json:"metadata"`
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,
// 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
// one, it looks up the service name in DNS. If that fails, it returns the default
// service URL.
func GetServiceURL(serviceURL, serviceName, servicePort string) string {
if serviceURL == "" {
serviceURL = MakeEnvVariableURL(serviceName)
if serviceURL == "" {
addrs, err := net.LookupHost(serviceName)
if err != nil || len(addrs) < 1 {
log.Fatalf("cannot resolve service:%v. environment:%v\n", serviceName, os.Environ())
}
serviceURL = fmt.Sprintf("http://%s:%s", addrs[0], servicePort)
}
}
return serviceURL
}
// MakeEnvVariableURL takes a service name and returns the value of the
// environment variable that identifies its URL, if it exists, or the empty
// string, if it doesn't.
func MakeEnvVariableURL(str string) string {
prefix := MakeEnvVariableName(str)
url := os.Getenv(prefix + "_PORT")
return strings.Replace(url, "tcp", "http", 1)
}
// MakeEnvVariableName is copied from the Kubernetes source,
// 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))
}
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