Skip to content

Commit

Permalink
Remove 'Synced' status condition from XRD controllers
Browse files Browse the repository at this point in the history
crossplane/crossplane-runtime#198

See the above issue for context.

Signed-off-by: Nic Cope <[email protected]>
  • Loading branch information
negz committed Sep 10, 2020
1 parent 190131e commit 4c898b2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 346 deletions.
11 changes: 0 additions & 11 deletions apis/apiextensions/v1alpha1/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const (
const (
ReasonWatchingComposite runtimev1alpha1.ConditionReason = "WatchingCompositeResource"
ReasonWatchingClaim runtimev1alpha1.ConditionReason = "WatchingCompositeResourceClaim"
ReasonDeleting runtimev1alpha1.ConditionReason = "DefinitionDeleted"
)

// WatchingComposite indicates that Crossplane has defined and is watching for a
Expand All @@ -62,13 +61,3 @@ func WatchingClaim() runtimev1alpha1.Condition {
Reason: ReasonWatchingClaim,
}
}

// Deleting returns a condition that indicates a definition is being deleted.
func Deleting() runtimev1alpha1.Condition {
return runtimev1alpha1.Condition{
Type: TypeEstablished,
Status: corev1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: ReasonDeleting,
}
}
59 changes: 21 additions & 38 deletions pkg/controller/apiextensions/definition/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
"github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
Expand Down Expand Up @@ -249,22 +248,17 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)
crd, err := r.composite.Render(d)
if err != nil {
log.Debug(errRenderCRD, "error", err)
r.record.Event(d, event.Warning(reasonRenderCRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errRenderCRD)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
r.record.Event(d, event.Warning(reasonRenderCRD, errors.Wrap(err, errRenderCRD)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}

r.record.Event(d, event.Normal(reasonRenderCRD, "Rendered composite resource CustomResourceDefinition"))

if meta.WasDeleted(d) {
d.Status.SetConditions(v1alpha1.Deleting())
r.record.Event(d, event.Normal(reasonDeleteXRD, "Removing support for composite resource"))

nn := types.NamespacedName{Name: crd.GetName()}
if err := r.client.Get(ctx, nn, crd); resource.IgnoreNotFound(err) != nil {
r.record.Event(d, event.Warning(reasonDeleteXRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errGetCRD)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
r.record.Event(d, event.Warning(reasonDeleteXRD, errors.Wrap(err, errGetCRD)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}

// The CRD has no creation timestamp, or we don't control it. Most
Expand All @@ -283,9 +277,8 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)

if err := r.composite.RemoveFinalizer(ctx, d); err != nil {
log.Debug(errRemoveFinalizer, "error", err)
r.record.Event(d, event.Warning(reasonDeleteXRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errRemoveFinalizer)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
r.record.Event(d, event.Warning(reasonDeleteXRD, errors.Wrap(err, errRemoveFinalizer)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}

// We're all done deleting and have removed our finalizer. There's
Expand All @@ -302,27 +295,24 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)
o.SetGroupVersionKind(d.GetCompositeGroupVersionKind())
if err := r.client.DeleteAllOf(ctx, o); err != nil && !kmeta.IsNoMatchError(err) && !kerrors.IsNotFound(err) {
log.Debug(errDeleteCRs, "error", err)
r.record.Event(d, event.Warning(reasonDeleteXRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errDeleteCRs)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
r.record.Event(d, event.Warning(reasonDeleteXRD, errors.Wrap(err, errDeleteCRs)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}

l := &kunstructured.UnstructuredList{}
l.SetGroupVersionKind(d.GetCompositeGroupVersionKind())
if err := r.client.List(ctx, l); resource.Ignore(kmeta.IsNoMatchError, err) != nil {
log.Debug(errListCRs, "error", err)
r.record.Event(d, event.Warning(reasonDeleteXRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errListCRs)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
r.record.Event(d, event.Warning(reasonDeleteXRD, errors.Wrap(err, errListCRs)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}

// Controller should be stopped only after all instances are gone so
// that deletion logic of the instances are processed by the controller.
if len(l.Items) > 0 {
log.Debug(waitCRDelete)
r.record.Event(d, event.Normal(reasonDeleteXRD, waitCRDelete))
d.Status.SetConditions(runtimev1alpha1.ReconcileSuccess().WithMessage(waitCRDelete))
return reconcile.Result{RequeueAfter: tinyWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
return reconcile.Result{RequeueAfter: tinyWait}, nil
}

// The controller should be stopped before the deletion of CRD so that
Expand All @@ -333,40 +323,35 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)

if err := r.client.Delete(ctx, crd); resource.IgnoreNotFound(err) != nil {
log.Debug(errDeleteCRD, "error", err)
r.record.Event(d, event.Warning(reasonDeleteXRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errDeleteCRD)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
r.record.Event(d, event.Warning(reasonDeleteXRD, errors.Wrap(err, errDeleteCRD)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}
log.Debug("Deleted composite resource CustomResourceDefinition")
r.record.Event(d, event.Normal(reasonDeleteXRD, "Deleted composite resource CustomResourceDefinition"))

// We should be requeued implicitly because we're watching the
// CustomResourceDefinition that we just deleted, but we requeue after
// a tiny wait just in case the CRD isn't gone after the first requeue.
d.Status.SetConditions(runtimev1alpha1.ReconcileSuccess())
return reconcile.Result{RequeueAfter: tinyWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
return reconcile.Result{RequeueAfter: tinyWait}, nil
}

if err := r.composite.AddFinalizer(ctx, d); err != nil {
log.Debug(errAddFinalizer, "error", err)
r.record.Event(d, event.Warning(reasonApplyXRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errAddFinalizer)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
r.record.Event(d, event.Warning(reasonApplyXRD, errors.Wrap(err, errAddFinalizer)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}

if err := r.client.Apply(ctx, crd, resource.MustBeControllableBy(d.GetUID())); err != nil {
log.Debug(errApplyCRD, "error", err)
r.record.Event(d, event.Warning(reasonApplyXRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errApplyCRD)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
r.record.Event(d, event.Warning(reasonApplyXRD, errors.Wrap(err, errApplyCRD)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}
r.record.Event(d, event.Normal(reasonApplyXRD, "Applied composite resource CustomResourceDefinition"))

if !ccrd.IsEstablished(crd.Status) {
log.Debug(waitCRDEstablish)
r.record.Event(d, event.Normal(reasonApplyXRD, waitCRDEstablish))
d.Status.SetConditions(runtimev1alpha1.ReconcileSuccess().WithMessage(waitCRDEstablish))
return reconcile.Result{RequeueAfter: tinyWait}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
return reconcile.Result{RequeueAfter: tinyWait}, nil
}
recorder := r.record.WithAnnotations("controller", composite.ControllerName(d.GetName()))
o := kcontroller.Options{Reconciler: composite.NewReconciler(r.mgr,
Expand All @@ -386,13 +371,11 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)

if err := r.composite.Start(composite.ControllerName(d.GetName()), o, controller.For(u, &handler.EnqueueRequestForObject{})); err != nil {
log.Debug(errStartController, "error", err)
r.record.Event(d, event.Warning(reasonApplyXRD, err))
d.Status.SetConditions(runtimev1alpha1.ReconcileError(errors.Wrap(err, errStartController)))
return reconcile.Result{RequeueAfter: shortWait}, errors.Wrap(r.client.Status().Update(ctx, d), errStartController)
r.record.Event(d, event.Warning(reasonApplyXRD, errors.Wrap(err, errStartController)))
return reconcile.Result{RequeueAfter: shortWait}, nil
}

d.Status.SetConditions(v1alpha1.WatchingComposite())
d.Status.SetConditions(runtimev1alpha1.ReconcileSuccess())
r.record.Event(d, event.Normal(reasonApplyXRD, "(Re)started composite resource controller"))
return reconcile.Result{Requeue: false}, errors.Wrap(r.client.Status().Update(ctx, d), errUpdateStatus)
}
Loading

0 comments on commit 4c898b2

Please sign in to comment.