From 6a5681e0fea69868074c591628f82515bfe706e8 Mon Sep 17 00:00:00 2001 From: Yifan Gu Date: Fri, 15 May 2015 16:14:08 -0700 Subject: [PATCH 1/2] kubelet: Move HashContainer to kubelet/container package. --- pkg/kubelet/container/helpers.go | 10 ++++++++++ pkg/kubelet/dockertools/docker.go | 10 ++-------- pkg/kubelet/dockertools/manager.go | 4 ++-- pkg/kubelet/kubelet_test.go | 14 +++++++------- pkg/kubelet/rkt/rkt.go | 14 ++------------ pkg/kubelet/runonce_test.go | 2 +- 6 files changed, 24 insertions(+), 30 deletions(-) diff --git a/pkg/kubelet/container/helpers.go b/pkg/kubelet/container/helpers.go index 31a4d08dd70..dbce6d08ce4 100644 --- a/pkg/kubelet/container/helpers.go +++ b/pkg/kubelet/container/helpers.go @@ -17,9 +17,11 @@ limitations under the License. package container import ( + "hash/adler32" "strings" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/golang/glog" ) @@ -80,3 +82,11 @@ func ShouldContainerBeRestarted(container *api.Container, pod *api.Pod, podStatu } return true } + +// HashContainer returns the hash of the container. It is used to compare +// the running container with its desired spec. +func HashContainer(container *api.Container) uint64 { + hash := adler32.New() + util.DeepHashObject(hash, *container) + return uint64(hash.Sum32()) +} diff --git a/pkg/kubelet/dockertools/docker.go b/pkg/kubelet/dockertools/docker.go index 340838e89e3..9954370591e 100644 --- a/pkg/kubelet/dockertools/docker.go +++ b/pkg/kubelet/dockertools/docker.go @@ -18,7 +18,6 @@ package dockertools import ( "fmt" - "hash/adler32" "math/rand" "os" "strconv" @@ -26,6 +25,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/credentialprovider" + kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/leaky" kubeletTypes "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/types" "github.com/GoogleCloudPlatform/kubernetes/pkg/types" @@ -212,15 +212,9 @@ func (c DockerContainers) FindPodContainer(podFullName string, uid types.UID, co const containerNamePrefix = "k8s" -func HashContainer(container *api.Container) uint64 { - hash := adler32.New() - util.DeepHashObject(hash, *container) - return uint64(hash.Sum32()) -} - // Creates a name which can be reversed to identify both full pod name and container name. func BuildDockerName(dockerName KubeletContainerName, container *api.Container) string { - containerName := dockerName.ContainerName + "." + strconv.FormatUint(HashContainer(container), 16) + containerName := dockerName.ContainerName + "." + strconv.FormatUint(kubecontainer.HashContainer(container), 16) return fmt.Sprintf("%s_%s_%s_%s_%08x", containerNamePrefix, containerName, diff --git a/pkg/kubelet/dockertools/manager.go b/pkg/kubelet/dockertools/manager.go index 4691bbaa287..7baae160f65 100644 --- a/pkg/kubelet/dockertools/manager.go +++ b/pkg/kubelet/dockertools/manager.go @@ -824,7 +824,7 @@ func (dm *DockerManager) podInfraContainerChanged(pod *api.Pod, podInfraContaine Image: dm.PodInfraContainerImage, Ports: ports, } - return podInfraContainer.Hash != HashContainer(expectedPodInfraContainer), nil + return podInfraContainer.Hash != kubecontainer.HashContainer(expectedPodInfraContainer), nil } type dockerVersion docker.APIVersion @@ -1355,7 +1355,7 @@ func (dm *DockerManager) computePodContainerChanges(pod *api.Pod, runningPod kub } for index, container := range pod.Spec.Containers { - expectedHash := HashContainer(&container) + expectedHash := kubecontainer.HashContainer(&container) c := runningPod.FindContainerByName(container.Name) if c == nil { diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 7e6713a95a0..8b347f5ff25 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -367,7 +367,7 @@ func generatePodInfraContainerHash(pod *api.Pod) uint64 { Image: dockertools.PodInfraContainerImage, Ports: ports, } - return dockertools.HashContainer(container) + return kubecontainer.HashContainer(container) } func TestSyncPodsDoesNothing(t *testing.T) { @@ -397,7 +397,7 @@ func TestSyncPodsDoesNothing(t *testing.T) { fakeDocker.ContainerList = []docker.APIContainers{ { // format is // k8s____ - Names: []string{"/k8s_bar." + strconv.FormatUint(dockertools.HashContainer(&container), 16) + "_foo_new_12345678_0"}, + Names: []string{"/k8s_bar." + strconv.FormatUint(kubecontainer.HashContainer(&container), 16) + "_foo_new_12345678_0"}, ID: "1234", }, { @@ -3900,12 +3900,12 @@ func TestSyncPodsWithRestartPolicy(t *testing.T) { exitedAPIContainers := []docker.APIContainers{ { // format is // k8s___ - Names: []string{"/k8s_succeeded." + strconv.FormatUint(dockertools.HashContainer(&containers[0]), 16) + "_foo_new_12345678_0"}, + Names: []string{"/k8s_succeeded." + strconv.FormatUint(kubecontainer.HashContainer(&containers[0]), 16) + "_foo_new_12345678_0"}, ID: "1234", }, { // format is // k8s___ - Names: []string{"/k8s_failed." + strconv.FormatUint(dockertools.HashContainer(&containers[1]), 16) + "_foo_new_12345678_0"}, + Names: []string{"/k8s_failed." + strconv.FormatUint(kubecontainer.HashContainer(&containers[1]), 16) + "_foo_new_12345678_0"}, ID: "5678", }, } @@ -4042,12 +4042,12 @@ func TestGetPodStatusWithLastTermination(t *testing.T) { exitedAPIContainers := []docker.APIContainers{ { // format is // k8s___ - Names: []string{"/k8s_succeeded." + strconv.FormatUint(dockertools.HashContainer(&containers[0]), 16) + "_foo_new_12345678_0"}, + Names: []string{"/k8s_succeeded." + strconv.FormatUint(kubecontainer.HashContainer(&containers[0]), 16) + "_foo_new_12345678_0"}, ID: "1234", }, { // format is // k8s___ - Names: []string{"/k8s_failed." + strconv.FormatUint(dockertools.HashContainer(&containers[1]), 16) + "_foo_new_12345678_0"}, + Names: []string{"/k8s_failed." + strconv.FormatUint(kubecontainer.HashContainer(&containers[1]), 16) + "_foo_new_12345678_0"}, ID: "5678", }, } @@ -4324,7 +4324,7 @@ func TestGetRestartCount(t *testing.T) { } // format is // k8s___ - names := []string{"/k8s_bar." + strconv.FormatUint(dockertools.HashContainer(&containers[0]), 16) + "_foo_new_12345678_0"} + names := []string{"/k8s_bar." + strconv.FormatUint(kubecontainer.HashContainer(&containers[0]), 16) + "_foo_new_12345678_0"} currTime := time.Now() containerMap := map[string]*docker.Container{ "1234": { diff --git a/pkg/kubelet/rkt/rkt.go b/pkg/kubelet/rkt/rkt.go index 690e1adfc1b..37d8f8f77fb 100644 --- a/pkg/kubelet/rkt/rkt.go +++ b/pkg/kubelet/rkt/rkt.go @@ -19,7 +19,6 @@ package rkt import ( "encoding/json" "fmt" - "hash/adler32" "io" "io/ioutil" "os" @@ -38,7 +37,6 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/probe" "github.com/GoogleCloudPlatform/kubernetes/pkg/securitycontext" "github.com/GoogleCloudPlatform/kubernetes/pkg/types" - "github.com/GoogleCloudPlatform/kubernetes/pkg/util" appcschema "github.com/appc/spec/schema" appctypes "github.com/appc/spec/schema/types" "github.com/coreos/go-systemd/dbus" @@ -460,14 +458,6 @@ func newUnitOption(section, name, value string) *unit.UnitOption { return &unit.UnitOption{Section: section, Name: name, Value: value} } -// TODO(yifan): Move this duplicated function to container runtime. -// hashContainer computes the hash of one api.Container. -func hashContainer(container *api.Container) uint64 { - hash := adler32.New() - util.DeepHashObject(hash, *container) - return uint64(hash.Sum32()) -} - // TODO(yifan): Remove the receiver once we can solve the appName->imageID problem. func (r *runtime) apiPodToruntimePod(uuid string, pod *api.Pod) *kubecontainer.Pod { p := &kubecontainer.Pod{ @@ -485,7 +475,7 @@ func (r *runtime) apiPodToruntimePod(uuid string, pod *api.Pod) *kubecontainer.P ID: types.UID(buildContainerID(&containerID{uuid, c.Name, img.id})), Name: c.Name, Image: c.Image, - Hash: hashContainer(c), + Hash: kubecontainer.HashContainer(c), Created: time.Now().Unix(), }) } @@ -847,7 +837,7 @@ func (r *runtime) SyncPod(pod *api.Pod, runningPod kubecontainer.Pod, podStatus restartPod := false for _, container := range pod.Spec.Containers { - expectedHash := hashContainer(&container) + expectedHash := kubecontainer.HashContainer(&container) c := runningPod.FindContainerByName(container.Name) if c == nil { diff --git a/pkg/kubelet/runonce_test.go b/pkg/kubelet/runonce_test.go index b39c8b8233d..08cc67d5c36 100644 --- a/pkg/kubelet/runonce_test.go +++ b/pkg/kubelet/runonce_test.go @@ -96,7 +96,7 @@ func TestRunOnce(t *testing.T) { } podContainers := []docker.APIContainers{ { - Names: []string{"/k8s_bar." + strconv.FormatUint(dockertools.HashContainer(&api.Container{Name: "bar"}), 16) + "_foo_new_12345678_42"}, + Names: []string{"/k8s_bar." + strconv.FormatUint(kubecontainer.HashContainer(&api.Container{Name: "bar"}), 16) + "_foo_new_12345678_42"}, ID: "1234", Status: "running", }, From 02eee4890bf6032bb2b58f5945ba1a046faae395 Mon Sep 17 00:00:00 2001 From: Yifan Gu Date: Fri, 15 May 2015 16:19:48 -0700 Subject: [PATCH 2/2] cluster/gce/coreos: Make rkt version configuable. --- cluster/gce/config-default.sh | 1 + cluster/gce/config-test.sh | 1 + cluster/gce/coreos/helper.sh | 2 ++ cluster/gce/coreos/node.yaml | 10 +++++----- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cluster/gce/config-default.sh b/cluster/gce/config-default.sh index de56775bc25..c245e6de9ea 100755 --- a/cluster/gce/config-default.sh +++ b/cluster/gce/config-default.sh @@ -32,6 +32,7 @@ MASTER_IMAGE_PROJECT=${KUBE_GCE_MASTER_PROJECT:-google-containers} MINION_IMAGE=${KUBE_GCE_MINION_IMAGE:-container-vm-v20150505} MINION_IMAGE_PROJECT=${KUBE_GCE_MINION_PROJECT:-google-containers} CONTAINER_RUNTIME=${KUBE_CONTAINER_RUNTIME:-docker} +RKT_VERSION=${KUBE_RKT_VERSION:-0.5.5} NETWORK=${KUBE_GCE_NETWORK:-default} INSTANCE_PREFIX="${KUBE_GCE_INSTANCE_PREFIX:-kubernetes}" diff --git a/cluster/gce/config-test.sh b/cluster/gce/config-test.sh index 56dc8163b18..52d6f8ca05c 100755 --- a/cluster/gce/config-test.sh +++ b/cluster/gce/config-test.sh @@ -32,6 +32,7 @@ MASTER_IMAGE_PROJECT=${KUBE_GCE_MASTER_PROJECT:-google-containers} MINION_IMAGE=${KUBE_GCE_MINION_IMAGE:-container-vm-v20150505} MINION_IMAGE_PROJECT=${KUBE_GCE_MINION_PROJECT:-google-containers} CONTAINER_RUNTIME=${KUBE_CONTAINER_RUNTIME:-docker} +RKT_VERSION=${KUBE_RKT_VERSION:-0.5.5} NETWORK=${KUBE_GCE_NETWORK:-e2e} INSTANCE_PREFIX="${KUBE_GCE_INSTANCE_PREFIX:-e2e-test-${USER}}" diff --git a/cluster/gce/coreos/helper.sh b/cluster/gce/coreos/helper.sh index a5c63c5ef2c..db469ee9aa9 100644 --- a/cluster/gce/coreos/helper.sh +++ b/cluster/gce/coreos/helper.sh @@ -51,6 +51,7 @@ KUBE_PROXY_TOKEN: $(yaml-quote ${KUBE_PROXY_TOKEN:-}) ADMISSION_CONTROL: $(yaml-quote ${ADMISSION_CONTROL:-}) MASTER_IP_RANGE: $(yaml-quote ${MASTER_IP_RANGE}) KUBERNETES_CONTAINER_RUNTIME: $(yaml-quote ${CONTAINER_RUNTIME}) +RKT_VERSION: $(yaml-quote ${RKT_VERSION}) CA_CERT: $(yaml-quote ${CA_CERT_BASE64}) MASTER_CERT: $(yaml-quote ${MASTER_CERT_BASE64:-}) MASTER_KEY: $(yaml-quote ${MASTER_KEY_BASE64:-}) @@ -84,6 +85,7 @@ EXTRA_DOCKER_OPTS=$(yaml-quote ${EXTRA_DOCKER_OPTS}) ENABLE_DOCKER_REGISTRY_CACHE=$(yaml-quote ${ENABLE_DOCKER_REGISTRY_CACHE:-false}) PROJECT_ID=$(yaml-quote ${PROJECT}) KUBERNETES_CONTAINER_RUNTIME=$(yaml-quote ${CONTAINER_RUNTIME}) +RKT_VERSION=$(yaml-quote ${RKT_VERSION}) CA_CERT: $(yaml-quote ${CA_CERT_BASE64}) KUBELET_CERT: $(yaml-quote ${KUBELET_CERT_BASE64:-}) KUBELET_KEY: $(yaml-quote ${KUBELET_KEY_BASE64:-}) diff --git a/cluster/gce/coreos/node.yaml b/cluster/gce/coreos/node.yaml index f81ddee832e..955f990d527 100644 --- a/cluster/gce/coreos/node.yaml +++ b/cluster/gce/coreos/node.yaml @@ -93,9 +93,9 @@ coreos: EnvironmentFile=/etc/kube-env ExecStartPre=/usr/bin/mkdir -p /opt/rkt ExecStartPre=/usr/bin/wget \ - -O /opt/rkt/rkt-v0.5.4.tar.gz \ - https://github.com/coreos/rkt/releases/download/v0.5.4/rkt-v0.5.4.tar.gz - ExecStart=/usr/bin/tar xzvf /opt/rkt/rkt-v0.5.4.tar.gz -C /opt --overwrite + -O /opt/rkt/rkt-v${RKT_VERSION}.tar.gz \ + https://github.com/coreos/rkt/releases/download/v${RKT_VERSION}/rkt-v${RKT_VERSION}.tar.gz + ExecStart=/usr/bin/tar xzvf /opt/rkt/rkt-v${RKT_VERSION}.tar.gz -C /opt --overwrite - name: kubernetes-install-minion.service command: start @@ -199,5 +199,5 @@ coreos: Requires=kubernetes-install-rkt.service After=kubernetes-install-rkt.service [Service] - # TODO(yifan): Make version configuable. - ExecStart=/opt/rkt-v0.5.4/rkt metadata-service + EnvironmentFile=/etc/kube-env + ExecStart=/opt/rkt-v${RKT_VERSION}/rkt metadata-service