controllerkuberneteskube-controller-manager

In Kubernetes controller what's the preferred way to get an item from the informer cache?


I am looking at following 2 examples, in the first example A lister is used to retrieve the item.

In the second example , an index is used.

I am wondering which is the preferred way and way to retrieve an element from the local cache.


Solution

  • The examples you showed above, they both use indexer, if you go deeper you will see it.

    For First example (see here)

    // Get retrieves the Node from the index for a given name.
    func (s *nodeLister) Get(name string) (*v1.Node, error) {
        obj, exists, err := s.indexer.GetByKey(name)
        if err != nil {
            return nil, err
        }
        if !exists {
            return nil, errors.NewNotFound(v1.Resource("node"), name)
        }
        return obj.(*v1.Node), nil
    }
    

    For second example

    item, exists, err := c.informer.GetIndexer().GetByKey(keyRaw)