Skip to content

Commit

Permalink
fix(engine): merge and upload node config
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasfrahm committed Apr 24, 2022
1 parent 8136f86 commit dbb23a2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
32 changes: 30 additions & 2 deletions pkg/engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"errors"
"io/ioutil"
"reflect"

"github.com/nicklasfrahm/k3se/pkg/sshx"
"gopkg.in/yaml.v3"
Expand All @@ -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
Expand All @@ -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.
Expand Down
38 changes: 29 additions & 9 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/nicklasfrahm/k3se/pkg/sshx"
"github.com/rs/zerolog"
"gopkg.in/yaml.v3"
)

const (
Expand All @@ -20,6 +21,8 @@ type Engine struct {

sync.Mutex
installer []byte

Spec *Config
}

// New creates a new Engine.
Expand Down Expand Up @@ -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
Expand All @@ -69,25 +86,28 @@ 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
}

// 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",
})
Expand Down
7 changes: 4 additions & 3 deletions pkg/ops/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down

0 comments on commit dbb23a2

Please sign in to comment.