From a7bbc4e940e5fd18b314d915993670702459a964 Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Tue, 7 Apr 2020 14:53:20 +0300 Subject: [PATCH] custom manifest support Signed-off-by: Jari Kolehmainen --- .travis.yml | 23 +++++++ README.md | 4 +- cmd/up.go | 2 + e2e/cluster.yml | 18 ++++++ e2e/footloose.yaml | 34 +++++++++++ e2e/manifests/redis.yaml | 7 +++ e2e/travis.sh | 34 +++++++++++ go.mod | 1 + go.sum | 3 + main.go | 5 ++ pkg/cluster/config.go | 7 ++- pkg/hosts/host.go | 87 +++++++++++++++++++++------ pkg/phases/connect_phase.go | 8 +-- pkg/phases/copy_manifests_phase.go | 66 ++++++++++++++++++++ pkg/phases/disconnect_phase.go | 2 +- pkg/phases/gather_host_facts_phase.go | 64 ++++++++++++++++++++ pkg/phases/setup_masters_phase.go | 16 +++-- pkg/phases/setup_workers_phase.go | 19 +++--- 18 files changed, 359 insertions(+), 41 deletions(-) create mode 100644 .travis.yml create mode 100644 e2e/cluster.yml create mode 100644 e2e/footloose.yaml create mode 100644 e2e/manifests/redis.yaml create mode 100755 e2e/travis.sh create mode 100644 pkg/phases/copy_manifests_phase.go create mode 100644 pkg/phases/gather_host_facts_phase.go diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f3a9c55 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +language: go +sudo: required +go: 1.13 +stages: + - name: e2e + if: type IN (pull_request, cron) +jobs: + include: + - stage: e2e + name: "e2e: ubuntu 18.04" + script: ./e2e/travis.sh + env: + - FOOTLOOSE_IMAGE=quay.io/footloose/ubuntu18.04 + - stage: e2e + name: "e2e: centos7" + script: ./e2e/travis.sh + env: + - FOOTLOOSE_IMAGE=quay.io/footloose/centos7 + - stage: e2e + name: "e2e: debian10" + script: ./e2e/travis.sh + env: + - FOOTLOOSE_IMAGE=quay.io/footloose/debian10 diff --git a/README.md b/README.md index 0b9b12c..9ac52d9 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ hosts: role: master ``` -### Fill cluster.yml example +### Full cluster.yml example ```yaml token: verysecret @@ -48,4 +48,6 @@ hosts: sshPort: 22 extraArgs: - "--node-label foo=bar" +manifests: + - ./manifests/*.yaml ``` diff --git a/cmd/up.go b/cmd/up.go index 2c8f1be..39b2d01 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -50,7 +50,9 @@ func clusterUp(ctx *cli.Context) error { phaseManager := phases.NewManager(&cluster) phaseManager.AddPhase(&phases.ConnectPhase{}) + phaseManager.AddPhase(&phases.GatherHostFactsPhase{}) phaseManager.AddPhase(&phases.SetupMastersPhase{}) + phaseManager.AddPhase(&phases.CopyManifestsPhase{}) phaseManager.AddPhase(&phases.SetupWorkersPhase{}) phaseManager.AddPhase(&phases.DisconnectPhase{}) diff --git a/e2e/cluster.yml b/e2e/cluster.yml new file mode 100644 index 0000000..02fe3b3 --- /dev/null +++ b/e2e/cluster.yml @@ -0,0 +1,18 @@ +hosts: + - address: "127.0.0.1" + sshPort: 9022 + user: "root" + role: "master" + sshKeyPath: ~/.ssh/id_rsa_travis + - address: "127.0.0.1" + sshPort: 9023 + user: "root" + role: "worker" + sshKeyPath: ~/.ssh/id_rsa_travis + - address: "127.0.0.1" + sshPort: 9024 + user: "root" + role: "worker" + sshKeyPath: ~/.ssh/id_rsa_travis +manifests: + - ./e2e/manifests/*.yaml diff --git a/e2e/footloose.yaml b/e2e/footloose.yaml new file mode 100644 index 0000000..1a8c640 --- /dev/null +++ b/e2e/footloose.yaml @@ -0,0 +1,34 @@ +cluster: + name: trieres + privateKey: ~/.ssh/id_rsa_travis +machines: +- count: 1 + backend: docker + spec: + image: $FOOTLOOSE_IMAGE + name: master%d + privileged: true + volumes: + - type: volume + destination: /var/lib/rancher + - type: volume + destination: /var/lib/containerd + portMappings: + - containerPort: 22 + hostPort: 9022 + - containerPort: 6443 + hostPort: 6443 +- count: 2 + backend: docker + spec: + image: $FOOTLOOSE_IMAGE + name: worker%d + privileged: true + volumes: + - type: volume + destination: /var/lib/rancher + - type: volume + destination: /var/lib/containerd + portMappings: + - containerPort: 22 + hostPort: 9022 diff --git a/e2e/manifests/redis.yaml b/e2e/manifests/redis.yaml new file mode 100644 index 0000000..4ce29a1 --- /dev/null +++ b/e2e/manifests/redis.yaml @@ -0,0 +1,7 @@ +apiVersion: helm.cattle.io/v1 +kind: HelmChart +metadata: + name: redis + namespace: kube-system +spec: + chart: stable/redis diff --git a/e2e/travis.sh b/e2e/travis.sh new file mode 100755 index 0000000..3288faf --- /dev/null +++ b/e2e/travis.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# shellcheck disable=SC2039 disable=SC1091 + +set -ue + +go build -o trieres main.go + +ssh-keygen -t rsa -f ~/.ssh/id_rsa_travis -N "" +cat ~/.ssh/id_rsa_travis.pub > ~/.ssh/authorized_keys +chmod 0600 ~/.ssh/authorized_keys + +envsubst < e2e/cluster.yml > cluster.yml +envsubst < e2e/footloose.yaml > footloose.yaml + +curl -L https://github.com/weaveworks/footloose/releases/download/0.6.3/footloose-0.6.3-linux-x86_64 > ./footloose +chmod +x ./footloose +./footloose create +./footloose ssh root@master0 -- 'apt-get install -y curl || yum install -y curl which openssh-clients' +./footloose ssh root@worker0 -- 'apt-get install -y curl || yum install -y curl which openssh-clients' +./footloose ssh root@worker1 -- 'apt-get install -y curl || yum install -y curl which openssh-clients' + +./trieres +./trieres -v +./trieres --debug up +./trieres kubeconfig > kubeconfig.e2e + +export KUBECONFIG=./kubeconfig.e2e + +echo "==> Test with sonobuoy" +curl -L https://github.com/vmware-tanzu/sonobuoy/releases/download/v0.18.0/sonobuoy_0.18.0_linux_amd64.tar.gz | tar xzv +chmod +x ./sonobuoy +sleep 30 +./sonobuoy run --mode quick --timeout 600 --wait + diff --git a/go.mod b/go.mod index 8449857..aef5ca8 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/avast/retry-go v2.5.0+incompatible + github.com/bramvdbogaerde/go-scp v0.0.0-20200119201711-987556b8bdd7 github.com/go-playground/validator/v10 v10.1.0 github.com/mitchellh/go-homedir v1.1.0 github.com/sirupsen/logrus v1.4.2 diff --git a/go.sum b/go.sum index 8303c63..856cc4f 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/alexellis/k3sup v0.0.0-20200125142521-8e396b7faa78/go.mod h1:BZUF5ulP github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/avast/retry-go v2.5.0+incompatible h1:8SaFqliw34WeeaPs+GEtMMkiwEsC2S6+YyqnLqI55Ks= github.com/avast/retry-go v2.5.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY= +github.com/bramvdbogaerde/go-scp v0.0.0-20200119201711-987556b8bdd7 h1:G24EOzrFCngJcgnDQgPWXTCBe3JP7lXE6n/Mnnn1yyM= +github.com/bramvdbogaerde/go-scp v0.0.0-20200119201711-987556b8bdd7/go.mod h1:aiQFnN5G0MivefWD+J4Em1a+CDyu/UBEmbNP5+8Gtd4= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -17,6 +19,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= diff --git a/main.go b/main.go index 79cc35a..18aa513 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,11 @@ var ( func init() { logrus.SetOutput(os.Stdout) logrus.SetLevel(logrus.InfoLevel) + + customFormatter := new(logrus.TextFormatter) + customFormatter.TimestampFormat = "2006-01-02 15:04:05" + customFormatter.FullTimestamp = true + logrus.SetFormatter(customFormatter) } func main() { diff --git a/pkg/cluster/config.go b/pkg/cluster/config.go index 06cbe1c..2874b73 100644 --- a/pkg/cluster/config.go +++ b/pkg/cluster/config.go @@ -11,9 +11,10 @@ import ( // Config describes cluster.yml type Config struct { - Hosts hosts.Hosts `validate:"required,dive,required,gt=0"` - Token string - Version string + Hosts hosts.Hosts `validate:"required,dive,required,gt=0"` + Token string `validate:"omitempty,gt=12"` + Manifests []string + Version string } // FromYaml parses config from YAML diff --git a/pkg/hosts/host.go b/pkg/hosts/host.go index 3832e55..cdabb07 100644 --- a/pkg/hosts/host.go +++ b/pkg/hosts/host.go @@ -5,8 +5,13 @@ import ( "fmt" "io" "io/ioutil" + "os" "path" + "strconv" + "strings" + "time" + "github.com/bramvdbogaerde/go-scp" "github.com/mitchellh/go-homedir" "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" @@ -19,22 +24,44 @@ type RemoteHost interface { // Config for host type Config struct { - Address string `yaml:"address" validate:"required,hostname|ip"` - User string `yaml:"user"` - SSHPort int `yaml:"sshPort" validate:"gt=0,lte=65535"` - SSHKeyPath string `yaml:"sshKeyPath" validate:"file"` - Role string `yaml:"role" validate:"oneof=master worker"` - ExtraArgs []string `yaml:"extraArgs"` + Address string `yaml:"address" validate:"required,hostname|ip"` + User string `yaml:"user"` + SSHPort int `yaml:"sshPort" validate:"gt=0,lte=65535"` + SSHKeyPath string `yaml:"sshKeyPath" validate:"file"` + Role string `yaml:"role" validate:"oneof=master worker"` + ExtraArgs []string `yaml:"extraArgs"` + PrivateInterface string `validate:"omitempty,gt=2"` +} + +// HostMetadata resolved metadata for host +type HostMetadata struct { + Hostname string + InternalAddress string } // Host describes connectable host type Host struct { Config sshClient *ssh.Client + Metadata *HostMetadata } +// Hosts array of hosts type Hosts []*Host +// FullAddress returns address and non-standard ssh port +func (h *Host) FullAddress() string { + address := h.Address + if h.Metadata != nil { + address = h.Metadata.Hostname + } + if h.SSHPort != 22 { + address = fmt.Sprintf("%s:%s", address, strconv.Itoa(h.SSHPort)) + } + + return address +} + // Connect to the host func (h *Host) Connect() error { key, err := ioutil.ReadFile(h.SSHKeyPath) @@ -88,29 +115,46 @@ func (h *Host) Exec(cmd string) error { outputScanner := bufio.NewScanner(multiReader) for outputScanner.Scan() { - logrus.Infof("%s: %s", h.Address, outputScanner.Text()) + logrus.Debugf("%s: %s", h.FullAddress(), outputScanner.Text()) } if err := outputScanner.Err(); err != nil { - logrus.Errorf("%s: %s", h.Address, err.Error()) + logrus.Errorf("%s: %s", h.FullAddress(), err.Error()) } return nil } -// Exec a command on the host and return output -func (h *Host) ExecWithOutput(cmd string) ([]byte, error) { +// ExecWithOutput execs a command on the host and return output +func (h *Host) ExecWithOutput(cmd string) (string, error) { session, err := h.sshClient.NewSession() if err != nil { - return []byte{}, err + return "", err } defer session.Close() output, err := session.CombinedOutput(cmd) if err != nil { - return []byte{}, nil + return "", nil + } + + return strings.TrimSpace(string(output)), nil +} + +// CopyFile copies a local file to host +func (h *Host) CopyFile(file os.File, remotePath string, permissions string) error { + session, err := h.sshClient.NewSession() + if err != nil { + return err + } + defer session.Close() + + scpClient := scp.Client{ + Session: session, + Timeout: time.Second * 60, + RemoteBinary: "scp", } - return output, nil + return scpClient.CopyFromFile(file, remotePath, permissions) } // Disconnect from the host @@ -122,21 +166,26 @@ func (h *Host) Disconnect() error { return h.sshClient.Close() } +// UnmarshalYAML unmarshals yaml func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { type rawConfig Config homeDir, _ := homedir.Dir() raw := rawConfig{ - Address: "127.0.0.1", - User: "root", - SSHKeyPath: path.Join(homeDir, ".ssh", "id_rsa"), - SSHPort: 22, - Role: "worker", - ExtraArgs: []string{}, + Address: "127.0.0.1", + User: "root", + SSHKeyPath: path.Join(homeDir, ".ssh", "id_rsa"), + SSHPort: 22, + Role: "worker", + ExtraArgs: []string{}, + PrivateInterface: "eth0", } if err := unmarshal(&raw); err != nil { return err } + if strings.HasPrefix(raw.SSHKeyPath, "~") { + raw.SSHKeyPath = path.Join(homeDir, raw.SSHKeyPath[2:]) + } *c = Config(raw) return nil diff --git a/pkg/phases/connect_phase.go b/pkg/phases/connect_phase.go index b36d690..e5c119f 100644 --- a/pkg/phases/connect_phase.go +++ b/pkg/phases/connect_phase.go @@ -30,19 +30,19 @@ func (p *ConnectPhase) connectHost(host *hosts.Host, wg *sync.WaitGroup) error { defer wg.Done() err := retry.Do( func() error { - logrus.Infof("%s: opening SSH connection", host.Address) + logrus.Infof("%s: opening SSH connection", host.FullAddress()) err := host.Connect() if err != nil { - logrus.Errorf("%s: failed to connect -> %s", host.Address, err.Error()) + logrus.Errorf("%s: failed to connect -> %s", host.FullAddress(), err.Error()) } return err }, ) if err != nil { - logrus.Errorf("%s: failed to open connection", host.Address) + logrus.Errorf("%s: failed to open connection", host.FullAddress()) return err } - logrus.Printf("%s: SSH connection opened", host.Address) + logrus.Printf("%s: SSH connection opened", host.FullAddress()) return nil } diff --git a/pkg/phases/copy_manifests_phase.go b/pkg/phases/copy_manifests_phase.go new file mode 100644 index 0000000..fe6f6a8 --- /dev/null +++ b/pkg/phases/copy_manifests_phase.go @@ -0,0 +1,66 @@ +package phases + +import ( + "crypto/md5" + "encoding/hex" + "fmt" + "os" + "path" + "path/filepath" + + "github.com/jakolehm/trieres/pkg/cluster" + "github.com/jakolehm/trieres/pkg/hosts" + "github.com/sirupsen/logrus" +) + +var hostPath = "/var/lib/rancher/k3s/server/manifests/trieres" + +type CopyManifestsPhase struct{} + +// Title returns phase title +func (p *CopyManifestsPhase) Title() string { + return "Copy manifests" +} + +// Run executes phase +func (p *CopyManifestsPhase) Run(config *cluster.Config) error { + masterHost := config.MasterHosts()[0] + + err := masterHost.Exec(fmt.Sprintf("sudo mkdir -p %s", hostPath)) + if err != nil { + return err + } + masterHost.Exec(fmt.Sprintf("sudo rm -f %s/*.yaml", hostPath)) + + for _, manifestGlob := range config.Manifests { + manifests, err := filepath.Glob(manifestGlob) + if err != nil { + return err + } + for _, manifest := range manifests { + file, err := os.Open(manifest) + if err != nil { + return err + } + logrus.Infof("%s: copying manifest %s", masterHost.FullAddress(), file.Name()) + err = handleManifest(masterHost, file) + if err != nil { + return err + } + } + } + + return nil +} + +func handleManifest(masterHost *hosts.Host, file *os.File) error { + hasher := md5.New() + hasher.Write([]byte(file.Name())) + targetFile := path.Join(hostPath, fmt.Sprintf("%s.yaml", hex.EncodeToString(hasher.Sum(nil)))) + copyErr := masterHost.CopyFile(*file, targetFile, "0600") + if copyErr != nil { + return copyErr + } + + return nil +} diff --git a/pkg/phases/disconnect_phase.go b/pkg/phases/disconnect_phase.go index 00a5864..41c738f 100644 --- a/pkg/phases/disconnect_phase.go +++ b/pkg/phases/disconnect_phase.go @@ -28,6 +28,6 @@ func (p *DisconnectPhase) Run(config *cluster.Config) error { func (p *DisconnectPhase) disconnectHost(host *hosts.Host, wg *sync.WaitGroup) error { defer wg.Done() host.Connect() - logrus.Printf("%s: SSH connection closed", host.Address) + logrus.Printf("%s: SSH connection closed", host.FullAddress()) return nil } diff --git a/pkg/phases/gather_host_facts_phase.go b/pkg/phases/gather_host_facts_phase.go new file mode 100644 index 0000000..620bc73 --- /dev/null +++ b/pkg/phases/gather_host_facts_phase.go @@ -0,0 +1,64 @@ +package phases + +import ( + "fmt" + "strings" + "sync" + + "github.com/jakolehm/trieres/pkg/cluster" + "github.com/jakolehm/trieres/pkg/hosts" + "github.com/sirupsen/logrus" +) + +type GatherHostFactsPhase struct{} + +func (p *GatherHostFactsPhase) Title() string { + return "Gather Host Facts" +} + +func (p *GatherHostFactsPhase) Run(config *cluster.Config) error { + var wg sync.WaitGroup + for _, host := range config.Hosts { + wg.Add(1) + logrus.Infof("%s: gathering facts", host.FullAddress()) + go investigateHost(host, &wg) + } + wg.Wait() + + return nil +} + +func investigateHost(host *hosts.Host, wg *sync.WaitGroup) error { + defer wg.Done() + + orgName := host.FullAddress() + host.Metadata = &hosts.HostMetadata{ + Hostname: resolveHostname(host), + InternalAddress: resolveInternalIP(host), + } + if host.Metadata.Hostname != "" { + logrus.Infof("%s: is now known as %s", orgName, host.FullAddress()) + } + return nil +} + +func resolveHostname(host *hosts.Host) string { + hostname, _ := host.ExecWithOutput("hostname -s") + + return hostname +} + +func resolveInternalIP(host *hosts.Host) string { + //ip -o addr show dev #{interface} scope global + output, _ := host.ExecWithOutput(fmt.Sprintf("ip -o addr show dev %s scope global", host.PrivateInterface)) + //logrus.Debugln(output) + lines := strings.Split(output, "\r\n") + for _, line := range lines { + items := strings.Fields(line) + addrItems := strings.Split(items[3], "/") + if addrItems[0] != host.Address { + return addrItems[0] + } + } + return host.Address +} diff --git a/pkg/phases/setup_masters_phase.go b/pkg/phases/setup_masters_phase.go index 344130a..9fb9db2 100644 --- a/pkg/phases/setup_masters_phase.go +++ b/pkg/phases/setup_masters_phase.go @@ -2,16 +2,17 @@ package phases import ( "fmt" + "strings" + retry "github.com/avast/retry-go" "github.com/jakolehm/trieres/pkg/cluster" "github.com/jakolehm/trieres/pkg/hosts" "github.com/sirupsen/logrus" - "strings" ) type SetupMastersPhase struct{} -var masterSetupCmd = "sh -c 'curl -sfL https://get.k3s.io | %s sh -s - server --agent-token %s %s'" +var masterSetupCmd = "sh -c 'curl -sfL https://get.k3s.io | %s sh -s - server --agent-token \"%s\" %s'" func (p *SetupMastersPhase) Title() string { return "Setup k3s masters" @@ -26,22 +27,25 @@ func (p *SetupMastersPhase) Run(config *cluster.Config) error { } func (p *SetupMastersPhase) setupMaster(host *hosts.Host, config *cluster.Config) error { + if strings.HasPrefix(host.Address, "127.") && host.Metadata.Hostname != "" { + host.ExtraArgs = append(host.ExtraArgs, fmt.Sprintf("--node-ip=%s", host.Metadata.InternalAddress)) + } err := retry.Do( func() error { - logrus.Infof("%s: setting up k3s master", host.Address) + logrus.Infof("%s: setting up k3s master", host.FullAddress()) setupCmd := fmt.Sprintf(masterSetupCmd, config.SetupEnvs(), config.Token, strings.Join(host.ExtraArgs, " ")) err := host.Exec(setupCmd) if err != nil { - logrus.Errorf("%s: failed -> %s", host.Address, err.Error()) + logrus.Errorf("%s: failed -> %s", host.FullAddress(), err.Error()) } return err }, ) if err != nil { - logrus.Errorf("%s: failed to setup k3s", host.Address) + logrus.Errorf("%s: failed to setup k3s", host.FullAddress()) return err } - logrus.Printf("%s: k3s setup succeeded", host.Address) + logrus.Printf("%s: k3s setup succeeded", host.FullAddress()) return nil } diff --git a/pkg/phases/setup_workers_phase.go b/pkg/phases/setup_workers_phase.go index 0a97be1..c2e29ac 100644 --- a/pkg/phases/setup_workers_phase.go +++ b/pkg/phases/setup_workers_phase.go @@ -2,12 +2,13 @@ package phases import ( "fmt" + "strings" + "sync" + retry "github.com/avast/retry-go" "github.com/jakolehm/trieres/pkg/cluster" "github.com/jakolehm/trieres/pkg/hosts" "github.com/sirupsen/logrus" - "strings" - "sync" ) type SetupWorkersPhase struct{} @@ -21,9 +22,13 @@ func (p *SetupWorkersPhase) Title() string { func (p *SetupWorkersPhase) Run(config *cluster.Config) error { master := config.MasterHosts()[0] wg := sync.WaitGroup{} + address := master.Address + if strings.HasPrefix(address, "127.") && master.Metadata.InternalAddress != "" { + address = master.Metadata.InternalAddress + } for _, host := range config.WorkerHosts() { wg.Add(1) - go p.setupWorker(&wg, host, master.Address, config) + go p.setupWorker(&wg, host, address, config) } wg.Wait() @@ -35,20 +40,20 @@ func (p *SetupWorkersPhase) setupWorker(wg *sync.WaitGroup, host *hosts.Host, ma err := retry.Do( func() error { - logrus.Infof("%s: setting up k3s worker", host.Address) + logrus.Infof("%s: setting up k3s worker", host.FullAddress()) setupCmd := fmt.Sprintf(workerSetupCmd, config.SetupEnvs(), master, config.Token, strings.Join(host.ExtraArgs, " ")) err := host.Exec(setupCmd) if err != nil { - logrus.Errorf("%s: failed -> %s", host.Address, err.Error()) + logrus.Errorf("%s: failed -> %s", host.FullAddress(), err.Error()) } return err }, ) if err != nil { - logrus.Errorf("%s: failed to setup k3s", host.Address) + logrus.Errorf("%s: failed to setup k3s", host.FullAddress()) return err } - logrus.Printf("%s: k3s worker setup succeeded", host.Address) + logrus.Printf("%s: k3s worker setup succeeded", host.FullAddress()) return nil }