Skip to content

Commit f177144

Browse files
authored
Implement cloud-init data templating for getting node IP (#16)
1 parent d80cd59 commit f177144

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

docs/advanced.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Advanced configurations
2+
3+
## Use private IP in Kubeadm configurations
4+
5+
When your nodes have both public and private IPs, Kubeadm will always advertise
6+
the public IP by default. This can be an issue if you want the control-plane to
7+
communicate through the Private Network and then block all public ingress traffic
8+
on your nodes using an Instance security group.
9+
10+
To solve this, you can include the node's private IP in your Kubeadm configurations using
11+
the `[[[ .NodeIP ]]]` placeholder value. This placeholder value will be replaced
12+
by the provider with the private IP of the node. If a Private Network is not enabled
13+
in the `ScalewayCluster`, `[[[ .NodeIP ]]]` will be replaced with the public IP
14+
of the node instead.
15+
16+
Here is an example of `KubeadmControlPlane` configuration:
17+
18+
```yaml
19+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
20+
kind: KubeadmControlPlane
21+
metadata:
22+
name: my-kubeadm-controlplane
23+
namespace: default
24+
spec:
25+
kubeadmConfigSpec:
26+
initConfiguration:
27+
localAPIEndpoint:
28+
advertiseAddress: "[[[ .NodeIP ]]]"
29+
nodeRegistration:
30+
kubeletExtraArgs:
31+
node-ip: "[[[ .NodeIP ]]]"
32+
joinConfiguration:
33+
controlPlane:
34+
localAPIEndpoint:
35+
advertiseAddress: "[[[ .NodeIP ]]]"
36+
nodeRegistration:
37+
kubeletExtraArgs:
38+
node-ip: "[[[ .NodeIP ]]]"
39+
# important: some fields were omitted...
40+
```
41+
42+
Here is an example of `KubeadmConfigTemplate` configuration:
43+
44+
```yaml
45+
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
46+
kind: KubeadmConfigTemplate
47+
metadata:
48+
name: my-kubeadmconfig-template
49+
namespace: default
50+
spec:
51+
template:
52+
spec:
53+
joinConfiguration:
54+
nodeRegistration:
55+
kubeletExtraArgs:
56+
node-ip: "[[[ .NodeIP ]]]"
57+
# important: some fields were omitted...
58+
```

internal/service/scaleway/instance/instance.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"fmt"
77
"slices"
8+
"strings"
9+
"text/template"
810
"time"
911

1012
infrav1 "github.com/scaleway/cluster-api-provider-scaleway/api/v1alpha1"
@@ -84,7 +86,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
8486
return fmt.Errorf("failed to ensure control-plane lbs acls: %w", err)
8587
}
8688

87-
if err := s.ensureCloudInit(ctx, server); err != nil {
89+
if err := s.ensureCloudInit(ctx, server, nodeIP); err != nil {
8890
return fmt.Errorf("failed to ensure cloud-init: %w", err)
8991
}
9092

@@ -558,7 +560,7 @@ func instanceIPsToStrings(ips []*instance.ServerIP) []string {
558560
return out
559561
}
560562

561-
func (s *Service) ensureCloudInit(ctx context.Context, server *instance.Server) error {
563+
func (s *Service) ensureCloudInit(ctx context.Context, server *instance.Server, nodeIP string) error {
562564
if server.State != instance.ServerStateStopped {
563565
return nil
564566
}
@@ -574,12 +576,25 @@ func (s *Service) ensureCloudInit(ctx context.Context, server *instance.Server)
574576
return err
575577
}
576578

579+
// Apply custom templating on cloud-init bootstrap data.
580+
tmpl, err := template.New("").Delims("[[[", "]]]").Parse(string(bootstrapData))
581+
if err != nil {
582+
return fmt.Errorf("failed to parse bootstrap data as template: %w", err)
583+
}
584+
585+
tmplExec := &strings.Builder{} // tmplExec will contain the executed template.
586+
tmplData := struct{ NodeIP string }{nodeIP}
587+
588+
if err := tmpl.ExecuteTemplate(tmplExec, "", tmplData); err != nil {
589+
return fmt.Errorf("failed to execute bootstrap data template: %w", err)
590+
}
591+
577592
if err := s.ScalewayClient.SetServerUserData(
578593
ctx,
579594
server.Zone,
580595
server.ID,
581596
cloudInitUserDataKey,
582-
string(bootstrapData),
597+
tmplExec.String(),
583598
); err != nil {
584599
return err
585600
}

0 commit comments

Comments
 (0)