Skip to content

Commit

Permalink
Use spec.api.port to check controller state (k0sproject#186)
Browse files Browse the repository at this point in the history
* fix custom apiserver port by passing port values

Signed-off-by: erdii <me@erdii.engineering>

* 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 <me@erdii.engineering>

Co-authored-by: Kimmo Lehto <kimmo.lehto@gmail.com>
  • Loading branch information
erdii and kke committed Aug 11, 2021
1 parent feb169a commit f6074e9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions config/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 8 additions & 3 deletions phase/get_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -40,15 +45,15 @@ 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
}

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")
Expand Down
6 changes: 5 additions & 1 deletion phase/initialize_k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion phase/install_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion phase/upgrade_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit f6074e9

Please sign in to comment.