Skip to content

Commit

Permalink
Validate MachineID existence & uniqueness (#435)
Browse files Browse the repository at this point in the history
* Validate MachineID existence & uniqueness

Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Dec 16, 2022
1 parent 53a7d8e commit b0cd9d9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions configurer/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,7 @@ func (l Linux) UpsertFile(h os.Host, path, content string) error {
func (l Linux) DeleteDir(h os.Host, path string, opts ...exec.Option) error {
return h.Exec(fmt.Sprintf(`rmdir %s`, shellescape.Quote(path)), opts...)
}

func (l Linux) MachineID(h os.Host) (string, error) {
return h.ExecOutput(`cat /etc/machine-id || cat /var/lib/dbus/machine-id`)
}
7 changes: 7 additions & 0 deletions phase/gather_facts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func (p *GatherFacts) investigateHost(h *cluster.Host) error {
return err
}
h.Metadata.Arch = output

id, err := h.Configurer.MachineID(h)
if err != nil {
return err
}
h.Metadata.MachineID = id

p.IncProp(h.Metadata.Arch)

if extra := h.InstallFlags.GetValue("--kubelet-extra-args"); extra != "" {
Expand Down
15 changes: 13 additions & 2 deletions phase/validate_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
// ValidateHosts performs remote OS detection
type ValidateHosts struct {
GenericPhase
hncount map[string]int
hncount map[string]int
machineidcount map[string]int
}

// Title for the phase
Expand All @@ -20,11 +21,13 @@ func (p *ValidateHosts) Title() string {
// Run the phase
func (p *ValidateHosts) Run() error {
p.hncount = make(map[string]int, len(p.Config.Spec.Hosts))
p.machineidcount = make(map[string]int, len(p.Config.Spec.Hosts))
for _, h := range p.Config.Spec.Hosts {
p.hncount[h.Metadata.Hostname]++
p.machineidcount[h.Metadata.MachineID]++
}

return p.parallelDo(p.Config.Spec.Hosts, p.validateUniqueHostname, p.validateSudo)
return p.parallelDo(p.Config.Spec.Hosts, p.validateUniqueHostname, p.validateUniqueMachineID, p.validateSudo)
}

func (p *ValidateHosts) validateUniqueHostname(h *cluster.Host) error {
Expand All @@ -35,6 +38,14 @@ func (p *ValidateHosts) validateUniqueHostname(h *cluster.Host) error {
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
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type configurer interface {
DeleteDir(os.Host, string, ...exec.Option) error
K0sctlLockFilePath(os.Host) string
UpsertFile(os.Host, string, string) error
MachineID(os.Host) (string, error)
}

// HostMetadata resolved metadata for host
Expand All @@ -138,6 +139,7 @@ type HostMetadata struct {
Hostname string
Ready bool
NeedsUpgrade bool
MachineID string
}

// UnmarshalYAML sets in some sane defaults when unmarshaling the data from yaml
Expand Down
8 changes: 7 additions & 1 deletion smoke-test/smoke.common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export K0S_VERSION
function createCluster() {
envsubst < "${FOOTLOOSE_TEMPLATE}" > footloose.yaml
footloose create
if [ "${LINUX_IMAGE}" = "quay.io/footloose/debian10" ]; then
for host in $(footloose status -o json|grep hostname|cut -d"\"" -f4); do
footloose ssh root@${host} -- rm -f /etc/machine-id /var/lib/dbus/machine-id
footloose ssh root@${host} -- systemd-machine-id-setup
done
fi
}

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

0 comments on commit b0cd9d9

Please sign in to comment.