Skip to content

Commit 358e072

Browse files
author
Yuvaraj Kakaraparthi
committed
improved error wrapping for hook tracking
1 parent 4b07b53 commit 358e072

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

internal/controllers/topology/cluster/cluster_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
355355
// The BeforeClusterDelete hook returned a non-blocking response. Now the cluster is ready to be deleted.
356356
// Lets mark the cluster as `ok-to-delete`
357357
if err := hooks.MarkAsOkToDelete(ctx, r.Client, cluster); err != nil {
358-
return ctrl.Result{}, errors.Wrapf(err, "failed to mark %s/%s cluster as ok to delete", cluster.Namespace, cluster.Name)
358+
return ctrl.Result{}, err
359359
}
360360
}
361361
}

internal/controllers/topology/cluster/desired_state.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131

3232
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3333
"sigs.k8s.io/cluster-api/controllers/external"
34-
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
3534
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
3635
"sigs.k8s.io/cluster-api/feature"
3736
"sigs.k8s.io/cluster-api/internal/contract"
@@ -338,7 +337,7 @@ func (r *Reconciler) computeControlPlaneVersion(ctx context.Context, s *scope.Sc
338337
s.UpgradeTracker.MachineDeployments.HoldUpgrades(true)
339338
} else {
340339
if err := hooks.MarkAsDone(ctx, r.Client, s.Current.Cluster, runtimehooksv1.AfterControlPlaneUpgrade); err != nil {
341-
return "", errors.Wrapf(err, "failed to remove the %s hook from pending hooks tracker", runtimecatalog.HookName(runtimehooksv1.AfterControlPlaneUpgrade))
340+
return "", err
342341
}
343342
}
344343
}
@@ -390,7 +389,7 @@ func (r *Reconciler) computeControlPlaneVersion(ctx context.Context, s *scope.Sc
390389
// We are picking up the new version here.
391390
// Track the intent of calling the AfterControlPlaneUpgrade and the AfterClusterUpgrade hooks once we are done with the upgrade.
392391
if err := hooks.MarkAsPending(ctx, r.Client, s.Current.Cluster, runtimehooksv1.AfterControlPlaneUpgrade, runtimehooksv1.AfterClusterUpgrade); err != nil {
393-
return "", errors.Wrapf(err, "failed to mark the %s hook as pending", []string{runtimecatalog.HookName(runtimehooksv1.AfterControlPlaneUpgrade), runtimecatalog.HookName(runtimehooksv1.AfterClusterUpgrade)})
392+
return "", err
394393
}
395394
}
396395

internal/controllers/topology/cluster/reconcile_state.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"sigs.k8s.io/controller-runtime/pkg/client"
3232

