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

Allow overriding hostnames #235

Merged
merged 2 commits into from
Oct 11, 2021
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down Expand Up @@ -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())))
}

Expand Down
32 changes: 31 additions & 1 deletion phase/gather_facts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package phase

import (
"fmt"
"strconv"

"github.com/k0sproject/k0sctl/config/cluster"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -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 == "" {
Expand Down