Skip to content

Commit 97fbdc8

Browse files
committed
fix: surface errors from mutator failures
Signed-off-by: Tarun Gupta Akirala <[email protected]>
1 parent 7b5b587 commit 97fbdc8

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

cmd/clusterctl/client/cluster/mover.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import (
4343
)
4444

4545
// ResourceMutatorFunc holds the type for mutators to be applied on resources during a move operation.
46-
type ResourceMutatorFunc func(u *unstructured.Unstructured)
46+
type ResourceMutatorFunc func(u *unstructured.Unstructured) error
4747

4848
// ObjectMover defines methods for moving Cluster API objects to another management cluster.
4949
type ObjectMover interface {
@@ -606,7 +606,10 @@ func patchCluster(proxy Proxy, n *node, patch client.Patch, mutators ...Resource
606606
clusterObj.SetName(n.identity.Name)
607607
clusterObj.SetNamespace(n.identity.Namespace)
608608
for _, mutator := range mutators {
609-
mutator(clusterObj)
609+
if err = mutator(clusterObj); err != nil {
610+
return errors.Wrapf(err, "error applying resource mutator to %q %s/%s",
611+
clusterObj.GroupVersionKind(), clusterObj.GetNamespace(), clusterObj.GetName())
612+
}
610613
}
611614

612615
if err := cFrom.Get(ctx, client.ObjectKeyFromObject(clusterObj), clusterObj); err != nil {
@@ -635,7 +638,10 @@ func pauseClusterClass(proxy Proxy, n *node, pause bool, mutators ...ResourceMut
635638
clusterClass.SetName(n.identity.Name)
636639
clusterClass.SetNamespace(n.identity.Namespace)
637640
for _, mutator := range mutators {
638-
mutator(clusterClass)
641+
if err = mutator(clusterClass); err != nil {
642+
return errors.Wrapf(err, "error applying resource mutator to %q %s/%s",
643+
clusterClass.GroupVersionKind(), clusterClass.GetNamespace(), clusterClass.GetName())
644+
}
639645
}
640646
if err := cFrom.Get(ctx, client.ObjectKeyFromObject(clusterClass), clusterClass); err != nil {
641647
return errors.Wrapf(err, "error reading ClusterClass %s/%s", n.identity.Namespace, n.identity.Name)
@@ -887,7 +893,10 @@ func (o *objectMover) createTargetObject(nodeToCreate *node, toProxy Proxy, muta
887893
}
888894

889895
for _, mutator := range mutators {
890-
mutator(obj)
896+
if err = mutator(obj); err != nil {
897+
return errors.Wrapf(err, "error applying resource mutator to %q %s/%s",
898+
obj.GroupVersionKind(), obj.GetNamespace(), obj.GetName())
899+
}
891900
}
892901
// Applying mutators MAY change the namespace, so ensure the namespace exists before creating the resource.
893902
if !nodeToCreate.isGlobal && !existingNamespaces.Has(obj.GetNamespace()) {

cmd/clusterctl/client/cluster/mover_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,9 +1185,9 @@ func Test_objectMover_move(t *testing.T) {
11851185
{"spec", "infrastructureRef", "namespace"},
11861186
},
11871187
}
1188-
var namespaceMutator ResourceMutatorFunc = func(u *unstructured.Unstructured) {
1188+
var namespaceMutator ResourceMutatorFunc = func(u *unstructured.Unstructured) error {
11891189
if u == nil || u.Object == nil {
1190-
return
1190+
return nil
11911191
}
11921192
if u.GetNamespace() != "" {
11931193
u.SetNamespace(toNamespace)
@@ -1201,6 +1201,7 @@ func Test_objectMover_move(t *testing.T) {
12011201
}
12021202
}
12031203
}
1204+
return nil
12041205
}
12051206

12061207
// Create an objectGraph bound a source cluster with all the CRDs for the types involved in the test.
@@ -1226,6 +1227,7 @@ func Test_objectMover_move(t *testing.T) {
12261227
if includeMutator {
12271228
mutators = append(mutators, namespaceMutator)
12281229
}
1230+
12291231
err := mover.move(graph, toProxy, mutators...)
12301232

12311233
if tt.wantErr {

0 commit comments

Comments
 (0)