Skip to content

Commit 9a6b99b

Browse files
authored
Merge pull request #8881 from fabriziopandini/automatically-set-kubelet-args-for-CAPD
🌱 Automatically set kubelet args for capd
2 parents 1d99742 + 8b15bc0 commit 9a6b99b

File tree

27 files changed

+461
-135
lines changed

27 files changed

+461
-135
lines changed

bootstrap/kubeadm/types/utils.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ func UnmarshalClusterStatus(yaml string) (*bootstrapv1.ClusterStatus, error) {
181181
return obj, nil
182182
}
183183

184+
// UnmarshalInitConfiguration tries to translate a Kubeadm API yaml back to the InitConfiguration type.
185+
// NOTE: The yaml could be any of the known formats for the kubeadm InitConfiguration type.
186+
func UnmarshalInitConfiguration(yaml string) (*bootstrapv1.InitConfiguration, error) {
187+
obj := &bootstrapv1.InitConfiguration{}
188+
if err := unmarshalFromVersions(yaml, initConfigurationVersionTypeMap, obj); err != nil {
189+
return nil, err
190+
}
191+
return obj, nil
192+
}
193+
194+
// UnmarshalJoinConfiguration tries to translate a Kubeadm API yaml back to the JoinConfiguration type.
195+
// NOTE: The yaml could be any of the known formats for the kubeadm JoinConfiguration type.
196+
func UnmarshalJoinConfiguration(yaml string) (*bootstrapv1.JoinConfiguration, error) {
197+
obj := &bootstrapv1.JoinConfiguration{}
198+
if err := unmarshalFromVersions(yaml, joinConfigurationVersionTypeMap, obj); err != nil {
199+
return nil, err
200+
}
201+
return obj, nil
202+
}
203+
184204
func unmarshalFromVersions(yaml string, kubeadmAPIVersions map[schema.GroupVersion]conversion.Convertible, capiObj conversion.Hub) error {
185205
// For each know kubeadm API version
186206
for gv, obj := range kubeadmAPIVersions {
@@ -192,7 +212,8 @@ func unmarshalFromVersions(yaml string, kubeadmAPIVersions map[schema.GroupVersi
192212
return errors.Wrapf(err, "failed to build scheme for kubeadm types conversions")
193213
}
194214

195-
if _, _, err := codecs.UniversalDeserializer().Decode([]byte(yaml), &gvk, kubeadmObj); err == nil {
215+
_, _, err = codecs.UniversalDeserializer().Decode([]byte(yaml), &gvk, kubeadmObj)
216+
if err == nil {
196217
// If conversion worked, then converts the kubeadmObj (spoke) back to the Cluster API ClusterConfiguration type (hub).
197218
if err := kubeadmObj.(conversion.Convertible).ConvertTo(capiObj); err != nil {
198219
return errors.Wrapf(err, "failed to convert kubeadm types to Cluster API types")

bootstrap/kubeadm/types/utils_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,121 @@ func TestUnmarshalClusterStatus(t *testing.T) {
555555
})
556556
}
557557
}
558+
559+
func TestUnmarshalInitConfiguration(t *testing.T) {
560+
type args struct {
561+
yaml string
562+
}
563+
tests := []struct {
564+
name string
565+
args args
566+
want *bootstrapv1.InitConfiguration
567+
wantErr bool
568+
}{
569+
{
570+
name: "Parses a v1beta1 kubeadm configuration",
571+
args: args{
572+
yaml: "apiVersion: kubeadm.k8s.io/v1beta1\n" + "" +
573+
"kind: InitConfiguration\n" +
574+
"localAPIEndpoint: {}\n" +
575+
"nodeRegistration: {}\n",
576+
},
577+
want: &bootstrapv1.InitConfiguration{},
578+
wantErr: false,
579+
},
580+
{
581+
name: "Parses a v1beta2 kubeadm configuration",
582+
args: args{
583+
yaml: "apiVersion: kubeadm.k8s.io/v1beta2\n" + "" +
584+
"kind: InitConfiguration\n" +
585+
"localAPIEndpoint: {}\n" +
586+
"nodeRegistration: {}\n",
587+
},
588+
want: &bootstrapv1.InitConfiguration{},
589+
wantErr: false,
590+
},
591+
{
592+
name: "Parses a v1beta3 kubeadm configuration",
593+
args: args{
594+
yaml: "apiVersion: kubeadm.k8s.io/v1beta3\n" + "" +
595+
"kind: InitConfiguration\n" +
596+
"localAPIEndpoint: {}\n" +
597+
"nodeRegistration: {}\n",
598+
},
599+
want: &bootstrapv1.InitConfiguration{},
600+
wantErr: false,
601+
},
602+
}
603+
for _, tt := range tests {
604+
t.Run(tt.name, func(t *testing.T) {
605+
g := NewWithT(t)
606+
607+
got, err := UnmarshalInitConfiguration(tt.args.yaml)
608+
if tt.wantErr {
609+
g.Expect(err).To(HaveOccurred())
610+
return
611+
}
612+
g.Expect(err).ToNot(HaveOccurred())
613+
g.Expect(got).To(Equal(tt.want), cmp.Diff(tt.want, got))
614+
})
615+
}
616+
}
617+
618+
func TestUnmarshalJoinConfiguration(t *testing.T) {
619+
type args struct {
620+
yaml string
621+
}
622+
tests := []struct {
623+
name string
624+
args args
625+
want *bootstrapv1.JoinConfiguration
626+
wantErr bool
627+
}{
628+
{
629+
name: "Parses a v1beta1 kubeadm configuration",
630+
args: args{
631+
yaml: "apiVersion: kubeadm.k8s.io/v1beta1\n" + "" +
632+
"caCertPath: \"\"\n" +
633+
"discovery: {}\n" +
634+
"kind: JoinConfiguration\n",
635+
},
636+
want: &bootstrapv1.JoinConfiguration{},
637+
wantErr: false,
638+
},
639+
{
640+
name: "Parses a v1beta2 kubeadm configuration",
641+
args: args{
642+
yaml: "apiVersion: kubeadm.k8s.io/v1beta2\n" + "" +
643+
"caCertPath: \"\"\n" +
644+
"discovery: {}\n" +
645+
"kind: JoinConfiguration\n",
646+
},
647+
want: &bootstrapv1.JoinConfiguration{},
648+
wantErr: false,
649+
},
650+
{
651+
name: "Parses a v1beta3 kubeadm configuration",
652+
args: args{
653+
yaml: "apiVersion: kubeadm.k8s.io/v1beta3\n" + "" +
654+
"caCertPath: \"\"\n" +
655+
"discovery: {}\n" +
656+
"kind: JoinConfiguration\n",
657+
},
658+
want: &bootstrapv1.JoinConfiguration{},
659+
wantErr: false,
660+
},
661+
}
662+
for _, tt := range tests {
663+
t.Run(tt.name, func(t *testing.T) {
664+
g := NewWithT(t)
665+
666+
got, err := UnmarshalJoinConfiguration(tt.args.yaml)
667+
if tt.wantErr {
668+
g.Expect(err).To(HaveOccurred())
669+
return
670+
}
671+
g.Expect(err).ToNot(HaveOccurred())
672+
g.Expect(got).To(Equal(tt.want), cmp.Diff(tt.want, got))
673+
})
674+
}
675+
}

docs/book/src/clusterctl/commands/alpha-topology-plan.md

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,9 @@ spec:
132132
apiServer:
133133
certSANs: [ localhost, 127.0.0.1 ]
134134
initConfiguration:
135-
nodeRegistration:
136-
criSocket: unix:///var/run/containerd/containerd.sock
137-
kubeletExtraArgs:
138-
cgroup-driver: cgroupfs
139-
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
135+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
140136
joinConfiguration:
141-
nodeRegistration:
142-
criSocket: unix:///var/run/containerd/containerd.sock
143-
kubeletExtraArgs:
144-
cgroup-driver: cgroupfs
145-
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
137+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
146138
---
147139
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
148140
kind: DockerMachineTemplate
@@ -174,10 +166,7 @@ spec:
174166
template:
175167
spec:
176168
joinConfiguration:
177-
nodeRegistration:
178-
kubeletExtraArgs:
179-
cgroup-driver: cgroupfs
180-
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
169+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
181170
```
182171
183172
</details>
@@ -485,4 +474,3 @@ If only one cluster is affected or if a Cluster is in the input it defaults as t
485474
Namespace used for objects with missing namespaces in the input.
486475

487476
If not provided, the namespace defined in kubeconfig is used. If a kubeconfig is not available the value `default` is used.
488-

docs/book/src/tasks/bootstrap/kubeadm-bootstrap.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ metadata:
4242
name: my-control-plane1-config
4343
spec:
4444
initConfiguration:
45-
nodeRegistration:
46-
kubeletExtraArgs:
47-
eviction-hard: nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%
45+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
4846
clusterConfiguration:
4947
controllerManager:
5048
extraArgs:
@@ -119,8 +117,7 @@ metadata:
119117
spec:
120118
initConfiguration:
121119
nodeRegistration:
122-
kubeletExtraArgs:
123-
eviction-hard: nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%
120+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
124121
clusterConfiguration:
125122
controllerManager:
126123
extraArgs:
@@ -136,8 +133,7 @@ metadata:
136133
spec:
137134
joinConfiguration:
138135
nodeRegistration:
139-
kubeletExtraArgs:
140-
eviction-hard: nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%
136+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
141137
controlPlane: {}
142138
```
143139

@@ -150,8 +146,7 @@ metadata:
150146
spec:
151147
joinConfiguration:
152148
nodeRegistration:
153-
kubeletExtraArgs:
154-
eviction-hard: nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%
149+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
155150
```
156151

157152
### Bootstrap Orchestration

docs/book/src/tasks/experimental-features/ignition.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ This initial implementation uses Ignition **v2** and was tested with **Flatcar C
1010

1111
</aside>
1212

13+
<aside class="note warning">
14+
15+
<h1>Note</h1>
16+
17+
If using ignition with CAPD you should take care of setting `kubeletExtraArgs` for the `kindest/node` image in use,
18+
because default CAPD templates do not include anymore those settings since when the cloud-init shim for CAPD is automatically taking care of this.
19+
An example of how to set `kubeletExtraArgs` for the `kindest/node` can be found under `cluster-api/test/e2e/data/infrastructure-docker/main/cluster-template-ignition`.
20+
21+
Hopefully, this will be automated for Ignition too in a future release.
22+
23+
</aside>
24+
1325
This guide explains how to deploy an AWS workload cluster using Ignition.
1426

1527
## Prerequisites

test/e2e/data/infrastructure-docker/main/bases/cluster-with-kcp.yaml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,7 @@ spec:
8585
# host.docker.internal is required by kubetest when running on MacOS because of the way ports are proxied.
8686
certSANs: [localhost, 127.0.0.1, 0.0.0.0, host.docker.internal]
8787
initConfiguration:
88-
nodeRegistration:
89-
criSocket: unix:///var/run/containerd/containerd.sock
90-
kubeletExtraArgs:
91-
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
92-
fail-swap-on: "false"
88+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
9389
joinConfiguration:
94-
nodeRegistration:
95-
criSocket: unix:///var/run/containerd/containerd.sock
96-
kubeletExtraArgs:
97-
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
98-
fail-swap-on: "false"
90+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
9991
version: "${KUBERNETES_VERSION}"

test/e2e/data/infrastructure-docker/main/bases/md.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ spec:
2323
template:
2424
spec:
2525
joinConfiguration:
26-
nodeRegistration:
27-
criSocket: unix:///var/run/containerd/containerd.sock
28-
kubeletExtraArgs:
29-
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
30-
fail-swap-on: "false"
26+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
3127
---
3228
# MachineDeployment object
3329
apiVersion: cluster.x-k8s.io/v1beta1

test/e2e/data/infrastructure-docker/main/bases/mp.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,4 @@ metadata:
4444
name: "${CLUSTER_NAME}-mp-0-config"
4545
spec:
4646
joinConfiguration:
47-
nodeRegistration:
48-
kubeletExtraArgs:
49-
eviction-hard: nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%
50-
fail-swap-on: "false"
47+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.

test/e2e/data/infrastructure-docker/main/cluster-template-ignition/ignition.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ metadata:
55
spec:
66
kubeadmConfigSpec:
77
format: ignition
8+
initConfiguration:
9+
nodeRegistration:
10+
# We have to set the criSocket to containerd as kubeadm defaults to docker runtime if both containerd and docker sockets are found
11+
criSocket: unix:///var/run/containerd/containerd.sock
12+
kubeletExtraArgs:
13+
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
14+
fail-swap-on: "false"
15+
joinConfiguration:
16+
nodeRegistration:
17+
# We have to set the criSocket to containerd as kubeadm defaults to docker runtime if both containerd and docker sockets are found
18+
criSocket: unix:///var/run/containerd/containerd.sock
19+
kubeletExtraArgs:
20+
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
21+
fail-swap-on: "false"
822
---
923
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
1024
kind: KubeadmConfigTemplate
@@ -24,3 +38,10 @@ spec:
2438
contents:
2539
inline: Howdy!
2640
mode: 0644
41+
joinConfiguration:
42+
nodeRegistration:
43+
# We have to set the criSocket to containerd as kubeadm defaults to docker runtime if both containerd and docker sockets are found
44+
criSocket: unix:///var/run/containerd/containerd.sock
45+
kubeletExtraArgs:
46+
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
47+
fail-swap-on: "false"

test/e2e/data/infrastructure-docker/main/cluster-template-kcp-adoption/step1/cluster-with-cp0.yaml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,9 @@ spec:
4545
apiServer:
4646
certSANs: [localhost, 127.0.0.1]
4747
initConfiguration:
48-
nodeRegistration:
49-
criSocket: unix:///var/run/containerd/containerd.sock
50-
kubeletExtraArgs:
51-
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
52-
fail-swap-on: "false"
48+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
5349
joinConfiguration:
54-
nodeRegistration:
55-
criSocket: unix:///var/run/containerd/containerd.sock
56-
kubeletExtraArgs:
57-
eviction-hard: 'nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%'
58-
fail-swap-on: "false"
50+
nodeRegistration: {} # node registration parameters are automatically injected by CAPD according to the kindest/node image in use.
5951
---
6052
# cp0 Machine
6153
apiVersion: cluster.x-k8s.io/v1beta1

0 commit comments

Comments
 (0)