@@ -18,13 +18,13 @@ package controllers
1818
1919import (
2020 "context"
21+ "fmt"
2122 "time"
2223
2324 "github.com/IBM-Cloud/power-go-client/power/models"
2425 "github.com/go-logr/logr"
2526 "github.com/pkg/errors"
2627
27- "github.com/IBM/go-sdk-core/v5/core"
2828 apierrors "k8s.io/apimachinery/pkg/api/errors"
2929 "k8s.io/apimachinery/pkg/runtime"
3030 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -108,26 +108,41 @@ func (r *IBMPowerVSImageReconciler) reconcile(ctx context.Context, cluster *v1be
108108 return ctrl.Result {}, nil
109109 }
110110
111+ if jobID := imageScope .GetJobID (); jobID != "" {
112+ job , err := imageScope .IBMPowerVSClient .GetJob (jobID )
113+ if err != nil {
114+ imageScope .Info ("Unable to get job details" )
115+ return ctrl.Result {RequeueAfter : 2 * time .Minute }, err
116+ }
117+ switch * job .Status .State {
118+ case "completed" :
119+ conditions .MarkTrue (imageScope .IBMPowerVSImage , v1beta1 .ImageImportedCondition )
120+ case "failed" :
121+ imageScope .SetNotReady ()
122+ imageScope .SetImageState (string (v1beta1 .PowerVSImageStateFailed ))
123+ conditions .MarkFalse (imageScope .IBMPowerVSImage , v1beta1 .ImageImportedCondition , v1beta1 .ImageImportFailedReason , clusterv1 .ConditionSeverityError , job .Status .Message )
124+ return ctrl.Result {RequeueAfter : 2 * time .Minute }, fmt .Errorf ("failed to import image, message: %s" , job .Status .Message )
125+ case "queued" :
126+ imageScope .SetNotReady ()
127+ imageScope .SetImageState (string (v1beta1 .PowerVSImageStateQue ))
128+ conditions .MarkFalse (imageScope .IBMPowerVSImage , v1beta1 .ImageImportedCondition , string (v1beta1 .PowerVSImageStateQue ), clusterv1 .ConditionSeverityInfo , job .Status .Message )
129+ return ctrl.Result {RequeueAfter : 2 * time .Minute }, nil
130+ default :
131+ imageScope .SetNotReady ()
132+ imageScope .SetImageState (string (v1beta1 .PowerVSImageStateImporting ))
133+ conditions .MarkFalse (imageScope .IBMPowerVSImage , v1beta1 .ImageImportedCondition , * job .Status .State , clusterv1 .ConditionSeverityInfo , job .Status .Message )
134+ return ctrl.Result {RequeueAfter : 2 * time .Minute }, nil
135+ }
136+ }
137+
111138 img , jobRef , err := r .getOrCreate (imageScope )
112139 if err != nil {
113140 imageScope .Error (err , "Unable to import image" )
114141 return ctrl.Result {}, errors .Wrapf (err , "failed to reconcile Image for IBMPowerVSImage %s/%s" , imageScope .IBMPowerVSImage .Namespace , imageScope .IBMPowerVSImage .Name )
115142 }
116143
117144 if jobRef != nil {
118- job , err := imageScope .IBMPowerVSClient .GetJob (* jobRef .ID )
119- if err != nil {
120- imageScope .Info ("Unable to get job details" )
121- return ctrl.Result {RequeueAfter : 2 * time .Minute }, err
122- }
123-
124- if * job .Status .State != "completed" && * job .Status .State != "failed" {
125- imageScope .Info ("Import job not yet finished - " + * job .Status .State )
126- imageScope .SetNotReady ()
127- imageScope .SetImageState (core .StringPtr ("" ))
128- conditions .MarkFalse (imageScope .IBMPowerVSImage , v1beta1 .ImageReadyCondition , v1beta1 .ImageNotReadyReason , clusterv1 .ConditionSeverityWarning , "" )
129- return ctrl.Result {}, nil
130- }
145+ imageScope .SetJobID (* jobRef .ID )
131146 }
132147 return reconcileImage (img , imageScope )
133148}
@@ -142,11 +157,11 @@ func reconcileImage(img *models.ImageReference, imageScope *scope.PowerVSImageSc
142157
143158 imageScope .SetImageID (image .ImageID )
144159 imageScope .Info ("ImageID - " + imageScope .GetImageID ())
145- imageScope .SetImageState (& image .State )
160+ imageScope .SetImageState (image .State )
146161 imageScope .Info ("ImageState - " + image .State )
147162
148163 switch imageScope .GetImageState () {
149- case v1beta1 .PowerVSInstanceStateQue :
164+ case v1beta1 .PowerVSImageStateQue :
150165 imageScope .Info ("Image is in queued state" )
151166 imageScope .SetNotReady ()
152167 conditions .MarkFalse (imageScope .IBMPowerVSImage , v1beta1 .ImageReadyCondition , v1beta1 .ImageNotReadyReason , clusterv1 .ConditionSeverityWarning , "" )
0 commit comments