@@ -787,8 +787,10 @@ func (r *ReconcileArgoCD) reconcileApplicationControllerStatefulSet(cr *argoproj
787787 podSpec .Volumes = getArgoImportVolumes (export )
788788 }
789789
790- invalidImagePod := containsInvalidImage (cr , r )
791- if invalidImagePod {
790+ invalidImagePod , err := containsInvalidImage (* cr , * r )
791+ if err != nil {
792+ return err
793+ } else if invalidImagePod {
792794 argoutil .LogResourceDeletion (log , ss , "one or more pods has an invalid image" )
793795 if err := r .Client .Delete (context .TODO (), ss ); err != nil {
794796 return err
@@ -1002,22 +1004,52 @@ func updateNodePlacementStateful(existing *appsv1.StatefulSet, ss *appsv1.Statef
10021004
10031005// Returns true if a StatefulSet has pods in ErrImagePull or ImagePullBackoff state.
10041006// These pods cannot be restarted automatially due to known kubernetes issue https://github.com/kubernetes/kubernetes/issues/67250
1005- func containsInvalidImage (cr * argoproj.ArgoCD , r * ReconcileArgoCD ) bool {
1006-
1007- brokenPod := false
1007+ func containsInvalidImage (cr argoproj.ArgoCD , r ReconcileArgoCD ) (bool , error ) {
10081008
10091009 podList := & corev1.PodList {}
1010- listOption := client.MatchingLabels {common .ArgoCDKeyName : fmt .Sprintf ("%s-%s" , cr .Name , "application-controller" )}
1010+ applicationControllerListOption := client.MatchingLabels {common .ArgoCDKeyName : fmt .Sprintf ("%s-%s" , cr .Name , "application-controller" )}
10111011
1012- if err := r .Client .List (context .TODO (), podList , listOption ); err != nil {
1012+ if err := r .Client .List (context .TODO (), podList , applicationControllerListOption , client . InNamespace ( cr . Namespace ) ); err != nil {
10131013 log .Error (err , "Failed to list Pods" )
1014+ return false , err
1015+ }
1016+
1017+ if len (podList .Items ) == 0 {
1018+ // No pods, no work to do
1019+ return false , nil
1020+ }
1021+
1022+ if len (podList .Items ) != 1 {
1023+ // There should only be 0 or 1. If this message is printed, it suggests a problem.
1024+ log .Info ("Unexpected number of pods in 'containsInvalidImage' pod list" , "podListItems" , fmt .Sprintf ("%d" , len (podList .Items )), "namespace" , cr .Namespace )
1025+ return false , nil
1026+ }
1027+
1028+ appControllerPod := podList .Items [0 ]
1029+
1030+ if len (appControllerPod .Status .ContainerStatuses ) == 0 {
1031+ // No container statuses for application-controller, no work to do.
1032+ return false , nil
10141033 }
1015- if len (podList .Items ) > 0 {
1016- if len (podList .Items [0 ].Status .ContainerStatuses ) > 0 {
1017- if podList .Items [0 ].Status .ContainerStatuses [0 ].State .Waiting != nil && (podList .Items [0 ].Status .ContainerStatuses [0 ].State .Waiting .Reason == "ImagePullBackOff" || podList .Items [0 ].Status .ContainerStatuses [0 ].State .Waiting .Reason == "ErrImagePull" ) {
1018- brokenPod = true
1034+
1035+ brokenPod := false
1036+
1037+ waitingState := appControllerPod .Status .ContainerStatuses [0 ].State .Waiting
1038+ if waitingState != nil {
1039+
1040+ waitingReason := waitingState .Reason
1041+ if waitingReason == "ImagePullBackOff" || waitingReason == "ErrImagePull" {
1042+
1043+ var containerImage string
1044+ if len (appControllerPod .Spec .Containers ) > 0 {
1045+ containerImage = appControllerPod .Spec .Containers [0 ].Image
10191046 }
1047+
1048+ log .Info ("A broken pod was detected" , "waitingReason" , waitingReason , "containerImage" , containerImage )
1049+ brokenPod = true
10201050 }
1051+
10211052 }
1022- return brokenPod
1053+
1054+ return brokenPod , nil
10231055}
0 commit comments