Skip to content

Commit 9fa7a71

Browse files
authored
Check connection to kube-api before worker installation (#202)
* Check networking from workers to controller before worker install * Move url to function * Also accept HTTP 401 * Minor finetuning
1 parent 1084aca commit 9fa7a71

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

config/cluster/spec.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package cluster
22

3-
import "github.com/creasty/defaults"
3+
import (
4+
"fmt"
5+
6+
"github.com/creasty/defaults"
7+
)
48

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

4549
return s.k0sLeader
4650
}
51+
52+
// KubeAPIURL returns an url to the cluster's kube api
53+
func (s *Spec) KubeAPIURL() string {
54+
var caddr string
55+
if a := s.K0s.Config.DigString("spec", "api", "externalAddress"); a != "" {
56+
caddr = a
57+
} else {
58+
leader := s.K0sLeader()
59+
if leader.PrivateAddress != "" {
60+
caddr = leader.PrivateAddress
61+
} else {
62+
caddr = leader.Address()
63+
}
64+
}
65+
66+
cport := 6443
67+
if p, ok := s.K0s.Config.Dig("spec", "api", "port").(int); ok {
68+
cport = p
69+
}
70+
71+
return fmt.Sprintf("https://%s:%d", caddr, cport)
72+
}

phase/install_workers.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package phase
22

33
import (
4+
"fmt"
45
"time"
56

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

5152
// Run the phase
5253
func (p *InstallWorkers) Run() error {
54+
url := p.Config.Spec.KubeAPIURL()
55+
healthz := fmt.Sprintf("%s/healthz", url)
56+
57+
err := p.hosts.ParallelEach(func(h *cluster.Host) error {
58+
log.Infof("%s: validating api connection to %s", h, url)
59+
if err := h.CheckHTTPStatus(healthz, 200, 401); err != nil {
60+
return fmt.Errorf("failed to connect from worker to kubernetes api at %s - check networking", url)
61+
}
62+
return nil
63+
})
64+
65+
if err != nil {
66+
return err
67+
}
68+
5369
log.Infof("%s: generating token", p.leader)
5470
token, err := p.Config.Spec.K0s.GenerateToken(
5571
p.leader,

0 commit comments

Comments
 (0)