From f6074e98d22cd95684db9a480604cf881aeb3915 Mon Sep 17 00:00:00 2001 From: Josh Gwosdz Date: Wed, 11 Aug 2021 09:26:26 +0200 Subject: [PATCH] Use spec.api.port to check controller state (#186) * fix custom apiserver port by passing port values Signed-off-by: erdii * Remove port from wait message 1 * Remove port from wait message 2 * check typecasts of k0sConfig.spec.api.port for proper handling of nil values Signed-off-by: erdii Co-authored-by: Kimmo Lehto --- config/cluster/host.go | 4 ++-- phase/get_kubeconfig.go | 11 ++++++++--- phase/initialize_k0s.go | 6 +++++- phase/install_controllers.go | 7 ++++++- phase/upgrade_controllers.go | 6 +++++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/config/cluster/host.go b/config/cluster/host.go index 94b5faa0..38d7109f 100644 --- a/config/cluster/host.go +++ b/config/cluster/host.go @@ -404,8 +404,8 @@ func (h *Host) NeedIPTables() bool { } // WaitKubeAPIReady blocks until the local kube api responds to /version -func (h *Host) WaitKubeAPIReady() error { +func (h *Host) WaitKubeAPIReady(port int) error { // If the anon-auth is disabled on kube api the version endpoint will give 401 // thus we need to accept both 200 and 401 as valid statuses when checking kube api - return h.WaitHTTPStatus("https://localhost:6443/version", 200, 401) + return h.WaitHTTPStatus(fmt.Sprintf("https://localhost:%d/version", port), 200, 401) } diff --git a/phase/get_kubeconfig.go b/phase/get_kubeconfig.go index 42686697..5b43641d 100644 --- a/phase/get_kubeconfig.go +++ b/phase/get_kubeconfig.go @@ -30,7 +30,12 @@ func (p *GetKubeconfig) Run() error { a = h.SSH.Address } - cfgString, err := kubeConfig(output, p.Config.Metadata.Name, a) + port := p.Config.Spec.K0s.Config.Dig("spec", "api", "port").(int) + if port == 0 { + port = 6443 + } + + cfgString, err := kubeConfig(output, p.Config.Metadata.Name, a, port) if err != nil { return err } @@ -40,7 +45,7 @@ func (p *GetKubeconfig) Run() error { // kubeConfig reads in the raw kubeconfig and changes the given address // and cluster name into it -func kubeConfig(raw string, name string, address string) (string, error) { +func kubeConfig(raw string, name string, address string, port int) (string, error) { cfg, err := clientcmd.Load([]byte(raw)) if err != nil { return "", err @@ -48,7 +53,7 @@ func kubeConfig(raw string, name string, address string) (string, error) { cfg.Clusters[name] = cfg.Clusters["local"] delete(cfg.Clusters, "local") - cfg.Clusters[name].Server = fmt.Sprintf("https://%s:6443", address) + cfg.Clusters[name].Server = fmt.Sprintf("https://%s:%d", address, port) cfg.Contexts[name] = cfg.Contexts["Default"] delete(cfg.Contexts, "Default") diff --git a/phase/initialize_k0s.go b/phase/initialize_k0s.go index 25b669fc..fe2f0b03 100644 --- a/phase/initialize_k0s.go +++ b/phase/initialize_k0s.go @@ -51,8 +51,12 @@ func (p *InitializeK0s) Run() error { return err } + port := 6443 + if p, ok := p.Config.Spec.K0s.Config.Dig("spec", "api", "port").(int); ok { + port = p + } log.Infof("%s: waiting for kubernetes api to respond", h) - if err := h.WaitKubeAPIReady(); err != nil { + if err := h.WaitKubeAPIReady(port); err != nil { return err } diff --git a/phase/install_controllers.go b/phase/install_controllers.go index 1e2e948a..e21fe9d8 100644 --- a/phase/install_controllers.go +++ b/phase/install_controllers.go @@ -79,6 +79,11 @@ func (p *InstallControllers) Run() error { } func (p *InstallControllers) waitJoined(h *cluster.Host) error { + port := 6443 + if p, ok := p.Config.Spec.K0s.Config.Dig("spec", "api", "port").(int); ok { + port = p + } + log.Infof("%s: waiting for kubernetes api to respond", h) - return h.WaitKubeAPIReady() + return h.WaitKubeAPIReady(port) } diff --git a/phase/upgrade_controllers.go b/phase/upgrade_controllers.go index 56530e74..fd86b6fa 100644 --- a/phase/upgrade_controllers.go +++ b/phase/upgrade_controllers.go @@ -63,7 +63,11 @@ func (p *UpgradeControllers) Run() error { if err := h.WaitK0sServiceRunning(); err != nil { return err } - if err := h.WaitKubeAPIReady(); err != nil { + port := 6443 + if p, ok := p.Config.Spec.K0s.Config.Dig("spec", "api", "port").(int); ok { + port = p + } + if err := h.WaitKubeAPIReady(port); err != nil { return err }