Skip to content

Commit 7b1cf36

Browse files
authored
Merge pull request #12933 from k8s-infra-cherrypick-robot/cherry-pick-12923-to-release-1.11
[release-1.11] 🌱 CAPD: recreate container if we re-enter reconciliation and it exists but is not running
2 parents 8b0daf1 + 85c03b3 commit 7b1cf36

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

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

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

100+
// 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.
101+
if externalMachine.Exists() && !externalMachine.IsRunning() {
102+
// This deletes the machine and results in re-creating it below.
103+
if err := externalMachine.Delete(ctx); err != nil {
104+
return errors.Wrap(err, "Failed to delete not running DockerMachine")
105+
}
106+
}
107+
100108
log.Info("Creating container for machinePool", "name", name, "MachinePool", klog.KObj(machinePool))
101109
if err := externalMachine.Create(ctx, dockerMachinePool.Spec.Template.CustomImage, constants.WorkerNodeRoleValue, machinePool.Spec.Template.Spec.Version, labels, dockerMachinePool.Spec.Template.ExtraMounts); err != nil {
102110
return errors.Wrapf(err, "failed to create docker machine with name %s", name)

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

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

196+
// 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.
197+
if externalMachine.Exists() && !externalMachine.IsRunning() {
198+
// This deletes the machine and results in re-creating it below.
199+
if err := externalMachine.Delete(ctx); err != nil {
200+
return ctrl.Result{}, errors.Wrap(err, "Failed to delete not running DockerMachine")
201+
}
202+
}
203+
196204
// Create the machine if not existing yet
197205
if !externalMachine.Exists() {
198206
// NOTE: FailureDomains don't mean much in CAPD since it's all local, but we are setting a label on

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)