@@ -23,6 +23,7 @@ import (
2323"os"
2424"path/filepath"
2525"strconv"
26+ "strings"
2627"time"
2728
2829"k8s.io/apimachinery/pkg/runtime"
@@ -293,30 +294,49 @@ func taskPhase(ctx *context, pg *arbv1.PodGroup, phase []v1.PodPhase, taskNum in
293294}
294295*/
295296
296- func podPhase (ctx * context , namespace string , pods []* v1.Pod , phase []v1.PodPhase , taskNum int ) wait.ConditionFunc {
297+ func podPhase (ctx * context , awNamespace string , awName string , pods []* v1.Pod , phase []v1.PodPhase , taskNum int ) wait.ConditionFunc {
297298return func () (bool , error ) {
298- podList , err := ctx .kubeclient .CoreV1 ().Pods (namespace ).List (gcontext .Background (), metav1.ListOptions {})
299+ podList , err := ctx .kubeclient .CoreV1 ().Pods (awNamespace ).List (gcontext .Background (), metav1.ListOptions {})
299300Expect (err ).NotTo (HaveOccurred ())
300301
302+ if podList == nil || podList .Size () < 1 {
303+ fmt .Fprintf (os .Stdout , "[podPhase] Listing podList found for Namespace: %s/%s resulting in no podList found that could match AppWrapper with pod count: %d\n " ,
304+ awNamespace , awName , len (pods ))
305+ }
306+
301307phaseListTaskNum := 0
302308
303- for _ , awPod := range pods {
304- var podFromList * v1.Pod
305- podFromList = nil
306- for _ , podFromPodList := range podList .Items {
307- if awn , found := podFromPodList .Labels ["appwrapper.mcad.ibm.com" ]; ! found || awn != awPod .Name {
308- continue
309- }
310- podFromList = & podFromPodList
311- break
309+ for _ , podFromPodList := range podList .Items {
310+
311+ // First find a pod from the list that is part of the AW
312+ if awn , found := podFromPodList .Labels ["appwrapper.mcad.ibm.com" ]; ! found || awn != awName {
313+ fmt .Fprintf (os .Stdout , "[podPhase] Pod %s in phase: %s not part of AppWrapper: %s, labels: %#v\n " ,
314+ podFromPodList .Name , podFromPodList .Status .Phase , awName , podFromPodList .Labels )
315+ continue
312316}
313317
314- if podFromList != nil {
315- for _ , p := range phase {
316- if podFromList .Status .Phase == p {
317- phaseListTaskNum ++
318- break
318+ // Next check to see if it is a phase we are looking for
319+ for _ , p := range phase {
320+
321+ // If we found the phase make sure it is part of the list of pod provided in the input
322+ if podFromPodList .Status .Phase == p {
323+ matchToPodsFromInput := false
324+ var inputPodIDs []string
325+ for _ , inputPod := range pods {
326+ inputPodIDs = append (inputPodIDs , fmt .Sprintf ("%s.%s" , inputPod .Namespace , inputPod .Name ))
327+ if strings .Compare (podFromPodList .Namespace , inputPod .Namespace ) == 0 &&
328+ strings .Compare (podFromPodList .Name , inputPod .Name ) == 0 {
329+ phaseListTaskNum ++
330+ matchToPodsFromInput = true
331+ break
332+ }
333+
334+ }
335+ if matchToPodsFromInput == false {
336+ fmt .Fprintf (os .Stdout , "[podPhase] Pod %s in phase: %s does not match any input pods: %#v \n " ,
337+ podFromPodList .Name , podFromPodList .Status .Phase , inputPodIDs )
319338}
339+ break
320340}
321341}
322342}
@@ -351,13 +371,17 @@ func cleanupTestObjects(context *context, appwrappers []*arbv1.AppWrapper) {
351371
352372pods := getPodsOfAppWrapper (context , aw )
353373awNamespace := aw .Namespace
374+ awName := aw .Name
354375fmt .Fprintf (os .Stdout , "[cleanupTestObjects] Deleting AW %s.\n " , aw .Name )
355376err := deleteAppWrapper (context , aw .Name )
356377Expect (err ).NotTo (HaveOccurred ())
357378
358379// Wait for the pods of the deleted the appwrapper to be destroyed
359- fmt .Fprintf (os .Stdout , "[cleanupTestObjects] Awaiting %d pods to be deleted for AW %s.\n " , len (pods ), aw .Name )
360- err = waitAWPodsDeleted (context , awNamespace , pods )
380+ for _ , pod := range pods {
381+ fmt .Fprintf (os .Stdout , "[cleanupTestObjects] Awaiting pod %s/%s to be deleted for AW %s.\n " ,
382+ pod .Namespace , pod .Name , aw .Name )
383+ }
384+ err = waitAWPodsDeleted (context , awNamespace , awName , pods )
361385
362386// Final check to see if pod exists
363387if err != nil {
@@ -370,7 +394,7 @@ func cleanupTestObjects(context *context, appwrappers []*arbv1.AppWrapper) {
370394}
371395}
372396if len (podsStillExisting ) > 0 {
373- err = waitAWPodsDeleted (context , awNamespace , podsStillExisting )
397+ err = waitAWPodsDeleted (context , awNamespace , awName , podsStillExisting )
374398}
375399}
376400Expect (err ).NotTo (HaveOccurred ())
@@ -528,11 +552,11 @@ func waitAWReadyQuiet(ctx *context, aw *arbv1.AppWrapper) error {
528552}
529553
530554func waitAWDeleted (ctx * context , aw * arbv1.AppWrapper , pods []* v1.Pod ) error {
531- return waitAWPodsTerminatedEx (ctx , aw .Namespace , pods , 0 )
555+ return waitAWPodsTerminatedEx (ctx , aw .Namespace , aw . Name , pods , 0 )
532556}
533557
534- func waitAWPodsDeleted (ctx * context , awNamespace string , pods []* v1.Pod ) error {
535- return waitAWPodsTerminatedEx (ctx , awNamespace , pods , 0 )
558+ func waitAWPodsDeleted (ctx * context , awNamespace string , awName string , pods []* v1.Pod ) error {
559+ return waitAWPodsTerminatedEx (ctx , awNamespace , awName , pods , 0 )
536560}
537561
538562func waitAWPending (ctx * context , aw * arbv1.AppWrapper ) error {
@@ -545,8 +569,8 @@ func waitAWPodsReadyEx(ctx *context, aw *arbv1.AppWrapper, taskNum int, quite bo
545569[]v1.PodPhase {v1 .PodRunning , v1 .PodSucceeded }, taskNum , quite ))
546570}
547571
548- func waitAWPodsTerminatedEx (ctx * context , namespace string , pods []* v1.Pod , taskNum int ) error {
549- return wait .Poll (100 * time .Millisecond , ninetySeconds , podPhase (ctx , namespace , pods ,
572+ func waitAWPodsTerminatedEx (ctx * context , namespace string , name string , pods []* v1.Pod , taskNum int ) error {
573+ return wait .Poll (100 * time .Millisecond , ninetySeconds , podPhase (ctx , namespace , name , pods ,
550574[]v1.PodPhase {v1 .PodRunning , v1 .PodSucceeded , v1 .PodUnknown , v1 .PodFailed , v1 .PodPending }, taskNum ))
551575}
552576
@@ -617,26 +641,26 @@ func createDeploymentAW(context *context, name string) *arbv1.AppWrapper {
617641rb := []byte (`{"apiVersion": "apps/v1",
618642"kind": "Deployment",
619643"metadata": {
620- "name": "aw-deployment-1 ",
644+ "name": "aw-deployment-3 ",
621645"namespace": "test",
622646"labels": {
623- "app": "aw-deployment-1 "
647+ "app": "aw-deployment-3 "
624648}
625649},
626650"spec": {
627651"replicas": 3,
628652"selector": {
629653"matchLabels": {
630- "app": "aw-deployment-1 "
654+ "app": "aw-deployment-3 "
631655}
632656},
633657"template": {
634658"metadata": {
635659"labels": {
636- "app": "aw-deployment-1 "
660+ "app": "aw-deployment-3 "
637661},
638662"annotations": {
639- "appwrapper.mcad.ibm.com/appwrapper-name": "aw-deployment-1 "
663+ "appwrapper.mcad.ibm.com/appwrapper-name": "aw-deployment-3 "
640664}
641665},
642666"spec": {
@@ -1569,16 +1593,16 @@ func createBadPodTemplateAW(context *context, name string) *arbv1.AppWrapper {
15691593"kind": "Pod",
15701594"metadata": {
15711595"labels": {
1572- "app": "nginx "
1596+ "app": "aw-bad-podtemplate-2 "
15731597},
15741598"annotations": {
1575- "appwrapper.mcad.ibm.com/appwrapper-name": "nginx "
1599+ "appwrapper.mcad.ibm.com/appwrapper-name": "aw-bad-podtemplate-2 "
15761600}
15771601},
15781602"spec": {
15791603"containers": [
15801604{
1581- "name": "nginx ",
1605+ "name": "aw-bad-podtemplate-2 ",
15821606"image": "k8s.gcr.io/echoserver:1.4",
15831607"ports": [
15841608{
@@ -1627,25 +1651,25 @@ func createBadPodTemplateAW(context *context, name string) *arbv1.AppWrapper {
16271651func createPodTemplateAW (context * context , name string ) * arbv1.AppWrapper {
16281652rb := []byte (`{"metadata":
16291653{
1630- "name": "nginx ",
1654+ "name": "aw-podtemplate-2 ",
16311655"namespace": "test",
16321656"labels": {
1633- "app": "nginx "
1657+ "app": "aw-podtemplate-2 "
16341658}
16351659},
16361660"template": {
16371661"metadata": {
16381662"labels": {
1639- "app": "nginx "
1663+ "app": "aw-podtemplate-2 "
16401664},
16411665"annotations": {
1642- "appwrapper.mcad.ibm.com/appwrapper-name": "nginx "
1666+ "appwrapper.mcad.ibm.com/appwrapper-name": "aw-podtemplate-2 "
16431667}
16441668},
16451669"spec": {
16461670"containers": [
16471671{
1648- "name": "nginx ",
1672+ "name": "aw-podtemplate-2 ",
16491673"image": "k8s.gcr.io/echoserver:1.4",
16501674"ports": [
16511675{
@@ -1811,22 +1835,25 @@ func createBadGenericPodAW(context *context, name string) *arbv1.AppWrapper {
18111835func createBadGenericPodTemplateAW (context * context , name string ) * arbv1.AppWrapper {
18121836rb := []byte (`{"metadata":
18131837{
1814- "name": "nginx ",
1838+ "name": "aw-generic-podtemplate-2 ",
18151839"namespace": "test",
18161840"labels": {
1817- "app": "nginx "
1841+ "app": "aw-generic-podtemplate-2 "
18181842}
18191843},
18201844"template": {
18211845"metadata": {
18221846"labels": {
1823- "app": "nginx"
1847+ "app": "aw-generic-podtemplate-2"
1848+ },
1849+ "annotations": {
1850+ "appwrapper.mcad.ibm.com/appwrapper-name": "aw-generic-podtemplate-2"
18241851}
18251852},
18261853"spec": {
18271854"containers": [
18281855{
1829- "name": "nginx ",
1856+ "name": "aw-generic-podtemplate-2 ",
18301857"image": "k8s.gcr.io/echoserver:1.4",
18311858"ports": [
18321859{
@@ -2048,12 +2075,14 @@ func getPodsOfAppWrapper(ctx *context, aw *arbv1.AppWrapper) []*v1.Pod {
20482075
20492076var awpods []* v1.Pod
20502077
2051- for _ , pod := range pods .Items {
2078+ for index , _ := range pods .Items {
2079+ // Get a pointer to the pod in the list not a pointer to the podCopy
2080+ pod := & pods .Items [index ]
2081+
20522082if gn , found := pod .Annotations [arbv1 .AppWrapperAnnotationKey ]; ! found || gn != aw .Name {
20532083continue
20542084}
2055- awpods = append (awpods , & pod )
2056-
2085+ awpods = append (awpods , pod )
20572086}
20582087
20592088return awpods
0 commit comments