mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
kubelet: Move pod name helpers to pkg/kubelet/container/runtime.go
This commit is contained in:
parent
13250c904f
commit
31bb11ac2a
@ -25,6 +25,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/config"
|
||||
utilerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
|
||||
@ -206,7 +207,7 @@ func (s *podStorage) merge(source string, change interface{}) (adds, updates, de
|
||||
|
||||
filtered := filterInvalidPods(update.Pods, source, s.recorder)
|
||||
for _, ref := range filtered {
|
||||
name := kubelet.GetPodFullName(ref)
|
||||
name := kubecontainer.GetPodFullName(ref)
|
||||
if existing, found := pods[name]; found {
|
||||
if !reflect.DeepEqual(existing.Spec, ref.Spec) {
|
||||
// this is an update
|
||||
@ -229,7 +230,7 @@ func (s *podStorage) merge(source string, change interface{}) (adds, updates, de
|
||||
case kubelet.REMOVE:
|
||||
glog.V(4).Infof("Removing a pod %v", update)
|
||||
for _, value := range update.Pods {
|
||||
name := kubelet.GetPodFullName(&value)
|
||||
name := kubecontainer.GetPodFullName(&value)
|
||||
if existing, found := pods[name]; found {
|
||||
// this is a delete
|
||||
delete(pods, name)
|
||||
@ -248,7 +249,7 @@ func (s *podStorage) merge(source string, change interface{}) (adds, updates, de
|
||||
|
||||
filtered := filterInvalidPods(update.Pods, source, s.recorder)
|
||||
for _, ref := range filtered {
|
||||
name := kubelet.GetPodFullName(ref)
|
||||
name := kubecontainer.GetPodFullName(ref)
|
||||
if existing, found := oldPods[name]; found {
|
||||
pods[name] = existing
|
||||
if !reflect.DeepEqual(existing.Spec, ref.Spec) {
|
||||
@ -306,7 +307,7 @@ func filterInvalidPods(pods []api.Pod, source string, recorder record.EventRecor
|
||||
// If validation fails, don't trust it any further -
|
||||
// even Name could be bad.
|
||||
} else {
|
||||
name := kubelet.GetPodFullName(pod)
|
||||
name := kubecontainer.GetPodFullName(pod)
|
||||
if names.Has(name) {
|
||||
errlist = append(errlist, fielderrors.NewFieldDuplicate("name", pod.Name))
|
||||
} else {
|
||||
|
@ -17,6 +17,9 @@ limitations under the License.
|
||||
package container
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
|
||||
@ -111,3 +114,24 @@ func (p *Pod) FindContainerByName(containerName string) *Container {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPodFullName returns a name that uniquely identifies a pod.
|
||||
func GetPodFullName(pod *api.Pod) string {
|
||||
// Use underscore as the delimiter because it is not allowed in pod name
|
||||
// (DNS subdomain format), while allowed in the container name format.
|
||||
return fmt.Sprintf("%s_%s", pod.Name, pod.Namespace)
|
||||
}
|
||||
|
||||
// Build the pod full name from pod name and namespace.
|
||||
func BuildPodFullName(name, namespace string) string {
|
||||
return name + "_" + namespace
|
||||
}
|
||||
|
||||
// Parse the pod full name.
|
||||
func ParsePodFullName(podFullName string) (string, string, error) {
|
||||
parts := strings.Split(podFullName, "_")
|
||||
if len(parts) != 2 {
|
||||
return "", "", fmt.Errorf("failed to parse the pod full name %q", podFullName)
|
||||
}
|
||||
return parts[0], parts[1], nil
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ import (
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/credentialprovider"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/leaky"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
@ -818,18 +818,9 @@ type ContainerCommandRunner interface {
|
||||
PortForward(podInfraContainerID string, port uint16, stream io.ReadWriteCloser) error
|
||||
}
|
||||
|
||||
// Parse the pod full name. TODO(yifan): This is duplicated with kubelet.ParsePodFullName.
|
||||
func parsePodFullName(podFullName string) (string, string, error) {
|
||||
parts := strings.Split(podFullName, "_")
|
||||
if len(parts) != 2 {
|
||||
return "", "", fmt.Errorf("failed to parse the pod full name %q", podFullName)
|
||||
}
|
||||
return parts[0], parts[1], nil
|
||||
}
|
||||
|
||||
func GetPods(client DockerInterface, all bool) ([]*container.Pod, error) {
|
||||
pods := make(map[types.UID]*container.Pod)
|
||||
var result []*container.Pod
|
||||
func GetPods(client DockerInterface, all bool) ([]*kubecontainer.Pod, error) {
|
||||
pods := make(map[types.UID]*kubecontainer.Pod)
|
||||
var result []*kubecontainer.Pod
|
||||
|
||||
containers, err := GetKubeletDockerContainers(client, all)
|
||||
if err != nil {
|
||||
@ -849,19 +840,19 @@ func GetPods(client DockerInterface, all bool) ([]*container.Pod, error) {
|
||||
}
|
||||
pod, found := pods[dockerName.PodUID]
|
||||
if !found {
|
||||
name, namespace, err := parsePodFullName(dockerName.PodFullName)
|
||||
name, namespace, err := kubecontainer.ParsePodFullName(dockerName.PodFullName)
|
||||
if err != nil {
|
||||
glog.Warningf("Parse pod full name %q error: %v", dockerName.PodFullName, err)
|
||||
continue
|
||||
}
|
||||
pod = &container.Pod{
|
||||
pod = &kubecontainer.Pod{
|
||||
ID: dockerName.PodUID,
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
}
|
||||
pods[dockerName.PodUID] = pod
|
||||
}
|
||||
pod.Containers = append(pod.Containers, &container.Container{
|
||||
pod.Containers = append(pod.Containers, &kubecontainer.Container{
|
||||
ID: types.UID(c.ID),
|
||||
Name: dockerName.ContainerName,
|
||||
Hash: hash,
|
||||
|
@ -39,7 +39,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/cadvisor"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/envvars"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/metrics"
|
||||
@ -746,7 +746,7 @@ func (kl *Kubelet) runContainer(pod *api.Pod, container *api.Container, podVolum
|
||||
containerHostname = containerHostname[:hostnameMaxLen]
|
||||
}
|
||||
opts := docker.CreateContainerOptions{
|
||||
Name: dockertools.BuildDockerName(dockertools.KubeletContainerName{GetPodFullName(pod), pod.UID, container.Name}, container),
|
||||
Name: dockertools.BuildDockerName(dockertools.KubeletContainerName{kubecontainer.GetPodFullName(pod), pod.UID, container.Name}, container),
|
||||
Config: &docker.Config{
|
||||
Cmd: container.Command,
|
||||
Env: envVariables,
|
||||
@ -823,7 +823,7 @@ func (kl *Kubelet) runContainer(pod *api.Pod, container *api.Container, podVolum
|
||||
}
|
||||
|
||||
if container.Lifecycle != nil && container.Lifecycle.PostStart != nil {
|
||||
handlerErr := kl.runHandler(GetPodFullName(pod), pod.UID, container, container.Lifecycle.PostStart)
|
||||
handlerErr := kl.runHandler(kubecontainer.GetPodFullName(pod), pod.UID, container, container.Lifecycle.PostStart)
|
||||
if handlerErr != nil {
|
||||
kl.killContainerByID(dockerContainer.ID)
|
||||
return dockertools.DockerID(""), fmt.Errorf("failed to call event handler: %v", handlerErr)
|
||||
@ -978,7 +978,7 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string,
|
||||
}
|
||||
|
||||
// Kill a docker container
|
||||
func (kl *Kubelet) killContainer(c *container.Container) error {
|
||||
func (kl *Kubelet) killContainer(c *kubecontainer.Container) error {
|
||||
return kl.killContainerByID(string(c.ID))
|
||||
}
|
||||
|
||||
@ -1073,8 +1073,8 @@ func (kl *Kubelet) pullImage(img string, ref *api.ObjectReference) error {
|
||||
}
|
||||
|
||||
// Kill all containers in a pod. Returns the number of containers deleted and an error if one occurs.
|
||||
func (kl *Kubelet) killContainersInPod(pod *api.Pod, runningPod container.Pod) (int, error) {
|
||||
podFullName := GetPodFullName(pod)
|
||||
func (kl *Kubelet) killContainersInPod(pod *api.Pod, runningPod kubecontainer.Pod) (int, error) {
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
|
||||
count := 0
|
||||
errs := make(chan error, len(pod.Spec.Containers))
|
||||
@ -1126,7 +1126,7 @@ func (kl *Kubelet) makePodDataDirs(pod *api.Pod) error {
|
||||
}
|
||||
|
||||
func (kl *Kubelet) shouldContainerBeRestarted(container *api.Container, pod *api.Pod) bool {
|
||||
podFullName := GetPodFullName(pod)
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
// Check RestartPolicy for dead container
|
||||
recentContainers, err := dockertools.GetRecentDockerContainersWithNameAndUUID(kl.dockerClient, podFullName, pod.UID, container.Name)
|
||||
if err != nil {
|
||||
@ -1170,7 +1170,7 @@ func (kl *Kubelet) getPodInfraContainer(podFullName string, uid types.UID,
|
||||
// if it was successful, and a non-nil error otherwise.
|
||||
func (kl *Kubelet) pullImageAndRunContainer(pod *api.Pod, container *api.Container, podVolumes *volumeMap,
|
||||
podInfraContainerID dockertools.DockerID) (dockertools.DockerID, error) {
|
||||
podFullName := GetPodFullName(pod)
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
ref, err := containerRef(pod, container)
|
||||
if err != nil {
|
||||
glog.Errorf("Couldn't make a ref to pod %v, container %v: '%v'", pod.Name, container.Name, err)
|
||||
@ -1219,8 +1219,8 @@ type podContainerChangesSpec struct {
|
||||
containersToKeep map[dockertools.DockerID]int
|
||||
}
|
||||
|
||||
func (kl *Kubelet) computePodContainerChanges(pod *api.Pod, hasMirrorPod bool, runningPod container.Pod) (podContainerChangesSpec, error) {
|
||||
podFullName := GetPodFullName(pod)
|
||||
func (kl *Kubelet) computePodContainerChanges(pod *api.Pod, hasMirrorPod bool, runningPod kubecontainer.Pod) (podContainerChangesSpec, error) {
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
uid := pod.UID
|
||||
glog.V(4).Infof("Syncing Pod %+v, podFullName: %q, uid: %q", pod, podFullName, uid)
|
||||
|
||||
@ -1329,8 +1329,8 @@ func (kl *Kubelet) computePodContainerChanges(pod *api.Pod, hasMirrorPod bool, r
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (kl *Kubelet) syncPod(pod *api.Pod, hasMirrorPod bool, runningPod container.Pod) error {
|
||||
podFullName := GetPodFullName(pod)
|
||||
func (kl *Kubelet) syncPod(pod *api.Pod, hasMirrorPod bool, runningPod kubecontainer.Pod) error {
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
uid := pod.UID
|
||||
|
||||
// Before returning, regenerate status and store it in the cache.
|
||||
@ -1511,7 +1511,7 @@ func (kl *Kubelet) SyncPods(allPods []api.Pod, podSyncTypes map[types.UID]metric
|
||||
// Remove obsolete entries in podStatus where the pod is no longer considered bound to this node.
|
||||
podFullNames := make(map[string]bool)
|
||||
for _, pod := range allPods {
|
||||
podFullNames[GetPodFullName(&pod)] = true
|
||||
podFullNames[kubecontainer.GetPodFullName(&pod)] = true
|
||||
}
|
||||
kl.statusManager.RemoveOrphanedStatuses(podFullNames)
|
||||
|
||||
@ -1519,7 +1519,7 @@ func (kl *Kubelet) SyncPods(allPods []api.Pod, podSyncTypes map[types.UID]metric
|
||||
kl.handleNotFittingPods(allPods)
|
||||
var pods []api.Pod
|
||||
for _, pod := range allPods {
|
||||
status, ok := kl.statusManager.GetPodStatus(GetPodFullName(&pod))
|
||||
status, ok := kl.statusManager.GetPodStatus(kubecontainer.GetPodFullName(&pod))
|
||||
if ok && status.Phase == api.PodFailed {
|
||||
continue
|
||||
}
|
||||
@ -1539,6 +1539,7 @@ func (kl *Kubelet) SyncPods(allPods []api.Pod, podSyncTypes map[types.UID]metric
|
||||
// Check for any containers that need starting
|
||||
for ix := range pods {
|
||||
pod := &pods[ix]
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
uid := pod.UID
|
||||
desiredPods[uid] = empty{}
|
||||
|
||||
@ -1639,7 +1640,7 @@ func checkHostPortConflicts(pods []api.Pod) (fitting []api.Pod, notFitting []api
|
||||
for i := range pods {
|
||||
pod := &pods[i]
|
||||
if errs := validation.AccumulateUniquePorts(pod.Spec.Containers, ports, extract); len(errs) != 0 {
|
||||
glog.Errorf("Pod %q: HostPort is already allocated, ignoring: %v", GetPodFullName(pod), errs)
|
||||
glog.Errorf("Pod %q: HostPort is already allocated, ignoring: %v", kubecontainer.GetPodFullName(pod), errs)
|
||||
notFitting = append(notFitting, *pod)
|
||||
continue
|
||||
}
|
||||
@ -1686,21 +1687,21 @@ func (kl *Kubelet) handleNotFittingPods(pods []api.Pod) {
|
||||
fitting, notFitting := checkHostPortConflicts(pods)
|
||||
for _, pod := range notFitting {
|
||||
kl.recorder.Eventf(&pod, "hostPortConflict", "Cannot start the pod due to host port conflict.")
|
||||
kl.statusManager.SetPodStatus(GetPodFullName(&pod), api.PodStatus{
|
||||
kl.statusManager.SetPodStatus(kubecontainer.GetPodFullName(&pod), api.PodStatus{
|
||||
Phase: api.PodFailed,
|
||||
Message: "Pod cannot be started due to host port conflict"})
|
||||
}
|
||||
fitting, notFitting = kl.checkNodeSelectorMatching(fitting)
|
||||
for _, pod := range notFitting {
|
||||
kl.recorder.Eventf(&pod, "nodeSelectorMismatching", "Cannot start the pod due to node selector mismatch.")
|
||||
kl.statusManager.SetPodStatus(GetPodFullName(&pod), api.PodStatus{
|
||||
kl.statusManager.SetPodStatus(kubecontainer.GetPodFullName(&pod), api.PodStatus{
|
||||
Phase: api.PodFailed,
|
||||
Message: "Pod cannot be started due to node selector mismatch"})
|
||||
}
|
||||
fitting, notFitting = kl.checkCapacityExceeded(fitting)
|
||||
for _, pod := range notFitting {
|
||||
kl.recorder.Eventf(&pod, "capacityExceeded", "Cannot start the pod due to exceeded capacity.")
|
||||
kl.statusManager.SetPodStatus(GetPodFullName(&pod), api.PodStatus{
|
||||
kl.statusManager.SetPodStatus(kubecontainer.GetPodFullName(&pod), api.PodStatus{
|
||||
Phase: api.PodFailed,
|
||||
Message: "Pod cannot be started due to exceeded capacity"})
|
||||
}
|
||||
@ -1982,7 +1983,7 @@ func (kl *Kubelet) generatePodStatus(podFullName string) (api.PodStatus, error)
|
||||
// By passing the pod directly, this method avoids pod lookup, which requires
|
||||
// grabbing a lock.
|
||||
func (kl *Kubelet) generatePodStatusByPod(pod *api.Pod) (api.PodStatus, error) {
|
||||
podFullName := GetPodFullName(pod)
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
glog.V(3).Infof("Generating status for %q", podFullName)
|
||||
|
||||
spec := &pod.Spec
|
||||
|
@ -39,6 +39,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/cadvisor"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/metrics"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/network"
|
||||
@ -341,7 +342,7 @@ func dockerContainersToPod(containers dockertools.DockerContainers) container.Po
|
||||
})
|
||||
// TODO(yifan): Only one evaluation is enough.
|
||||
pod.ID = dockerName.PodUID
|
||||
name, namespace, _ := ParsePodFullName(dockerName.PodFullName)
|
||||
name, namespace, _ := kubecontainer.ParsePodFullName(dockerName.PodFullName)
|
||||
pod.Name = name
|
||||
pod.Namespace = namespace
|
||||
}
|
||||
@ -1536,7 +1537,7 @@ func TestRunInContainerNoSuchPod(t *testing.T) {
|
||||
podNamespace := "nsFoo"
|
||||
containerName := "containerFoo"
|
||||
output, err := kubelet.RunInContainer(
|
||||
GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{Name: podName, Namespace: podNamespace}}),
|
||||
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{Name: podName, Namespace: podNamespace}}),
|
||||
"",
|
||||
containerName,
|
||||
[]string{"ls"})
|
||||
@ -1569,7 +1570,7 @@ func TestRunInContainer(t *testing.T) {
|
||||
|
||||
cmd := []string{"ls"}
|
||||
_, err := kubelet.RunInContainer(
|
||||
GetPodFullName(&api.Pod{
|
||||
kubecontainer.GetPodFullName(&api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
UID: "12345678",
|
||||
Name: podName,
|
||||
@ -2603,7 +2604,7 @@ func TestExecInContainerNoSuchPod(t *testing.T) {
|
||||
podNamespace := "nsFoo"
|
||||
containerName := "containerFoo"
|
||||
err := kubelet.ExecInContainer(
|
||||
GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{Name: podName, Namespace: podNamespace}}),
|
||||
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{Name: podName, Namespace: podNamespace}}),
|
||||
"",
|
||||
containerName,
|
||||
[]string{"ls"},
|
||||
@ -2639,7 +2640,7 @@ func TestExecInContainerNoSuchContainer(t *testing.T) {
|
||||
}
|
||||
|
||||
err := kubelet.ExecInContainer(
|
||||
GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
|
||||
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
|
||||
UID: "12345678",
|
||||
Name: podName,
|
||||
Namespace: podNamespace,
|
||||
@ -2698,7 +2699,7 @@ func TestExecInContainer(t *testing.T) {
|
||||
}
|
||||
|
||||
err := kubelet.ExecInContainer(
|
||||
GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
|
||||
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
|
||||
UID: "12345678",
|
||||
Name: podName,
|
||||
Namespace: podNamespace,
|
||||
@ -2747,7 +2748,7 @@ func TestPortForwardNoSuchPod(t *testing.T) {
|
||||
var port uint16 = 5000
|
||||
|
||||
err := kubelet.PortForward(
|
||||
GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{Name: podName, Namespace: podNamespace}}),
|
||||
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{Name: podName, Namespace: podNamespace}}),
|
||||
"",
|
||||
port,
|
||||
nil,
|
||||
@ -2779,7 +2780,7 @@ func TestPortForwardNoSuchContainer(t *testing.T) {
|
||||
}
|
||||
|
||||
err := kubelet.PortForward(
|
||||
GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
|
||||
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
|
||||
UID: "12345678",
|
||||
Name: podName,
|
||||
Namespace: podNamespace,
|
||||
@ -2824,7 +2825,7 @@ func TestPortForward(t *testing.T) {
|
||||
}
|
||||
|
||||
err := kubelet.PortForward(
|
||||
GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
|
||||
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
|
||||
UID: "12345678",
|
||||
Name: podName,
|
||||
Namespace: podNamespace,
|
||||
@ -2900,7 +2901,7 @@ func TestHandlePortConflicts(t *testing.T) {
|
||||
pods[1].CreationTimestamp = util.NewTime(time.Now())
|
||||
pods[0].CreationTimestamp = util.NewTime(time.Now().Add(1 * time.Second))
|
||||
// The newer pod should be rejected.
|
||||
conflictedPodName := GetPodFullName(&pods[0])
|
||||
conflictedPodName := kubecontainer.GetPodFullName(&pods[0])
|
||||
|
||||
kl.handleNotFittingPods(pods)
|
||||
// Check pod status stored in the status map.
|
||||
@ -2950,7 +2951,7 @@ func TestHandleNodeSelector(t *testing.T) {
|
||||
},
|
||||
}
|
||||
// The first pod should be rejected.
|
||||
notfittingPodName := GetPodFullName(&pods[0])
|
||||
notfittingPodName := kubecontainer.GetPodFullName(&pods[0])
|
||||
|
||||
kl.handleNotFittingPods(pods)
|
||||
// Check pod status stored in the status map.
|
||||
@ -3006,7 +3007,7 @@ func TestHandleMemExceeded(t *testing.T) {
|
||||
pods[1].CreationTimestamp = util.NewTime(time.Now())
|
||||
pods[0].CreationTimestamp = util.NewTime(time.Now().Add(1 * time.Second))
|
||||
// The newer pod should be rejected.
|
||||
notfittingPodName := GetPodFullName(&pods[0])
|
||||
notfittingPodName := kubecontainer.GetPodFullName(&pods[0])
|
||||
|
||||
kl.handleNotFittingPods(pods)
|
||||
// Check pod status stored in the status map.
|
||||
@ -3041,12 +3042,12 @@ func TestPurgingObsoleteStatusMapEntries(t *testing.T) {
|
||||
}
|
||||
// Run once to populate the status map.
|
||||
kl.handleNotFittingPods(pods)
|
||||
if _, err := kl.GetPodStatus(BuildPodFullName("pod2", "")); err != nil {
|
||||
if _, err := kl.GetPodStatus(kubecontainer.BuildPodFullName("pod2", "")); err != nil {
|
||||
t.Fatalf("expected to have status cached for %q: %v", "pod2", err)
|
||||
}
|
||||
// Sync with empty pods so that the entry in status map will be removed.
|
||||
kl.SyncPods([]api.Pod{}, emptyPodUIDs, map[string]*api.Pod{}, time.Now())
|
||||
if _, err := kl.GetPodStatus(BuildPodFullName("pod2", "")); err == nil {
|
||||
if _, err := kl.GetPodStatus(kubecontainer.BuildPodFullName("pod2", "")); err == nil {
|
||||
t.Fatalf("expected to not have status cached for %q: %v", "pod2", err)
|
||||
}
|
||||
}
|
||||
@ -3291,7 +3292,7 @@ func TestCreateMirrorPod(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
podFullName := GetPodFullName(&pod)
|
||||
podFullName := kubecontainer.GetPodFullName(&pod)
|
||||
if !manager.HasPod(podFullName) {
|
||||
t.Errorf("expected mirror pod %q to be created", podFullName)
|
||||
}
|
||||
@ -3341,7 +3342,7 @@ func TestDeleteOrphanedMirrorPods(t *testing.T) {
|
||||
t.Errorf("expected zero mirror pods, got %v", manager.GetPods())
|
||||
}
|
||||
for _, pod := range orphanPods {
|
||||
name := GetPodFullName(&pod)
|
||||
name := kubecontainer.GetPodFullName(&pod)
|
||||
creates, deletes := manager.GetCounts(name)
|
||||
if creates != 0 || deletes != 1 {
|
||||
t.Errorf("expected 0 creation and one deletion of %q, got %d, %d", name, creates, deletes)
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
@ -59,7 +60,7 @@ func (self *basicMirrorClient) DeleteMirrorPod(podFullName string) error {
|
||||
if self.apiserverClient == nil {
|
||||
return nil
|
||||
}
|
||||
name, namespace, err := ParsePodFullName(podFullName)
|
||||
name, namespace, err := kubecontainer.ParsePodFullName(podFullName)
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to parse a pod full name %q", podFullName)
|
||||
return err
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
@ -36,7 +37,7 @@ type fakeMirrorClient struct {
|
||||
func (self *fakeMirrorClient) CreateMirrorPod(pod api.Pod, _ string) error {
|
||||
self.mirrorPodLock.Lock()
|
||||
defer self.mirrorPodLock.Unlock()
|
||||
podFullName := GetPodFullName(&pod)
|
||||
podFullName := kubecontainer.GetPodFullName(&pod)
|
||||
self.mirrorPods.Insert(podFullName)
|
||||
self.createCounts[podFullName]++
|
||||
return nil
|
||||
@ -95,7 +96,7 @@ func TestParsePodFullName(t *testing.T) {
|
||||
failedCases := []string{"barfoo", "bar_foo_foo", ""}
|
||||
|
||||
for podFullName, expected := range successfulCases {
|
||||
name, namespace, err := ParsePodFullName(podFullName)
|
||||
name, namespace, err := kubecontainer.ParsePodFullName(podFullName)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error when parsing the full name: %v", err)
|
||||
continue
|
||||
@ -106,7 +107,7 @@ func TestParsePodFullName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
for _, podFullName := range failedCases {
|
||||
_, _, err := ParsePodFullName(podFullName)
|
||||
_, _, err := kubecontainer.ParsePodFullName(podFullName)
|
||||
if err == nil {
|
||||
t.Errorf("expected error when parsing the full name, got none")
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/metrics"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
||||
"github.com/golang/glog"
|
||||
@ -139,7 +140,7 @@ func (self *basicPodManager) setPods(newPods []api.Pod) {
|
||||
|
||||
for i := range newPods {
|
||||
pod := newPods[i]
|
||||
podFullName := GetPodFullName(&pod)
|
||||
podFullName := kubecontainer.GetPodFullName(&pod)
|
||||
if isMirrorPod(&pod) {
|
||||
mirrorPodByUID[pod.UID] = &pod
|
||||
mirrorPodByFullName[podFullName] = &pod
|
||||
@ -207,7 +208,7 @@ func (self *basicPodManager) GetPodsAndMirrorMap() ([]api.Pod, map[string]*api.P
|
||||
// GetPodByName provides the (non-mirror) pod that matches namespace and name,
|
||||
// as well as whether the pod was found.
|
||||
func (self *basicPodManager) GetPodByName(namespace, name string) (*api.Pod, bool) {
|
||||
podFullName := BuildPodFullName(name, namespace)
|
||||
podFullName := kubecontainer.BuildPodFullName(name, namespace)
|
||||
return self.GetPodByFullName(podFullName)
|
||||
}
|
||||
|
||||
@ -234,7 +235,7 @@ func (self *basicPodManager) TranslatePodUID(uid types.UID) types.UID {
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
if mirrorPod, ok := self.mirrorPodByUID[uid]; ok {
|
||||
podFullName := GetPodFullName(mirrorPod)
|
||||
podFullName := kubecontainer.GetPodFullName(mirrorPod)
|
||||
if pod, ok := self.podByFullName[podFullName]; ok {
|
||||
return pod.UID
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
)
|
||||
|
||||
// Stub out mirror client for testing purpose.
|
||||
@ -78,9 +79,9 @@ func TestGetSetPods(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(&mirrorPod, actualPod) {
|
||||
t.Errorf("mirror pod is recorded incorrectly. expect: %v, got: %v", mirrorPod, actualPod)
|
||||
}
|
||||
actualPod, ok = podManager.mirrorPodByFullName[GetPodFullName(&mirrorPod)]
|
||||
actualPod, ok = podManager.mirrorPodByFullName[kubecontainer.GetPodFullName(&mirrorPod)]
|
||||
if !ok {
|
||||
t.Errorf("mirror pod %q is not found in the mirror pod map by full name", GetPodFullName(&mirrorPod))
|
||||
t.Errorf("mirror pod %q is not found in the mirror pod map by full name", kubecontainer.GetPodFullName(&mirrorPod))
|
||||
} else if !reflect.DeepEqual(&mirrorPod, actualPod) {
|
||||
t.Errorf("mirror pod is recorded incorrectly. expect: %v, got: %v", mirrorPod, actualPod)
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||
execprobe "github.com/GoogleCloudPlatform/kubernetes/pkg/probe/exec"
|
||||
httprobe "github.com/GoogleCloudPlatform/kubernetes/pkg/probe/http"
|
||||
@ -189,7 +190,7 @@ type execInContainer struct {
|
||||
|
||||
func (kl *Kubelet) newExecInContainer(pod *api.Pod, container api.Container) exec.Cmd {
|
||||
uid := pod.UID
|
||||
podFullName := GetPodFullName(pod)
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
return execInContainer{func() ([]byte, error) {
|
||||
return kl.RunInContainer(podFullName, uid, container.Name, container.LivenessProbe.Exec.Command)
|
||||
}}
|
||||
|
@ -35,6 +35,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/healthz"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/httpstream"
|
||||
@ -251,7 +252,7 @@ func (s *Server) handleContainerLogs(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
w.Header().Set("Transfer-Encoding", "chunked")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
err = s.host.GetKubeletContainerLogs(GetPodFullName(pod), containerName, tail, follow, &fw, &fw)
|
||||
err = s.host.GetKubeletContainerLogs(kubecontainer.GetPodFullName(pod), containerName, tail, follow, &fw, &fw)
|
||||
if err != nil {
|
||||
s.error(w, err)
|
||||
return
|
||||
@ -303,7 +304,7 @@ func (s *Server) handlePodStatus(w http.ResponseWriter, req *http.Request, versi
|
||||
http.Error(w, "Pod does not exist", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
status, err := s.host.GetPodStatus(GetPodFullName(pod))
|
||||
status, err := s.host.GetPodStatus(kubecontainer.GetPodFullName(pod))
|
||||
if err != nil {
|
||||
s.error(w, err)
|
||||
return
|
||||
@ -407,7 +408,7 @@ func (s *Server) handleRun(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
command := strings.Split(u.Query().Get("cmd"), " ")
|
||||
data, err := s.host.RunInContainer(GetPodFullName(pod), uid, container, command)
|
||||
data, err := s.host.RunInContainer(kubecontainer.GetPodFullName(pod), uid, container, command)
|
||||
if err != nil {
|
||||
s.error(w, err)
|
||||
return
|
||||
@ -515,7 +516,7 @@ WaitForStreams:
|
||||
stdinStream.Close()
|
||||
}
|
||||
|
||||
err = s.host.ExecInContainer(GetPodFullName(pod), uid, container, u.Query()[api.ExecCommandParamm], stdinStream, stdoutStream, stderrStream, tty)
|
||||
err = s.host.ExecInContainer(kubecontainer.GetPodFullName(pod), uid, container, u.Query()[api.ExecCommandParamm], stdinStream, stdoutStream, stderrStream, tty)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Error executing command in container: %v", err)
|
||||
glog.Error(msg)
|
||||
@ -596,7 +597,7 @@ Loop:
|
||||
case "error":
|
||||
ch := make(chan httpstream.Stream)
|
||||
dataStreamChans[port] = ch
|
||||
go waitForPortForwardDataStreamAndRun(GetPodFullName(pod), uid, stream, ch, s.host)
|
||||
go waitForPortForwardDataStreamAndRun(kubecontainer.GetPodFullName(pod), uid, stream, ch, s.host)
|
||||
case "data":
|
||||
ch, ok := dataStreamChans[port]
|
||||
if ok {
|
||||
@ -678,14 +679,14 @@ func (s *Server) serveStats(w http.ResponseWriter, req *http.Request) {
|
||||
http.Error(w, "Pod does not exist", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
stats, err = s.host.GetContainerInfo(GetPodFullName(pod), "", components[2], &query)
|
||||
stats, err = s.host.GetContainerInfo(kubecontainer.GetPodFullName(pod), "", components[2], &query)
|
||||
case 5:
|
||||
pod, ok := s.host.GetPodByName(components[1], components[2])
|
||||
if !ok {
|
||||
http.Error(w, "Pod does not exist", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
stats, err = s.host.GetContainerInfo(GetPodFullName(pod), types.UID(components[3]), components[4], &query)
|
||||
stats, err = s.host.GetContainerInfo(kubecontainer.GetPodFullName(pod), types.UID(components[3]), components[4], &query)
|
||||
default:
|
||||
http.Error(w, "unknown resource.", http.StatusNotFound)
|
||||
return
|
||||
|
@ -23,6 +23,7 @@ import (
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
@ -101,7 +102,7 @@ func (s *statusManager) SyncBatch() {
|
||||
podFullName := syncRequest.podFullName
|
||||
status := syncRequest.status
|
||||
glog.V(3).Infof("Syncing status for %s", podFullName)
|
||||
name, namespace, err := ParsePodFullName(podFullName)
|
||||
name, namespace, err := kubecontainer.ParsePodFullName(podFullName)
|
||||
if err != nil {
|
||||
glog.Warningf("Cannot parse pod full name %q: %s", podFullName, err)
|
||||
}
|
||||
|
@ -16,12 +16,7 @@ limitations under the License.
|
||||
|
||||
package kubelet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
)
|
||||
import "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
|
||||
const ConfigSourceAnnotationKey = "kubernetes.io/config.source"
|
||||
const ConfigMirrorAnnotationKey = "kubernetes.io/config.mirror"
|
||||
@ -71,24 +66,3 @@ type PodUpdate struct {
|
||||
Op PodOperation
|
||||
Source string
|
||||
}
|
||||
|
||||
// GetPodFullName returns a name that uniquely identifies a pod.
|
||||
func GetPodFullName(pod *api.Pod) string {
|
||||
// Use underscore as the delimiter because it is not allowed in pod name
|
||||
// (DNS subdomain format), while allowed in the container name format.
|
||||
return fmt.Sprintf("%s_%s", pod.Name, pod.Namespace)
|
||||
}
|
||||
|
||||
// Build the pod full name from pod name and namespace.
|
||||
func BuildPodFullName(name, namespace string) string {
|
||||
return name + "_" + namespace
|
||||
}
|
||||
|
||||
// Parse the pod full name.
|
||||
func ParsePodFullName(podFullName string) (string, string, error) {
|
||||
parts := strings.Split(podFullName, "_")
|
||||
if len(parts) != 2 {
|
||||
return "", "", fmt.Errorf("failed to parse the pod full name %q", podFullName)
|
||||
}
|
||||
return parts[0], parts[1], nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user