From dc91ff264b580458d869f38a8cb7f7f0040f7d9a Mon Sep 17 00:00:00 2001 From: jackgr Date: Tue, 5 Apr 2016 08:37:27 -0700 Subject: [PATCH] Move service lookup to pkg/util --- cmd/manager/deployments.go | 43 +++------------------------------- pkg/util/kubernetes.go | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/cmd/manager/deployments.go b/cmd/manager/deployments.go index 52e215020..e2702fb7a 100644 --- a/cmd/manager/deployments.go +++ b/cmd/manager/deployments.go @@ -20,9 +20,7 @@ import ( "errors" "fmt" "log" - "net" "net/http" - "os" "strings" "github.com/kubernetes/helm/cmd/manager/manager" @@ -93,9 +91,9 @@ func newManager(c *router.Context) manager.Manager { service := repo.NewInmemRepoService() cp := c.CredentialProvider rp := repo.NewRepoProvider(service, repo.NewGCSRepoProvider(cp), cp) - expander := manager.NewExpander(getServiceURL(cfg.ExpanderURL, cfg.ExpanderName, expanderPort), rp) - deployer := manager.NewDeployer(getServiceURL(cfg.DeployerURL, cfg.DeployerName, deployerPort)) - address := strings.TrimPrefix(getServiceURL(cfg.MongoAddress, cfg.MongoName, cfg.MongoPort), "http://") + expander := manager.NewExpander(util.GetServiceURL(cfg.ExpanderURL, cfg.ExpanderName, expanderPort), rp) + deployer := manager.NewDeployer(util.GetServiceURL(cfg.DeployerURL, cfg.DeployerName, deployerPort)) + address := strings.TrimPrefix(util.GetServiceURL(cfg.MongoAddress, cfg.MongoName, cfg.MongoPort), "http://") repository := createRepository(address) return manager.NewManager(expander, deployer, repository, rp, service, c.CredentialProvider) } @@ -109,41 +107,6 @@ func createRepository(address string) repository.Repository { 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 { handler := "manager: list deployments" util.LogHandlerEntry(handler, r) diff --git a/pkg/util/kubernetes.go b/pkg/util/kubernetes.go index 81bc3aa23..8a444e9d3 100644 --- a/pkg/util/kubernetes.go +++ b/pkg/util/kubernetes.go @@ -16,6 +16,14 @@ limitations under the License. package util +import ( + "fmt" + "log" + "net" + "os" + "strings" +) + // KubernetesConfig defines the configuration options for talking to Kubernetes master type KubernetesConfig struct { KubePath string // The path to kubectl binary @@ -56,3 +64,43 @@ type KubernetesSecret struct { Metadata map[string]string `json:"metadata"` 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)) +}