@@ -109,44 +109,20 @@ func (r *OperatorReconciler) reconcile(ctx context.Context, op *operatorsv1alpha
109109 // Set the TypeInstalled condition to Unknown to indicate that the resolution
110110 // hasn't been attempted yet, due to the spec being invalid.
111111 op .Status .InstalledBundleResource = ""
112- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
113- Type : operatorsv1alpha1 .TypeInstalled ,
114- Status : metav1 .ConditionUnknown ,
115- Reason : operatorsv1alpha1 .ReasonInstallationStatusUnknown ,
116- Message : "installation has not been attempted as spec is invalid" ,
117- ObservedGeneration : op .GetGeneration (),
118- })
112+ setInstalledStatusConditionUnknown (& op .Status .Conditions , "installation has not been attempted as spec is invalid" , op .GetGeneration ())
119113 // Set the TypeResolved condition to Unknown to indicate that the resolution
120114 // hasn't been attempted yet, due to the spec being invalid.
121115 op .Status .ResolvedBundleResource = ""
122- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
123- Type : operatorsv1alpha1 .TypeResolved ,
124- Status : metav1 .ConditionUnknown ,
125- Reason : operatorsv1alpha1 .ReasonResolutionUnknown ,
126- Message : "validation has not been attempted as spec is invalid" ,
127- ObservedGeneration : op .GetGeneration (),
128- })
116+ setResolvedStatusConditionUnknown (& op .Status .Conditions , "validation has not been attempted as spec is invalid" , op .GetGeneration ())
129117 return ctrl.Result {}, nil
130118 }
131119 // run resolution
132120 solution , err := r .Resolver .Resolve (ctx )
133121 if err != nil {
134122 op .Status .InstalledBundleResource = ""
135- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
136- Type : operatorsv1alpha1 .TypeInstalled ,
137- Status : metav1 .ConditionUnknown ,
138- Reason : operatorsv1alpha1 .ReasonInstallationStatusUnknown ,
139- Message : "installation has not been attempted as resolution failed" ,
140- ObservedGeneration : op .GetGeneration (),
141- })
123+ setInstalledStatusConditionUnknown (& op .Status .Conditions , "installation has not been attempted as resolution failed" , op .GetGeneration ())
142124 op .Status .ResolvedBundleResource = ""
143- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
144- Type : operatorsv1alpha1 .TypeResolved ,
145- Status : metav1 .ConditionFalse ,
146- Reason : operatorsv1alpha1 .ReasonResolutionFailed ,
147- Message : err .Error (),
148- ObservedGeneration : op .GetGeneration (),
149- })
125+ setResolvedStatusConditionFailed (& op .Status .Conditions , err .Error (), op .GetGeneration ())
150126 return ctrl.Result {}, err
151127 }
152128
@@ -155,69 +131,34 @@ func (r *OperatorReconciler) reconcile(ctx context.Context, op *operatorsv1alpha
155131 bundleEntity , err := r .getBundleEntityFromSolution (solution , op .Spec .PackageName )
156132 if err != nil {
157133 op .Status .InstalledBundleResource = ""
158- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
159- Type : operatorsv1alpha1 .TypeInstalled ,
160- Status : metav1 .ConditionUnknown ,
161- Reason : operatorsv1alpha1 .ReasonInstallationStatusUnknown ,
162- Message : "installation has not been attempted as resolution failed" ,
163- ObservedGeneration : op .GetGeneration (),
164- })
134+ setInstalledStatusConditionUnknown (& op .Status .Conditions , "installation has not been attempted as resolution failed" , op .GetGeneration ())
165135 op .Status .ResolvedBundleResource = ""
166- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
167- Type : operatorsv1alpha1 .TypeResolved ,
168- Status : metav1 .ConditionFalse ,
169- Reason : operatorsv1alpha1 .ReasonResolutionFailed ,
170- Message : err .Error (),
171- ObservedGeneration : op .GetGeneration (),
172- })
136+ setResolvedStatusConditionFailed (& op .Status .Conditions , err .Error (), op .GetGeneration ())
173137 return ctrl.Result {}, err
174138 }
175139
176140 // Get the bundle image reference for the bundle
177141 bundleImage , err := bundleEntity .BundlePath ()
178142 if err != nil {
179143 op .Status .InstalledBundleResource = ""
180- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
181- Type : operatorsv1alpha1 .TypeInstalled ,
182- Status : metav1 .ConditionUnknown ,
183- Reason : operatorsv1alpha1 .ReasonInstallationStatusUnknown ,
184- Message : "installation has not been attempted as resolution failed" ,
185- ObservedGeneration : op .GetGeneration (),
186- })
144+ setInstalledStatusConditionUnknown (& op .Status .Conditions , "installation has not been attempted as resolution failed" , op .GetGeneration ())
145+
187146 op .Status .ResolvedBundleResource = ""
188- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
189- Type : operatorsv1alpha1 .TypeResolved ,
190- Status : metav1 .ConditionFalse ,
191- Reason : operatorsv1alpha1 .ReasonResolutionFailed ,
192- Message : err .Error (),
193- ObservedGeneration : op .GetGeneration (),
194- })
147+ setResolvedStatusConditionFailed (& op .Status .Conditions , err .Error (), op .GetGeneration ())
195148 return ctrl.Result {}, err
196149 }
197150
198151 // Now we can set the Resolved Condition, and the resolvedBundleSource field to the bundleImage value.
199152 op .Status .ResolvedBundleResource = bundleImage
200- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
201- Type : operatorsv1alpha1 .TypeResolved ,
202- Status : metav1 .ConditionTrue ,
203- Reason : operatorsv1alpha1 .ReasonSuccess ,
204- Message : fmt .Sprintf ("resolved to %q" , bundleImage ),
205- ObservedGeneration : op .GetGeneration (),
206- })
153+ setResolvedStatusConditionSuccess (& op .Status .Conditions , fmt .Sprintf ("resolved to %q" , bundleImage ), op .GetGeneration ())
207154
208155 // Ensure a BundleDeployment exists with its bundle source from the bundle
209156 // image we just looked up in the solution.
210157 dep := r .generateExpectedBundleDeployment (* op , bundleImage )
211158 if err := r .ensureBundleDeployment (ctx , dep ); err != nil {
212159 // originally Reason: operatorsv1alpha1.ReasonInstallationFailed
213160 op .Status .InstalledBundleResource = ""
214- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
215- Type : operatorsv1alpha1 .TypeInstalled ,
216- Status : metav1 .ConditionFalse ,
217- Reason : operatorsv1alpha1 .ReasonInstallationFailed ,
218- Message : err .Error (),
219- ObservedGeneration : op .GetGeneration (),
220- })
161+ setInstalledStatusConditionFailed (& op .Status .Conditions , err .Error (), op .GetGeneration ())
221162 return ctrl.Result {}, err
222163 }
223164
@@ -226,74 +167,60 @@ func (r *OperatorReconciler) reconcile(ctx context.Context, op *operatorsv1alpha
226167 if err := runtime .DefaultUnstructuredConverter .FromUnstructured (dep .UnstructuredContent (), existingTypedBundleDeployment ); err != nil {
227168 // originally Reason: operatorsv1alpha1.ReasonInstallationStatusUnknown
228169 op .Status .InstalledBundleResource = ""
229- apimeta .SetStatusCondition (& op .Status .Conditions , metav1.Condition {
230- Type : operatorsv1alpha1 .TypeInstalled ,
231- Status : metav1 .ConditionUnknown ,
232- Reason : operatorsv1alpha1 .ReasonInstallationStatusUnknown ,
233- Message : err .Error (),
234- ObservedGeneration : op .GetGeneration (),
235- })
170+ setInstalledStatusConditionUnknown (& op .Status .Conditions , err .Error (), op .GetGeneration ())
236171 return ctrl.Result {}, err
237172 }
238173
239174 // Let's set the proper Installed condition and InstalledBundleResource field based on the
240175 // existing BundleDeployment object status.
241- installedCond , InstalledBundleResource := mapBDStatusToInstalledCondition (existingTypedBundleDeployment , op )
242- apimeta .SetStatusCondition (& op .Status .Conditions , installedCond )
243- op .Status .InstalledBundleResource = InstalledBundleResource
176+ mapBDStatusToInstalledCondition (existingTypedBundleDeployment , op )
244177
245178 // set the status of the operator based on the respective bundle deployment status conditions.
246179 return ctrl.Result {}, nil
247180}
248181
249- func mapBDStatusToInstalledCondition (existingTypedBundleDeployment * rukpakv1alpha1.BundleDeployment , op * operatorsv1alpha1.Operator ) (metav1. Condition , string ) {
182+ func mapBDStatusToInstalledCondition (existingTypedBundleDeployment * rukpakv1alpha1.BundleDeployment , op * operatorsv1alpha1.Operator ) {
250183 bundleDeploymentReady := apimeta .FindStatusCondition (existingTypedBundleDeployment .Status .Conditions , rukpakv1alpha1 .TypeInstalled )
251184 if bundleDeploymentReady == nil {
252- return metav1.Condition {
253- Type : operatorsv1alpha1 .TypeInstalled ,
254- Status : metav1 .ConditionUnknown ,
255- Reason : operatorsv1alpha1 .ReasonInstallationStatusUnknown ,
256- Message : "bundledeployment status is unknown" ,
257- ObservedGeneration : op .GetGeneration (),
258- }, ""
185+ op .Status .InstalledBundleResource = ""
186+ setInstalledStatusConditionUnknown (& op .Status .Conditions , "bundledeployment status is unknown" , op .GetGeneration ())
187+ return
259188 }
260189
261190 if bundleDeploymentReady .Status != metav1 .ConditionTrue {
262- return metav1. Condition {
263- Type : operatorsv1alpha1 . TypeInstalled ,
264- Status : metav1 . ConditionFalse ,
265- Reason : operatorsv1alpha1 . ReasonInstallationFailed ,
266- Message : fmt . Sprintf ( "bundledeployment not ready: %s" , bundleDeploymentReady . Message ),
267- ObservedGeneration : op . GetGeneration (),
268- }, ""
191+ op . Status . InstalledBundleResource = ""
192+ setInstalledStatusConditionFailed (
193+ & op . Status . Conditions ,
194+ fmt . Sprintf ( "bundledeployment not ready: %s" , bundleDeploymentReady . Message ) ,
195+ op . GetGeneration ( ),
196+ )
197+ return
269198 }
270199
271200 bundleDeploymentSource := existingTypedBundleDeployment .Spec .Template .Spec .Source
272201 switch bundleDeploymentSource .Type {
273202 case rukpakv1alpha1 .SourceTypeImage :
274- return metav1.Condition {
275- Type : operatorsv1alpha1 .TypeInstalled ,
276- Status : metav1 .ConditionTrue ,
277- Reason : operatorsv1alpha1 .ReasonSuccess ,
278- Message : fmt .Sprintf ("installed from %q" , bundleDeploymentSource .Image .Ref ),
279- ObservedGeneration : op .GetGeneration (),
280- }, bundleDeploymentSource .Image .Ref
203+ op .Status .InstalledBundleResource = bundleDeploymentSource .Image .Ref
204+ setInstalledStatusConditionSuccess (
205+ & op .Status .Conditions ,
206+ fmt .Sprintf ("installed from %q" , bundleDeploymentSource .Image .Ref ),
207+ op .GetGeneration (),
208+ )
281209 case rukpakv1alpha1 .SourceTypeGit :
282- return metav1. Condition {
283- Type : operatorsv1alpha1 . TypeInstalled ,
284- Status : metav1 . ConditionTrue ,
285- Reason : operatorsv1alpha1 . ReasonSuccess ,
286- Message : fmt .Sprintf ("installed from %q" , bundleDeploymentSource . Git . Repository + "@" + bundleDeploymentSource . Git . Ref . Commit ),
287- ObservedGeneration : op .GetGeneration (),
288- }, bundleDeploymentSource . Git . Repository + "@" + bundleDeploymentSource . Git . Ref . Commit
210+ resource := bundleDeploymentSource . Git . Repository + "@" + bundleDeploymentSource . Git . Ref . Commit
211+ op . Status . InstalledBundleResource = resource
212+ setInstalledStatusConditionSuccess (
213+ & op . Status . Conditions ,
214+ fmt .Sprintf ("installed from %q" , resource ),
215+ op .GetGeneration (),
216+ )
289217 default :
290- return metav1.Condition {
291- Type : operatorsv1alpha1 .TypeInstalled ,
292- Status : metav1 .ConditionUnknown ,
293- Reason : operatorsv1alpha1 .ReasonInstallationStatusUnknown ,
294- Message : fmt .Sprintf ("unknown bundledeployment source type %q" , bundleDeploymentSource .Type ),
295- ObservedGeneration : op .GetGeneration (),
296- }, ""
218+ op .Status .InstalledBundleResource = ""
219+ setInstalledStatusConditionUnknown (
220+ & op .Status .Conditions ,
221+ fmt .Sprintf ("unknown bundledeployment source type %q" , bundleDeploymentSource .Type ),
222+ op .GetGeneration (),
223+ )
297224 }
298225}
299226
@@ -429,3 +356,69 @@ func verifyBDStatus(dep *rukpakv1alpha1.BundleDeployment) (metav1.ConditionStatu
429356func isBundleDepStale (bd * rukpakv1alpha1.BundleDeployment ) bool {
430357 return bd != nil && bd .Status .ObservedGeneration != bd .GetGeneration ()
431358}
359+
360+ // setResolvedStatusConditionSuccess sets the resolved status condition to success.
361+ func setResolvedStatusConditionSuccess (conditions * []metav1.Condition , message string , generation int64 ) {
362+ apimeta .SetStatusCondition (conditions , metav1.Condition {
363+ Type : operatorsv1alpha1 .TypeResolved ,
364+ Status : metav1 .ConditionTrue ,
365+ Reason : operatorsv1alpha1 .ReasonSuccess ,
366+ Message : message ,
367+ ObservedGeneration : generation ,
368+ })
369+ }
370+
371+ // setResolvedStatusConditionFailed sets the resolved status condition to failed.
372+ func setResolvedStatusConditionFailed (conditions * []metav1.Condition , message string , generation int64 ) {
373+ apimeta .SetStatusCondition (conditions , metav1.Condition {
374+ Type : operatorsv1alpha1 .TypeResolved ,
375+ Status : metav1 .ConditionFalse ,
376+ Reason : operatorsv1alpha1 .ReasonResolutionFailed ,
377+ Message : message ,
378+ ObservedGeneration : generation ,
379+ })
380+ }
381+
382+ // setResolvedStatusConditionUnknown sets the resolved status condition to unknown.
383+ func setResolvedStatusConditionUnknown (conditions * []metav1.Condition , message string , generation int64 ) {
384+ apimeta .SetStatusCondition (conditions , metav1.Condition {
385+ Type : operatorsv1alpha1 .TypeResolved ,
386+ Status : metav1 .ConditionUnknown ,
387+ Reason : operatorsv1alpha1 .ReasonResolutionUnknown ,
388+ Message : message ,
389+ ObservedGeneration : generation ,
390+ })
391+ }
392+
393+ // setInstalledStatusConditionSuccess sets the installed status condition to success.
394+ func setInstalledStatusConditionSuccess (conditions * []metav1.Condition , message string , generation int64 ) {
395+ apimeta .SetStatusCondition (conditions , metav1.Condition {
396+ Type : operatorsv1alpha1 .TypeInstalled ,
397+ Status : metav1 .ConditionTrue ,
398+ Reason : operatorsv1alpha1 .ReasonSuccess ,
399+ Message : message ,
400+ ObservedGeneration : generation ,
401+ })
402+ }
403+
404+ // setInstalledStatusConditionFailed sets the installed status condition to failed.
405+ func setInstalledStatusConditionFailed (conditions * []metav1.Condition , message string , generation int64 ) {
406+ apimeta .SetStatusCondition (conditions , metav1.Condition {
407+ Type : operatorsv1alpha1 .TypeInstalled ,
408+ Status : metav1 .ConditionFalse ,
409+ Reason : operatorsv1alpha1 .ReasonInstallationFailed ,
410+ Message : message ,
411+ ObservedGeneration : generation ,
412+ })
413+ }
414+
415+ // setInstalledStatusConditionUnknown sets the installed status condition to unknown.
416+ func setInstalledStatusConditionUnknown (conditions * []metav1.Condition , message string , generation int64 ) {
417+ apimeta .SetStatusCondition (conditions , metav1.Condition {
418+ Type : operatorsv1alpha1 .TypeInstalled ,
419+ Status : metav1 .ConditionUnknown ,
420+ Reason : operatorsv1alpha1 .ReasonInstallationStatusUnknown ,
421+ Message : message ,
422+ ObservedGeneration : generation ,
423+ })
424+ }
0 commit comments