Skip to content

Commit

Permalink
Use the same template for cloud-config
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Sep 22, 2024
1 parent de9cce4 commit 2b64f45
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 70 deletions.
2 changes: 2 additions & 0 deletions pkg/cidata/cidata.TEMPLATE.d/user-data
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ users:
- {{ printf "%q" $val }}
{{- end }}

{{- if .BootScripts }}
write_files:
- content: |
#!/bin/sh
Expand All @@ -52,6 +53,7 @@ write_files:
owner: root:root
path: /var/lib/cloud/scripts/per-boot/00-lima.boot.sh
permissions: '0755'
{{- end }}

{{- if .DNSAddresses }}
# This has no effect on systems using systemd-resolved, but is used
Expand Down
11 changes: 8 additions & 3 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func setupEnv(y *limayaml.LimaYAML, args TemplateArgs) (map[string]string, error
return env, nil
}

func templateArgs(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string) (*TemplateArgs, error) {
func templateArgs(bootScripts bool, instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string) (*TemplateArgs, error) {
if err := limayaml.Validate(y, false); err != nil {
return nil, err
}
Expand All @@ -124,6 +124,7 @@ func templateArgs(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, t
return nil, err
}
args := TemplateArgs{
BootScripts: bootScripts,
Name: name,
User: u.Username,
UID: uid,
Expand Down Expand Up @@ -328,10 +329,14 @@ func templateArgs(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, t
}

func GenerateCloudConfig(instDir, name string, y *limayaml.LimaYAML) error {
args, err := templateArgs(instDir, name, y, 0, 0, 0, "")
args, err := templateArgs(false, instDir, name, y, 0, 0, 0, "")
if err != nil {
return err
}
// mounts are not included here
args.Mounts = nil
// resolv_conf is not included here
args.DNSAddresses = nil

if err := ValidateTemplateArgs(args); err != nil {
return err
Expand All @@ -346,7 +351,7 @@ func GenerateCloudConfig(instDir, name string, y *limayaml.LimaYAML) error {
}

func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, nerdctlArchive string, vsockPort int, virtioPort string) error {
args, err := templateArgs(instDir, name, y, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
args, err := templateArgs(true, instDir, name, y, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
if err != nil {
return err
}
Expand Down
64 changes: 0 additions & 64 deletions pkg/cidata/cloud-config.yaml

This file was deleted.

1 change: 1 addition & 0 deletions pkg/cidata/cloud-config.yaml
10 changes: 7 additions & 3 deletions pkg/cidata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ var templateFS embed.FS

const templateFSRoot = "cidata.TEMPLATE.d"

//go:embed cloud-config.yaml
var cloudConfigYaml string

type CACerts struct {
RemoveDefaults *bool
Trusted []Cert
Expand Down Expand Up @@ -77,6 +74,7 @@ type TemplateArgs struct {
TCPDNSLocalPort int
Env map[string]string
Param map[string]string
BootScripts bool
DNSAddresses []string
CACerts CACerts
HostHomeMountPoint string
Expand Down Expand Up @@ -123,7 +121,13 @@ func ExecuteTemplateCloudConfig(args *TemplateArgs) ([]byte, error) {
if err := ValidateTemplateArgs(args); err != nil {
return nil, err
}

userData, err := templateFS.ReadFile(path.Join(templateFSRoot, "user-data"))
if err != nil {
return nil, err
}

cloudConfigYaml := string(userData)
return textutil.ExecuteTemplate(cloudConfigYaml, args)
}

Expand Down

0 comments on commit 2b64f45

Please sign in to comment.