mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 04:06:03 +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 {
|
||||
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)
|
||||
}
|
||||
cID = lastState.Terminated.ContainerID
|
||||
@ -1138,9 +1138,21 @@ func (kl *Kubelet) validateContainerLogStatus(podName string, podStatus *v1.PodS
|
||||
cID = cStatus.ContainerID
|
||||
|
||||
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:
|
||||
if lastState.Terminated.ContainerID == "" {
|
||||
return kubecontainer.ContainerID{}, fmt.Errorf("container %q in pod %q is terminated", containerName, podName)
|
||||
}
|
||||
cID = lastState.Terminated.ContainerID
|
||||
|
||||
case waiting != nil:
|
||||
|
@ -731,7 +731,7 @@ func TestValidateContainerLogStatus(t *testing.T) {
|
||||
Running: &v1.ContainerStateRunning{},
|
||||
},
|
||||
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,
|
||||
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{
|
||||
{
|
||||
|
@ -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) {
|
||||
status, err := m.runtimeService.ContainerStatus(containerID.ID)
|
||||
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)
|
||||
annotatedInfo := getContainerInfoFromAnnotations(status.Annotations)
|
||||
|
Loading…
Reference in New Issue
Block a user