AmitKumarDas / metac

It is metacontroller and more
Apache License 2.0
57 stars 16 forks source link

Custom en-queue logic #13

Open AmitKumarDas opened 5 years ago

AmitKumarDas commented 5 years ago

As a developer, I would like Metac to support custom logic before en-queuing a watch resource. For example, I want below en-queue condition to be supported by Metac

import (
  "k8s.io/apimachinery/pkg/api/equality"
)

// shouldEnqueueVAChange checks if a changed VolumeAttachment should be enqueued.
//
// It filters out changes in Status.Attach/DetachError - these were posted by the
// controller just few moments ago. If they were enqueued, Attach()/Detach() would
// be called again, breaking exponential backoff.
func shouldEnqueueVAChange(old, new *storage.VolumeAttachment) bool {
    if old.ResourceVersion == new.ResourceVersion {
        // This is most probably periodic sync, enqueue it
        return true
    }

    if new.Status.AttachError == nil &&
        new.Status.DetachError == nil &&
        old.Status.AttachError == nil &&
        old.Status.DetachError == nil {
        // The difference between old and new must be elsewhere than
        // Status.Attach/DetachError
        return true
    }

    sanitized := new.DeepCopy()
    sanitized.ResourceVersion = old.ResourceVersion
    sanitized.Status.AttachError = old.Status.AttachError
    sanitized.Status.DetachError = old.Status.DetachError

    if equality.Semantic.DeepEqual(old, sanitized) {
        // The objects are the same except Status.Attach/DetachError.
        // Don't enqueue them now. Let them be enqueued due to resync
        // i.e. after sync interval
        return false
    }
    return true
}
AmitKumarDas commented 5 years ago

Metac is coming up with advanced selectors both for watch as well as for attachments. Though selector is purely for the purposes of filtering, IMO it can go a long way to solve the problem statement mentioned in this issue.

In addition, it will be advisable to keep metac away from introducing reconcile specific logic or fragments of these logic into meta controller specifications.

I shall close this issue, once selectors are supported by at-least one of the meta controllers (it will be supported by GenericController to begin with)