Repository: metal3-io/baremetal-operator
Pull request #1128 (2022-05-31 15:13:24)

Skip the inspecting state when inspection is disabled
Currently, a host moves to the inspecting state and requests
a preprovisioning image even when inspection is disabled.

dtantsur: /test-v1b1-centos-integration
/test-v1b1-ubuntu-integration
elfosardo: /lgtm
metal3-io-bot: New changes are detected. LGTM label has been removed.

Patch:
diff --git a/controllers/metal3.io/host_state_machine.go b/controllers/metal3.io/host_state_machine.go
index 1f1512b8..0b18d661 100644
--- a/controllers/metal3.io/host_state_machine.go
+++ b/controllers/metal3.io/host_state_machine.go
@@ -348,6 +348,8 @@ func (hsm *hostStateMachine) handleRegistering(info *reconcileInfo) actionResult
 	// if the credentials change and the Host must be re-registered.
 	if hsm.Host.Spec.ExternallyProvisioned {
 		hsm.NextState = metal3v1alpha1.StateExternallyProvisioned
+	} else if inspectionDisabled(hsm.Host) {
+		hsm.NextState = metal3v1alpha1.StatePreparing
 	} else {
 		hsm.NextState = metal3v1alpha1.StateInspecting
 	}
@@ -377,10 +379,10 @@ func (hsm *hostStateMachine) handleExternallyProvisioned(info *reconcileInfo) ac
 		return hsm.Reconciler.actionManageSteadyState(hsm.Provisioner, info)
 	}
 
-	switch {
-	case hsm.Host.NeedsHardwareInspection():
+	// TODO(dtantsur): move this logic inside NeedsHardwareInspection?
+	if hsm.Host.NeedsHardwareInspection() && !inspectionDisabled(hsm.Host) {
 		hsm.NextState = metal3v1alpha1.StateInspecting
-	default:
+	} else {
 		hsm.NextState = metal3v1alpha1.StatePreparing
 	}
 	return actionComplete{}
diff --git a/docs/BaremetalHost_ProvisioningState.dot b/docs/BaremetalHost_ProvisioningState.dot
index 2100affc..d91d0661 100644
--- a/docs/BaremetalHost_ProvisioningState.dot
+++ b/docs/BaremetalHost_ProvisioningState.dot
@@ -12,6 +12,7 @@ digraph BaremetalHost {
     ExternallyProvisioned [label="Externally\nProvisioned"]
 
     Registering -> Inspecting [label="!externallyProvisioned && NeedsHardwareInspection()"]
+    Registering -> Preparing [label="!externallyProvisioned && inspectionDisabled()"]
     Registering -> ExternallyProvisioned [label="externallyProvisioned"]
     Registering -> Deleting2 [label="!DeletionTimestamp.IsZero()"]
 


Test patch:
diff --git a/controllers/metal3.io/host_state_machine_test.go b/controllers/metal3.io/host_state_machine_test.go
index f35a9b1e..f7901c8c 100644
--- a/controllers/metal3.io/host_state_machine_test.go
+++ b/controllers/metal3.io/host_state_machine_test.go
@@ -975,6 +975,11 @@ func TestErrorCountClearedOnStateTransition(t *testing.T) {
 			Host:        host(metal3v1alpha1.StateRegistering).build(),
 			TargetState: metal3v1alpha1.StateInspecting,
 		},
+		{
+			Scenario:    "registering-to-preparing",
+			Host:        host(metal3v1alpha1.StateRegistering).DisableInspection().build(),
+			TargetState: metal3v1alpha1.StatePreparing,
+		},
 		{
 			Scenario:    "inspecting-to-preparing",
 			Host:        host(metal3v1alpha1.StateInspecting).build(),
@@ -1175,6 +1180,14 @@ func (hb *hostBuilder) SetOperationalStatus(status metal3v1alpha1.OperationalSta
 	return hb
 }
 
+func (hb *hostBuilder) DisableInspection() *hostBuilder {
+	if hb.Annotations == nil {
+		hb.Annotations = make(map[string]string, 1)
+	}
+	hb.Annotations[inspectAnnotationPrefix] = "disabled"
+	return hb
+}
+
 func (hb *hostBuilder) setDeletion() *hostBuilder {
 	date := metav1.Date(2021, time.January, 18, 10, 18, 0, 0, time.UTC)
 	hb.DeletionTimestamp = &date