Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get deleted object from work queue #297

Closed
tamalsaha opened this issue Sep 19, 2017 · 3 comments
Closed

Get deleted object from work queue #297

tamalsaha opened this issue Sep 19, 2017 · 3 comments

Comments

@tamalsaha
Copy link
Member

We are in the process of using workqueue for our various controllers. One of the issue I see is that dequeue from workqueue does not return the object (it is nil). I understand the idea that the desired state by user is nil, after an object is deleted. But there are cases where we need access to the metadata/spec on the object. One work around I have found is to register my controller as a finalizer so I can get the object in the UpdateFunc(). One issue with finalizers is that someone can remove them by mistake. Can WorkQueue return deleted objects?

// syncToStdout is the business logic of the controller. In this controller it simply prints
// information about the pod to stdout. In case an error happened, it has to simply return the error.
// The retry logic should not be part of the business logic.
func (c *Controller) syncToStdout(key string) error {
obj, exists, err := c.indexer.GetByKey(key)
if err != nil {
glog.Errorf("Fetching object with key %s from store failed with %v", key, err)
return err
}
if !exists {
// Below we will warm up our cache with a Pod, so that we will see a delete for one pod
fmt.Printf("Pod %s does not exist anymore\n", key)
} else {
// Note that you also have to check the uid if you have a local controlled resource, which
// is dependent on the actual instance, to detect that a Pod was recreated with the same name
fmt.Printf("Sync/Add/Update for Pod %s\n", obj.(*v1.Pod).GetName())
}
return nil
}

@DogLi
Copy link

DogLi commented Nov 24, 2017

I'm also waiting for the answer!

@liggitt
Copy link
Member

liggitt commented Dec 12, 2017

The queue just stores the key (typically namespace/name) of the object that changed in a way that needs to be processed. The workqueue should take the absence of the object in the store/indexer to mean the object doesn't exist and should be removed.

If you need the object, hook in at the DeleteFunc level to temporarily store the deleted object for use but be aware it is not always available (if your watch drops and gets reestablished right at deletion time you might not have the final version of the object available, or if your controller is down while an object is created and then deleted, you won't ever know it existed)

@liggitt liggitt closed this as completed Dec 12, 2017
@tamalsaha
Copy link
Member Author

Thank @liggitt @munnerz . I have used a separate indexer to keep track of deleted items. Here is what my modification looks like https://github.com/tamalsaha/workqueue-demo/pull/1/files . If you have any comments, please let me know in the pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants