Skip to content

Commit b0cd9d9

Browse files
authored
Validate MachineID existence & uniqueness (#435)
* Validate MachineID existence & uniqueness Signed-off-by: Kimmo Lehto <[email protected]>
1 parent 53a7d8e commit b0cd9d9

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

configurer/linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,7 @@ func (l Linux) UpsertFile(h os.Host, path, content string) error {
250250
func (l Linux) DeleteDir(h os.Host, path string, opts ...exec.Option) error {
251251
return h.Exec(fmt.Sprintf(`rmdir %s`, shellescape.Quote(path)), opts...)
252252
}
253+
254+
func (l Linux) MachineID(h os.Host) (string, error) {
255+
return h.ExecOutput(`cat /etc/machine-id || cat /var/lib/dbus/machine-id`)
256+
}

phase/gather_facts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ func (p *GatherFacts) investigateHost(h *cluster.Host) error {
3232
return err
3333
}
3434
h.Metadata.Arch = output
35+
36+
id, err := h.Configurer.MachineID(h)
37+
if err != nil {
38+
return err
39+
}
40+
h.Metadata.MachineID = id
41+
3542
p.IncProp(h.Metadata.Arch)
3643

3744
if extra := h.InstallFlags.GetValue("--kubelet-extra-args"); extra != "" {

phase/validate_hosts.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
// ValidateHosts performs remote OS detection
1010
type ValidateHosts struct {
1111
GenericPhase
12-
hncount map[string]int
12+
hncount map[string]int
13+
machineidcount map[string]int
1314
}
1415

1516
// Title for the phase
@@ -20,11 +21,13 @@ func (p *ValidateHosts) Title() string {
2021
// Run the phase
2122
func (p *ValidateHosts) Run() error {
2223
p.hncount = make(map[string]int, len(p.Config.Spec.Hosts))
24+
p.machineidcount = make(map[string]int, len(p.Config.Spec.Hosts))
2325
for _, h := range p.Config.Spec.Hosts {
2426
p.hncount[h.Metadata.Hostname]++
27+
p.machineidcount[h.Metadata.MachineID]++
2528
}
2629

27-
return p.parallelDo(p.Config.Spec.Hosts, p.validateUniqueHostname, p.validateSudo)
30+
return p.parallelDo(p.Config.Spec.Hosts, p.validateUniqueHostname, p.validateUniqueMachineID, p.validateSudo)
2831
}
2932

3033
func (p *ValidateHosts) validateUniqueHostname(h *cluster.Host) error {
@@ -35,6 +38,14 @@ func (p *ValidateHosts) validateUniqueHostname(h *cluster.Host) error {
3538
return nil
3639
}
3740

41+
func (p *ValidateHosts) validateUniqueMachineID(h *cluster.Host) error {
42+
if p.machineidcount[h.Metadata.MachineID] > 1 {
43+
return fmt.Errorf("machine id %s is not unique: %s", h.Metadata.MachineID, h.Metadata.Hostname)
44+
}
45+
46+
return nil
47+
}
48+
3849
func (p *ValidateHosts) validateSudo(h *cluster.Host) error {
3950
if err := h.Configurer.CheckPrivilege(h); err != nil {
4051
return err

pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type configurer interface {
127127
DeleteDir(os.Host, string, ...exec.Option) error
128128
K0sctlLockFilePath(os.Host) string
129129
UpsertFile(os.Host, string, string) error
130+
MachineID(os.Host) (string, error)
130131
}
131132

132133
// HostMetadata resolved metadata for host
@@ -138,6 +139,7 @@ type HostMetadata struct {
138139
Hostname string
139140
Ready bool
140141
NeedsUpgrade bool
142+
MachineID string
141143
}
142144

143145
// UnmarshalYAML sets in some sane defaults when unmarshaling the data from yaml

smoke-test/smoke.common.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export K0S_VERSION
88
function createCluster() {
99
envsubst < "${FOOTLOOSE_TEMPLATE}" > footloose.yaml
1010
footloose create
11+
if [ "${LINUX_IMAGE}" = "quay.io/footloose/debian10" ]; then
12+
for host in $(footloose status -o json|grep hostname|cut -d"\"" -f4); do
13+
footloose ssh root@${host} -- rm -f /etc/machine-id /var/lib/dbus/machine-id
14+
footloose ssh root@${host} -- systemd-machine-id-setup
15+
done
16+
fi
1117
}
1218

1319
function deleteCluster() {
@@ -33,4 +39,4 @@ function downloadKubectl() {
3339
esac
3440
[ -f kubectl ] || (curl -L https://storage.googleapis.com/kubernetes-release/release/v1.21.3/bin/${OS}/${ARCH}/kubectl > ./kubectl && chmod +x ./kubectl)
3541
./kubectl version --client
36-
}
42+
}

0 commit comments

Comments
 (0)