Skip to content

Commit e31a757

Browse files
authored
Merge pull request #2195 from shiftstack/image-switch
🌱 refactoring: Untangle four image-fetching strategies
2 parents eefb044 + ea670c3 commit e31a757

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

internal/controllers/image/reconcile.go

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -103,59 +103,59 @@ func (r *orcImageReconciler) reconcileNormal(ctx context.Context, orcImage *orcv
103103
return ctrl.Result{}, err
104104
}
105105

106-
glanceImage, err := func() (*images.Image, error) {
107-
if orcImage.Status.ID != nil {
108-
log.V(4).Info("Fetching existing glance image", "ID", *orcImage.Status.ID)
109-
110-
image, err := imageClient.GetImage(*orcImage.Status.ID)
106+
var glanceImage *images.Image
107+
switch {
108+
case orcImage.Status.ID != nil:
109+
log.V(4).Info("Fetching existing glance image", "ID", *orcImage.Status.ID)
110+
glanceImage, err = imageClient.GetImage(*orcImage.Status.ID)
111+
if err != nil {
111112
if capoerrors.IsNotFound(err) {
112113
// An image we previously referenced has been deleted unexpectedly. We can't recover from this.
113-
err = capoerrors.Terminal(orcv1alpha1.OpenStackConditionReasonUnrecoverableError, "image has been deleted from glance")
114+
return ctrl.Result{}, capoerrors.Terminal(orcv1alpha1.OpenStackConditionReasonUnrecoverableError, "image has been deleted from Glance")
114115
}
115-
return image, err
116+
return ctrl.Result{}, err
116117
}
117118

118-
if orcImage.Spec.Import != nil {
119-
log.V(4).Info("Importing existing glance image")
120-
121-
if orcImage.Spec.Import.ID != nil {
122-
image, err := imageClient.GetImage(*orcImage.Spec.Import.ID)
123-
if capoerrors.IsNotFound(err) {
124-
// We assume that an image imported by ID must already exist. It's a terminal error if it doesn't.
125-
err = capoerrors.Terminal(orcv1alpha1.OpenStackConditionReasonUnrecoverableError, "referenced image does not exist in glance")
126-
}
127-
return image, err
119+
case orcImage.Spec.Import != nil && orcImage.Spec.Import.ID != nil:
120+
log.V(4).Info("Importing existing Glance image by ID")
121+
glanceImage, err = imageClient.GetImage(*orcImage.Spec.Import.ID)
122+
if err != nil {
123+
if capoerrors.IsNotFound(err) {
124+
// We assume that an image imported by ID must already exist. It's a terminal error if it doesn't.
125+
return ctrl.Result{}, capoerrors.Terminal(orcv1alpha1.OpenStackConditionReasonUnrecoverableError, "referenced image does not exist in Glance")
128126
}
129-
130-
listOpts := listOptsFromImportFilter(orcImage.Spec.Import.Filter)
131-
return getGlanceImageFromList(ctx, listOpts, imageClient)
132-
133-
// TODO: When we support 'import and manage' we need to implement
134-
// setting spec.resource from the discovered glance image here.
127+
return ctrl.Result{}, err
135128
}
136129

137-
log.V(4).Info("Checking for previously created image")
138-
139-
listOpts := listOptsFromCreation(orcImage)
140-
return getGlanceImageFromList(ctx, listOpts, imageClient)
141-
}()
142-
if err != nil {
143-
return ctrl.Result{}, err
144-
}
145-
146-
if glanceImage == nil {
147-
if orcImage.Spec.Import != nil {
148-
log.V(3).Info("Image does not yet exist")
149-
addStatus(withProgressMessage("Waiting for glance image to be created externally"))
150-
130+
case orcImage.Spec.Import != nil && orcImage.Spec.Import.Filter != nil:
131+
log.V(4).Info("Importing existing Glance image by filter")
132+
listOpts := listOptsFromImportFilter(orcImage.Spec.Import.Filter)
133+
glanceImage, err = getGlanceImageFromList(ctx, listOpts, imageClient)
134+
if err != nil {
135+
return ctrl.Result{}, err
136+
}
137+
if glanceImage == nil {
138+
log.V(3).Info("Glance image does not yet exist")
139+
addStatus(withProgressMessage("Waiting for Glance image to be created externally"))
151140
return ctrl.Result{RequeueAfter: waitForGlanceImageStatusUpdate}, err
152141
}
153142

154-
glanceImage, err = createImage(ctx, orcImage, imageClient)
143+
default:
144+
log.V(4).Info("Checking for previously created image")
145+
listOpts := listOptsFromCreation(orcImage)
146+
glanceImage, err = getGlanceImageFromList(ctx, listOpts, imageClient)
155147
if err != nil {
156148
return ctrl.Result{}, err
157149
}
150+
151+
if glanceImage == nil {
152+
glanceImage, err = createImage(ctx, orcImage, imageClient)
153+
if err != nil {
154+
return ctrl.Result{}, err
155+
}
156+
}
158157
}
158+
159159
addStatus(withGlanceImage(glanceImage))
160160

161161
if orcImage.Status.ID == nil {
@@ -164,11 +164,14 @@ func (r *orcImageReconciler) reconcileNormal(ctx context.Context, orcImage *orcv
164164
}
165165
}
166166

167+
log.V(4).Info("Got glance image", "status", glanceImage.Status)
167168
log = log.WithValues("ID", glanceImage.ID)
168169
ctx = ctrl.LoggerInto(ctx, log)
169170

170-
log.V(4).Info("Got glance image", "status", glanceImage.Status)
171+
return r.handleImageUpload(ctx, imageClient, orcImage, glanceImage, addStatus)
172+
}
171173

174+
func (r *orcImageReconciler) handleImageUpload(ctx context.Context, imageClient clients.ImageClient, orcImage *orcv1alpha1.Image, glanceImage *images.Image, addStatus func(updateStatusOpt)) (_ ctrl.Result, err error) {
172175
switch glanceImage.Status {
173176
// Cases where we're not going to take any action until the next resync
174177
case images.ImageStatusActive, images.ImageStatusDeactivated:
@@ -399,7 +402,7 @@ func glancePropertiesFromStruct(propStruct interface{}, properties map[string]st
399402
return nil
400403
}
401404

402-
// createImage creates a glance image for an ORC Image.
405+
// createImage creates a Glance image for an ORC Image.
403406
func createImage(ctx context.Context, orcImage *orcv1alpha1.Image, imageClient clients.ImageClient) (*images.Image, error) {
404407
if orcImage.Spec.ManagementPolicy == orcv1alpha1.ManagementPolicyUnmanaged {
405408
// Should have been caught by API validation

0 commit comments

Comments
 (0)