mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 22:17:14 +00:00
Handle fetch of container logs of error containers during pod termination
* improve error returned when failing to fetch container logs * handle cases where logs are requested for containers without the container ID
This commit is contained in:
parent
f821a54d39
commit
749980b726
@ -1129,7 +1129,7 @@ func (kl *Kubelet) validateContainerLogStatus(podName string, podStatus *v1.PodS
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case previous:
|
case previous:
|
||||||
if lastState.Terminated == nil {
|
if lastState.Terminated == nil || lastState.Terminated.ContainerID == "" {
|
||||||
return kubecontainer.ContainerID{}, fmt.Errorf("previous terminated container %q in pod %q not found", containerName, podName)
|
return kubecontainer.ContainerID{}, fmt.Errorf("previous terminated container %q in pod %q not found", containerName, podName)
|
||||||
}
|
}
|
||||||
cID = lastState.Terminated.ContainerID
|
cID = lastState.Terminated.ContainerID
|
||||||
@ -1138,9 +1138,21 @@ func (kl *Kubelet) validateContainerLogStatus(podName string, podStatus *v1.PodS
|
|||||||
cID = cStatus.ContainerID
|
cID = cStatus.ContainerID
|
||||||
|
|
||||||
case terminated != nil:
|
case terminated != nil:
|
||||||
cID = terminated.ContainerID
|
// in cases where the next container didn't start, terminated.ContainerID will be empty, so get logs from the lastState.Terminated.
|
||||||
|
if terminated.ContainerID == "" {
|
||||||
|
if lastState.Terminated != nil && lastState.Terminated.ContainerID != "" {
|
||||||
|
cID = lastState.Terminated.ContainerID
|
||||||
|
} else {
|
||||||
|
return kubecontainer.ContainerID{}, fmt.Errorf("container %q in pod %q is terminated", containerName, podName)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cID = terminated.ContainerID
|
||||||
|
}
|
||||||
|
|
||||||
case lastState.Terminated != nil:
|
case lastState.Terminated != nil:
|
||||||
|
if lastState.Terminated.ContainerID == "" {
|
||||||
|
return kubecontainer.ContainerID{}, fmt.Errorf("container %q in pod %q is terminated", containerName, podName)
|
||||||
|
}
|
||||||
cID = lastState.Terminated.ContainerID
|
cID = lastState.Terminated.ContainerID
|
||||||
|
|
||||||
case waiting != nil:
|
case waiting != nil:
|
||||||
|
@ -731,7 +731,7 @@ func TestValidateContainerLogStatus(t *testing.T) {
|
|||||||
Running: &v1.ContainerStateRunning{},
|
Running: &v1.ContainerStateRunning{},
|
||||||
},
|
},
|
||||||
LastTerminationState: v1.ContainerState{
|
LastTerminationState: v1.ContainerState{
|
||||||
Terminated: &v1.ContainerStateTerminated{},
|
Terminated: &v1.ContainerStateTerminated{ContainerID: "docker://fakeid"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -759,9 +759,51 @@ func TestValidateContainerLogStatus(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
success: false,
|
||||||
|
pSuccess: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statuses: []v1.ContainerStatus{
|
||||||
|
{
|
||||||
|
Name: containerName,
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Terminated: &v1.ContainerStateTerminated{ContainerID: "docker://fakeid"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
success: true,
|
success: true,
|
||||||
pSuccess: false,
|
pSuccess: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
statuses: []v1.ContainerStatus{
|
||||||
|
{
|
||||||
|
Name: containerName,
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Terminated: &v1.ContainerStateTerminated{},
|
||||||
|
},
|
||||||
|
LastTerminationState: v1.ContainerState{
|
||||||
|
Terminated: &v1.ContainerStateTerminated{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
success: false,
|
||||||
|
pSuccess: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statuses: []v1.ContainerStatus{
|
||||||
|
{
|
||||||
|
Name: containerName,
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Terminated: &v1.ContainerStateTerminated{},
|
||||||
|
},
|
||||||
|
LastTerminationState: v1.ContainerState{
|
||||||
|
Terminated: &v1.ContainerStateTerminated{ContainerID: "docker://fakeid"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
pSuccess: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
statuses: []v1.ContainerStatus{
|
statuses: []v1.ContainerStatus{
|
||||||
{
|
{
|
||||||
|
@ -758,7 +758,8 @@ func findNextInitContainerToRun(pod *v1.Pod, podStatus *kubecontainer.PodStatus)
|
|||||||
func (m *kubeGenericRuntimeManager) GetContainerLogs(pod *v1.Pod, containerID kubecontainer.ContainerID, logOptions *v1.PodLogOptions, stdout, stderr io.Writer) (err error) {
|
func (m *kubeGenericRuntimeManager) GetContainerLogs(pod *v1.Pod, containerID kubecontainer.ContainerID, logOptions *v1.PodLogOptions, stdout, stderr io.Writer) (err error) {
|
||||||
status, err := m.runtimeService.ContainerStatus(containerID.ID)
|
status, err := m.runtimeService.ContainerStatus(containerID.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get container status %q: %v", containerID, err)
|
glog.V(4).Infof("failed to get container status for %v: %v", containerID.String(), err)
|
||||||
|
return fmt.Errorf("Unable to retrieve container logs for %v", containerID.String())
|
||||||
}
|
}
|
||||||
labeledInfo := getContainerInfoFromLabels(status.Labels)
|
labeledInfo := getContainerInfoFromLabels(status.Labels)
|
||||||
annotatedInfo := getContainerInfoFromAnnotations(status.Annotations)
|
annotatedInfo := getContainerInfoFromAnnotations(status.Annotations)
|
||||||
|
Loading…
Reference in New Issue
Block a user