Skip to content

Commit 2101848

Browse files
authored
Allow overriding hostnames (#235)
* Override hostname via spec.host.hostname or kubelet-extra-args installFlag Update docs Whoops Whoops 2 * Simplify error
1 parent 93bfa18 commit 2101848

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ When `false`, the k0s binary downloading is performed on the target host itself
239239

240240
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.
241241

242+
###### `spec.hosts[*].hostname` <string> (optional)
243+
244+
Override host's hostname. When not set, the hostname reported by the operating system is used.
245+
242246
###### `spec.hosts[*].installFlags` <sequence> (optional)
243247

244248
Extra flags passed to the `k0s install` command on the target host. See `k0s install --help` for a list of options.

config/cluster/host.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Host struct {
3030
InstallFlags Flags `yaml:"installFlags,omitempty"`
3131
Files []UploadFile `yaml:"files,omitempty"`
3232
OSIDOverride string `yaml:"os,omitempty"`
33+
HostnameOverride string `yaml:"hostname,omitempty"`
3334
Hooks Hooks `yaml:"hooks,omitempty"`
3435

3536
UploadBinaryPath string `yaml:"-"`
@@ -204,6 +205,9 @@ func (h *Host) K0sInstallCommand() string {
204205
extra = Flags{unQE(old)}
205206
}
206207
extra.AddUnlessExist(fmt.Sprintf("--node-ip=%s", h.PrivateAddress))
208+
if h.HostnameOverride != "" {
209+
extra.AddOrReplace(fmt.Sprintf("--hostname-override=%s", h.HostnameOverride))
210+
}
207211
flags.AddOrReplace(fmt.Sprintf("--kubelet-extra-args=%s", strconv.Quote(extra.Join())))
208212
}
209213

phase/gather_facts.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package phase
22

33
import (
4+
"fmt"
5+
"strconv"
6+
47
"github.com/k0sproject/k0sctl/config/cluster"
58
log "github.com/sirupsen/logrus"
69
)
@@ -32,7 +35,34 @@ func (p *GatherFacts) investigateHost(h *cluster.Host) error {
3235
h.Metadata.Arch = output
3336
p.IncProp(h.Metadata.Arch)
3437

35-
h.Metadata.Hostname = h.Configurer.Hostname(h)
38+
extra := h.InstallFlags.GetValue("--kubelet-extra-args")
39+
if extra != "" {
40+
unq, err := strconv.Unquote(extra)
41+
if err != nil {
42+
return err
43+
}
44+
ef := cluster.Flags{unq}
45+
if over := ef.GetValue("--hostname-override"); over != "" {
46+
unq, err = strconv.Unquote(over)
47+
if err != nil {
48+
return err
49+
}
50+
if over != "" {
51+
if h.HostnameOverride != unq {
52+
return fmt.Errorf("hostname and installFlags kubelet-extra-args hostname-override mismatch, only define either one")
53+
}
54+
h.HostnameOverride = unq
55+
}
56+
}
57+
}
58+
59+
if h.HostnameOverride != "" {
60+
log.Infof("%s: using %s from configuration as hostname", h, h.HostnameOverride)
61+
h.Metadata.Hostname = h.HostnameOverride
62+
} else {
63+
h.Metadata.Hostname = h.Configurer.Hostname(h)
64+
log.Infof("%s: using %s as hostname", h, h.Metadata.Hostname)
65+
}
3666

3767
if h.PrivateAddress == "" {
3868
if h.PrivateInterface == "" {

0 commit comments

Comments
 (0)