mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 18:54:06 +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=${KUBE_GCE_MINION_IMAGE:-container-vm-v20150505}
|
||||||
MINION_IMAGE_PROJECT=${KUBE_GCE_MINION_PROJECT:-google-containers}
|
MINION_IMAGE_PROJECT=${KUBE_GCE_MINION_PROJECT:-google-containers}
|
||||||
CONTAINER_RUNTIME=${KUBE_CONTAINER_RUNTIME:-docker}
|
CONTAINER_RUNTIME=${KUBE_CONTAINER_RUNTIME:-docker}
|
||||||
|
RKT_VERSION=${KUBE_RKT_VERSION:-0.5.5}
|
||||||
|
|
||||||
NETWORK=${KUBE_GCE_NETWORK:-default}
|
NETWORK=${KUBE_GCE_NETWORK:-default}
|
||||||
INSTANCE_PREFIX="${KUBE_GCE_INSTANCE_PREFIX:-kubernetes}"
|
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=${KUBE_GCE_MINION_IMAGE:-container-vm-v20150505}
|
||||||
MINION_IMAGE_PROJECT=${KUBE_GCE_MINION_PROJECT:-google-containers}
|
MINION_IMAGE_PROJECT=${KUBE_GCE_MINION_PROJECT:-google-containers}
|
||||||
CONTAINER_RUNTIME=${KUBE_CONTAINER_RUNTIME:-docker}
|
CONTAINER_RUNTIME=${KUBE_CONTAINER_RUNTIME:-docker}
|
||||||
|
RKT_VERSION=${KUBE_RKT_VERSION:-0.5.5}
|
||||||
|
|
||||||
NETWORK=${KUBE_GCE_NETWORK:-e2e}
|
NETWORK=${KUBE_GCE_NETWORK:-e2e}
|
||||||
INSTANCE_PREFIX="${KUBE_GCE_INSTANCE_PREFIX:-e2e-test-${USER}}"
|
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:-})
|
ADMISSION_CONTROL: $(yaml-quote ${ADMISSION_CONTROL:-})
|
||||||
MASTER_IP_RANGE: $(yaml-quote ${MASTER_IP_RANGE})
|
MASTER_IP_RANGE: $(yaml-quote ${MASTER_IP_RANGE})
|
||||||
KUBERNETES_CONTAINER_RUNTIME: $(yaml-quote ${CONTAINER_RUNTIME})
|
KUBERNETES_CONTAINER_RUNTIME: $(yaml-quote ${CONTAINER_RUNTIME})
|
||||||
|
RKT_VERSION: $(yaml-quote ${RKT_VERSION})
|
||||||
CA_CERT: $(yaml-quote ${CA_CERT_BASE64})
|
CA_CERT: $(yaml-quote ${CA_CERT_BASE64})
|
||||||
MASTER_CERT: $(yaml-quote ${MASTER_CERT_BASE64:-})
|
MASTER_CERT: $(yaml-quote ${MASTER_CERT_BASE64:-})
|
||||||
MASTER_KEY: $(yaml-quote ${MASTER_KEY_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})
|
ENABLE_DOCKER_REGISTRY_CACHE=$(yaml-quote ${ENABLE_DOCKER_REGISTRY_CACHE:-false})
|
||||||
PROJECT_ID=$(yaml-quote ${PROJECT})
|
PROJECT_ID=$(yaml-quote ${PROJECT})
|
||||||
KUBERNETES_CONTAINER_RUNTIME=$(yaml-quote ${CONTAINER_RUNTIME})
|
KUBERNETES_CONTAINER_RUNTIME=$(yaml-quote ${CONTAINER_RUNTIME})
|
||||||
|
RKT_VERSION=$(yaml-quote ${RKT_VERSION})
|
||||||
CA_CERT: $(yaml-quote ${CA_CERT_BASE64})
|
CA_CERT: $(yaml-quote ${CA_CERT_BASE64})
|
||||||
KUBELET_CERT: $(yaml-quote ${KUBELET_CERT_BASE64:-})
|
KUBELET_CERT: $(yaml-quote ${KUBELET_CERT_BASE64:-})
|
||||||
KUBELET_KEY: $(yaml-quote ${KUBELET_KEY_BASE64:-})
|
KUBELET_KEY: $(yaml-quote ${KUBELET_KEY_BASE64:-})
|
||||||
|
@ -93,9 +93,9 @@ coreos:
|
|||||||
EnvironmentFile=/etc/kube-env
|
EnvironmentFile=/etc/kube-env
|
||||||
ExecStartPre=/usr/bin/mkdir -p /opt/rkt
|
ExecStartPre=/usr/bin/mkdir -p /opt/rkt
|
||||||
ExecStartPre=/usr/bin/wget \
|
ExecStartPre=/usr/bin/wget \
|
||||||
-O /opt/rkt/rkt-v0.5.4.tar.gz \
|
-O /opt/rkt/rkt-v${RKT_VERSION}.tar.gz \
|
||||||
https://github.com/coreos/rkt/releases/download/v0.5.4/rkt-v0.5.4.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-v0.5.4.tar.gz -C /opt --overwrite
|
ExecStart=/usr/bin/tar xzvf /opt/rkt/rkt-v${RKT_VERSION}.tar.gz -C /opt --overwrite
|
||||||
|
|
||||||
- name: kubernetes-install-minion.service
|
- name: kubernetes-install-minion.service
|
||||||
command: start
|
command: start
|
||||||
@ -199,5 +199,5 @@ coreos:
|
|||||||
Requires=kubernetes-install-rkt.service
|
Requires=kubernetes-install-rkt.service
|
||||||
After=kubernetes-install-rkt.service
|
After=kubernetes-install-rkt.service
|
||||||
[Service]
|
[Service]
|
||||||
# TODO(yifan): Make version configuable.
|
EnvironmentFile=/etc/kube-env
|
||||||
ExecStart=/opt/rkt-v0.5.4/rkt metadata-service
|
ExecStart=/opt/rkt-v${RKT_VERSION}/rkt metadata-service
|
||||||
|
@ -17,9 +17,11 @@ limitations under the License.
|
|||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"hash/adler32"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -80,3 +82,11 @@ func ShouldContainerBeRestarted(container *api.Container, pod *api.Pod, podStatu
|
|||||||
}
|
}
|
||||||
return true
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/adler32"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -26,6 +25,7 @@ import (
|
|||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/credentialprovider"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/credentialprovider"
|
||||||
|
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/leaky"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/leaky"
|
||||||
kubeletTypes "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/types"
|
kubeletTypes "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/types"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
||||||
@ -212,15 +212,9 @@ func (c DockerContainers) FindPodContainer(podFullName string, uid types.UID, co
|
|||||||
|
|
||||||
const containerNamePrefix = "k8s"
|
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.
|
// Creates a name which can be reversed to identify both full pod name and container name.
|
||||||
func BuildDockerName(dockerName KubeletContainerName, container *api.Container) string {
|
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",
|
return fmt.Sprintf("%s_%s_%s_%s_%08x",
|
||||||
containerNamePrefix,
|
containerNamePrefix,
|
||||||
containerName,
|
containerName,
|
||||||
|
@ -824,7 +824,7 @@ func (dm *DockerManager) podInfraContainerChanged(pod *api.Pod, podInfraContaine
|
|||||||
Image: dm.PodInfraContainerImage,
|
Image: dm.PodInfraContainerImage,
|
||||||
Ports: ports,
|
Ports: ports,
|
||||||
}
|
}
|
||||||
return podInfraContainer.Hash != HashContainer(expectedPodInfraContainer), nil
|
return podInfraContainer.Hash != kubecontainer.HashContainer(expectedPodInfraContainer), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type dockerVersion docker.APIVersion
|
type dockerVersion docker.APIVersion
|
||||||
@ -1355,7 +1355,7 @@ func (dm *DockerManager) computePodContainerChanges(pod *api.Pod, runningPod kub
|
|||||||
}
|
}
|
||||||
|
|
||||||
for index, container := range pod.Spec.Containers {
|
for index, container := range pod.Spec.Containers {
|
||||||
expectedHash := HashContainer(&container)
|
expectedHash := kubecontainer.HashContainer(&container)
|
||||||
|
|
||||||
c := runningPod.FindContainerByName(container.Name)
|
c := runningPod.FindContainerByName(container.Name)
|
||||||
if c == nil {
|
if c == nil {
|
||||||
|
@ -365,7 +365,7 @@ func generatePodInfraContainerHash(pod *api.Pod) uint64 {
|
|||||||
Image: dockertools.PodInfraContainerImage,
|
Image: dockertools.PodInfraContainerImage,
|
||||||
Ports: ports,
|
Ports: ports,
|
||||||
}
|
}
|
||||||
return dockertools.HashContainer(container)
|
return kubecontainer.HashContainer(container)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSyncPodsDoesNothing(t *testing.T) {
|
func TestSyncPodsDoesNothing(t *testing.T) {
|
||||||
@ -395,7 +395,7 @@ func TestSyncPodsDoesNothing(t *testing.T) {
|
|||||||
fakeDocker.ContainerList = []docker.APIContainers{
|
fakeDocker.ContainerList = []docker.APIContainers{
|
||||||
{
|
{
|
||||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>_<random>
|
// 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",
|
ID: "1234",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -3895,12 +3895,12 @@ func TestSyncPodsWithRestartPolicy(t *testing.T) {
|
|||||||
exitedAPIContainers := []docker.APIContainers{
|
exitedAPIContainers := []docker.APIContainers{
|
||||||
{
|
{
|
||||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>
|
// 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",
|
ID: "1234",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>
|
// 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",
|
ID: "5678",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -4037,12 +4037,12 @@ func TestGetPodStatusWithLastTermination(t *testing.T) {
|
|||||||
exitedAPIContainers := []docker.APIContainers{
|
exitedAPIContainers := []docker.APIContainers{
|
||||||
{
|
{
|
||||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>
|
// 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",
|
ID: "1234",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>
|
// 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",
|
ID: "5678",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -4319,7 +4319,7 @@ func TestGetRestartCount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>
|
// 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()
|
currTime := time.Now()
|
||||||
containerMap := map[string]*docker.Container{
|
containerMap := map[string]*docker.Container{
|
||||||
"1234": {
|
"1234": {
|
||||||
|
@ -19,7 +19,6 @@ package rkt
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/adler32"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@ -38,7 +37,6 @@ import (
|
|||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/securitycontext"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/securitycontext"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
||||||
appcschema "github.com/appc/spec/schema"
|
appcschema "github.com/appc/spec/schema"
|
||||||
appctypes "github.com/appc/spec/schema/types"
|
appctypes "github.com/appc/spec/schema/types"
|
||||||
"github.com/coreos/go-systemd/dbus"
|
"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}
|
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.
|
// TODO(yifan): Remove the receiver once we can solve the appName->imageID problem.
|
||||||
func (r *runtime) apiPodToruntimePod(uuid string, pod *api.Pod) *kubecontainer.Pod {
|
func (r *runtime) apiPodToruntimePod(uuid string, pod *api.Pod) *kubecontainer.Pod {
|
||||||
p := &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})),
|
ID: types.UID(buildContainerID(&containerID{uuid, c.Name, img.id})),
|
||||||
Name: c.Name,
|
Name: c.Name,
|
||||||
Image: c.Image,
|
Image: c.Image,
|
||||||
Hash: hashContainer(c),
|
Hash: kubecontainer.HashContainer(c),
|
||||||
Created: time.Now().Unix(),
|
Created: time.Now().Unix(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -847,7 +837,7 @@ func (r *runtime) SyncPod(pod *api.Pod, runningPod kubecontainer.Pod, podStatus
|
|||||||
|
|
||||||
restartPod := false
|
restartPod := false
|
||||||
for _, container := range pod.Spec.Containers {
|
for _, container := range pod.Spec.Containers {
|
||||||
expectedHash := hashContainer(&container)
|
expectedHash := kubecontainer.HashContainer(&container)
|
||||||
|
|
||||||
c := runningPod.FindContainerByName(container.Name)
|
c := runningPod.FindContainerByName(container.Name)
|
||||||
if c == nil {
|
if c == nil {
|
||||||
|
@ -96,7 +96,7 @@ func TestRunOnce(t *testing.T) {
|
|||||||
}
|
}
|
||||||
podContainers := []docker.APIContainers{
|
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",
|
ID: "1234",
|
||||||
Status: "running",
|
Status: "running",
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user