@@ -46,12 +46,12 @@ import (
46
46
// reconcileDockerContainers manages the Docker containers for a MachinePool such that it
47
47
// - Ensures the number of up-to-date Docker containers is equal to the MachinePool's desired replica count.
48
48
// - Does not delete any containers as that must be triggered in reconcileDockerMachines to ensure node cordon/drain.
49
+ // - Create the DockerMachine CR after creating the container.
49
50
//
50
51
// Providers should similarly create their infrastructure instances and reconcile any additional logic.
51
52
func (r * DockerMachinePoolReconciler ) reconcileDockerContainers (ctx context.Context , cluster * clusterv1.Cluster , machinePool * expv1.MachinePool , dockerMachinePool * infraexpv1.DockerMachinePool ) error {
52
53
log := ctrl .LoggerFrom (ctx )
53
-
54
- log .V (2 ).Info ("Reconciling Docker containers" , "DockerMachinePool" , klog .KObj (dockerMachinePool ))
54
+ log .Info ("Reconciling Docker containers" , "DockerMachinePool" , klog .KObj (dockerMachinePool ))
55
55
56
56
labelFilters := map [string ]string {dockerMachinePoolLabel : dockerMachinePool .Name }
57
57
@@ -63,11 +63,17 @@ func (r *DockerMachinePoolReconciler) reconcileDockerContainers(ctx context.Cont
63
63
matchingMachineCount := len (machinesMatchingInfrastructureSpec (ctx , machines , machinePool , dockerMachinePool ))
64
64
numToCreate := int (* machinePool .Spec .Replicas ) - matchingMachineCount
65
65
for range numToCreate {
66
- log .V ( 2 ). Info ("Creating a new Docker container for machinePool" , "MachinePool" , klog .KObj (machinePool ))
66
+ log .Info ("Creating a new Docker container for machinePool" , "MachinePool" , klog .KObj (machinePool ))
67
67
name := fmt .Sprintf ("worker-%s" , util .RandomString (6 ))
68
68
if err := createDockerContainer (ctx , name , cluster , machinePool , dockerMachinePool ); err != nil {
69
69
return errors .Wrap (err , "failed to create a new docker machine" )
70
70
}
71
+
72
+ log .Info ("Creating a new DockerMachine for dockerMachinePool" , "DockerMachinePool" , klog .KObj (dockerMachinePool ))
73
+ dockerMachine := computeDesiredDockerMachine (name , cluster , machinePool , dockerMachinePool , nil )
74
+ if err := ssa .Patch (ctx , r .Client , dockerMachinePoolControllerName , dockerMachine ); err != nil {
75
+ return errors .Wrap (err , "failed to create a new docker machine" )
76
+ }
71
77
}
72
78
73
79
return nil
@@ -107,15 +113,14 @@ func createDockerContainer(ctx context.Context, name string, cluster *clusterv1.
107
113
108
114
// reconcileDockerMachines creates and deletes DockerMachines to match the MachinePool's desired number of replicas and infrastructure spec.
109
115
// It is responsible for
110
- // - Ensuring each Docker container has an associated DockerMachine by creating one if it doesn't already exist.
111
116
// - Ensuring that deletion for Docker container happens by calling delete on the associated Machine so that the node is cordoned/drained and the infrastructure is cleaned up.
112
117
// - Deleting DockerMachines referencing a container whose Kubernetes version or custom image no longer matches the spec.
113
118
// - Deleting DockerMachines that correspond to a deleted/non-existent Docker container.
114
119
// - Deleting DockerMachines when scaling down such that DockerMachines whose owner Machine has the clusterv1.DeleteMachineAnnotation is given priority.
115
120
func (r * DockerMachinePoolReconciler ) reconcileDockerMachines (ctx context.Context , cluster * clusterv1.Cluster , machinePool * expv1.MachinePool , dockerMachinePool * infraexpv1.DockerMachinePool ) error {
116
121
log := ctrl .LoggerFrom (ctx )
117
122
118
- log .V ( 2 ). Info ("Reconciling DockerMachines" , "DockerMachinePool" , klog .KObj (dockerMachinePool ))
123
+ log .Info ("Reconciling DockerMachines" , "DockerMachinePool" , klog .KObj (dockerMachinePool ))
119
124
120
125
dockerMachineList , err := getDockerMachines (ctx , r .Client , * cluster , * machinePool , * dockerMachinePool )
121
126
if err != nil {
@@ -140,36 +145,13 @@ func (r *DockerMachinePoolReconciler) reconcileDockerMachines(ctx context.Contex
140
145
}
141
146
142
147
// Step 1:
143
- // Create a DockerMachine for each Docker container so we surface the information to the user. Use the same name as the Docker container for the Docker Machine for ease of lookup.
144
- // Providers should iterate through their infrastructure instances and ensure that each instance has a corresponding InfraMachine.
145
- for _ , machine := range externalMachines {
146
- if existingMachine , ok := dockerMachineMap [machine .Name ()]; ok {
147
- log .V (2 ).Info ("Patching existing DockerMachine" , "DockerMachine" , klog .KObj (& existingMachine ))
148
- desiredMachine := computeDesiredDockerMachine (machine .Name (), cluster , machinePool , dockerMachinePool , & existingMachine )
149
- if err := ssa .Patch (ctx , r .Client , dockerMachinePoolControllerName , desiredMachine , ssa.WithCachingProxy {Cache : r .ssaCache , Original : & existingMachine }); err != nil {
150
- return errors .Wrapf (err , "failed to update DockerMachine %q" , klog .KObj (desiredMachine ))
151
- }
152
-
153
- dockerMachineMap [desiredMachine .Name ] = * desiredMachine
154
- } else {
155
- log .V (2 ).Info ("Creating a new DockerMachine for Docker container" , "container" , machine .Name ())
156
- desiredMachine := computeDesiredDockerMachine (machine .Name (), cluster , machinePool , dockerMachinePool , nil )
157
- if err := ssa .Patch (ctx , r .Client , dockerMachinePoolControllerName , desiredMachine ); err != nil {
158
- return errors .Wrap (err , "failed to create a new docker machine" )
159
- }
160
-
161
- dockerMachineMap [desiredMachine .Name ] = * desiredMachine
162
- }
163
- }
164
-
165
- // Step 2:
166
148
// Delete any DockerMachines that correspond to a deleted Docker container.
167
149
// Providers should iterate through the InfraMachines to ensure each one still corresponds to an existing infrastructure instance.
168
150
// This allows the InfraMachine (and owner Machine) to be deleted and avoid hanging resources when a user deletes an instance out-of-band.
169
151
for _ , dockerMachine := range dockerMachineMap {
170
152
if _ , ok := externalMachineMap [dockerMachine .Name ]; ! ok {
171
153
dockerMachine := dockerMachine
172
- log .V ( 2 ). Info ("Deleting DockerMachine with no underlying infrastructure" , "DockerMachine" , klog .KObj (& dockerMachine ))
154
+ log .Info ("Deleting DockerMachine with no underlying infrastructure" , "DockerMachine" , klog .KObj (& dockerMachine ))
173
155
if err := r .deleteMachinePoolMachine (ctx , dockerMachine ); err != nil {
174
156
return err
175
157
}
@@ -178,7 +160,7 @@ func (r *DockerMachinePoolReconciler) reconcileDockerMachines(ctx context.Contex
178
160
}
179
161
}
180
162
181
- // Step 3 :
163
+ // Step 2 :
182
164
// This handles the scale down/excess replicas case and the case where a rolling upgrade is needed.
183
165
// If there are more ready DockerMachines than desired replicas, start to delete the excess DockerMachines such that
184
166
// - DockerMachines with an outdated Kubernetes version or custom image are deleted first (i.e. the rolling upgrade).
@@ -218,7 +200,7 @@ func (r *DockerMachinePoolReconciler) reconcileDockerMachines(ctx context.Contex
218
200
for _ , dockerMachine := range outdatedMachines {
219
201
if overProvisionCount > 0 {
220
202
dockerMachine := dockerMachine
221
- log .V ( 2 ). Info ("Deleting DockerMachine because it is outdated" , "DockerMachine" , klog .KObj (& dockerMachine ))
203
+ log .Info ("Deleting DockerMachine because it is outdated" , "DockerMachine" , klog .KObj (& dockerMachine ))
222
204
if err := r .deleteMachinePoolMachine (ctx , dockerMachine ); err != nil {
223
205
return err
224
206
}
@@ -231,7 +213,7 @@ func (r *DockerMachinePoolReconciler) reconcileDockerMachines(ctx context.Contex
231
213
for _ , dockerMachine := range readyMachines {
232
214
if overProvisionCount > 0 {
233
215
dockerMachine := dockerMachine
234
- log .V ( 2 ). Info ("Deleting DockerMachine because it is an excess replica" , "DockerMachine" , klog .KObj (& dockerMachine ))
216
+ log .Info ("Deleting DockerMachine because it is an excess replica" , "DockerMachine" , klog .KObj (& dockerMachine ))
235
217
if err := r .deleteMachinePoolMachine (ctx , dockerMachine ); err != nil {
236
218
return err
237
219
}
@@ -272,6 +254,7 @@ func computeDesiredDockerMachine(name string, cluster *clusterv1.Cluster, machin
272
254
Name : dockerMachinePool .Name ,
273
255
UID : dockerMachinePool .UID ,
274
256
}))
257
+
275
258
dockerMachine .Labels [clusterv1 .ClusterNameLabel ] = cluster .Name
276
259
dockerMachine .Labels [clusterv1 .MachinePoolNameLabel ] = format .MustFormatValue (machinePool .Name )
277
260
@@ -288,7 +271,7 @@ func (r *DockerMachinePoolReconciler) deleteMachinePoolMachine(ctx context.Conte
288
271
}
289
272
// util.GetOwnerMachine() returns a nil Machine without error if there is no Machine kind in the ownerRefs, so we must verify that machine is not nil.
290
273
if machine == nil {
291
- log .V ( 2 ). Info ("No owner Machine exists for DockerMachine" , "dockerMachine" , klog .KObj (& dockerMachine ))
274
+ log .Info ("No owner Machine exists for DockerMachine" , "dockerMachine" , klog .KObj (& dockerMachine ))
292
275
293
276
// If the DockerMachine does not have an owner Machine, do not attempt to delete the DockerMachine as the MachinePool controller will create the
294
277
// Machine and we want to let it catch up. If we are too hasty to delete, that introduces a race condition where the DockerMachine could be deleted
@@ -297,7 +280,8 @@ func (r *DockerMachinePoolReconciler) deleteMachinePoolMachine(ctx context.Conte
297
280
// In the case where the MachinePool is being deleted and the Machine will never come online, the DockerMachine will be deleted via its ownerRef to the
298
281
// DockerMachinePool, so that is covered as well.
299
282
300
- return nil
283
+ // Returning error as we need the dockerMachine not to proceed.
284
+ return errors .New ("No owner Machine exists for DockerMachine" )
301
285
}
302
286
303
287
log .Info ("Deleting Machine for DockerMachine" , "Machine" , klog .KObj (machine ), "DockerMachine" , klog .KObj (& dockerMachine ))
0 commit comments