Skip to content

Commit 6384bca

Browse files
authored
Merge pull request #12923 from chrischdi/pr-e2e-capd-recreate-container-if-not-running
🌱 CAPD: recreate container if we re-enter reconciliation and it exists but is not running
2 parents 2a3fabb + 9bfdc0e commit 6384bca

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

test/infrastructure/docker/internal/controllers/backends/docker/dockermachine_backend.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ func (r *MachineBackendReconciler) ReconcileNormal(ctx context.Context, cluster
192192
role = constants.ControlPlaneNodeRoleValue
193193
}
194194

195+
// If re-entering the reconcile loop and reaching this point, the container is expected to be running. If it is not, delete it so we can try to create it again.
196+
if externalMachine.Exists() && !externalMachine.IsRunning() {
197+
// This deletes the machine and results in re-creating it below.
198+
if err := externalMachine.Delete(ctx); err != nil {
199+
return ctrl.Result{}, errors.Wrap(err, "Failed to delete not running DockerMachine")
200+
}
201+
}
202+
195203
// Create the machine if not existing yet
196204
if !externalMachine.Exists() {
197205
// NOTE: FailureDomains don't mean much in CAPD since it's all local, but we are setting a label on

test/infrastructure/docker/internal/controllers/dockermachinepool_controller_phases.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func createDockerContainer(ctx context.Context, name string, cluster *clusterv1.
9696
}
9797
}
9898

99+
// If re-entering the reconcile loop and reaching this point, the container is expected to be running. If it is not, delete it so we can try to create it again.
100+
if externalMachine.Exists() && !externalMachine.IsRunning() {
101+
// This deletes the machine and results in re-creating it below.
102+
if err := externalMachine.Delete(ctx); err != nil {
103+
return errors.Wrap(err, "Failed to delete not running DockerMachine")
104+
}
105+
}
106+
99107
log.Info("Creating container for machinePool", "name", name, "MachinePool", klog.KObj(machinePool))
100108
if err := externalMachine.Create(ctx, dockerMachinePool.Spec.Template.CustomImage, constants.WorkerNodeRoleValue, machinePool.Spec.Template.Spec.Version, labels, dockerMachinePool.Spec.Template.ExtraMounts); err != nil {
101109
return errors.Wrapf(err, "failed to create docker machine with name %s", name)

test/infrastructure/docker/internal/docker/machine.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ func (m *Machine) Exists() bool {
158158
return m.container != nil
159159
}
160160

161+
// IsRunning returns true if the container for this machine is running.
162+
func (m *Machine) IsRunning() bool {
163+
if !m.Exists() {
164+
return false
165+
}
166+
167+
return m.container.IsRunning()
168+
}
169+
161170
// Name returns the name of the machine.
162171
func (m *Machine) Name() string {
163172
return m.machine
@@ -540,6 +549,8 @@ func (m *Machine) Delete(ctx context.Context) error {
540549
if err := m.container.Delete(ctx); err != nil {
541550
return err
542551
}
552+
553+
m.container = nil
543554
}
544555
return nil
545556
}

0 commit comments

Comments
 (0)