Skip to content

Commit

Permalink
Check connection to kube-api before worker installation (#202)
Browse files Browse the repository at this point in the history
* Check networking from workers to controller before worker install

* Move url to function

* Also accept HTTP 401

* Minor finetuning
  • Loading branch information
kke committed Sep 9, 2021
1 parent 1084aca commit 9fa7a71
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
28 changes: 27 additions & 1 deletion config/cluster/spec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cluster

import "github.com/creasty/defaults"
import (
"fmt"

"github.com/creasty/defaults"
)

// Spec defines cluster config spec section
type Spec struct {
Expand Down Expand Up @@ -44,3 +48,25 @@ func (s *Spec) K0sLeader() *Host {

return s.k0sLeader
}

// KubeAPIURL returns an url to the cluster's kube api
func (s *Spec) KubeAPIURL() string {
var caddr string
if a := s.K0s.Config.DigString("spec", "api", "externalAddress"); a != "" {
caddr = a
} else {
leader := s.K0sLeader()
if leader.PrivateAddress != "" {
caddr = leader.PrivateAddress
} else {
caddr = leader.Address()
}
}

cport := 6443
if p, ok := s.K0s.Config.Dig("spec", "api", "port").(int); ok {
cport = p
}

return fmt.Sprintf("https://%s:%d", caddr, cport)
}
16 changes: 16 additions & 0 deletions phase/install_workers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package phase

import (
"fmt"
"time"

"github.com/k0sproject/k0sctl/config"
Expand Down Expand Up @@ -50,6 +51,21 @@ func (p *InstallWorkers) CleanUp() {

// Run the phase
func (p *InstallWorkers) Run() error {
url := p.Config.Spec.KubeAPIURL()
healthz := fmt.Sprintf("%s/healthz", url)

err := p.hosts.ParallelEach(func(h *cluster.Host) error {
log.Infof("%s: validating api connection to %s", h, url)
if err := h.CheckHTTPStatus(healthz, 200, 401); err != nil {
return fmt.Errorf("failed to connect from worker to kubernetes api at %s - check networking", url)
}
return nil
})

if err != nil {
return err
}

log.Infof("%s: generating token", p.leader)
token, err := p.Config.Spec.K0s.GenerateToken(
p.leader,
Expand Down

0 comments on commit 9fa7a71

Please sign in to comment.