Skip to content

Commit c93e66b

Browse files
authored
Merge pull request #85 from canonical/KU-344/cloudinit-run
Add support for boot, preRun and postRun commands on cloud-init
2 parents b095c3f + cbdbda5 commit c93e66b

11 files changed

+208
-4
lines changed

apis/v1beta1/microk8sconfig_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ type InitConfiguration struct {
104104
// ExtraKubeletArgs is a list of extra arguments to add to the kubelet.
105105
// +optional
106106
ExtraKubeletArgs []string `json:"extraKubeletArgs,omitempty"`
107+
108+
// BootCommands is a list of commands to run during boot.
109+
// These will be injected into the `bootcmd` section of cloud-init.
110+
BootCommands []string `json:"bootCommands,omitempty"`
111+
112+
// PreRunCommands is a list of commands to run before installing MicroK8s.
113+
// These will be injected into the `runcmd` section of cloud-init.
114+
// +optional
115+
PreRunCommands []string `json:"preRunCommands,omitempty"`
116+
117+
// PostRunCommands is a list of commands to run after installing MicroK8s.
118+
// These will be injected into the `runcmd` section of cloud-init.
119+
// +optional
120+
PostRunCommands []string `json:"postRunCommands,omitempty"`
107121
}
108122

109123
// CloudInitWriteFile is a file that will be injected by cloud-init

apis/v1beta1/zz_generated.deepcopy.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bootstrap-components.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ spec:
8585
schemas to the latest internal value, and may reject unrecognized
8686
values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
8787
type: string
88+
bootCommands:
89+
description: BootCommands is a list of commands to run during
90+
boot. These will be injected into the `bootcmd` section of cloud-init.
91+
items:
92+
type: string
93+
type: array
8894
confinement:
8995
description: The confinement (strict or classic) configuration
9096
enum:
@@ -145,6 +151,20 @@ spec:
145151
noProxy:
146152
description: The optional no proxy configuration
147153
type: string
154+
postRunCommands:
155+
description: PostRunCommands is a list of commands to run after
156+
installing MicroK8s. These will be injected into the `runcmd`
157+
section of cloud-init.
158+
items:
159+
type: string
160+
type: array
161+
preRunCommands:
162+
description: PreRunCommands is a list of commands to run before
163+
installing MicroK8s. These will be injected into the `runcmd`
164+
section of cloud-init.
165+
items:
166+
type: string
167+
type: array
148168
riskLevel:
149169
default: stable
150170
description: The risk-level (stable, candidate, beta, or edge)
@@ -336,6 +356,13 @@ spec:
336356
convert recognized schemas to the latest internal value,
337357
and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
338358
type: string
359+
bootCommands:
360+
description: BootCommands is a list of commands to run
361+
during boot. These will be injected into the `bootcmd`
362+
section of cloud-init.
363+
items:
364+
type: string
365+
type: array
339366
confinement:
340367
description: The confinement (strict or classic) configuration
341368
enum:
@@ -397,6 +424,20 @@ spec:
397424
noProxy:
398425
description: The optional no proxy configuration
399426
type: string
427+
postRunCommands:
428+
description: PostRunCommands is a list of commands to
429+
run after installing MicroK8s. These will be injected
430+
into the `runcmd` section of cloud-init.
431+
items:
432+
type: string
433+
type: array
434+
preRunCommands:
435+
description: PreRunCommands is a list of commands to run
436+
before installing MicroK8s. These will be injected into
437+
the `runcmd` section of cloud-init.
438+
items:
439+
type: string
440+
type: array
400441
riskLevel:
401442
default: stable
402443
description: The risk-level (stable, candidate, beta,

config/crd/bases/bootstrap.cluster.x-k8s.io_microk8sconfigs.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ spec:
7575
schemas to the latest internal value, and may reject unrecognized
7676
values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
7777
type: string
78+
bootCommands:
79+
description: BootCommands is a list of commands to run during
80+
boot. These will be injected into the `bootcmd` section of cloud-init.
81+
items:
82+
type: string
83+
type: array
7884
confinement:
7985
description: The confinement (strict or classic) configuration
8086
enum:
@@ -135,6 +141,20 @@ spec:
135141
noProxy:
136142
description: The optional no proxy configuration
137143
type: string
144+
postRunCommands:
145+
description: PostRunCommands is a list of commands to run after
146+
installing MicroK8s. These will be injected into the `runcmd`
147+
section of cloud-init.
148+
items:
149+
type: string
150+
type: array
151+
preRunCommands:
152+
description: PreRunCommands is a list of commands to run before
153+
installing MicroK8s. These will be injected into the `runcmd`
154+
section of cloud-init.
155+
items:
156+
type: string
157+
type: array
138158
riskLevel:
139159
default: stable
140160
description: The risk-level (stable, candidate, beta, or edge)

config/crd/bases/bootstrap.cluster.x-k8s.io_microk8sconfigtemplates.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ spec:
8282
convert recognized schemas to the latest internal value,
8383
and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
8484
type: string
85+
bootCommands:
86+
description: BootCommands is a list of commands to run
87+
during boot. These will be injected into the `bootcmd`
88+
section of cloud-init.
89+
items:
90+
type: string
91+
type: array
8592
confinement:
8693
description: The confinement (strict or classic) configuration
8794
enum:
@@ -143,6 +150,20 @@ spec:
143150
noProxy:
144151
description: The optional no proxy configuration
145152
type: string
153+
postRunCommands:
154+
description: PostRunCommands is a list of commands to
155+
run after installing MicroK8s. These will be injected
156+
into the `runcmd` section of cloud-init.
157+
items:
158+
type: string
159+
type: array
160+
preRunCommands:
161+
description: PreRunCommands is a list of commands to run
162+
before installing MicroK8s. These will be injected into
163+
the `runcmd` section of cloud-init.
164+
items:
165+
type: string
166+
type: array
146167
riskLevel:
147168
default: stable
148169
description: The risk-level (stable, candidate, beta,

controllers/cloudinit/cloudinit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func NewBaseCloudConfig() *CloudConfig {
6969
})
7070
}
7171
return &CloudConfig{
72-
WriteFiles: writeFiles,
72+
WriteFiles: writeFiles,
73+
RunCommands: []string{"set -x"},
7374
}
7475
}

