mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 18:00:08 +00:00
Merge pull request #16693 from Random-Liu/put-termination-message-path-into-docker-label
Auto commit by PR queue bot
This commit is contained in:
commit
c1d380db94
@ -32,10 +32,12 @@ const (
|
|||||||
kubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace"
|
kubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace"
|
||||||
kubernetesPodUID = "io.kubernetes.pod.uid"
|
kubernetesPodUID = "io.kubernetes.pod.uid"
|
||||||
|
|
||||||
|
kubernetesContainerRestartCountLabel = "io.kubernetes.container.restartCount"
|
||||||
|
kubernetesContainerTerminationMessagePath = "io.kubernetes.container.terminationMessagePath"
|
||||||
|
|
||||||
kubernetesPodLabel = "io.kubernetes.pod.data"
|
kubernetesPodLabel = "io.kubernetes.pod.data"
|
||||||
kubernetesTerminationGracePeriodLabel = "io.kubernetes.pod.terminationGracePeriod"
|
kubernetesTerminationGracePeriodLabel = "io.kubernetes.pod.terminationGracePeriod"
|
||||||
kubernetesContainerLabel = "io.kubernetes.container.name"
|
kubernetesContainerLabel = "io.kubernetes.container.name"
|
||||||
kubernetesContainerRestartCountLabel = "io.kubernetes.container.restartCount"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newLabels(container *api.Container, pod *api.Pod, restartCount int) map[string]string {
|
func newLabels(container *api.Container, pod *api.Pod, restartCount int) map[string]string {
|
||||||
@ -46,6 +48,7 @@ func newLabels(container *api.Container, pod *api.Pod, restartCount int) map[str
|
|||||||
labels[kubernetesPodUID] = string(pod.UID)
|
labels[kubernetesPodUID] = string(pod.UID)
|
||||||
|
|
||||||
labels[kubernetesContainerRestartCountLabel] = strconv.Itoa(restartCount)
|
labels[kubernetesContainerRestartCountLabel] = strconv.Itoa(restartCount)
|
||||||
|
labels[kubernetesContainerTerminationMessagePath] = container.TerminationMessagePath
|
||||||
|
|
||||||
return labels
|
return labels
|
||||||
}
|
}
|
||||||
@ -65,3 +68,14 @@ func getRestartCountFromLabel(labels map[string]string) (restartCount int, err e
|
|||||||
}
|
}
|
||||||
return restartCount, err
|
return restartCount, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTerminationMessagePathFromLabel(labels map[string]string) string {
|
||||||
|
if terminationMessagePath, found := labels[kubernetesContainerTerminationMessagePath]; found {
|
||||||
|
return terminationMessagePath
|
||||||
|
} else {
|
||||||
|
// Do not report error, because there should be many old containers without this label now.
|
||||||
|
// Return empty string "" for these containers, the caller will get terminationMessagePath by other ways.
|
||||||
|
glog.V(3).Infof("Container doesn't have label %s, it may be an old or invalid container", kubernetesContainerTerminationMessagePath)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -338,6 +338,7 @@ func (dm *DockerManager) determineContainerIP(podNamespace, podName string, cont
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO (random-liu) Remove parameter tPath when old containers are deprecated.
|
||||||
func (dm *DockerManager) inspectContainer(dockerID, containerName, tPath string, pod *api.Pod) *containerStatusResult {
|
func (dm *DockerManager) inspectContainer(dockerID, containerName, tPath string, pod *api.Pod) *containerStatusResult {
|
||||||
result := containerStatusResult{api.ContainerStatus{}, "", nil}
|
result := containerStatusResult{api.ContainerStatus{}, "", nil}
|
||||||
|
|
||||||
@ -408,8 +409,14 @@ func (dm *DockerManager) inspectContainer(dockerID, containerName, tPath string,
|
|||||||
FinishedAt: finishedAt,
|
FinishedAt: finishedAt,
|
||||||
ContainerID: DockerPrefix + dockerID,
|
ContainerID: DockerPrefix + dockerID,
|
||||||
}
|
}
|
||||||
if tPath != "" {
|
terminationMessagePath := getTerminationMessagePathFromLabel(inspectResult.Config.Labels)
|
||||||
path, found := inspectResult.Volumes[tPath]
|
if terminationMessagePath == "" {
|
||||||
|
// Because old containers have no terminationMessagePath Label, we stil have to rely on the information from apiserver here.
|
||||||
|
// TODO (random-liu) Remove this later when old containers with no labels are deprecated.
|
||||||
|
terminationMessagePath = tPath
|
||||||
|
}
|
||||||
|
if terminationMessagePath != "" {
|
||||||
|
path, found := inspectResult.Volumes[terminationMessagePath]
|
||||||
if found {
|
if found {
|
||||||
data, err := ioutil.ReadFile(path)
|
data, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1600,6 +1600,44 @@ func TestGetRestartCount(t *testing.T) {
|
|||||||
pod.Status = verifyRestartCount(&pod, 4)
|
pod.Status = verifyRestartCount(&pod, 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetTerminationMessagePath(t *testing.T) {
|
||||||
|
dm, fakeDocker := newTestDockerManager()
|
||||||
|
containers := []api.Container{
|
||||||
|
{
|
||||||
|
Name: "bar",
|
||||||
|
TerminationMessagePath: "/dev/somepath",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
pod := &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
UID: "12345678",
|
||||||
|
Name: "foo",
|
||||||
|
Namespace: "new",
|
||||||
|
},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Containers: containers,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fakeDocker.ContainerMap = map[string]*docker.Container{}
|
||||||
|
|
||||||
|
runSyncPod(t, dm, fakeDocker, pod, nil)
|
||||||
|
|
||||||
|
containerList := fakeDocker.ContainerList
|
||||||
|
if len(containerList) != 2 {
|
||||||
|
// One for infra container, one for container "bar"
|
||||||
|
t.Fatalf("unexpected container list length %d", len(containerList))
|
||||||
|
}
|
||||||
|
inspectResult, err := dm.client.InspectContainer(containerList[0].ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected inspect error %v", err)
|
||||||
|
}
|
||||||
|
terminationMessagePath := getTerminationMessagePathFromLabel(inspectResult.Config.Labels)
|
||||||
|
if terminationMessagePath != containers[0].TerminationMessagePath {
|
||||||
|
t.Errorf("expected termination message path %s, got %s", containers[0].TerminationMessagePath, terminationMessagePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSyncPodWithPodInfraCreatesContainerCallsHandler(t *testing.T) {
|
func TestSyncPodWithPodInfraCreatesContainerCallsHandler(t *testing.T) {
|
||||||
fakeHTTPClient := &fakeHTTP{}
|
fakeHTTPClient := &fakeHTTP{}
|
||||||
dm, fakeDocker := newTestDockerManagerWithHTTPClient(fakeHTTPClient)
|
dm, fakeDocker := newTestDockerManagerWithHTTPClient(fakeHTTPClient)
|
||||||
|
Loading…
Reference in New Issue
Block a user