diff --git a/README.md b/README.md index 8baf6d4b..6d9191c4 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,10 @@ When `false`, the k0s binary downloading is performed on the target host itself A path to a file on the local host that contains a k0s binary to be uploaded to the host. Can be used to test drive a custom development build of k0s. +###### `spec.hosts[*].hostname` <string> (optional) + +Override host's hostname. When not set, the hostname reported by the operating system is used. + ###### `spec.hosts[*].installFlags` <sequence> (optional) Extra flags passed to the `k0s install` command on the target host. See `k0s install --help` for a list of options. diff --git a/config/cluster/host.go b/config/cluster/host.go index 1578b074..d7d9a162 100644 --- a/config/cluster/host.go +++ b/config/cluster/host.go @@ -30,6 +30,7 @@ type Host struct { InstallFlags Flags `yaml:"installFlags,omitempty"` Files []UploadFile `yaml:"files,omitempty"` OSIDOverride string `yaml:"os,omitempty"` + HostnameOverride string `yaml:"hostname,omitempty"` Hooks Hooks `yaml:"hooks,omitempty"` UploadBinaryPath string `yaml:"-"` @@ -204,6 +205,9 @@ func (h *Host) K0sInstallCommand() string { extra = Flags{unQE(old)} } extra.AddUnlessExist(fmt.Sprintf("--node-ip=%s", h.PrivateAddress)) + if h.HostnameOverride != "" { + extra.AddOrReplace(fmt.Sprintf("--hostname-override=%s", h.HostnameOverride)) + } flags.AddOrReplace(fmt.Sprintf("--kubelet-extra-args=%s", strconv.Quote(extra.Join()))) } diff --git a/phase/gather_facts.go b/phase/gather_facts.go index 3715ee17..1654bed5 100644 --- a/phase/gather_facts.go +++ b/phase/gather_facts.go @@ -1,6 +1,9 @@ package phase import ( + "fmt" + "strconv" + "github.com/k0sproject/k0sctl/config/cluster" log "github.com/sirupsen/logrus" ) @@ -32,7 +35,34 @@ func (p *GatherFacts) investigateHost(h *cluster.Host) error { h.Metadata.Arch = output p.IncProp(h.Metadata.Arch) - h.Metadata.Hostname = h.Configurer.Hostname(h) + extra := h.InstallFlags.GetValue("--kubelet-extra-args") + if extra != "" { + unq, err := strconv.Unquote(extra) + if err != nil { + return err + } + ef := cluster.Flags{unq} + if over := ef.GetValue("--hostname-override"); over != "" { + unq, err = strconv.Unquote(over) + if err != nil { + return err + } + if over != "" { + if h.HostnameOverride != unq { + return fmt.Errorf("hostname and installFlags kubelet-extra-args hostname-override mismatch, only define either one") + } + h.HostnameOverride = unq + } + } + } + + if h.HostnameOverride != "" { + log.Infof("%s: using %s from configuration as hostname", h, h.HostnameOverride) + h.Metadata.Hostname = h.HostnameOverride + } else { + h.Metadata.Hostname = h.Configurer.Hostname(h) + log.Infof("%s: using %s as hostname", h, h.Metadata.Hostname) + } if h.PrivateAddress == "" { if h.PrivateInterface == "" {