3333
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
34-
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
3534
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
3635
"sigs.k8s.io/cluster-api/feature"
3736
"sigs.k8s.io/cluster-api/internal/contract"
@@ -204,7 +203,7 @@ func (r *Reconciler) callAfterControlPlaneInitialized(ctx context.Context, s *sc
204203
// If the cluster topology is being created then track to intent to call the AfterControlPlaneInitialized hook so that we can call it later.
205204
if s.Current.Cluster.Spec.InfrastructureRef == nil && s.Current.Cluster.Spec.ControlPlaneRef == nil {
206205
if err := hooks.MarkAsPending(ctx, r.Client, s.Current.Cluster, runtimehooksv1.AfterControlPlaneInitialized); err != nil {
207-
return errors.Wrapf(err, "failed to remove the %s hook from pending hooks tracker", runtimecatalog.HookName(runtimehooksv1.AfterControlPlaneInitialized))
206+
return err
208207
}
209208
}
210209

internal/hooks/tracking.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ import (
3434
// MarkAsPending adds to the object's PendingHooksAnnotation the intent to execute a hook after an operation completes.
3535
// Usually this function is called when an operation is starting in order to track the intent to call an After<operation> hook later in the process.
3636
func MarkAsPending(ctx context.Context, c client.Client, obj client.Object, hooks ...runtimecatalog.Hook) error {
37+
hookNames := []string{}
38+
for _, hook := range hooks {
39+
hookNames = append(hookNames, runtimecatalog.HookName(hook))
40+
}
41+
3742
patchHelper, err := patch.NewHelper(obj, c)
3843
if err != nil {
39-
return errors.Wrapf(err, "failed to create patch helper for %s", tlog.KObj{Obj: obj})
44+
return errors.Wrapf(err, "failed to mark %q hook(s) as pending: failed to create patch helper for %s", strings.Join(hookNames, ","), tlog.KObj{Obj: obj})
4045
}
4146

4247
// Read the annotation of the objects and add the hook to the comma separated list
43-
hookNames := []string{}
44-
for _, hook := range hooks {
45-
hookNames = append(hookNames, runtimecatalog.HookName(hook))
46-
}
4748
annotations := obj.GetAnnotations()
4849
if annotations == nil {
4950
annotations = map[string]string{}
@@ -52,7 +53,7 @@ func MarkAsPending(ctx context.Context, c client.Client, obj client.Object, hook
5253
obj.SetAnnotations(annotations)
5354

5455
if err := patchHelper.Patch(ctx, obj); err != nil {
55-
return errors.Wrapf(err, "failed to patch %s", tlog.KObj{Obj: obj})
56+
return errors.Wrapf(err, "failed to mark %q hook(s) as pending: failed to patch %s", strings.Join(hookNames, ","), tlog.KObj{Obj: obj})
5657
}
5758

5859
return nil
@@ -72,16 +73,17 @@ func IsPending(hook runtimecatalog.Hook, obj client.Object) bool {
7273
// Usually this func is called after all the registered extensions for the Hook returned an answer without requests
7374
// to hold on to the object's lifecycle (retryAfterSeconds).
7475
func MarkAsDone(ctx context.Context, c client.Client, obj client.Object, hooks ...runtimecatalog.Hook) error {
76+
hookNames := []string{}
77+
for _, hook := range hooks {
78+
hookNames = append(hookNames, runtimecatalog.HookName(hook))
79+
}
80+
7581
patchHelper, err := patch.NewHelper(obj, c)
7682
if err != nil {
77-
return errors.Wrapf(err, "failed to create patch helper for %s", tlog.KObj{Obj: obj})
83+
return errors.Wrapf(err, "failed to mark %s hook(s) as done: failed to create patch helper for %s", strings.Join(hookNames, ","), tlog.KObj{Obj: obj})
7884
}
7985

8086
// Read the annotation of the objects and add the hook to the comma separated list
81-
hookNames := []string{}
82-
for _, hook := range hooks {
83-
hookNames = append(hookNames, runtimecatalog.HookName(hook))
84-
}
8587
annotations := obj.GetAnnotations()
8688
if annotations == nil {
8789
annotations = map[string]string{}
@@ -93,7 +95,7 @@ func MarkAsDone(ctx context.Context, c client.Client, obj client.Object, hooks .
9395
obj.SetAnnotations(annotations)
9496

9597
if err := patchHelper.Patch(ctx, obj); err != nil {
96-
return errors.Wrapf(err, "failed to patch %s", tlog.KObj{Obj: obj})
98+
return errors.Wrapf(err, "failed to mark %s hook(s) as done: failed to patch %s", strings.Join(hookNames, ","), tlog.KObj{Obj: obj})
9799
}
98100

99101
return nil
@@ -132,7 +134,7 @@ func IsOkToDelete(obj client.Object) bool {
132134
func MarkAsOkToDelete(ctx context.Context, c client.Client, obj client.Object) error {
133135
patchHelper, err := patch.NewHelper(obj, c)
134136
if err != nil {
135-
return errors.Wrapf(err, "failed to create patch helper for %s", tlog.KObj{Obj: obj})
137+
return errors.Wrapf(err, "failed to mark %s as ok to delete: failed to create patch helper", tlog.KObj{Obj: obj})
136138
}
137139

138140
annotations := obj.GetAnnotations()
@@ -143,7 +145,7 @@ func MarkAsOkToDelete(ctx context.Context, c client.Client, obj client.Object) e
143145
obj.SetAnnotations(annotations)
144146

145147
if err := patchHelper.Patch(ctx, obj); err != nil {
146-
return errors.Wrapf(err, "failed to patch %s", tlog.KObj{Obj: obj})
148+
return errors.Wrapf(err, "failed to mark %s as ok to delete: failed to patch", tlog.KObj{Obj: obj})
147149
}
148150

149151
return nil

0 commit comments

Comments
 (0)