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

WIP: Support externally defined machine files. #425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions example/moreMachineFiles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- content: |
ANOTHER_TS_AUTHKEY=123456
permissions: 0o644
path: /var/etc/tailscale/another_auth.env
op: create
- content: "@./tsauth.env"
permissions: 0o644
path: /var/etc/tailscale2/another_auth.env
op: create
1 change: 1 addition & 0 deletions example/talconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ nodes:
permissions: 0o644
path: /var/etc/tailscale2/auth.env
op: create
- "@./moreMachineFiles.yaml"
installDiskSelector:
size: 4GB
model: WDC*
Expand Down
11 changes: 7 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ type Node struct {
}

type NodeConfigs struct {
NodeLabels map[string]string `yaml:"nodeLabels" jsonschema:"description=Labels to be added to the node"`
NodeTaints map[string]string `yaml:"nodeTaints" jsonschema:"description=Node taints for the node. Effect is optional"`
MachineDisks []*v1alpha1.MachineDisk `yaml:"machineDisks,omitempty" jsonschema:"description=List of additional disks to partition, format, mount"`
MachineFiles []*v1alpha1.MachineFile `yaml:"machineFiles,omitempty" jsonschema:"description=List of files to create inside the node"`
NodeLabels map[string]string `yaml:"nodeLabels" jsonschema:"description=Labels to be added to the node"`
NodeTaints map[string]string `yaml:"nodeTaints" jsonschema:"description=Node taints for the node. Effect is optional"`
MachineDisks []*v1alpha1.MachineDisk `yaml:"machineDisks,omitempty" jsonschema:"description=List of additional disks to partition, format, mount"`
MachineFileConfigs []MachineFile `yaml:"machineFiles,omitempty" jsonschema:"description=List of files to create inside the node"`
MachineFiles []*v1alpha1.MachineFile
DisableSearchDomain bool `yaml:"disableSearchDomain,omitempty" jsonschema:"description=Whether to disable generating default search domain"`
KernelModules []*v1alpha1.KernelModuleConfig `yaml:"kernelModules,omitempty" jsonschema:"description=List of additional kernel modules to load inside the node"`
Nameservers []string `yaml:"nameservers,omitempty" jsonschema:"description=List of nameservers for the node"`
Expand All @@ -56,6 +57,8 @@ type NodeConfigs struct {
IngressFirewall *IngressFirewall `yaml:"ingressFirewall,omitempty" jsonschema:"description=Machine firewall specification"`
}

type MachineFile interface{}

type ImageFactory struct {
RegistryURL string `yaml:"registryURL,omitempty" jsonschema:"default=factory.talos.dev,description=Registry url or the image"`
SchematicEndpoint string `yaml:"schematicEndpoint,omitempty" jsonschema:"default=/schematics,description:Endpoint to get schematic ID from the registry"`
Expand Down
85 changes: 81 additions & 4 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/budimanjojo/talhelper/pkg/substitute"
"github.com/fatih/color"
"github.com/mitchellh/mapstructure"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -49,13 +51,78 @@ func LoadAndValidateFromFile(filePath string, envPaths []string, showWarns bool)
node.OverrideGlobalCfg(cfg.Worker)
}

if len(node.MachineFiles) > 0 {
for i, file := range node.MachineFiles {
contents, err := ensureFileContent(file.FileContent)
if len(node.MachineFileConfigs) > 0 {
var additionalMachineFiles []map[string]interface{}
for i, file := range node.MachineFileConfigs {
switch f := file.(type) {
case string:
// check file and read from, convert to a machineFile fallthrough
slog.Debug(fmt.Sprintf("Reading a file: %s", f))
content, err := ensureFileContent(f)
if err != nil {
return nil, fmt.Errorf("failed to read machine file config for %s in `machineFiles[%d]`: %s", node.Hostname, i, err)
}

mf, err := newMachineFilesConfig([]byte(content))
if err != nil {
return nil, fmt.Errorf("failed to parse machine file config for %s in `machineFiles[%d]`: %s", node.Hostname, i, err)
}
additionalMachineFiles = append(additionalMachineFiles, mf...)

case map[string]interface{}:
// This is a machine file definition
mf := new(v1alpha1.MachineFile)

cfg := &mapstructure.DecoderConfig{
Metadata: nil,
Result: mf,
TagName: "yaml",
}
decoder, _ := mapstructure.NewDecoder(cfg)
err = decoder.Decode(f)
if err != nil {
return nil, fmt.Errorf("failed to decode machine file struct for %s in `machineFiles[%d]`: %s", node.Hostname, i, err)
}
contents, err := ensureFileContent(mf.FileContent)
if err != nil {
return nil, fmt.Errorf("failed to get machine file content for %s in `machineFiles[%d]`: %s", node.Hostname, i, err)
}
mf.FileContent = contents
node.MachineFiles = append(node.MachineFiles, mf)
default:
return nil, fmt.Errorf("failed to get machine file for %s in `machineFiles[%d]`", node.Hostname, i)
}
}

// Read the additionally created machine files as well
for i, file := range additionalMachineFiles {
mf := new(v1alpha1.MachineFile)

cfg := &mapstructure.DecoderConfig{
Metadata: nil,
Result: mf,
TagName: "yaml",
}
decoder, err := mapstructure.NewDecoder(cfg)
if err != nil {
return nil, fmt.Errorf("failed to set up the decoder")
}
err = decoder.Decode(file)
if err != nil {
return nil, fmt.Errorf("failed to decode machine file struct for %s in `machineFiles[%d]`: %s", node.Hostname, i, err)
}
contents, err := ensureFileContent(mf.FileContent)
if err != nil {
return nil, fmt.Errorf("failed to get machine file content for %s in `machineFiles[%d]`: %s", node.Hostname, i, err)
}
file.FileContent = contents
mf.FileContent = contents
slog.Debug(fmt.Sprintf("Read machine File %s", mf.FilePath))
node.MachineFiles = append(node.MachineFiles, mf)

}

for _, file := range node.MachineFiles {
slog.Debug(fmt.Sprintf("Read machine files: %s", file.FilePath))
}
}
}
Expand Down Expand Up @@ -123,6 +190,16 @@ func newConfig(source []byte) (c *TalhelperConfig, err error) {
return c, nil
}

// newMachineFilesConfig takes bytes and convert to a list of MachineFiles.
// It also returns an error, if any.
func newMachineFilesConfig(source []byte) (m []map[string]interface{}, err error) {
err = yaml.Unmarshal(source, &m)
if err != nil {
return nil, err
}
return m, nil
}

func ensureFileContent(value string) (string, error) {
if strings.HasPrefix(value, "@") {
slog.Debug(fmt.Sprintf("getting file content of %s", value))
Expand Down