Skip to content

Commit 2893b28

Browse files
committed
extracted status update logic and removed hardcoded statuses
1 parent 1301b28 commit 2893b28

File tree

9 files changed

+88
-62
lines changed

9 files changed

+88
-62
lines changed

api/v1alpha1/condition_types.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,25 @@ const (
6161

6262
// CreateRepositoryNameReason is used when the generating a new repository name fails.
6363
CreateRepositoryNameReason = "CreateRepositoryNameFailed"
64+
65+
// ConfigRefNotReadyWithErrorReason is used when configuration reference is not ready yet with an error.
66+
ConfigRefNotReadyWithErrorReason = "ConfigRefNotReadyWithError"
67+
68+
// ConfigRefNotReadyReason is used when configuration ref is not ready yet and there was no error.
69+
ConfigRefNotReadyReason = "ConfigRefNotReady"
70+
71+
// SourceRefNotReadyWithErrorReason is used when the source ref is not ready and there was an error.
72+
SourceRefNotReadyWithErrorReason = "SourceRefNotReadyWithError"
73+
74+
// SourceRefNotReadyReason is used when the source ref is not ready and there was no error.
75+
SourceRefNotReadyReason = "SourceRefNotReady"
76+
77+
// PatchStrategicMergeSourceRefNotReadyWithErrorReason is used when source ref for patch strategic merge is not ready and there was an error.
78+
PatchStrategicMergeSourceRefNotReadyWithErrorReason = "PatchStrategicMergeSourceRefNotReadyWithError"
79+
80+
// PatchStrategicMergeSourceRefNotReadyReason is used when source ref for patch strategic merge is not ready and there was no error.
81+
PatchStrategicMergeSourceRefNotReadyReason = "PatchStrategicMergeSourceRefNotReady"
82+
83+
// SnapshotNameEmptyReason is used for a failure to generate a snapshot name.
84+
SnapshotNameEmptyReason = "SnapshotNameEmpty"
6485
)

controllers/componentversion_controller.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/fluxcd/pkg/runtime/conditions"
1616
"github.com/fluxcd/pkg/runtime/patch"
1717
rreconcile "github.com/fluxcd/pkg/runtime/reconcile"
18+
"github.com/open-component-model/ocm-controller/pkg/status"
1819
corev1 "k8s.io/api/core/v1"
1920
apierrors "k8s.io/apimachinery/pkg/api/errors"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -139,7 +140,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req
139140

