Skip to content

Commit 752f075

Browse files
committed
capi: shutdown system even when capi failed to run
`clusterapi.System().Run()` is not atomic and it can fail after local controlplane, kube-apiserver, etcd or some controllers are already running. To make sure the capi system is properly shut down and the etcd data is cleaned up on errors, we need to `defer` the cleanup before we even attempt to run the capi system.
1 parent 9949ca9 commit 752f075

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

pkg/infrastructure/clusterapi/clusterapi.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,6 @@ func (i *InfraProvider) Provision(ctx context.Context, dir string, parents asset
128128
logrus.Debugf("No pre-provisioning requirements for the %s provider", i.impl.Name())
129129
}
130130

131-
// Run the CAPI system.
132-
timer.StartTimer(infrastructureStage)
133-
capiSystem := clusterapi.System()
134-
if err := capiSystem.Run(ctx); err != nil {
135-
return fileList, fmt.Errorf("failed to run cluster api system: %w", err)
136-
}
137-
138-
// Grab the client.
139-
cl := capiSystem.Client()
140-
141131
// If we're skipping bootstrap destroy, shutdown the local control plane.
142132
// Otherwise, we will shut it down after bootstrap destroy.
143133
// This has to execute as the last defer in the stack since previous defers might still need the local controlplane.
@@ -149,17 +139,27 @@ func (i *InfraProvider) Provision(ctx context.Context, dir string, parents asset
149139
}
150140

151141
// Make sure to always return generated manifests, even if errors happened
152-
defer func(ctx context.Context, cl client.Client) {
142+
defer func(ctx context.Context) {
153143
var errs []error
154144
// Overriding the named return with the generated list
155-
fileList, errs = i.collectManifests(ctx, cl)
145+
fileList, errs = i.collectManifests(ctx, clusterapi.System().Client())
156146
// If Provision returned an error, add it to the list
157147
if err != nil {
158-
capiSystem.CleanEtcd()
148+
clusterapi.System().CleanEtcd()
159149
errs = append(errs, err)
160150
}
161151
err = utilerrors.NewAggregate(errs)
162-
}(ctx, cl)
152+
}(ctx)
153+
154+
// Run the CAPI system.
155+
timer.StartTimer(infrastructureStage)
156+
capiSystem := clusterapi.System()
157+
if err := capiSystem.Run(ctx); err != nil {
158+
return fileList, fmt.Errorf("failed to run cluster api system: %w", err)
159+
}
160+
161+
// Grab the client.
162+
cl := capiSystem.Client()
163163

164164
i.appliedManifests = []client.Object{}
165165

0 commit comments

Comments
 (0)