Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add bootCommands to cloud-init file generation #11271

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type KubeadmConfigSpec struct {
// +optional
Mounts []MountPoints `json:"mounts,omitempty"`

// BootCommands specifies extra commands to run very early in the boot process
// +optional
davidumea marked this conversation as resolved.
Show resolved Hide resolved
BootCommands []BootCommand `json:"bootCommands,omitempty"`

// PreKubeadmCommands specifies extra commands to run before kubeadm runs
// +optional
davidumea marked this conversation as resolved.
Show resolved Hide resolved
PreKubeadmCommands []string `json:"preKubeadmCommands,omitempty"`
Expand Down Expand Up @@ -682,3 +686,6 @@ type Filesystem struct {

// MountPoints defines input for generated mounts in cloud-init.
type MountPoints []string

// BootCommand defines input for each bootcmd command in cloud-init.
type BootCommand []string
30 changes: 30 additions & 0 deletions bootstrap/kubeadm/api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions bootstrap/kubeadm/internal/cloudinit/boot_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cloudinit

const (
bootCommandsTemplate = `{{ define "boot_commands" -}}
{{- if . }}
bootcmd:{{ range . }}
- {{ range . }}- {{ . }}
{{ end -}}
{{- end -}}
{{- end -}}
{{- end -}}
`
)
5 changes: 5 additions & 0 deletions bootstrap/kubeadm/internal/cloudinit/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
// BaseUserData is shared across all the various types of files written to disk.
type BaseUserData struct {
Header string
BootCommands []bootstrapv1.BootCommand
PreKubeadmCommands []string
PostKubeadmCommands []string
AdditionalFiles []bootstrapv1.File
Expand Down Expand Up @@ -83,6 +84,10 @@ func generate(kind string, tpl string, data interface{}) ([]byte, error) {
return nil, errors.Wrap(err, "failed to parse files template")
}

if _, err := tm.Parse(bootCommandsTemplate); err != nil {
return nil, errors.Wrap(err, "failed to parse boot commands template")
}

if _, err := tm.Parse(commandsTemplate); err != nil {
return nil, errors.Wrap(err, "failed to parse commands template")
}
Expand Down
5 changes: 5 additions & 0 deletions bootstrap/kubeadm/internal/cloudinit/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) {
cpinput := &ControlPlaneInput{
BaseUserData: BaseUserData{
Header: "test",
BootCommands: nil,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a test for BootCommands not nil for init, join CP, join workers?

Copy link
Author

@davidumea davidumea Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added non-nil values for BootCommands in TestNewInitControlPlaneCommands and TestNewJoinControlPlaneAdditionalFileEncodings, did you want me to add more tests or is this enough? I couldn't find one for "join workers" daf4f97

PreKubeadmCommands: nil,
PostKubeadmCommands: nil,
AdditionalFiles: []bootstrapv1.File{
Expand Down Expand Up @@ -95,6 +96,7 @@ func TestNewInitControlPlaneCommands(t *testing.T) {
cpinput := &ControlPlaneInput{
BaseUserData: BaseUserData{
Header: "test",
BootCommands: nil,
PreKubeadmCommands: []string{`"echo $(date) ': hello world!'"`},
PostKubeadmCommands: []string{"echo $(date) ': hello world!'"},
AdditionalFiles: nil,
Expand Down Expand Up @@ -132,6 +134,7 @@ func TestNewInitControlPlaneDiskMounts(t *testing.T) {
cpinput := &ControlPlaneInput{
BaseUserData: BaseUserData{
Header: "test",
BootCommands: nil,
PreKubeadmCommands: nil,
PostKubeadmCommands: nil,
WriteFiles: nil,
Expand Down Expand Up @@ -195,6 +198,7 @@ func TestNewJoinControlPlaneAdditionalFileEncodings(t *testing.T) {
cpinput := &ControlPlaneJoinInput{
BaseUserData: BaseUserData{
Header: "test",
BootCommands: nil,
PreKubeadmCommands: nil,
PostKubeadmCommands: nil,
AdditionalFiles: []bootstrapv1.File{
Expand Down Expand Up @@ -247,6 +251,7 @@ func TestNewJoinControlPlaneExperimentalRetry(t *testing.T) {
cpinput := &ControlPlaneJoinInput{
BaseUserData: BaseUserData{
Header: "test",
BootCommands: nil,
PreKubeadmCommands: nil,
PostKubeadmCommands: nil,
UseExperimentalRetry: true,
Expand Down
1 change: 1 addition & 0 deletions bootstrap/kubeadm/internal/cloudinit/controlplane_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
owner: root:root
permissions: '0640'
content: "This placeholder file is used to create the /run/cluster-api sub directory in a way that is compatible with both Linux and Windows (mkdir -p /run/cluster-api does not work with Windows)"
{{- template "boot_commands" .BootCommands }}
runcmd:
{{- template "commands" .PreKubeadmCommands }}
- 'kubeadm init --config /run/kubeadm/kubeadm.yaml {{.KubeadmVerbosity}} && {{ .SentinelFileCommand }}'
Expand Down
1 change: 1 addition & 0 deletions bootstrap/kubeadm/internal/cloudinit/controlplane_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
owner: root:root
permissions: '0640'
content: "This placeholder file is used to create the /run/cluster-api sub directory in a way that is compatible with both Linux and Windows (mkdir -p /run/cluster-api does not work with Windows)"
{{- template "boot_commands" .BootCommands }}
runcmd:
{{- template "commands" .PreKubeadmCommands }}
- {{ .KubeadmCommand }} && {{ .SentinelFileCommand }}
Expand Down
1 change: 1 addition & 0 deletions bootstrap/kubeadm/internal/cloudinit/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
owner: root:root
permissions: '0640'
content: "This placeholder file is used to create the /run/cluster-api sub directory in a way that is compatible with both Linux and Windows (mkdir -p /run/cluster-api does not work with Windows)"
{{- template "boot_commands" .BootCommands }}
runcmd:
{{- template "commands" .PreKubeadmCommands }}
- {{ .KubeadmCommand }} && {{ .SentinelFileCommand }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ func (r *KubeadmConfigReconciler) handleClusterNotInitialized(ctx context.Contex
BaseUserData: cloudinit.BaseUserData{
AdditionalFiles: files,
NTP: scope.Config.Spec.NTP,
BootCommands: scope.Config.Spec.BootCommands,
PreKubeadmCommands: scope.Config.Spec.PreKubeadmCommands,
PostKubeadmCommands: scope.Config.Spec.PostKubeadmCommands,
Users: users,
Expand Down Expand Up @@ -651,6 +652,7 @@ func (r *KubeadmConfigReconciler) joinWorker(ctx context.Context, scope *Scope)
BaseUserData: cloudinit.BaseUserData{
AdditionalFiles: files,
NTP: scope.Config.Spec.NTP,
BootCommands: scope.Config.Spec.BootCommands,
PreKubeadmCommands: scope.Config.Spec.PreKubeadmCommands,
PostKubeadmCommands: scope.Config.Spec.PostKubeadmCommands,
Users: users,
Expand Down Expand Up @@ -770,6 +772,7 @@ func (r *KubeadmConfigReconciler) joinControlplane(ctx context.Context, scope *S
BaseUserData: cloudinit.BaseUserData{
AdditionalFiles: files,
NTP: scope.Config.Spec.NTP,
BootCommands: scope.Config.Spec.BootCommands,
PreKubeadmCommands: scope.Config.Spec.PreKubeadmCommands,
PostKubeadmCommands: scope.Config.Spec.PostKubeadmCommands,
Users: users,
Expand Down
21 changes: 20 additions & 1 deletion bootstrap/kubeadm/internal/ignition/clc/clc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func TestRender(t *testing.T) {
// Test multi-line commands as well.
"cat <<EOF > /etc/modules-load.d/containerd.conf\noverlay\nbr_netfilter\nEOF\n",
}

tc := []struct {
desc string
input *cloudinit.BaseUserData
Expand All @@ -73,6 +72,11 @@ func TestRender(t *testing.T) {
{
desc: "renders valid Ignition JSON",
input: &cloudinit.BaseUserData{
BootCommands: []bootstrapv1.BootCommand{
{
"boot-command", "another-boot-command",
},
},
PreKubeadmCommands: preKubeadmCommands,
PostKubeadmCommands: postKubeadmCommands,
KubeadmCommand: "kubeadm join",
Expand Down Expand Up @@ -269,6 +273,11 @@ func TestRender(t *testing.T) {
{
desc: "multiple users with password auth",
input: &cloudinit.BaseUserData{
BootCommands: []bootstrapv1.BootCommand{
{
"boot-command", "another-boot-command",
},
},
PreKubeadmCommands: preKubeadmCommands,
PostKubeadmCommands: postKubeadmCommands,
KubeadmCommand: "kubeadm join",
Expand Down Expand Up @@ -351,6 +360,11 @@ func TestRender(t *testing.T) {
{
desc: "base64 encoded content",
input: &cloudinit.BaseUserData{
BootCommands: []bootstrapv1.BootCommand{
{
"boot-command", "another-boot-command",
},
},
PreKubeadmCommands: preKubeadmCommands,
PostKubeadmCommands: postKubeadmCommands,
KubeadmCommand: "kubeadm join",
Expand Down Expand Up @@ -434,6 +448,11 @@ func TestRender(t *testing.T) {
{
desc: "all file ownership combinations",
input: &cloudinit.BaseUserData{
BootCommands: []bootstrapv1.BootCommand{
{
"boot-command", "another-boot-command",
},
},
PreKubeadmCommands: preKubeadmCommands,
PostKubeadmCommands: postKubeadmCommands,
KubeadmCommand: "kubeadm join",
Expand Down
Loading
Loading