@@ -21,6 +21,7 @@ import (
21
21
"compress/gzip"
22
22
"context"
23
23
"encoding/base64"
24
+ "fmt"
24
25
"strings"
25
26
"time"
26
27
@@ -41,6 +42,27 @@ import (
41
42
"sigs.k8s.io/cluster-api-provider-aws/pkg/record"
42
43
)
43
44
45
+ const (
46
+ // localIPV4lookup resolves via cloudinit and looks up the instance's IP through the provider's metadata service.
47
+ // See https://cloudinit.readthedocs.io/en/latest/topics/instancedata.html
48
+ localIPV4Lookup = "{{ ds.meta_data.local_ipv4 }}"
49
+
50
+ // hostnameLookup resolves via cloud init and uses cloud provider's metadata service to lookup its own hostname.
51
+ hostnameLookup = "{{ ds.meta_data.hostname }}"
52
+
53
+ // containerdSocket is the path to containerd socket.
54
+ containerdSocket = "/var/run/containerd/containerd.sock"
55
+
56
+ // apiServerBindPort is the default port for the kube-apiserver to bind to.
57
+ apiServerBindPort = 6443
58
+
59
+ // cloudProvider is the name of the cloud provider passed to various kubernetes components.
60
+ cloudProvider = "aws"
61
+
62
+ // nodeRole is the label assigned to every node in the cluster.
63
+ nodeRole = "node-role.kubernetes.io/node="
64
+ )
65
+
44
66
// InstanceByTags returns the existing instance or nothing if it doesn't exist.
45
67
func (s * Service ) InstanceByTags (machine * actuators.MachineScope ) (* v1alpha1.Instance , error ) {
46
68
s .scope .V (2 ).Info ("Looking for existing machine instance by tags" )
@@ -176,9 +198,26 @@ func (s *Service) createInstance(machine *actuators.MachineScope, bootstrapToken
176
198
if bootstrapToken != "" {
177
199
s .scope .V (2 ).Info ("Allowing a machine to join the control plane" )
178
200
179
- updatedJoinConfiguration := kubeadm .SetJoinNodeConfigurationOverrides (caCertHash , bootstrapToken , machine , & machine .MachineConfig .KubeadmConfiguration .Join )
180
- updatedJoinConfiguration = kubeadm .SetControlPlaneJoinConfigurationOverrides (updatedJoinConfiguration )
181
- joinConfigurationYAML , err := kubeadm .ConfigurationToYAML (updatedJoinConfiguration )
201
+ kubeadm .SetJoinConfigurationOptions (
202
+ & machine .MachineConfig .KubeadmConfiguration .Join ,
203
+ kubeadm .WithBootstrapTokenDiscovery (
204
+ kubeadm .NewBootstrapTokenDiscovery (
205
+ kubeadm .WithAPIServerEndpoint (fmt .Sprintf ("%s:%d" , machine .Network ().APIServerELB .DNSName , apiServerBindPort )),
206
+ kubeadm .WithToken (bootstrapToken ),
207
+ kubeadm .WithCACertificateHash (caCertHash ),
208
+ ),
209
+ ),
210
+ kubeadm .WithJoinNodeRegistrationOptions (
211
+ kubeadm .NewNodeRegistration (
212
+ kubeadm .WithTaints (machine .GetMachine ().Spec .Taints ),
213
+ kubeadm .WithNodeRegistrationName (hostnameLookup ),
214
+ kubeadm .WithCRISocket (containerdSocket ),
215
+ kubeadm .WithKubeletExtraArgs (map [string ]string {"cloud-provider" : cloudProvider }),
216
+ ),
217
+ ),
218
+ kubeadm .WithLocalAPIEndpointAndPort (localIPV4Lookup , apiServerBindPort ),
219
+ )
220
+ joinConfigurationYAML , err := kubeadm .ConfigurationToYAML (& machine .MachineConfig .KubeadmConfiguration .Join )
182
221
if err != nil {
183
222
return nil , err
184
223
}
@@ -205,14 +244,33 @@ func (s *Service) createInstance(machine *actuators.MachineScope, bootstrapToken
205
244
)
206
245
}
207
246
208
- clusterConfiguration := kubeadm .SetClusterConfigurationOverrides (machine , & s .scope .ClusterConfig .ClusterConfiguration )
209
- clusterConfigYAML , err := kubeadm .ConfigurationToYAML (clusterConfiguration )
247
+ kubeadm .SetClusterConfigurationOptions (
248
+ & s .scope .ClusterConfig .ClusterConfiguration ,
249
+ kubeadm .WithControlPlaneEndpoint (fmt .Sprintf ("%s:%d" , s .scope .Network ().APIServerELB .DNSName , apiServerBindPort )),
250
+ kubeadm .WithAPIServerCertificateSANs (localIPV4Lookup , s .scope .Network ().APIServerELB .DNSName ),
251
+ kubeadm .WithAPIServerExtraArgs (map [string ]string {"cloud-provider" : cloudProvider }),
252
+ kubeadm .WithControllerManagerExtraArgs (map [string ]string {"cloud-provider" : cloudProvider }),
253
+ kubeadm .WithClusterName (s .scope .Name ()),
254
+ kubeadm .WithClusterNetworkFromClusterNetworkingConfig (s .scope .Cluster .Spec .ClusterNetwork ),
255
+ kubeadm .WithKubernetesVersion (machine .GetMachine ().Spec .Versions .ControlPlane ),
256
+ )
257
+ clusterConfigYAML , err := kubeadm .ConfigurationToYAML (& s .scope .ClusterConfig .ClusterConfiguration )
210
258
if err != nil {
211
259
return nil , err
212
260
}
213
261
214
- initConfiguration := kubeadm .SetInitConfigurationOverrides (machine , & machine .MachineConfig .KubeadmConfiguration .Init )
215
- initConfigYAML , err := kubeadm .ConfigurationToYAML (initConfiguration )
262
+ kubeadm .SetInitConfigurationOptions (
263
+ & machine .MachineConfig .KubeadmConfiguration .Init ,
264
+ kubeadm .WithNodeRegistrationOptions (
265
+ kubeadm .NewNodeRegistration (
266
+ kubeadm .WithTaints (machine .GetMachine ().Spec .Taints ),
267
+ kubeadm .WithNodeRegistrationName (hostnameLookup ),
268
+ kubeadm .WithCRISocket (containerdSocket ),
269
+ kubeadm .WithKubeletExtraArgs (map [string ]string {"cloud-provider" : cloudProvider }),
270
+ ),
271
+ ),
272
+ )
273
+ initConfigYAML , err := kubeadm .ConfigurationToYAML (& machine .MachineConfig .KubeadmConfiguration .Init )
216
274
if err != nil {
217
275
return nil , err
218
276
}
@@ -239,8 +297,26 @@ func (s *Service) createInstance(machine *actuators.MachineScope, bootstrapToken
239
297
case "node" :
240
298
s .scope .V (2 ).Info ("Joining a worker node to the cluster" )
241
299
242
- joinConfiguration := kubeadm .SetJoinNodeConfigurationOverrides (caCertHash , bootstrapToken , machine , & machine .MachineConfig .KubeadmConfiguration .Join )
243
- joinConfigurationYAML , err := kubeadm .ConfigurationToYAML (joinConfiguration )
300
+ kubeadm .SetJoinConfigurationOptions (
301
+ & machine .MachineConfig .KubeadmConfiguration .Join ,
302
+ kubeadm .WithBootstrapTokenDiscovery (
303
+ kubeadm .NewBootstrapTokenDiscovery (
304
+ kubeadm .WithAPIServerEndpoint (fmt .Sprintf ("%s:%d" , machine .Network ().APIServerELB .DNSName , apiServerBindPort )),
305
+ kubeadm .WithToken (bootstrapToken ),
306
+ kubeadm .WithCACertificateHash (caCertHash ),
307
+ ),
308
+ ),
309
+ kubeadm .WithJoinNodeRegistrationOptions (
310
+ kubeadm .NewNodeRegistration (
311
+ kubeadm .WithNodeRegistrationName (hostnameLookup ),
312
+ kubeadm .WithCRISocket (containerdSocket ),
313
+ kubeadm .WithKubeletExtraArgs (map [string ]string {"cloud-provider" : cloudProvider }),
314
+ kubeadm .WithTaints (machine .GetMachine ().Spec .Taints ),
315
+ kubeadm .WithKubeletExtraArgs (map [string ]string {"node-labels" : nodeRole }),
316
+ ),
317
+ ),
318
+ )
319
+ joinConfigurationYAML , err := kubeadm .ConfigurationToYAML (& machine .MachineConfig .KubeadmConfiguration .Join )
244
320
if err != nil {
245
321
return nil , err
246
322
}
0 commit comments