mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Merge pull request #8575 from yifan-gu/kube_refactor
Minor kubelet/rkt refactors
This commit is contained in:
commit
017fb6a818
@ -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}"
|
||||
|
@ -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}}"
|
||||
|
@ -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:-})
|
||||
|
@ -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
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -365,7 +365,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) {
|
||||
@ -395,7 +395,7 @@ func TestSyncPodsDoesNothing(t *testing.T) {
|
||||
fakeDocker.ContainerList = []docker.APIContainers{
|
||||
{
|
||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>_<random>
|
||||
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",
|
||||
},
|
||||
{
|
||||
@ -3895,12 +3895,12 @@ func TestSyncPodsWithRestartPolicy(t *testing.T) {
|
||||
exitedAPIContainers := []docker.APIContainers{
|
||||
{
|
||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>
|
||||
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_<container-id>_<pod-fullname>_<pod-uid>
|
||||
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",
|
||||
},
|
||||
}
|
||||
@ -4037,12 +4037,12 @@ func TestGetPodStatusWithLastTermination(t *testing.T) {
|
||||
exitedAPIContainers := []docker.APIContainers{
|
||||
{
|
||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>
|
||||
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_<container-id>_<pod-fullname>_<pod-uid>
|
||||
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",
|
||||
},
|
||||
}
|
||||
@ -4319,7 +4319,7 @@ func TestGetRestartCount(t *testing.T) {
|
||||
}
|
||||
|
||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>
|
||||
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": {
|
||||
|
@ -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 {
|
||||
|
@ -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",
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user