Skip to content

Commit ce225df

Browse files
committed
🐛 Enable removal of ExternallyProvisioned attribute from BareMetalHosts
Signed-off-by: Ethan J. Gallant <[email protected]>
1 parent 3d9a0cd commit ce225df

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

internal/controller/metal3.io/host_state_machine.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ func (hsm *hostStateMachine) handleRegistering(_ *reconcileInfo) actionResult {
408408
// if the credentials change and the Host must be re-registered.
409409
if hsm.Host.Spec.ExternallyProvisioned {
410410
hsm.NextState = metal3api.StateExternallyProvisioned
411+
} else if hsm.Host.Status.Provisioning.State == metal3api.StateExternallyProvisioned {
412+
// Removing externallyManaged moves hosts to provisioned state
413+
hsm.NextState = metal3api.StateProvisioned
411414
} else if inspectionDisabled(hsm.Host) {
412415
hsm.NextState = metal3api.StatePreparing
413416
} else {

internal/webhooks/metal3.io/v1alpha1/baremetalhost_validation.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ func (webhook *BareMetalHost) validateChanges(oldObj *metal3api.BareMetalHost, n
109109
errs = append(errs, errors.New("bootMACAddress can not be changed once it is set"))
110110
}
111111

112-
if oldObj.Spec.ExternallyProvisioned != newObj.Spec.ExternallyProvisioned {
113-
errs = append(errs, errors.New("externallyProvisioned can not be changed"))
114-
}
115-
116112
return errs
117113
}
118114

internal/webhooks/metal3.io/v1alpha1/baremetalhost_validation_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -970,14 +970,6 @@ func TestValidateUpdate(t *testing.T) {
970970
TypeMeta: tm, ObjectMeta: om, Spec: metal3api.BareMetalHostSpec{BootMACAddress: "test-mac"}},
971971
wantedErr: "bootMACAddress can not be changed once it is set",
972972
},
973-
{
974-
name: "updateExternallyProvisioned",
975-
newBMH: &metal3api.BareMetalHost{
976-
TypeMeta: tm, ObjectMeta: om, Spec: metal3api.BareMetalHostSpec{}},
977-
oldBMH: &metal3api.BareMetalHost{
978-
TypeMeta: tm, ObjectMeta: om, Spec: metal3api.BareMetalHostSpec{ExternallyProvisioned: true}},
979-
wantedErr: "externallyProvisioned can not be changed",
980-
},
981973
}
982974

983975
for _, tt := range tests {

test/e2e/externally_provisioned_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"k8s.io/apimachinery/pkg/types"
1919
"sigs.k8s.io/cluster-api/test/framework"
2020
"sigs.k8s.io/cluster-api/util"
21+
kclient "sigs.k8s.io/controller-runtime/pkg/client"
2122
)
2223

2324
var _ = Describe("Create as externally provisioned, deprovision", Label("required", "provision", "deprovision"),
@@ -79,14 +80,28 @@ var _ = Describe("Create as externally provisioned, deprovision", Label("require
7980
}, &bmh)
8081
Expect(err).NotTo(HaveOccurred())
8182

82-
By("checking that the BMH was not inspected or deployed")
83+
By("Checking that the BMH was not inspected or deployed")
8384
Expect(bmh.Status.OperationHistory.Inspect.Start.IsZero()).To(BeTrue())
8485
Expect(bmh.Status.OperationHistory.Provision.Start.IsZero()).To(BeTrue())
8586

87+
By("Removing the ExternallyProvisioned field")
88+
bmh.Spec.ExternallyProvisioned = false
89+
90+
err = clusterProxy.GetClient().Update(ctx, &bmh)
91+
Expect(err).NotTo(HaveOccurred())
92+
93+
Eventually(func(g Gomega) {
94+
err = clusterProxy.GetClient().Get(ctx, kclient.ObjectKeyFromObject(&bmh), &bmh)
95+
g.Expect(bmh.Status.Provisioning.State).To(Equal(metal3api.StateProvisioned))
96+
}, 2*time.Second, 100*time.Millisecond).Should(Succeed())
97+
8698
By("Deleting the BMH")
87-
// Wait for 2 seconds to allow time to confirm annotation is set
88-
// TODO: fix this so we do not need the sleep
89-
time.Sleep(2 * time.Second)
99+
// Wait for the finalizer to be there, otherwise cleanup logic can't hold the resource
100+
Eventually(func(g Gomega) {
101+
err = clusterProxy.GetClient().Get(ctx, kclient.ObjectKeyFromObject(&bmh), &bmh)
102+
g.Expect(err).NotTo(HaveOccurred())
103+
g.Expect(bmh.Finalizers).To(ContainElement(metal3api.BareMetalHostFinalizer))
104+
}, 2*time.Second, 100*time.Millisecond).Should(Succeed())
90105

91106
err = clusterProxy.GetClient().Delete(ctx, &bmh)
92107
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)