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