diff --git a/cmd/kubelet/kubelet.go b/cmd/kubelet/kubelet.go index a0487fb6777..00cf4a2c1cd 100644 --- a/cmd/kubelet/kubelet.go +++ b/cmd/kubelet/kubelet.go @@ -124,7 +124,7 @@ func main() { CAdvisorPort: *cAdvisorPort, EnableServer: *enableServer, EnableDebuggingHandlers: *enableDebuggingHandlers, - DockerClient: kubelet.ConnectToDockerOrDie(*dockerEndpoint), + DockerClient: util.ConnectToDockerOrDie(*dockerEndpoint), EtcdClient: kubelet.EtcdClientOrDie(etcdServerList, *etcdConfigFile), } diff --git a/cmd/kubernetes/kubernetes.go b/cmd/kubernetes/kubernetes.go index bd7400b4ef7..785048ecda4 100644 --- a/cmd/kubernetes/kubernetes.go +++ b/cmd/kubernetes/kubernetes.go @@ -27,7 +27,6 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" - "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet" "github.com/GoogleCloudPlatform/kubernetes/pkg/standalone" "github.com/GoogleCloudPlatform/kubernetes/pkg/tools" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" @@ -52,7 +51,7 @@ func startComponents(etcdClient tools.EtcdClient, cl *client.Client, addr string standalone.RunScheduler(cl) standalone.RunControllerManager(machineList, cl, *nodeMilliCPU, *nodeMemory) - dockerClient := kubelet.ConnectToDockerOrDie(*dockerEndpoint) + dockerClient := util.ConnectToDockerOrDie(*dockerEndpoint) standalone.SimpleRunKubelet(etcdClient, dockerClient, machineList[0], "/tmp/kubernetes", "", "127.0.0.1", 10250) } diff --git a/pkg/kubelet/util.go b/pkg/kubelet/util.go index aea6316ffd3..fdb6b4afd6f 100644 --- a/pkg/kubelet/util.go +++ b/pkg/kubelet/util.go @@ -20,10 +20,8 @@ import ( "fmt" "net/http" "os" - "os/exec" "path" "strconv" - "strings" "github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" @@ -32,50 +30,10 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/health" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/coreos/go-etcd/etcd" - "github.com/fsouza/go-dockerclient" "github.com/golang/glog" cadvisor "github.com/google/cadvisor/client" ) -// TODO: move this into a pkg/util -func GetHostname(hostnameOverride string) string { - hostname := []byte(hostnameOverride) - if string(hostname) == "" { - // Note: We use exec here instead of os.Hostname() because we - // want the FQDN, and this is the easiest way to get it. - fqdn, err := exec.Command("hostname", "-f").Output() - if err != nil { - glog.Fatalf("Couldn't determine hostname: %v", err) - } - hostname = fqdn - } - return strings.TrimSpace(string(hostname)) -} - -// TODO: move this into a pkg/util -func GetDockerEndpoint(dockerEndpoint string) string { - var endpoint string - if len(dockerEndpoint) > 0 { - endpoint = dockerEndpoint - } else if len(os.Getenv("DOCKER_HOST")) > 0 { - endpoint = os.Getenv("DOCKER_HOST") - } else { - endpoint = "unix:///var/run/docker.sock" - } - glog.Infof("Connecting to docker on %s", endpoint) - - return endpoint -} - -// TODO: move this into pkg/util -func ConnectToDockerOrDie(dockerEndpoint string) *docker.Client { - client, err := docker.NewClient(GetDockerEndpoint(dockerEndpoint)) - if err != nil { - glog.Fatal("Couldn't connect to docker.") - } - return client -} - // TODO: move this into the kubelet itself func MonitorCAdvisor(k *Kubelet, cp uint) { defer util.HandleCrash() diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index fa6d9d1fb3b..1c0cbe77115 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -21,7 +21,6 @@ import ( "math" "net" "net/http" - "os" "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" @@ -56,21 +55,6 @@ func (h *delegateHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusNotFound) } -// Get a docker endpoint, either from the string passed in, or $DOCKER_HOST environment variables -func GetDockerEndpoint(dockerEndpoint string) string { - var endpoint string - if len(dockerEndpoint) > 0 { - endpoint = dockerEndpoint - } else if len(os.Getenv("DOCKER_HOST")) > 0 { - endpoint = os.Getenv("DOCKER_HOST") - } else { - endpoint = "unix:///var/run/docker.sock" - } - glog.Infof("Connecting to docker on %s", endpoint) - - return endpoint -} - // RunApiServer starts an API server in a go routine. func RunApiServer(cl *client.Client, etcdClient tools.EtcdClient, addr string, port int) { handler := delegateHandler{} @@ -172,7 +156,7 @@ func RunKubelet(kcfg *KubeletConfig) { kubelet.SetupLogging() kubelet.SetupCapabilities(kcfg.AllowPrivileged) - kcfg.Hostname = kubelet.GetHostname(kcfg.HostnameOverride) + kcfg.Hostname = util.GetHostname(kcfg.HostnameOverride) if len(kcfg.RootDirectory) > 0 { kubelet.SetupRootDirectoryOrDie(kcfg.RootDirectory) } diff --git a/pkg/util/node.go b/pkg/util/node.go new file mode 100644 index 00000000000..e427394b166 --- /dev/null +++ b/pkg/util/node.go @@ -0,0 +1,63 @@ +/* +Copyright 2015 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "os" + "os/exec" + "strings" + + "github.com/fsouza/go-dockerclient" + "github.com/golang/glog" +) + +func GetHostname(hostnameOverride string) string { + hostname := []byte(hostnameOverride) + if string(hostname) == "" { + // Note: We use exec here instead of os.Hostname() because we + // want the FQDN, and this is the easiest way to get it. + fqdn, err := exec.Command("hostname", "-f").Output() + if err != nil { + glog.Fatalf("Couldn't determine hostname: %v", err) + } + hostname = fqdn + } + return strings.TrimSpace(string(hostname)) +} + +// Get a docker endpoint, either from the string passed in, or $DOCKER_HOST environment variables +func GetDockerEndpoint(dockerEndpoint string) string { + var endpoint string + if len(dockerEndpoint) > 0 { + endpoint = dockerEndpoint + } else if len(os.Getenv("DOCKER_HOST")) > 0 { + endpoint = os.Getenv("DOCKER_HOST") + } else { + endpoint = "unix:///var/run/docker.sock" + } + glog.Infof("Connecting to docker on %s", endpoint) + + return endpoint +} + +func ConnectToDockerOrDie(dockerEndpoint string) *docker.Client { + client, err := docker.NewClient(GetDockerEndpoint(dockerEndpoint)) + if err != nil { + glog.Fatal("Couldn't connect to docker.") + } + return client +}