From dbb23a26da59d947ac4e94499c0c366a3842edbf Mon Sep 17 00:00:00 2001 From: Nicklas Frahm Date: Mon, 25 Apr 2022 00:18:04 +0200 Subject: [PATCH] fix(engine): merge and upload node config --- pkg/engine/config.go | 32 ++++++++++++++++++++++++++++++-- pkg/engine/engine.go | 38 +++++++++++++++++++++++++++++--------- pkg/ops/up.go | 7 ++++--- 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/pkg/engine/config.go b/pkg/engine/config.go index 2cecdf4..874ac27 100644 --- a/pkg/engine/config.go +++ b/pkg/engine/config.go @@ -3,6 +3,7 @@ package engine import ( "errors" "io/ioutil" + "reflect" "github.com/nicklasfrahm/k3se/pkg/sshx" "gopkg.in/yaml.v3" @@ -25,6 +26,33 @@ type K3sConfig struct { WriteKubeconfigMode string `yaml:"write-kubeconfig-mode"` TLSSAN []string `yaml:"tls-san"` NodeLabel []string `yaml:"node-label"` + Disable []string `yaml:"disable"` + // TODO: Add missing config options as specified here: + // https://rancher.com/docs/k3s/latest/en/installation/install-options/server-config/#k3s-server-cli-help +} + +// Merge combines two configurations. +func (c K3sConfig) Merge(config *K3sConfig) K3sConfig { + merged := c + + dst := reflect.ValueOf(&merged).Elem() + src := reflect.ValueOf(config).Elem() + + for i := 0; i < src.Type().NumField(); i++ { + field := src.Type().Field(i) + + if field.Type.Kind() == reflect.Slice { + // Merge slices. + dst.Field(i).Set(reflect.AppendSlice(dst.Field(i), src.Field(i))) + } else { + // Overwrite field value if not empty. + if !src.Field(i).IsZero() { + dst.Field(i).Set(src.Field(i)) + } + } + } + + return merged } // Config describes the state of a k3s cluster. For general @@ -35,9 +63,9 @@ type Config struct { // channel as specified in the k3s installation options. Version string `yaml:"version"` - // Config is the desired content of the k3s configuration file + // Cluster is the desired content of the k3s configuration file // that is shared among all nodes. - Cluster K3sConfig `yaml:"config"` + Cluster K3sConfig `yaml:"cluster"` // Nodes is a list of nodes to deploy the cluster on. It stores // both, connection information and node-specific configuration. diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 374978f..ebdea88 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -8,6 +8,7 @@ import ( "github.com/nicklasfrahm/k3se/pkg/sshx" "github.com/rs/zerolog" + "gopkg.in/yaml.v3" ) const ( @@ -20,6 +21,8 @@ type Engine struct { sync.Mutex installer []byte + + Spec *Config } // New creates a new Engine. @@ -57,9 +60,23 @@ func (e *Engine) Installer() ([]byte, error) { return e.installer, nil } +// SetSpec configures the desired state of the cluster. Note +// that the config will only be applied if the verification +// succeeds. +func (e *Engine) SetSpec(config *Config) error { + if err := config.Verify(); err != nil { + return err + } + + e.Spec = config + + return nil +} + // Configure uploads the installer and the configuration // prior to a node prior to running the installation. func (e *Engine) Configure(node *Node) error { + // Upload the installer. installer, err := e.Installer() if err != nil { return err @@ -69,9 +86,21 @@ func (e *Engine) Configure(node *Node) error { return err } + // Create the node configuration. + config := e.Spec.Cluster.Merge(&node.Config) + // TODO: Configure the "advertise address" based on the first SAN and modify the // kubeconfig accordingly. + configBytes, err := yaml.Marshal(config) + if err != nil { + return err + } + + if err := node.Upload("/tmp/k3se/config.yaml", bytes.NewReader(configBytes)); err != nil { + return err + } + // TODO: Upload configuration and move it to appropriate location using "sudo". return nil @@ -79,15 +108,6 @@ func (e *Engine) Configure(node *Node) error { // Cleanup removes all temporary files from the node. func (e *Engine) Cleanup(node *Node) error { - if err := node.Do(sshx.Cmd{ - Cmd: "echo $MYVAR > ~/test.txt", - Env: map[string]string{ - "MYVAR": "hello", - }, - }); err != nil { - return err - } - return node.Do(sshx.Cmd{ Cmd: "rm -rf /tmp/k3se", }) diff --git a/pkg/ops/up.go b/pkg/ops/up.go index 0814fcc..b97b17f 100644 --- a/pkg/ops/up.go +++ b/pkg/ops/up.go @@ -17,15 +17,16 @@ func Up(options ...Option) error { if err != nil { return err } - if err := config.Verify(); err != nil { - return err - } eng, err := engine.New(engine.WithLogger(opts.Logger)) if err != nil { return err } + if err := eng.SetSpec(config); err != nil { + return err + } + // Establish connection to proxy if host is specified. var sshProxy *sshx.Client if config.SSHProxy.Host != "" {