-
Notifications
You must be signed in to change notification settings - Fork 73
/
validate_hosts.go
88 lines (72 loc) · 2.23 KB
/
validate_hosts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package phase
import (
"fmt"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
)
// ValidateHosts performs remote OS detection
type ValidateHosts struct {
GenericPhase
hncount map[string]int
machineidcount map[string]int
privateaddrcount map[string]int
}
// Title for the phase
func (p *ValidateHosts) Title() string {
return "Validate hosts"
}
// Run the phase
func (p *ValidateHosts) Run() error {
p.hncount = make(map[string]int, len(p.Config.Spec.Hosts))
if uniqueMachineIDVersion.Check(p.Config.Spec.K0s.Version) {
p.machineidcount = make(map[string]int, len(p.Config.Spec.Hosts))
}
p.privateaddrcount = make(map[string]int, len(p.Config.Spec.Hosts))
controllerCount := len(p.Config.Spec.Hosts.Controllers())
var resetControllerCount int
for _, h := range p.Config.Spec.Hosts {
p.hncount[h.Metadata.Hostname]++
if p.machineidcount != nil {
p.machineidcount[h.Metadata.MachineID]++
}
if h.PrivateAddress != "" {
p.privateaddrcount[h.PrivateAddress]++
}
if h.IsController() && h.Reset {
resetControllerCount++
}
}
if resetControllerCount >= controllerCount {
return fmt.Errorf("all controllers are marked to be reset - this will break the cluster. use `k0sctl reset` instead if that is intentional")
}
return p.parallelDo(
p.Config.Spec.Hosts,
p.validateUniqueHostname,
p.validateUniqueMachineID,
p.validateUniquePrivateAddress,
p.validateSudo,
)
}
func (p *ValidateHosts) validateUniqueHostname(h *cluster.Host) error {
if p.hncount[h.Metadata.Hostname] > 1 {
return fmt.Errorf("hostname is not unique: %s", h.Metadata.Hostname)
}
return nil
}
func (p *ValidateHosts) validateUniquePrivateAddress(h *cluster.Host) error {
if p.privateaddrcount[h.PrivateAddress] > 1 {
return fmt.Errorf("privateAddress %q is not unique: %s", h.PrivateAddress, h.Metadata.Hostname)
}
return nil
}
func (p *ValidateHosts) validateUniqueMachineID(h *cluster.Host) error {
if p.machineidcount[h.Metadata.MachineID] > 1 {
return fmt.Errorf("machine id %s is not unique: %s", h.Metadata.MachineID, h.Metadata.Hostname)
}
return nil
}
func (p *ValidateHosts) validateSudo(h *cluster.Host) error {
if err := h.Configurer.CheckPrivilege(h); err != nil {
return err
}
return nil
}