Skip to content

Commit 8fee921

Browse files
committed
Use the same template for cloud-config
Signed-off-by: Anders F Björklund <[email protected]>
1 parent ec8c2f7 commit 8fee921

File tree

4 files changed

+19
-70
lines changed

4 files changed

+19
-70
lines changed

pkg/cidata/cidata.TEMPLATE.d/user-data

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ users:
3939
- {{ printf "%q" $val }}
4040
{{- end }}
4141

42+
{{- if .BootScripts }}
4243
write_files:
4344
- content: |
4445
#!/bin/sh
@@ -52,6 +53,7 @@ write_files:
5253
owner: root:root
5354
path: /var/lib/cloud/scripts/per-boot/00-lima.boot.sh
5455
permissions: '0755'
56+
{{- end }}
5557

5658
{{- if .DNSAddresses }}
5759
# This has no effect on systems using systemd-resolved, but is used

pkg/cidata/cidata.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func setupEnv(y *limayaml.LimaYAML, args TemplateArgs) (map[string]string, error
111111
return env, nil
112112
}
113113

114-
func templateArgs(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string) (*TemplateArgs, error) {
114+
func templateArgs(bootScripts bool, instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string) (*TemplateArgs, error) {
115115
if err := limayaml.Validate(y, false); err != nil {
116116
return nil, err
117117
}
@@ -124,6 +124,7 @@ func templateArgs(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, t
124124
return nil, err
125125
}
126126
args := TemplateArgs{
127+
BootScripts: bootScripts,
127128
Name: name,
128129
User: u.Username,
129130
UID: uid,
@@ -328,10 +329,14 @@ func templateArgs(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, t
328329
}
329330

330331
func GenerateCloudConfig(instDir, name string, y *limayaml.LimaYAML) error {
331-
args, err := templateArgs(instDir, name, y, 0, 0, 0, "")
332+
args, err := templateArgs(false, instDir, name, y, 0, 0, 0, "")
332333
if err != nil {
333334
return err
334335
}
336+
// mounts are not included here
337+
args.Mounts = nil
338+
// resolv_conf is not included here
339+
args.DNSAddresses = nil
335340

336341
if err := ValidateTemplateArgs(args); err != nil {
337342
return err
@@ -342,11 +347,12 @@ func GenerateCloudConfig(instDir, name string, y *limayaml.LimaYAML) error {
342347
return err
343348
}
344349

350+
os.RemoveAll(filepath.Join(instDir, filenames.CloudConfig)) // delete existing
345351
return os.WriteFile(filepath.Join(instDir, filenames.CloudConfig), config, 0o444)
346352
}
347353

348354
func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, nerdctlArchive string, vsockPort int, virtioPort string) error {
349-
args, err := templateArgs(instDir, name, y, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
355+
args, err := templateArgs(true, instDir, name, y, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
350356
if err != nil {
351357
return err
352358
}

pkg/cidata/cloud-config.yaml

-64
This file was deleted.

pkg/cidata/cloud-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cidata.TEMPLATE.d/user-data

pkg/cidata/template.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ var templateFS embed.FS
1919

2020
const templateFSRoot = "cidata.TEMPLATE.d"
2121

22-
//go:embed cloud-config.yaml
23-
var cloudConfigYaml string
24-
2522
type CACerts struct {
2623
RemoveDefaults *bool
2724
Trusted []Cert
@@ -77,6 +74,7 @@ type TemplateArgs struct {
7774
TCPDNSLocalPort int
7875
Env map[string]string
7976
Param map[string]string
77+
BootScripts bool
8078
DNSAddresses []string
8179
CACerts CACerts
8280
HostHomeMountPoint string
@@ -123,7 +121,13 @@ func ExecuteTemplateCloudConfig(args *TemplateArgs) ([]byte, error) {
123121
if err := ValidateTemplateArgs(args); err != nil {
124122
return nil, err
125123
}
124+
125+
userData, err := templateFS.ReadFile(path.Join(templateFSRoot, "user-data"))
126+
if err != nil {
127+
return nil, err
126128
}
129+
130+
cloudConfigYaml := string(userData)
127131
return textutil.ExecuteTemplate(cloudConfigYaml, args)
128132
}
129133

0 commit comments

Comments
 (0)