Skip to content

Commit f80c5d7

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

File tree

4 files changed

+19
-70
lines changed

4 files changed

+19
-70
lines changed

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func setupEnv(instConfigEnv map[string]string, propagateProxyEnv bool, slirpGate
111111
return env, nil
112112
}
113113

114-
func templateArgs(instDir, name string, instConfig *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string) (*TemplateArgs, error) {
114+
func templateArgs(bootScripts bool, instDir, name string, instConfig *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string) (*TemplateArgs, error) {
115115
if err := limayaml.Validate(instConfig, false); err != nil {
116116
return nil, err
117117
}
@@ -124,6 +124,7 @@ func templateArgs(instDir, name string, instConfig *limayaml.LimaYAML, udpDNSLoc
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, instConfig *limayaml.LimaYAML, udpDNSLoc
328329
}
329330

330331
func GenerateCloudConfig(instDir, name string, instConfig *limayaml.LimaYAML) error {
331-
args, err := templateArgs(instDir, name, instConfig, 0, 0, 0, "")
332+
args, err := templateArgs(false, instDir, name, instConfig, 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, instConfig *limayaml.LimaYAML) er
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, instConfig *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, nerdctlArchive string, vsockPort int, virtioPort string) error {
349-
args, err := templateArgs(instDir, name, instConfig, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
355+
args, err := templateArgs(true, instDir, name, instConfig, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
350356
if err != nil {
351357
return err
352358
}

pkg/cidata/cloud-config.yaml

Lines changed: 0 additions & 64 deletions
This file was deleted.

pkg/cidata/cloud-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cidata.TEMPLATE.d/user-data

pkg/cidata/template.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ var templateFS embed.FS
2020

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

23-
//go:embed cloud-config.yaml
24-
var cloudConfigYaml string
25-
2623
type CACerts struct {
2724
RemoveDefaults *bool
2825
Trusted []Cert
@@ -78,6 +75,7 @@ type TemplateArgs struct {
7875
TCPDNSLocalPort int
7976
Env map[string]string
8077
Param map[string]string
78+
BootScripts bool
8179
DNSAddresses []string
8280
CACerts CACerts
8381
HostHomeMountPoint string
@@ -124,7 +122,13 @@ func ExecuteTemplateCloudConfig(args *TemplateArgs) ([]byte, error) {
124122
if err := ValidateTemplateArgs(args); err != nil {
125123
return nil, err
126124
}
125+
126+
userData, err := templateFS.ReadFile(path.Join(templateFSRoot, "user-data"))
127+
if err != nil {
128+
return nil, err
127129
}
130+
131+
cloudConfigYaml := string(userData)
128132
return textutil.ExecuteTemplate(cloudConfigYaml, args)
129133
}
130134

0 commit comments

Comments
 (0)