@@ -48,6 +48,31 @@ func (w *K8sClient) WaitForPodRunningByLabelWithNamespace(label string, namespac
4848 }
4949}
5050
51+ // WaitForPodFailedByLabelWithNamespace waits for the pod matching the specified label in a specified namespace to become running
52+ // An error is returned if the pod does not exist or the timeout is reached
53+ func (w * K8sClient ) WaitForPodFailedByLabelWithNamespace (label string , namespace string ) (deployed bool , err error ) {
54+ timeout := time .After (6 * time .Minute )
55+ tick := time .Tick (1 * time .Second )
56+
57+ for {
58+ select {
59+ case <- timeout :
60+ return false , errors .New ("timed out" )
61+ case <- tick :
62+ err := w .WaitForFailedPodBySelector (namespace , label , 3 * time .Minute )
63+ if err == nil {
64+ return true , nil
65+ }
66+ }
67+ }
68+ }
69+
70+ // WaitForPodFailedByLabel waits for the pod matching the specified label to become running
71+ // An error is returned if the pod does not exist or the timeout is reached
72+ func (w * K8sClient ) WaitForPodFailedByLabel (label string ) (deployed bool , err error ) {
73+ return w .WaitForPodFailedByLabelWithNamespace (label , config .Namespace )
74+ }
75+
5176// WaitForPodRunningByLabel waits for the pod matching the specified label to become running
5277// An error is returned if the pod does not exist or the timeout is reached
5378func (w * K8sClient ) WaitForPodRunningByLabel (label string ) (deployed bool , err error ) {
@@ -77,6 +102,29 @@ func (w *K8sClient) WaitForRunningPodBySelector(namespace, selector string, time
77102 return nil
78103}
79104
105+ // WaitForFailedPodBySelector waits up to timeout seconds for all pods in 'namespace' with given 'selector' to enter running state.
106+ // Returns an error if no pods are found or not all discovered pods enter running state.
107+ func (w * K8sClient ) WaitForFailedPodBySelector (namespace , selector string , timeout time.Duration ) error {
108+ podList , err := w .ListPods (namespace , selector )
109+ if err != nil {
110+ return err
111+ }
112+ if len (podList .Items ) == 0 {
113+ fmt .Println ("No pods for " + selector + " in namespace " + namespace )
114+
115+ return nil
116+ }
117+
118+ for _ , pod := range podList .Items {
119+ fmt .Println ("Pod " + pod .Name + " created in namespace " + namespace + "...Checking for failure." )
120+ if err := w .waitForPodFailing (namespace , pod .Name , timeout ); err != nil {
121+ return err
122+ }
123+ }
124+
125+ return nil
126+ }
127+
80128// ListPods returns the list of currently scheduled or running pods in `namespace` with the given selector
81129func (w * K8sClient ) ListPods (namespace , selector string ) (* v1.PodList , error ) {
82130 listOptions := metav1.ListOptions {LabelSelector : selector }
@@ -94,6 +142,12 @@ func (w *K8sClient) waitForPodRunning(namespace, podName string, timeout time.Du
94142 return wait .PollImmediate (time .Second , timeout , w .isPodRunning (podName , namespace ))
95143}
96144
145+ // Poll up to timeout seconds for pod to enter running state.
146+ // Returns an error if the pod never enters the running state.
147+ func (w * K8sClient ) waitForPodFailing (namespace , podName string , timeout time.Duration ) error {
148+ return wait .PollImmediate (time .Second , timeout , w .isPodFailing (podName , namespace ))
149+ }
150+
97151// return a condition function that indicates whether the given pod is
98152// currently running
99153func (w * K8sClient ) isPodRunning (podName , namespace string ) wait.ConditionFunc {
@@ -111,3 +165,18 @@ func (w *K8sClient) isPodRunning(podName, namespace string) wait.ConditionFunc {
111165 return false , nil
112166 }
113167}
168+
169+ // return a condition function that indicates whether the given pod is
170+ // currently running
171+ func (w * K8sClient ) isPodFailing (podName , namespace string ) wait.ConditionFunc {
172+ return func () (bool , error ) {
173+ pod , _ := w .Kube ().CoreV1 ().Pods (namespace ).Get (context .TODO (), podName , metav1.GetOptions {})
174+
175+ if pod .Status .Phase == v1 .PodRunning {
176+ return false , nil
177+ } else {
178+ fmt .Printf ("Pod terminated %s\n " , podName )
179+ return true , nil
180+ }
181+ }
182+ }
0 commit comments