@@ -86,9 +86,12 @@ func (rc *Client) FindBundleThatProvides(ctx context.Context, group, version, ki
86
86
if err != nil {
87
87
return nil , err
88
88
}
89
- entry := rc .filterChannelEntries (it , excludedPkgName )
89
+ entry , err := rc .filterChannelEntries (ctx , it , excludedPkgName )
90
+ if err != nil {
91
+ return nil , err
92
+ }
90
93
if entry == nil {
91
- return nil , fmt .Errorf ("Unable to find a channel entry which doesn't belong to package %s" , excludedPkgName )
94
+ return nil , fmt .Errorf ("Unable to find a channel entry which doesn't belong to package %s's default channel " , excludedPkgName )
92
95
}
93
96
bundle , err := rc .Client .Registry .GetBundle (ctx , & registryapi.GetBundleRequest {PkgName : entry .PackageName , ChannelName : entry .ChannelName , CsvName : entry .BundleName })
94
97
if err != nil {
@@ -97,25 +100,56 @@ func (rc *Client) FindBundleThatProvides(ctx context.Context, group, version, ki
97
100
return bundle , nil
98
101
}
99
102
100
- // FilterChannelEntries filters out a channel entries that provide the requested
103
+ // FilterChannelEntries filters out channel entries that provide the requested
101
104
// API and come from the same package with original operator and returns the
102
- // first entry on the list
103
- func (rc * Client ) filterChannelEntries (it * ChannelEntryIterator , excludedPkgName string ) * opregistry.ChannelEntry {
105
+ // first entry on the list from the default channel of that package
106
+ func (rc * Client ) filterChannelEntries (ctx context.Context , it * ChannelEntryIterator , excludedPkgName string ) (* opregistry.ChannelEntry , error ) {
107
+ defChannels := make (map [string ]string , 0 )
108
+
104
109
var entries []* opregistry.ChannelEntry
105
110
for e := it .Next (); e != nil ; e = it .Next () {
106
111
if e .PackageName != excludedPkgName {
107
- entry := & opregistry.ChannelEntry {
108
- PackageName : e .PackageName ,
109
- ChannelName : e .ChannelName ,
110
- BundleName : e .BundleName ,
111
- Replaces : e .Replaces ,
112
+ // keep track of the default channel for each package
113
+ if _ , ok := defChannels [e .PackageName ]; ! ok {
114
+ defChannel , err := rc .getDefaultPackageChannel (ctx , e .PackageName )
115
+ if err != nil {
116
+ return nil , err
117
+ }
118
+ defChannels [e .PackageName ] = defChannel
119
+ }
120
+
121
+ // only add entry to the list if the entry is in the default channel
122
+ if e .ChannelName == defChannels [e .PackageName ] {
123
+ entry := & opregistry.ChannelEntry {
124
+ PackageName : e .PackageName ,
125
+ ChannelName : e .ChannelName ,
126
+ BundleName : e .BundleName ,
127
+ Replaces : e .Replaces ,
128
+ }
129
+ entries = append (entries , entry )
112
130
}
113
- entries = append (entries , entry )
114
131
}
115
132
}
116
133
117
134
if entries != nil {
118
- return entries [0 ]
135
+ return entries [0 ], nil
136
+ }
137
+ return nil , nil
138
+ }
139
+
140
+ // GetDefaultPackageChannel uses registry client to get the default
141
+ // channel name for a given package name
142
+ func (rc * Client ) getDefaultPackageChannel (ctx context.Context , pkgName string ) (string , error ) {
143
+ pkg , err := rc .Client .Registry .GetPackage (ctx , & registryapi.GetPackageRequest {Name : pkgName })
144
+ if err != nil {
145
+ return "" , err
146
+ }
147
+
148
+ var defChan string
149
+ if pkg != nil {
150
+ defChan = pkg .DefaultChannelName
151
+ } else {
152
+ return "" , fmt .Errorf ("package %s not found in registry" , pkgName )
119
153
}
120
- return nil
154
+ return defChan , nil
121
155
}
0 commit comments