@@ -103,59 +103,59 @@ func (r *orcImageReconciler) reconcileNormal(ctx context.Context, orcImage *orcv
103
103
return ctrl.Result {}, err
104
104
}
105
105
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 {
111
112
if capoerrors .IsNotFound (err ) {
112
113
// 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 " )
114
115
}
115
- return image , err
116
+ return ctrl. Result {} , err
116
117
}
117
118
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" )
128
126
}
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
135
128
}
136
129
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" ))
151
140
return ctrl.Result {RequeueAfter : waitForGlanceImageStatusUpdate }, err
152
141
}
153
142
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 )
155
147
if err != nil {
156
148
return ctrl.Result {}, err
157
149
}
150
+
151
+ if glanceImage == nil {
152
+ glanceImage , err = createImage (ctx , orcImage , imageClient )
153
+ if err != nil {
154
+ return ctrl.Result {}, err
155
+ }
156
+ }
158
157
}
158
+
159
159
addStatus (withGlanceImage (glanceImage ))
160
160
161
161
if orcImage .Status .ID == nil {
@@ -164,11 +164,14 @@ func (r *orcImageReconciler) reconcileNormal(ctx context.Context, orcImage *orcv
164
164
}
165
165
}
166
166
167
+ log .V (4 ).Info ("Got glance image" , "status" , glanceImage .Status )
167
168
log = log .WithValues ("ID" , glanceImage .ID )
168
169
ctx = ctrl .LoggerInto (ctx , log )
169
170
170
- log .V (4 ).Info ("Got glance image" , "status" , glanceImage .Status )
171
+ return r .handleImageUpload (ctx , imageClient , orcImage , glanceImage , addStatus )
172
+ }
171
173
174
+ func (r * orcImageReconciler ) handleImageUpload (ctx context.Context , imageClient clients.ImageClient , orcImage * orcv1alpha1.Image , glanceImage * images.Image , addStatus func (updateStatusOpt )) (_ ctrl.Result , err error ) {
172
175
switch glanceImage .Status {
173
176
// Cases where we're not going to take any action until the next resync
174
177
case images .ImageStatusActive , images .ImageStatusDeactivated :
@@ -396,7 +399,7 @@ func glancePropertiesFromStruct(propStruct interface{}, properties map[string]st
396
399
return nil
397
400
}
398
401
399
- // createImage creates a glance image for an ORC Image.
402
+ // createImage creates a Glance image for an ORC Image.
400
403
func createImage (ctx context.Context , orcImage * orcv1alpha1.Image , imageClient clients.ImageClient ) (* images.Image , error ) {
401
404
if orcImage .Spec .ManagementPolicy == orcv1alpha1 .ManagementPolicyUnmanaged {
402
405
// Should have been caught by API validation
0 commit comments