controllers/cloudinit/cloudinit_common_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,60 @@ func TestCloudConfigInput(t *testing.T) {
326326
})
327327
}
328328
})
329+
330+
t.Run("CustomCommands", func(t *testing.T) {
331+
for _, tc := range []struct {
332+
name string
333+
makeCloudConfig func() (*cloudinit.CloudConfig, error)
334+
}{
335+
{
336+
name: "ControlPlaneInit",
337+
makeCloudConfig: func() (*cloudinit.CloudConfig, error) {
338+
return cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInitInput{
339+
BootCommands: []string{"cmd1"},
340+
PreRunCommands: []string{"cmd2"},
341+
PostRunCommands: []string{"cmd3"},
342+
KubernetesVersion: "v1.25.0",
343+
Token: strings.Repeat("a", 32),
344+
TokenTTL: 100,
345+
})
346+
},
347+
},
348+
{
349+
name: "ControlPlaneJoin",
350+
makeCloudConfig: func() (*cloudinit.CloudConfig, error) {
351+
return cloudinit.NewJoinControlPlane(&cloudinit.ControlPlaneJoinInput{
352+
BootCommands: []string{"cmd1"},
353+
PreRunCommands: []string{"cmd2"},
354+
PostRunCommands: []string{"cmd3"},
355+
KubernetesVersion: "v1.25.0",
356+
Token: strings.Repeat("a", 32),
357+
TokenTTL: 100,
358+
})
359+
},
360+
},
361+
{
362+
name: "Worker",
363+
makeCloudConfig: func() (*cloudinit.CloudConfig, error) {
364+
return cloudinit.NewJoinWorker(&cloudinit.WorkerInput{
365+
BootCommands: []string{"cmd1"},
366+
PreRunCommands: []string{"cmd2"},
367+
PostRunCommands: []string{"cmd3"},
368+
KubernetesVersion: "v1.25.0",
369+
Token: strings.Repeat("a", 32),
370+
})
371+
},
372+
},
373+
} {
374+
t.Run(tc.name, func(t *testing.T) {
375+
g := NewWithT(t)
376+
c, err := tc.makeCloudConfig()
377+
g.Expect(err).NotTo(HaveOccurred())
378+
379+
g.Expect(c.BootCommands).To(ConsistOf(`cmd1`))
380+
g.Expect(c.RunCommands).To(ContainElement(`cmd2`))
381+
g.Expect(c.RunCommands[len(c.RunCommands)-1]).To(Equal(`cmd3`))
382+
})
383+
}
384+
})
329385
}

controllers/cloudinit/controlplane_init.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ type ControlPlaneInitInput struct {
6969
SnapstoreHTTPProxy string
7070
// SnapstoreHTTPSProxy is https_proxy configuration for snap store.
7171
SnapstoreHTTPSProxy string
72+
// BootCommands is a list of commands to add to the "bootcmd" section of cloud-init.
73+
BootCommands []string
74+
// PreRunCommands is a list of commands to add to the "runcmd" section of cloud-init before installing MicroK8s.
75+
PreRunCommands []string
76+
// PostRunCommands is a list of commands to add to the "runcmd" section of cloud-init after installing MicroK8s.
77+
PostRunCommands []string
7278
}
7379

