-
Notifications
You must be signed in to change notification settings - Fork 102
AAP-46619 Get pod container status #1371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7ce4cfb
partial test of pr#1332
c1752ed
add error handling for failed pod without a failed container
00a67a9
unit test
58cbc2d
simplify pod and container error handling per review comment
f509e06
move error handling outside loop
56d2103
Update pkg/workceptor/pod_test.go
85323da
review comment - oomkill comment
efc38bd
response to aaron & lisa's suggestions
55874cc
containerDiag empty if container is healthy
64d1cd0
remove GetPodStatus
d133a4e
fix comment
63fe122
make PodHealthy the beallendall
113c23b
lint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package workceptor | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| type KubePodStateHelper interface { | ||
| GetPodStatus(pod *corev1.Pod) (bool, error) | ||
|
arrestle marked this conversation as resolved.
Outdated
|
||
| PodHealthy(pod *corev1.Pod, containerName string) (bool, error) | ||
| PodContainerHealthy(pod *corev1.Pod, containerName string) (bool, error) | ||
| } | ||
|
|
||
| // PodContainerHealthy checks if the pod has successfully completed its application logic. | ||
| // this is called after podInfrastructureSuccess has confirmed the pod is in a terminal state. | ||
| func (kw KubeUnit) PodContainerHealthy(pod *corev1.Pod, containerName string) (bool, error) { | ||
| if pod == nil { | ||
| return false, fmt.Errorf("pod is nil") | ||
| } | ||
|
|
||
| for _, cs := range pod.Status.ContainerStatuses { | ||
|
arrestle marked this conversation as resolved.
Outdated
|
||
| if cs.Name == containerName { | ||
| if cs.State.Terminated == nil { // means it is waiting or running, so application logic has not completed yet. Normal behavior when job completes successfully. | ||
| return true, nil | ||
| } | ||
|
|
||
| if cs.State.Terminated.ExitCode != 0 { // exit code of 0 means success | ||
| return false, fmt.Errorf("container %s exited with code %d: %s", cs.Name, cs.State.Terminated.ExitCode, cs.State.Terminated.Reason) | ||
| } | ||
|
|
||
| return true, nil // container terminated with exit code of 0 | ||
| } | ||
| } | ||
|
|
||
| return false, fmt.Errorf("pod does not contain container %s", containerName) | ||
| } | ||
|
|
||
| // PodInfrastructureSuccess checks if the pod has either successfully started, is pending or is running, or has is successfully terminated. | ||
|
arrestle marked this conversation as resolved.
Outdated
|
||
| // Any other state is considered an infrastructure failure. | ||
| func (kw KubeUnit) PodHealthy(pod *corev1.Pod, containerName string) (bool, error) { | ||
| if pod == nil { | ||
| return false, fmt.Errorf("pod is nil") | ||
| } | ||
|
|
||
| for _, cs := range pod.Status.ContainerStatuses { | ||
|
arrestle marked this conversation as resolved.
Outdated
|
||
| // Check if this is the container we care about first | ||
| if cs.Name == containerName { | ||
| switch pod.Status.Phase { | ||
| case corev1.PodFailed: | ||
| podError := fmt.Errorf("pod failed with reason: %s", pod.Status.Reason) | ||
| if pod.Status.Message != "" { | ||
| podError = fmt.Errorf("%s: %s", podError.Error(), pod.Status.Message) | ||
| } | ||
| ok, containerError := kw.PodContainerHealthy(pod, containerName) | ||
| if !ok || containerError != nil { | ||
| return false, fmt.Errorf("%s %v", podError.Error(), containerError) | ||
| } | ||
|
|
||
| return false, podError | ||
|
|
||
| case corev1.PodSucceeded: | ||
| return true, nil | ||
| case corev1.PodRunning, corev1.PodPending: | ||
| return true, nil | ||
| default: | ||
| return false, fmt.Errorf("unknown phase: %s", pod.Status.Phase) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false, fmt.Errorf("pod does not contain container %s", containerName) | ||
| } | ||
|
|
||
| // GetPodStatus checks if the pod has successfully completed its application logic and infrastructure is healthy. | ||
| func (kw KubeUnit) GetPodStatus(pod *corev1.Pod) (bool, error) { | ||
|
arrestle marked this conversation as resolved.
Outdated
|
||
| if pod == nil { | ||
| return false, fmt.Errorf("pod is nil") | ||
| } | ||
|
|
||
| ok, err := kw.PodHealthy(pod, WorkerContainerName) | ||
| if !ok || err != nil { | ||
| return ok, err | ||
| } | ||
|
|
||
| return ok, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.