github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/csv/csvset.go (about) 1 package csv 2 3 import ( 4 "github.com/operator-framework/api/pkg/operators/v1alpha1" 5 "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister" 6 "github.com/sirupsen/logrus" 7 "k8s.io/apimachinery/pkg/labels" 8 ) 9 10 // NewSetGenerator returns a new instance of SetGenerator. 11 func NewSetGenerator(logger *logrus.Logger, lister operatorlister.OperatorLister) SetGenerator { 12 return &csvSet{ 13 logger: logger, 14 lister: lister, 15 } 16 } 17 18 // SetGenerator is an interface that returns a map of ClusterServiceVersion 19 // objects that match a certain set of criteria. 20 // 21 // SetGenerator gathers all CSV(s) in the given namespace into a map keyed by 22 // CSV name; if metav1.NamespaceAll gets the set across all namespaces 23 type SetGenerator interface { 24 WithNamespace(namespace string, phase v1alpha1.ClusterServiceVersionPhase) map[string]*v1alpha1.ClusterServiceVersion 25 WithNamespaceAndLabels(namespace string, phase v1alpha1.ClusterServiceVersionPhase, selector labels.Selector) map[string]*v1alpha1.ClusterServiceVersion 26 } 27 28 type csvSet struct { 29 lister operatorlister.OperatorLister 30 logger *logrus.Logger 31 } 32 33 // WithNamespace returns all ClusterServiceVersion resource(s) that matches the 34 // specified phase from a given namespace. 35 func (s *csvSet) WithNamespace(namespace string, phase v1alpha1.ClusterServiceVersionPhase) map[string]*v1alpha1.ClusterServiceVersion { 36 return s.with(namespace, phase, labels.Everything()) 37 } 38 39 // WithNamespaceAndLabels returns all ClusterServiceVersion resource(s) that 40 // matches the specified phase and label selector from a given namespace. 41 func (s *csvSet) WithNamespaceAndLabels(namespace string, phase v1alpha1.ClusterServiceVersionPhase, selector labels.Selector) map[string]*v1alpha1.ClusterServiceVersion { 42 return s.with(namespace, phase, selector) 43 } 44 45 func (s *csvSet) with(namespace string, phase v1alpha1.ClusterServiceVersionPhase, selector labels.Selector) map[string]*v1alpha1.ClusterServiceVersion { 46 csvsInNamespace, err := s.lister.OperatorsV1alpha1().ClusterServiceVersionLister().ClusterServiceVersions(namespace).List(selector) 47 48 if err != nil { 49 s.logger.Warnf("could not list CSVs while constructing CSV set") 50 return nil 51 } 52 53 csvs := make(map[string]*v1alpha1.ClusterServiceVersion, len(csvsInNamespace)) 54 for _, csv := range csvsInNamespace { 55 if phase != v1alpha1.CSVPhaseAny && csv.Status.Phase != phase { 56 continue 57 } 58 csvs[csv.Name] = csv.DeepCopy() 59 } 60 61 return csvs 62 }