github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/index/catalog.go (about)

     1  package indexer
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/operator-framework/api/pkg/operators/v1alpha1"
     7  	"k8s.io/client-go/tools/cache"
     8  )
     9  
    10  const (
    11  	// PresentCatalogIndexFuncKey is the recommended key to use for registering
    12  	// the index func with an indexer.
    13  	PresentCatalogIndexFuncKey string = "presentcatalogindexfunc"
    14  )
    15  
    16  // PresentCatalogIndexFunc returns index from CatalogSource/CatalogSourceNamespace
    17  // of the given object (Subscription)
    18  func PresentCatalogIndexFunc(obj interface{}) ([]string, error) {
    19  	sub, ok := obj.(*v1alpha1.Subscription)
    20  	if !ok {
    21  		return []string{""}, fmt.Errorf("invalid object of type: %T", obj)
    22  	}
    23  
    24  	if sub.Spec.CatalogSource != "" && sub.Spec.CatalogSourceNamespace != "" {
    25  		return []string{sub.Spec.CatalogSource + "/" + sub.Spec.CatalogSourceNamespace}, nil
    26  	}
    27  
    28  	return []string{""}, nil
    29  }
    30  
    31  // CatalogSubscriberNamespaces returns the list of namespace (as a map with namespace as key)
    32  // which has Suscriptions(s) that subscribe(s) to a given CatalogSource (name/namespace)
    33  func CatalogSubscriberNamespaces(indexers map[string]cache.Indexer, name, namespace string) (map[string]struct{}, error) {
    34  	nsSet := map[string]struct{}{}
    35  	index := fmt.Sprintf("%s/%s", name, namespace)
    36  
    37  	for _, indexer := range indexers {
    38  		subs, err := indexer.ByIndex(PresentCatalogIndexFuncKey, index)
    39  		if err != nil {
    40  			return nil, err
    41  		}
    42  		for _, item := range subs {
    43  			s, ok := item.(*v1alpha1.Subscription)
    44  			if !ok {
    45  				continue
    46  			}
    47  			// Add to set
    48  			nsSet[s.GetNamespace()] = struct{}{}
    49  		}
    50  	}
    51  
    52  	return nsSet, nil
    53  }