Created
May 8, 2020 04:10
-
-
Save alaypatel07/837d4cf0ad2a6e52033b64551887dd9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (r *ReconcileMigToken) Reconcile(request reconcile.Request) (reconcile.Result, error) { | |
var err error | |
log.Reset() | |
token := &migapi.MigToken{} | |
// prefer a separate function call instead of defer func() {}() | |
// two advantages: | |
// 1. defers are hard(er) to debug/reason about than normal calls | |
// 2. with normal function we can have a matching else condition | |
err = r.syncMigToken(request) | |
if err != nil { | |
token.Status.DeleteCondition(migapi.Ready) | |
token.Status.SetCondition(migapi.Condition{ | |
Type: migapi.ReconcileFailed, | |
Status: True, | |
Category: Critical, | |
Message: "Reconcile failed: []. See controller logs for details.", | |
Items: []string{err.Error()}, | |
}) | |
token.Status.EndStagingConditions() | |
statusUpdateErr := r.Update(context.TODO(), token) | |
if statusUpdateErr != nil { | |
log.Trace(statusUpdateErr) | |
return reconcile.Result{Requeue: true}, nil | |
} | |
return reconcile.Result{Requeue: true}, nil | |
} else { | |
token.Status.SetReady(true, "reconciliation succeeded") | |
token.Status.SetCondition(migapi.Condition{ | |
Type: migapi.ReconcileFailed, | |
Status: False, | |
Category: "Info", | |
Message: fmt.Sprintf("Reconcile succeeded for %s", token.Status.ObservedDigest), | |
}) | |
statusUpdateErr := r.Update(context.TODO(), token) | |
if statusUpdateErr != nil { | |
log.Trace(statusUpdateErr) | |
return reconcile.Result{Requeue: true}, nil | |
} | |
} | |
return reconcile.Result{}, nil | |
} | |
func (r *ReconcileMigToken) syncMigToken(request reconcile.Request) error { | |
// Fetch the MigCluster | |
token := &migapi.MigToken{} | |
err := r.Get(context.TODO(), request.NamespacedName, token) | |
// prefer switching rather than nested ifs | |
switch { | |
case errors.IsNotFound(err): | |
return err | |
case err != nil: | |
log.Trace(err) | |
return err | |
default: | |
// fall through on the default case | |
} | |
// Begin staging conditions. | |
token.Status.BeginStagingConditions() | |
// Validations. | |
err = r.validate(token) | |
if err != nil { | |
log.Trace(err) | |
return err | |
} | |
// Ready | |
token.Status.SetReady( | |
!token.Status.HasBlockerCondition(), | |
ReadyMessage) | |
// End staging conditions. | |
token.Status.EndStagingConditions() | |
// Apply changes. | |
token.MarkReconciled() | |
err = r.Update(context.TODO(), token) | |
if err != nil { | |
log.Trace(err) | |
return err | |
} | |
// Done | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment