|
| 1 | +package operatorlister |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/apimachinery/pkg/labels" |
| 9 | + "k8s.io/apimachinery/pkg/types" |
| 10 | + |
| 11 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1" |
| 12 | + listers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1" |
| 13 | +) |
| 14 | + |
| 15 | +type UnionCatalogSourceLister struct { |
| 16 | + catsrcListers map[string]listers.CatalogSourceLister |
| 17 | + catsrcLock sync.RWMutex |
| 18 | +} |
| 19 | + |
| 20 | +// List lists all CatalogSources in the indexer. |
| 21 | +func (ucl *UnionCatalogSourceLister) List(selector labels.Selector) (ret []*v1alpha1.CatalogSource, err error) { |
| 22 | + ucl.catsrcLock.RLock() |
| 23 | + defer ucl.catsrcLock.RUnlock() |
| 24 | + |
| 25 | + set := make(map[types.UID]*v1alpha1.CatalogSource) |
| 26 | + for _, cl := range ucl.catsrcListers { |
| 27 | + catsrcs, err := cl.List(selector) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + for _, catsrc := range catsrcs { |
| 33 | + set[catsrc.GetUID()] = catsrc |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + for _, catsrc := range set { |
| 38 | + ret = append(ret, catsrc) |
| 39 | + } |
| 40 | + |
| 41 | + return |
| 42 | +} |
| 43 | + |
| 44 | +// CatalogSources returns an object that can list and get CatalogSources. |
| 45 | +func (ucl *UnionCatalogSourceLister) CatalogSources(namespace string) listers.CatalogSourceNamespaceLister { |
| 46 | + ucl.catsrcLock.RLock() |
| 47 | + defer ucl.catsrcLock.RUnlock() |
| 48 | + |
| 49 | + // Check for specific namespace listers |
| 50 | + if cl, ok := ucl.catsrcListers[namespace]; ok { |
| 51 | + return cl.CatalogSources(namespace) |
| 52 | + } |
| 53 | + |
| 54 | + // Check for any namespace-all listers |
| 55 | + if cl, ok := ucl.catsrcListers[metav1.NamespaceAll]; ok { |
| 56 | + return cl.CatalogSources(namespace) |
| 57 | + } |
| 58 | + |
| 59 | + return &NullCatalogSourceNamespaceLister{} |
| 60 | +} |
| 61 | + |
| 62 | +func (ucl *UnionCatalogSourceLister) RegisterCatalogSourceLister(namespace string, lister listers.CatalogSourceLister) { |
| 63 | + ucl.catsrcLock.Lock() |
| 64 | + defer ucl.catsrcLock.Unlock() |
| 65 | + |
| 66 | + if ucl.catsrcListers == nil { |
| 67 | + ucl.catsrcListers = make(map[string]listers.CatalogSourceLister) |
| 68 | + } |
| 69 | + |
| 70 | + ucl.catsrcListers[namespace] = lister |
| 71 | +} |
| 72 | + |
| 73 | +func (l *operatorsV1alpha1Lister) RegisterCatalogSourceLister(namespace string, lister listers.CatalogSourceLister) { |
| 74 | + l.catalogSourceLister.RegisterCatalogSourceLister(namespace, lister) |
| 75 | +} |
| 76 | + |
| 77 | +func (l *operatorsV1alpha1Lister) CatalogSourceLister() listers.CatalogSourceLister { |
| 78 | + return l.catalogSourceLister |
| 79 | +} |
| 80 | + |
| 81 | +// NullCatalogSourceNamespaceLister is an implementation of a null CatalogSourceNamespaceLister. It is |
| 82 | +// used to prevent nil pointers when no CatalogSourceNamespaceLister has been registered for a given |
| 83 | +// namespace. |
| 84 | +type NullCatalogSourceNamespaceLister struct { |
| 85 | + listers.CatalogSourceNamespaceLister |
| 86 | +} |
| 87 | + |
| 88 | +// List returns nil and an error explaining that this is a NullCatalogSourceNamespaceLister. |
| 89 | +func (n *NullCatalogSourceNamespaceLister) List(selector labels.Selector) (ret []*v1alpha1.CatalogSource, err error) { |
| 90 | + return nil, fmt.Errorf("cannot list CatalogSources with a NullCatalogSourceNamespaceLister") |
| 91 | +} |
| 92 | + |
| 93 | +// Get returns nil and an error explaining that this is a NullCatalogSourceNamespaceLister. |
| 94 | +func (n *NullCatalogSourceNamespaceLister) Get(name string) (*v1alpha1.CatalogSource, error) { |
| 95 | + return nil, fmt.Errorf("cannot get CatalogSource with a NullCatalogSourceNamespaceLister") |
| 96 | +} |
0 commit comments