Skip to content

Commit

Permalink
Use the same credentials validation process for deletion
Browse files Browse the repository at this point in the history
Instead of duplicating the code for getting credentials and creating a
provisioner to handle the Deleting action, just use the regular code path
with a special case only to avoid returning due to invalid credentials.
  • Loading branch information
zaneb committed Oct 10, 2019
1 parent 468a071 commit c625c52
Showing 1 changed file with 12 additions and 41 deletions.
53 changes: 12 additions & 41 deletions pkg/controller/baremetalhost/baremetalhost_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,18 @@ func (r *ReconcileBareMetalHost) Reconcile(request reconcile.Request) (result re
return reconcile.Result{Requeue: true}, nil
}

var bmcCreds *bmc.Credentials
var bmcCredsSecret *corev1.Secret
if host.DeletionTimestamp.IsZero() {
// Retrieve the BMC details from the host spec and validate host
// BMC details and build the credentials for talking to the
// management controller.
bmcCreds, bmcCredsSecret, err = r.buildAndValidateBMCCredentials(request, host)
if err != nil {
// Retrieve the BMC details from the host spec and validate host
// BMC details and build the credentials for talking to the
// management controller.
bmcCreds, bmcCredsSecret, err := r.buildAndValidateBMCCredentials(request, host)
if err != nil || bmcCreds == nil {
if !host.DeletionTimestamp.IsZero() {
// If we are in the process of deletion, try with empty credentials
bmcCreds = &bmc.Credentials{}
bmcCredsSecret = &corev1.Secret{}
} else {
return r.credentialsErrorResult(err, request, host)
}
} else {
// If we are in the process of deletion, these creds will be ignored.
// The deleteHost() method will build its own creds.
bmcCreds = &bmc.Credentials{}
bmcCredsSecret = &corev1.Secret{}
}

initialState := host.Status.Provisioning.State
Expand Down Expand Up @@ -323,15 +320,8 @@ func (r *ReconcileBareMetalHost) credentialsErrorResult(err error, request recon

// Manage deletion of the host
func (r *ReconcileBareMetalHost) actionDeleting(prov provisioner.Provisioner, info *reconcileInfo) (result reconcile.Result, err error) {
return r.deleteHost(info.request, info.host)
}

// Handle all delete cases
func (r *ReconcileBareMetalHost) deleteHost(request reconcile.Request, host *metal3v1alpha1.BareMetalHost) (result reconcile.Result, err error) {

reqLogger := log.WithValues("Request.Namespace",
request.Namespace, "Request.Name", request.Name)

host := info.host
reqLogger := info.log
reqLogger.Info(
"marked to be deleted",
"timestamp", host.DeletionTimestamp,
Expand All @@ -345,25 +335,6 @@ func (r *ReconcileBareMetalHost) deleteHost(request reconcile.Request, host *met
return reconcile.Result{}, nil
}

// Retrieve the BMC secret from Kubernetes for this host and
// try and build credentials. If we fail, resort to an empty
// credentials object to give the provisioner
bmcCreds, _, err := r.buildAndValidateBMCCredentials(request, host)
if err != nil || bmcCreds == nil {
bmcCreds = &bmc.Credentials{}
}

eventPublisher := func(reason, message string) {
r.publishEvent(request, host.NewEvent(reason, message))
}

prov, err := r.provisionerFactory(host, *bmcCreds, eventPublisher)
if err != nil {
return result, errors.Wrap(err, "failed to create provisioner")
}

host.Status.Provisioning.State = metal3v1alpha1.StateDeleting

if host.NeedsDeprovisioning() {
reqLogger.Info("deprovisioning before deleting")
provResult, err := prov.Deprovision()
Expand Down

0 comments on commit c625c52

Please sign in to comment.