@@ -85,11 +85,6 @@ func (r *OperatorStepResolver) ResolveSteps(namespace string, _ SourceQuerier) (
85
85
return nil , nil , nil , err
86
86
}
87
87
88
- // create a map of operatorsourceinfo (subscription+catalogsource data) to the original subscriptions
89
- subMap := r .sourceInfoToSubscriptions (subs )
90
- // get a list of new operators to add to the generation
91
- add := r .sourceInfoForNewSubscriptions (namespace , subMap )
92
-
93
88
var operators OperatorSet
94
89
namespaces := []string {namespace , r .globalCatalogNamespace }
95
90
operators , err = r .satResolver .SolveOperators (namespaces , csvs , subs )
@@ -103,21 +98,36 @@ func (r *OperatorStepResolver) ResolveSteps(namespace string, _ SourceQuerier) (
103
98
updatedSubs := []* v1alpha1.Subscription {}
104
99
bundleLookups := []v1alpha1.BundleLookup {}
105
100
for name , op := range operators {
106
- // TODO: added "is default channel" to sourceinfo out
107
- // of convenience, which breaks map key equality here
108
- // because it requires information that can't be
109
- // gleaned from subscriptions alone. need to revisit
110
- // TODO: this check also doesn't properly account for
111
- // subscriptions made without a catalog specified,
112
- // which means the sourceinfo won't match for the
113
- // realized operator
101
+ // Find an existing subscription that resolves to this operator.
102
+ var (
103
+ existingSubscription * v1alpha1.Subscription
104
+ alreadyExists bool
105
+ )
114
106
sourceInfo := * op .SourceInfo ()
115
- sourceInfo .DefaultChannel = false
116
- _ , isAdded := add [sourceInfo ]
117
- existingSubscription , subExists := subMap [sourceInfo ]
107
+ for _ , sub := range subs {
108
+ if sub .Spec .Package != sourceInfo .Package {
109
+ continue
110
+ }
111
+ if sub .Spec .Channel != "" && sub .Spec .Channel != sourceInfo .Channel {
112
+ continue
113
+ }
114
+ subCatalogKey := registry.CatalogKey {
115
+ Name : sub .Spec .CatalogSource ,
116
+ Namespace : sub .Spec .CatalogSourceNamespace ,
117
+ }
118
+ if ! subCatalogKey .Empty () && ! subCatalogKey .Equal (sourceInfo .Catalog ) {
119
+ continue
120
+ }
121
+ alreadyExists , err = r .hasExistingCurrentCSV (sub )
122
+ if err != nil {
123
+ return nil , nil , nil , fmt .Errorf ("unable to determine whether subscription has a preexisting CSV" )
124
+ }
125
+ existingSubscription = sub
126
+ break
127
+ }
118
128
119
129
// subscription exists and is up to date
120
- if subExists && existingSubscription .Status .CurrentCSV == op .Identifier () && ! isAdded {
130
+ if existingSubscription != nil && existingSubscription .Status .CurrentCSV == op .Identifier () && alreadyExists {
121
131
continue
122
132
}
123
133
@@ -155,7 +165,7 @@ func (r *OperatorStepResolver) ResolveSteps(namespace string, _ SourceQuerier) (
155
165
})
156
166
}
157
167
158
- if ! subExists {
168
+ if existingSubscription == nil {
159
169
// explicitly track the resolved CSV as the starting CSV on the resolved subscriptions
160
170
op .SourceInfo ().StartingCSV = op .Identifier ()
161
171
subStep , err := NewSubscriptionStepResource (namespace , * op .SourceInfo ())
@@ -171,7 +181,7 @@ func (r *OperatorStepResolver) ResolveSteps(namespace string, _ SourceQuerier) (
171
181
}
172
182
173
183
// add steps for subscriptions for bundles that were added through resolution
174
- if subExists && existingSubscription .Status .CurrentCSV != op .Identifier () {
184
+ if existingSubscription != nil && existingSubscription .Status .CurrentCSV != op .Identifier () {
175
185
// update existing subscription status
176
186
existingSubscription .Status .CurrentCSV = op .Identifier ()
177
187
updatedSubs = append (updatedSubs , existingSubscription )
@@ -183,56 +193,30 @@ func (r *OperatorStepResolver) ResolveSteps(namespace string, _ SourceQuerier) (
183
193
return steps , bundleLookups , updatedSubs , nil
184
194
}
185
195
186
- func (r * OperatorStepResolver ) sourceInfoForNewSubscriptions (namespace string , subs map [OperatorSourceInfo ]* v1alpha1.Subscription ) (add map [OperatorSourceInfo ]struct {}) {
187
- add = make (map [OperatorSourceInfo ]struct {})
188
- for key , sub := range subs {
189
- if sub .Status .CurrentCSV == "" {
190
- add [key ] = struct {}{}
191
- continue
192
- }
193
- csv , err := r .csvLister .ClusterServiceVersions (namespace ).Get (sub .Status .CurrentCSV )
194
- if csv == nil || errors .IsNotFound (err ) {
195
- add [key ] = struct {}{}
196
- }
196
+ func (r * OperatorStepResolver ) hasExistingCurrentCSV (sub * v1alpha1.Subscription ) (bool , error ) {
197
+ if sub .Status .CurrentCSV == "" {
198
+ return false , nil
197
199
}
198
- return
199
- }
200
-
201
- func (r * OperatorStepResolver ) sourceInfoToSubscriptions (subs []* v1alpha1.Subscription ) (add map [OperatorSourceInfo ]* v1alpha1.Subscription ) {
202
- add = make (map [OperatorSourceInfo ]* v1alpha1.Subscription )
203
- var sourceNamespace string
204
- for _ , s := range subs {
205
- startingCSV := s .Spec .StartingCSV
206
- if s .Status .CurrentCSV != "" {
207
- // If a csv has previously been resolved for the operator, don't enable
208
- // a starting csv search.
209
- startingCSV = ""
210
- }
211
- if s .Spec .CatalogSourceNamespace == "" {
212
- sourceNamespace = s .GetNamespace ()
213
- } else {
214
- sourceNamespace = s .Spec .CatalogSourceNamespace
215
- }
216
- add [OperatorSourceInfo {
217
- Package : s .Spec .Package ,
218
- Channel : s .Spec .Channel ,
219
- StartingCSV : startingCSV ,
220
- Catalog : registry.CatalogKey {Name : s .Spec .CatalogSource , Namespace : sourceNamespace },
221
- }] = s .DeepCopy ()
200
+ _ , err := r .csvLister .ClusterServiceVersions (sub .GetNamespace ()).Get (sub .Status .CurrentCSV )
201
+ if err == nil {
202
+ return true , nil
203
+ }
204
+ if errors .IsNotFound (err ) {
205
+ return false , nil
222
206
}
223
- return
207
+ return false , err // Can't answer this question right now.
224
208
}
225
209
226
- func (r * OperatorStepResolver ) listSubscriptions (namespace string ) (subs []* v1alpha1.Subscription , err error ) {
210
+ func (r * OperatorStepResolver ) listSubscriptions (namespace string ) ([]* v1alpha1.Subscription , error ) {
227
211
list , err := r .client .OperatorsV1alpha1 ().Subscriptions (namespace ).List (context .TODO (), metav1.ListOptions {})
228
212
if err != nil {
229
- return
213
+ return nil , err
230
214
}
231
215
232
- subs = make ( []* v1alpha1.Subscription , 0 )
216
+ var subs []* v1alpha1.Subscription
233
217
for i := range list .Items {
234
218
subs = append (subs , & list .Items [i ])
235
219
}
236
220
237
- return
221
+ return subs , nil
238
222
}
0 commit comments