-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9d9a64
commit 0e8193e
Showing
4 changed files
with
120 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,50 @@ | ||
package conditions | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/kubernetes/pkg/kubectl" | ||
) | ||
|
||
// PodContainerRunning returns false until the named container has ContainerStatus running (at least once), | ||
// and will return an error if the pod is deleted, runs to completion, or the container pod is not available. | ||
func PodContainerRunning(containerName string) watch.ConditionFunc { | ||
return func(event watch.Event) (bool, error) { | ||
switch event.Type { | ||
case watch.Deleted: | ||
return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "") | ||
} | ||
switch t := event.Object.(type) { | ||
case *corev1.Pod: | ||
switch t.Status.Phase { | ||
case corev1.PodRunning, corev1.PodPending: | ||
case corev1.PodFailed, corev1.PodSucceeded: | ||
return false, kubectl.ErrPodCompleted | ||
default: | ||
return false, nil | ||
} | ||
for _, s := range t.Status.ContainerStatuses { | ||
if s.Name != containerName { | ||
continue | ||
} | ||
if s.State.Terminated != nil { | ||
return false, kubectl.ErrContainerTerminated | ||
} | ||
return s.State.Running != nil, nil | ||
} | ||
for _, s := range t.Status.InitContainerStatuses { | ||
if s.Name != containerName { | ||
continue | ||
} | ||
if s.State.Terminated != nil { | ||
return false, kubectl.ErrContainerTerminated | ||
} | ||
return s.State.Running != nil, nil | ||
} | ||
return false, nil | ||
} | ||
return false, nil | ||
} | ||
} |