7480
func NewInitControlPlane(input *ControlPlaneInitInput) (*CloudConfig, error) {
@@ -128,8 +134,10 @@ func NewInitControlPlane(input *ControlPlaneInitInput) (*CloudConfig, error) {
128134
Owner: "root:root",
129135
})
130136
}
137+
cloudConfig.BootCommands = append(cloudConfig.BootCommands, input.BootCommands...)
138+
139+
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PreRunCommands...)
131140
cloudConfig.RunCommands = append(cloudConfig.RunCommands,
132-
"set -x",
133141
fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy),
134142
fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId),
135143
scriptPath(disableHostServicesScript),
@@ -146,6 +154,7 @@ func NewInitControlPlane(input *ControlPlaneInitInput) (*CloudConfig, error) {
146154
fmt.Sprintf("%s %s", scriptPath(microk8sEnableScript), strings.Join(addons, " ")),
147155
fmt.Sprintf("microk8s add-node --token-ttl %v --token %q", input.TokenTTL, input.Token),
148156
)
157+
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PostRunCommands...)
149158

150159
return cloudConfig, nil
151160
}

controllers/cloudinit/controlplane_join.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ type ControlPlaneJoinInput struct {
6565
SnapstoreHTTPProxy string
6666
// SnapstoreHTTPSProxy is https_proxy configuration for snap store.
6767
SnapstoreHTTPSProxy string
68+
// BootCommands is a list of commands to add to the "bootcmd" section of cloud-init.
69+
BootCommands []string
70+
// PreRunCommands is a list of commands to add to the "runcmd" section of cloud-init before installing MicroK8s.
71+
PreRunCommands []string
72+
// PostRunCommands is a list of commands to add to the "runcmd" section of cloud-init after installing MicroK8s.
73+
PostRunCommands []string
6874
}
6975

7076
func NewJoinControlPlane(input *ControlPlaneJoinInput) (*CloudConfig, error) {
@@ -108,8 +114,10 @@ func NewJoinControlPlane(input *ControlPlaneJoinInput) (*CloudConfig, error) {
108114
joinStr := fmt.Sprintf("%s:%s/%s", input.JoinNodeIPs[0], input.ClusterAgentPort, input.Token)
109115
joinStrAlt := fmt.Sprintf("%s:%s/%s", input.JoinNodeIPs[1], input.ClusterAgentPort, input.Token)
110116

117+
cloudConfig.BootCommands = append(cloudConfig.BootCommands, input.BootCommands...)
118+
119+
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PreRunCommands...)
111120
cloudConfig.RunCommands = append(cloudConfig.RunCommands,
112-
"set -x",
113121
fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy),
114122
fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId),
115123
scriptPath(disableHostServicesScript),
@@ -126,6 +134,7 @@ func NewJoinControlPlane(input *ControlPlaneJoinInput) (*CloudConfig, error) {
126134
scriptPath(configureAPIServerScript),
127135
fmt.Sprintf("microk8s add-node --token-ttl %v --token %q", input.TokenTTL, input.Token),
128136
)
137+
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PostRunCommands...)
129138

130139
return cloudConfig, nil
131140
}

controllers/cloudinit/worker_join.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ type WorkerInput struct {
5858
SnapstoreHTTPProxy string
5959
// SnapstoreHTTPSProxy is https_proxy configuration for snap store.
6060
SnapstoreHTTPSProxy string
61+
// BootCommands is a list of commands to add to the "bootcmd" section of cloud-init.
62+
BootCommands []string
63+
// PreRunCommands is a list of commands to add to the "runcmd" section of cloud-init before installing MicroK8s.
64+
PreRunCommands []string
65+
// PostRunCommands is a list of commands to add to the "runcmd" section of cloud-init after installing MicroK8s.
66+
PostRunCommands []string
6167
}
6268

6369
func NewJoinWorker(input *WorkerInput) (*CloudConfig, error) {
@@ -97,8 +103,10 @@ func NewJoinWorker(input *WorkerInput) (*CloudConfig, error) {
97103
joinStr := fmt.Sprintf("%s:%s/%s", input.JoinNodeIPs[0], input.ClusterAgentPort, input.Token)
98104
joinStrAlt := fmt.Sprintf("%s:%s/%s", input.JoinNodeIPs[1], input.ClusterAgentPort, input.Token)
99105

106+
cloudConfig.BootCommands = append(cloudConfig.BootCommands, input.BootCommands...)
107+
108+
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PreRunCommands...)
100109
cloudConfig.RunCommands = append(cloudConfig.RunCommands,
101-
"set -x",
102110
fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy),
103111
fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId),
104112
scriptPath(disableHostServicesScript),
@@ -110,6 +118,7 @@ func NewJoinWorker(input *WorkerInput) (*CloudConfig, error) {
110118
fmt.Sprintf("%s yes %q %q", scriptPath(microk8sJoinScript), joinStr, joinStrAlt),
111119
fmt.Sprintf("%s %s 6443 %s", scriptPath(configureTraefikScript), input.ControlPlaneEndpoint, stopApiServerProxyRefreshes),
112120
)
121+
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PostRunCommands...)
113122

114123
return cloudConfig, nil
115124
}

0 commit comments

Comments
 (0)