cloud/shepherd: rename shepherd.State to shepherd.Availability
Change-Id: I8e1fa243317bf4f97af98303b10b979b98943156
Reviewed-on: https://review.monogon.dev/c/monogon/+/2779
Reviewed-by: Serge Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/cloud/shepherd/manager/provider_test.go b/cloud/shepherd/manager/provider_test.go
index b08c8b7..b18dc45 100644
--- a/cloud/shepherd/manager/provider_test.go
+++ b/cloud/shepherd/manager/provider_test.go
@@ -17,7 +17,7 @@
type dummyMachine struct {
id shepherd.ProviderID
addr netip.Addr
- state shepherd.State
+ availability shepherd.Availability
agentStarted bool
}
@@ -33,8 +33,8 @@
return dm.addr
}
-func (dm *dummyMachine) State() shepherd.State {
- return dm.state
+func (dm *dummyMachine) Availability() shepherd.Availability {
+ return dm.availability
}
type dummySSHClient struct {
@@ -106,9 +106,9 @@
uid := uuid.Must(uuid.NewRandom())
m, err := dp.CreateMachine(ctx, session, shepherd.CreateMachineRequest{
UnusedMachine: &dummyMachine{
- id: shepherd.ProviderID(uid.String()),
- state: shepherd.StateKnownUsed,
- addr: netip.AddrFrom16(uid),
+ id: shepherd.ProviderID(uid.String()),
+ availability: shepherd.AvailabilityKnownUsed,
+ addr: netip.AddrFrom16(uid),
},
})
if err != nil {
@@ -130,9 +130,9 @@
for i := 0; i < unusedMachineCount; i++ {
uid := uuid.Must(uuid.NewRandom())
machines = append(machines, &dummyMachine{
- id: shepherd.ProviderID(uid.String()),
- state: shepherd.StateKnownUnused,
- addr: netip.AddrFrom16(uid),
+ id: shepherd.ProviderID(uid.String()),
+ availability: shepherd.AvailabilityKnownUnused,
+ addr: netip.AddrFrom16(uid),
})
}
@@ -175,7 +175,7 @@
return nil, err
}
- dm.state = shepherd.StateKnownUsed
+ dm.availability = shepherd.AvailabilityKnownUsed
dp.machines[dm.id] = dm
return dm, nil
diff --git a/cloud/shepherd/manager/provisioner.go b/cloud/shepherd/manager/provisioner.go
index 5f19c6d..63f74f4 100644
--- a/cloud/shepherd/manager/provisioner.go
+++ b/cloud/shepherd/manager/provisioner.go
@@ -225,8 +225,8 @@
return addr
}
-func (p providedMachine) State() shepherd.State {
- return shepherd.StateKnownUsed
+func (p providedMachine) Availability() shepherd.Availability {
+ return shepherd.AvailabilityKnownUsed
}
// listInBMDB returns all the machines that the BMDB thinks we should be managing.
@@ -258,27 +258,27 @@
return res, nil
}
-// resolvePossiblyUsed checks if the state is set to possibly used and finds out
-// which state is the correct one.
-func (p *Provisioner) resolvePossiblyUsed(machine shepherd.Machine, providedMachines map[shepherd.ProviderID]shepherd.Machine) shepherd.State {
- state, id := machine.State(), machine.ID()
+// resolvePossiblyUsed checks if the availability is set to possibly used and
+// resolves it to the correct one.
+func (p *Provisioner) resolvePossiblyUsed(machine shepherd.Machine, providedMachines map[shepherd.ProviderID]shepherd.Machine) shepherd.Availability {
+ state, id := machine.Availability(), machine.ID()
- // Bail out if this isn't a possibly used state.
- if state != shepherd.StatePossiblyUsed {
+ // Bail out if this isn't possibly used.
+ if state != shepherd.AvailabilityPossiblyUsed {
return state
}
// If a machine does not have a valid id, its always seen as unused.
if !id.IsValid() {
- return shepherd.StateKnownUnused
+ return shepherd.AvailabilityKnownUnused
}
// If the machine is not inside the bmdb, it's seen as unused.
if _, ok := providedMachines[id]; !ok {
- return shepherd.StateKnownUnused
+ return shepherd.AvailabilityKnownUnused
}
- return shepherd.StateKnownUsed
+ return shepherd.AvailabilityKnownUsed
}
// reconcile takes a list of machines that the provider thinks we should be
@@ -291,7 +291,7 @@
bmdb := make(map[shepherd.ProviderID]shepherd.Machine)
for _, machine := range bmdbMachines {
- // Dont check the state here as its hardcoded to be known used.
+ // Dont check the availability here as its hardcoded to be known used.
bmdb[machine.ID()] = machine
}
@@ -301,14 +301,14 @@
state := p.resolvePossiblyUsed(machine, bmdb)
switch state {
- case shepherd.StateKnownUnused:
+ case shepherd.AvailabilityKnownUnused:
availableMachines = append(availableMachines, machine)
- case shepherd.StateKnownUsed:
+ case shepherd.AvailabilityKnownUsed:
provider[machine.ID()] = machine
default:
- return fmt.Errorf("machine has invalid state (ID: %s, Addr: %s): %s", machine.ID(), machine.Addr(), state)
+ return fmt.Errorf("machine has invalid availability (ID: %s, Addr: %s): %s", machine.ID(), machine.Addr(), state)
}
}
@@ -411,7 +411,7 @@
UnusedMachine: machine,
})
if err != nil {
- klog.Errorf("while creating new device (ID: %s, Addr: %s, State: %s): %w", machine.ID(), machine.Addr(), machine.State(), err)
+ klog.Errorf("while creating new device (ID: %s, Addr: %s, Availability: %s): %w", machine.ID(), machine.Addr(), machine.Availability(), err)
continue
}
klog.Infof("Created new machine with ID: %s", nd.ID())
diff --git a/cloud/shepherd/manager/provisioner_test.go b/cloud/shepherd/manager/provisioner_test.go
index a145bdc..079bf96 100644
--- a/cloud/shepherd/manager/provisioner_test.go
+++ b/cloud/shepherd/manager/provisioner_test.go
@@ -83,7 +83,7 @@
}
}
-// TestProvisioner_resolvePossiblyUsed makes sure the PossiblyUsed state is
+// TestProvisioner_resolvePossiblyUsed makes sure the PossiblyUsed availability is
// resolved correctly.
func TestProvisioner_resolvePossiblyUsed(t *testing.T) {
const providedMachineID = "provided-machine"
@@ -93,45 +93,45 @@
}
tests := []struct {
- name string
- machineID shepherd.ProviderID
- machineState shepherd.State
- wantedState shepherd.State
+ name string
+ machineID shepherd.ProviderID
+ machineAvailability shepherd.Availability
+ wantedAvailability shepherd.Availability
}{
{
- name: "skip KnownUsed",
- machineState: shepherd.StateKnownUsed,
- wantedState: shepherd.StateKnownUsed,
+ name: "skip KnownUsed",
+ machineAvailability: shepherd.AvailabilityKnownUsed,
+ wantedAvailability: shepherd.AvailabilityKnownUsed,
},
{
- name: "skip KnownUnused",
- machineState: shepherd.StateKnownUnused,
- wantedState: shepherd.StateKnownUnused,
+ name: "skip KnownUnused",
+ machineAvailability: shepherd.AvailabilityKnownUnused,
+ wantedAvailability: shepherd.AvailabilityKnownUnused,
},
{
- name: "invalid ID",
- machineID: shepherd.InvalidProviderID,
- machineState: shepherd.StatePossiblyUsed,
- wantedState: shepherd.StateKnownUnused,
+ name: "invalid ID",
+ machineID: shepherd.InvalidProviderID,
+ machineAvailability: shepherd.AvailabilityPossiblyUsed,
+ wantedAvailability: shepherd.AvailabilityKnownUnused,
},
{
- name: "valid ID, not in providedMachines",
- machineID: "unused-machine",
- machineState: shepherd.StatePossiblyUsed,
- wantedState: shepherd.StateKnownUnused,
+ name: "valid ID, not in providedMachines",
+ machineID: "unused-machine",
+ machineAvailability: shepherd.AvailabilityPossiblyUsed,
+ wantedAvailability: shepherd.AvailabilityKnownUnused,
},
{
- name: "valid ID, in providedMachines",
- machineID: providedMachineID,
- machineState: shepherd.StatePossiblyUsed,
- wantedState: shepherd.StateKnownUsed,
+ name: "valid ID, in providedMachines",
+ machineID: providedMachineID,
+ machineAvailability: shepherd.AvailabilityPossiblyUsed,
+ wantedAvailability: shepherd.AvailabilityKnownUsed,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Provisioner{}
- if got := p.resolvePossiblyUsed(&dummyMachine{id: tt.machineID, state: tt.machineState}, providedMachines); got != tt.wantedState {
- t.Errorf("resolvePossiblyUsed() = %v, want %v", got, tt.wantedState)
+ if got := p.resolvePossiblyUsed(&dummyMachine{id: tt.machineID, availability: tt.machineAvailability}, providedMachines); got != tt.wantedAvailability {
+ t.Errorf("resolvePossiblyUsed() = %v, want %v", got, tt.wantedAvailability)
}
})
}
diff --git a/cloud/shepherd/mini/provider.go b/cloud/shepherd/mini/provider.go
index 42b6348..eb6fc08 100644
--- a/cloud/shepherd/mini/provider.go
+++ b/cloud/shepherd/mini/provider.go
@@ -38,8 +38,8 @@
return d.Address
}
-func (d machine) State() shepherd.State {
- return shepherd.StatePossiblyUsed
+func (d machine) Availability() shepherd.Availability {
+ return shepherd.AvailabilityPossiblyUsed
}
func (p *provider) ListMachines(ctx context.Context) ([]shepherd.Machine, error) {
diff --git a/cloud/shepherd/provider/equinix/provider.go b/cloud/shepherd/provider/equinix/provider.go
index 7d3c0d2..22cf8fa 100644
--- a/cloud/shepherd/provider/equinix/provider.go
+++ b/cloud/shepherd/provider/equinix/provider.go
@@ -139,8 +139,8 @@
return netip.Addr{}
}
-func (e reservation) State() shepherd.State {
- return shepherd.StateKnownUnused
+func (e reservation) Availability() shepherd.Availability {
+ return shepherd.AvailabilityKnownUnused
}
type machine struct {
@@ -148,7 +148,7 @@
}
func (e *machine) Failed() bool {
- return e.Device.State == "failed"
+ return e.State == "failed"
}
func (e *machine) ID() shepherd.ProviderID {
@@ -177,8 +177,8 @@
return a
}
-func (e *machine) State() shepherd.State {
- return shepherd.StateKnownUsed
+func (e *machine) Availability() shepherd.Availability {
+ return shepherd.AvailabilityKnownUsed
}
// listReservations doesn't lock the mutex and expects the caller to lock.
diff --git a/cloud/shepherd/shepherd.go b/cloud/shepherd/shepherd.go
index db5e75d..2c2a655 100644
--- a/cloud/shepherd/shepherd.go
+++ b/cloud/shepherd/shepherd.go
@@ -26,41 +26,41 @@
return p != InvalidProviderID
}
-// State defines in which state the machine is.
+// Availability defines the availability state according to the provider.
// See the different states for more information.
-type State int
+type Availability int
const (
- // StateUndefined is used as a placeholder to prevent that the default
+ // AvailabilityUndefined is used as a placeholder to prevent that the default
// value can create any type of bad behaviour.
- StateUndefined State = iota
- // StatePossiblyUsed defines the state where a machine is possibly used,
+ AvailabilityUndefined Availability = iota
+ // AvailabilityPossiblyUsed defines the state where a machine is possibly used,
// this is a state for use in stateless providers where the shepherd has
// to check against the bmdb if Machine.ID is already provisioned or not.
// These machines must have a valid ID and Addr.
- StatePossiblyUsed
- // StateKnownUnused defines the state where a machine is know to be free,
+ AvailabilityPossiblyUsed
+ // AvailabilityKnownUnused defines the state where a machine is know to be free,
// e.g. a hardware reservation at equinix. These machines may not have an
// ID or Addr.
- StateKnownUnused
- // StateKnownUsed defines the state where a machine is known to be used,
+ AvailabilityKnownUnused
+ // AvailabilityKnownUsed defines the state where a machine is known to be used,
// e.g. a deployed machine that is in use. These machines must have a
// valid ID and Addr.
- StateKnownUsed
+ AvailabilityKnownUsed
)
-func (s State) String() string {
- switch s {
- case StateUndefined:
+func (a Availability) String() string {
+ switch a {
+ case AvailabilityUndefined:
return "Undefined"
- case StateKnownUnused:
+ case AvailabilityKnownUnused:
return "KnownUnused"
- case StateKnownUsed:
+ case AvailabilityKnownUsed:
return "KnownUsed"
- case StatePossiblyUsed:
+ case AvailabilityPossiblyUsed:
return "PossiblyUsed"
default:
- return fmt.Sprintf("<invalid value %d>", s)
+ return fmt.Sprintf("<invalid value %d>", a)
}
}
@@ -71,8 +71,8 @@
// shepherd. It is used to connect to the machine via SSH to execute
// all takeover tasks, etc.
Addr() netip.Addr
- // State returns the state in which the machine is
- State() State
+ // Availability returns the availability of the machine.
+ Availability() Availability
// Failed should return true if the machine is in a failed state and
// should be ignored if there are inconsistencies between the provider
// and BMDB.