140141
// Always attempt to patch the object and status after each reconciliation.
141142
defer func() {
142-
if derr := updateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil {
143+
if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil {
143144
retErr = errors.Join(retErr, derr)
144145
}
145146
}()
@@ -153,7 +154,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req
153154
if err != nil {
154155
// we don't fail here, because all manifests might have been applied at once or the secret
155156
// for authentication is being reconciled.
156-
MarkAsStalled(
157+
status.MarkAsStalled(
157158
r.EventRecorder,
158159
obj,
159160
v1alpha1.AuthenticatedContextCreationFailedReason,
@@ -167,7 +168,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req
167168
update, version, err := r.checkVersion(ctx, octx, obj)
168169
if err != nil {
169170
// The component might not be there yet. We don't fail but keep polling instead.
170-
MarkNotReady(
171+
status.MarkNotReady(
171172
r.EventRecorder,
172173
obj,
173174
v1alpha1.CheckVersionFailedReason,
@@ -196,7 +197,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req
196197

197198
ok, err := r.OCMClient.VerifyComponent(ctx, octx, obj, version)
198199
if err != nil {
199-
MarkNotReady(
200+
status.MarkNotReady(
200201
r.EventRecorder,
201202
obj,
202203
v1alpha1.VerificationFailedReason,
@@ -209,7 +210,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req
209210
}
210211

211212
if !ok {
212-
MarkNotReady(
213+
status.MarkNotReady(
213214
r.EventRecorder,
214215
obj,
215216
v1alpha1.VerificationFailedReason,
@@ -233,7 +234,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con
233234
cv, err := r.OCMClient.GetComponentVersion(ctx, octx, obj, obj.Spec.Component, version)
234235
if err != nil {
235236
err = fmt.Errorf("failed to get component version: %w", err)
236-
MarkNotReady(
237+
status.MarkNotReady(
237238
r.EventRecorder,
238239
obj,
239240
v1alpha1.ComponentVersionInvalidReason,
@@ -249,7 +250,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con
249250
cd, err := dv.ConvertFrom(cv.GetDescriptor())
250251
if err != nil {
251252
err = fmt.Errorf("failed to convert component descriptor: %w", err)
252-
MarkNotReady(
253+
status.MarkNotReady(
253254
r.EventRecorder,
254255
obj,
255256
v1alpha1.ConvertComponentDescriptorFailedReason,
@@ -264,7 +265,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con
264265
componentName, err := component.ConstructUniqueName(cd.GetName(), cd.GetVersion(), nil)
265266
if err != nil {
266267
err = fmt.Errorf("failed to generate name: %w", err)
267-
MarkNotReady(
268+
status.MarkNotReady(
268269
r.EventRecorder,
269270
obj,
270271
v1alpha1.NameGenerationFailedReason,
@@ -301,7 +302,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con
301302

302303
if err != nil {
303304
err = fmt.Errorf("failed to create or update component descriptor: %w", err)
304-
MarkNotReady(
305+
status.MarkNotReady(
305306
r.EventRecorder,
306307
obj,
307308
v1alpha1.CreateOrUpdateComponentDescriptorFailedReason,
@@ -325,7 +326,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con
325326
componentDescriptor.References, err = r.parseReferences(ctx, octx, obj, cv.GetDescriptor().References)
326327
if err != nil {
327328
err = fmt.Errorf("failed to parse references: %w", err)
328-
MarkNotReady(
329+
status.MarkNotReady(
329330
r.EventRecorder,
330331
obj,
331332
v1alpha1.ParseReferencesFailedReason,

controllers/configuration_controller.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/fluxcd/pkg/runtime/patch"
1616
rreconcile "github.com/fluxcd/pkg/runtime/reconcile"
1717
sourcev1 "github.com/fluxcd/source-controller/api/v1"
18+
"github.com/open-component-model/ocm-controller/pkg/status"
1819
"golang.org/x/exp/slices"
1920
apierrors "k8s.io/apimachinery/pkg/api/errors"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -168,7 +169,7 @@ func (r *ConfigurationReconciler) Reconcile(
168169

169170
// Always attempt to patch the object and status after each reconciliation.
170171
defer func() {
171-
if derr := updateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil {
172+
if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil {
172173
err = errors.Join(err, derr)
173174
}
174175
}()
@@ -181,16 +182,16 @@ func (r *ConfigurationReconciler) Reconcile(
181182
// check dependencies are ready
182183
ready, err := r.checkReadiness(ctx, obj.GetNamespace(), &obj.Spec.SourceRef)
183184
if err != nil {
184-
MarkNotReady(r.EventRecorder, obj, "SourceRefNotReadyWithError", err.Error())
185+
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceRefNotReadyWithErrorReason, err.Error())
185186

186187
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
187188
}
188189

189190
if !ready {
190-
MarkNotReady(
191+
status.MarkNotReady(
191192
r.EventRecorder,
192193
obj,
193-
"SourceRefNotReady",
194+
v1alpha1.SourceRefNotReadyReason,
194195
fmt.Sprintf("source ref not yet ready: %s", obj.Spec.SourceRef.Name),
195196
)
196197

@@ -200,20 +201,20 @@ func (r *ConfigurationReconciler) Reconcile(
200201
if obj.Spec.ConfigRef != nil {
201202
ready, err := r.checkReadiness(ctx, obj.GetNamespace(), obj.Spec.ConfigRef)
202203
if err != nil {
203-
MarkNotReady(
204+
status.MarkNotReady(
204205
r.EventRecorder,
205206
obj,
206-
"ConfigRefNotReadyWithError",
207+
v1alpha1.ConfigRefNotReadyWithErrorReason,
207208
fmt.Sprintf("config ref not yet ready with error: %s: %s", obj.Spec.ConfigRef.Name, err),
208209
)
209210

210211
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
211212
}
212213
if !ready {
213-
MarkNotReady(
214+
status.MarkNotReady(
214215
r.EventRecorder,
215216
obj,
216-
"ConfigRefNotReady",
217+
v1alpha1.ConfigRefNotReadyReason,
217218
fmt.Sprintf("config ref not yet ready: %s", obj.Spec.ConfigRef.Name),
218219
)
219220

@@ -224,21 +225,21 @@ func (r *ConfigurationReconciler) Reconcile(
224225
if obj.Spec.PatchStrategicMerge != nil {
225226
ready, err := r.checkFluxSourceReadiness(ctx, obj.Spec.PatchStrategicMerge.Source.SourceRef)
226227
if err != nil {
227-
MarkNotReady(
228+
status.MarkNotReady(
228229
r.EventRecorder,
229230
obj,
230-
"PatchStrategicMergeSourceRefNotReadyWithError",
231+
v1alpha1.PatchStrategicMergeSourceRefNotReadyWithErrorReason,
231232
fmt.Sprintf("patch strategic merge source ref not yet ready with error: %s: %s", obj.Spec.PatchStrategicMerge.Source.SourceRef.Name, err),
232233
)
233234

234235
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
235236
}
236237

237238
if !ready {
238-
MarkNotReady(
239+
status.MarkNotReady(
239240
r.EventRecorder,
240241
obj,
241-
"PatchStrategicMergeSourceRefNotReady",
242+
v1alpha1.PatchStrategicMergeSourceRefNotReadyReason,
242243
fmt.Sprintf("patch strategic merge source ref not yet ready: %s", obj.Spec.PatchStrategicMerge.Source.SourceRef.Name),
243244
)
244245

@@ -252,7 +253,7 @@ func (r *ConfigurationReconciler) Reconcile(
252253
name, err := snapshot.GenerateSnapshotName(obj.GetName())
253254
if err != nil {
254255
err := fmt.Errorf("failed to generate snapshot name for: %s: %s", obj.GetName(), err)
255-
MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error())
256+
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error())
256257

257258
return ctrl.Result{}, err
258259
}
@@ -288,13 +289,13 @@ func (r *ConfigurationReconciler) reconcile(
288289

289290
if errors.Is(err, errTar) {
290291
err = fmt.Errorf("source resource is not a tar archive: %w", err)
291-
MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceReasonNotATarArchiveReason, err.Error())
292+
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceReasonNotATarArchiveReason, err.Error())
292293

293294
return ctrl.Result{}, err
294295
}
295296

296297
err = fmt.Errorf("failed to reconcile mutation object: %w", err)
297-
MarkNotReady(r.EventRecorder, obj, v1alpha1.ReconcileMutationObjectFailedReason, err.Error())
298+
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ReconcileMutationObjectFailedReason, err.Error())
298299

299300
return ctrl.Result{}, err
300301
}

controllers/localization_controller.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
rreconcile "github.com/fluxcd/pkg/runtime/reconcile"
1717
sourcev1 "github.com/fluxcd/source-controller/api/v1"
1818
"github.com/fluxcd/source-controller/api/v1beta2"
19+
"github.com/open-component-model/ocm-controller/pkg/status"
1920
"golang.org/x/exp/slices"
2021
apierrors "k8s.io/apimachinery/pkg/api/errors"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -151,7 +152,7 @@ func (r *LocalizationReconciler) Reconcile(
151152

152153
// Always attempt to patch the object and status after each reconciliation.
153154
defer func() {
154-
if derr := updateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil {
155+
if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil {
155156
err = errors.Join(err, derr)
156157
}
157158
}()
@@ -164,16 +165,16 @@ func (r *LocalizationReconciler) Reconcile(
164165
// check dependencies are ready
165166
ready, err := r.checkReadiness(ctx, obj.GetNamespace(), &obj.Spec.SourceRef)
166167
if err != nil {
167-
MarkNotReady(r.EventRecorder, obj, "SourceRefNotReadyWithError", err.Error())
168+
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceRefNotReadyWithErrorReason, err.Error())
168169

169170
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
170171
}
171172

172173
if !ready {
173-
MarkNotReady(
174+
status.MarkNotReady(
174175
r.EventRecorder,
175176
obj,
176-
"SourceRefNotReady",
177+
v1alpha1.SourceRefNotReadyReason,
177178
fmt.Sprintf("source ref not yet ready: %s", obj.Spec.SourceRef.Name),
178179
)
179180

@@ -183,20 +184,20 @@ func (r *LocalizationReconciler) Reconcile(
183184
if obj.Spec.ConfigRef != nil {
184185
ready, err := r.checkReadiness(ctx, obj.GetNamespace(), obj.Spec.ConfigRef)
185186
if err != nil {
186-
MarkNotReady(
187+
status.MarkNotReady(
187188
r.EventRecorder,
188189
obj,
189-
"ConfigRefNotReadyWithError",
190+
v1alpha1.ConfigRefNotReadyWithErrorReason,
190191
fmt.Sprintf("config ref not yet ready with error: %s: %s", obj.Spec.ConfigRef.Name, err),
191192
)
192193

193194
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
194195
}
195196
if !ready {
196-
MarkNotReady(
197+
status.MarkNotReady(
197198
r.EventRecorder,
198199
obj,
199-
"ConfigRefNotReady",
200+
v1alpha1.ConfigRefNotReadyReason,
200201
fmt.Sprintf("config ref not yet ready: %s", obj.Spec.ConfigRef.Name),
201202
)
202203

@@ -207,21 +208,21 @@ func (r *LocalizationReconciler) Reconcile(
207208
if obj.Spec.PatchStrategicMerge != nil {
208209
ready, err := r.checkFluxSourceReadiness(ctx, obj.Spec.PatchStrategicMerge.Source.SourceRef)
209210
if err != nil {
210-
MarkNotReady(
211+
status.MarkNotReady(
211212
r.EventRecorder,
212213
obj,
213-
"PatchStrategicMergeSourceRefNotReadyWithError",
214+
v1alpha1.PatchStrategicMergeSourceRefNotReadyWithErrorReason,
214215
fmt.Sprintf("patch strategic merge source ref not yet ready with error: %s: %s", obj.Spec.PatchStrategicMerge.Source.SourceRef.Name, err),
215216
)
216217

217218
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
218219
}
219220

220221
if !ready {
221-
MarkNotReady(
222+
status.MarkNotReady(
222223
r.EventRecorder,
223224
obj,
224-
"PatchStrategicMergeSourceRefNotReady",
225+
v1alpha1.PatchStrategicMergeSourceRefNotReadyReason,
225226
fmt.Sprintf("patch strategic merge source ref not yet ready: %s", obj.Spec.PatchStrategicMerge.Source.SourceRef.Name),
226227
)
227228

@@ -235,7 +236,7 @@ func (r *LocalizationReconciler) Reconcile(
235236
name, err := snapshot.GenerateSnapshotName(obj.GetName())
236237
if err != nil {
237238
err = fmt.Errorf("failed to generate snapshot name for: %s: %s", obj.GetName(), err)
238-
MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error())
239+
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error())
239240

240241
return ctrl.Result{}, err
241242
}
@@ -269,13 +270,13 @@ func (r *LocalizationReconciler) reconcile(
269270

270271
if errors.Is(err, errTar) {
271272
err = fmt.Errorf("source resource is not a tar archive: %w", err)
272-
MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceReasonNotATarArchiveReason, err.Error())
273+
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceReasonNotATarArchiveReason, err.Error())
273274

274275
return ctrl.Result{}, err
275276
}
276277

277278
err = fmt.Errorf("failed to reconcile mutation object: %w", err)
278-
MarkNotReady(r.EventRecorder, obj, v1alpha1.ReconcileMutationObjectFailedReason, err.Error())
279+
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ReconcileMutationObjectFailedReason, err.Error())
279280

280281
return ctrl.Result{}, err
281282
}
@@ -431,9 +432,9 @@ func makeRequestsForLocalizations(ll ...v1alpha1.Localization) []reconcile.Reque
431432

432433
requests := make([]reconcile.Request, len(refs))
433434
for i, item := range refs {
434-
// if the observedgeneration is -1
435+
// if the ObservedGeneration is -1
435436
// then the object has not been initialised yet
436-
// so we should not trigger a reconcilation for sourceRef/configRefs
437+
// so we should not trigger a reconciliation for sourceRef/configRefs
437438
if item.Status.ObservedGeneration != -1 {
438439
requests[i] = reconcile.Request{
439440
NamespacedName: types.NamespacedName{

0 commit comments

Comments
 (0)