diff --git a/pkg/kubelet/container/helpers.go b/pkg/kubelet/container/helpers.go index 31a4d08dd7..dbce6d08ce 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 340838e89e..9954370591 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 4691bbaa28..7baae160f6 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 7e6713a95a..8b347f5ff2 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 690e1adfc1..37d8f8f77f 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 b39c8b8233..08cc67d5c3 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", },