@@ -27,8 +27,6 @@ const (
27
27
AdminSuffix = "admin"
28
28
EditSuffix = "edit"
29
29
ViewSuffix = "view"
30
-
31
- operatorGroupLabelTemplate = "olm.operatorgroup/%s.%s"
32
30
)
33
31
34
32
var (
@@ -100,9 +98,8 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
100
98
logger .WithField ("targetNamespaces" , targetNamespaces ).Debug ("updated target namespaces" )
101
99
102
100
if namespacesChanged (targetNamespaces , op .Status .Namespaces ) {
103
- // Labels are only applied to namespaces that are not empty strings.
104
- prunedStatusNamespaces := pruneEmptyStrings (op .Status .Namespaces )
105
- prunedTargetNamespaces := pruneEmptyStrings (targetNamespaces )
101
+ logger .Debug ("OperatorGroup namespaces change detected" )
102
+ outOfSyncNamespaces := namespacesAddedOrRemoved (op .Spec .TargetNamespaces , op .Status .Namespaces )
106
103
107
104
// Update operatorgroup target namespace selection
108
105
logger .WithField ("targets" , targetNamespaces ).Debug ("namespace change detected" )
@@ -116,25 +113,15 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
116
113
return err
117
114
}
118
115
119
- // Remove labels from targetNamespaces that were dropped.
120
- ogLabel := getOperatorGroupLabel (* op )
121
- for _ , namespaceName := range setDifference (prunedStatusNamespaces , prunedTargetNamespaces ) {
122
- err = a .removeNamespaceLabel (namespaceName , ogLabel )
123
- if err != nil {
124
- logger .WithError (err ).Warnf ("failed to remove operatorgroup label from namespace %s" , namespaceName )
125
- return err
126
- }
127
- }
116
+ logger .Debug ("operatorgroup status updated" )
128
117
129
- // Add labels to targetNamespaces that were added.
130
- for _ , namespaceName := range setDifference (prunedTargetNamespaces , prunedStatusNamespaces ) {
131
- err = a .addNamespaceLabel (namespaceName , ogLabel , "" )
132
- if err != nil {
133
- logger .WithError (err ).Warnf ("failed to add operatorgroup to from namespace %s" , namespaceName )
134
- return err
135
- }
118
+ // Requeueing out of sync namespaces
119
+ logger .Debug ("Requeueing out of sync namespaces" )
120
+ for _ , ns := range outOfSyncNamespaces {
121
+ logger .WithField ("namespace" , ns ).Debug ("requeueing" )
122
+ a .nsQueueSet .Add (ns )
136
123
}
137
- logger . Debug ( "namespace change detected and operatorgroup status updated" )
124
+
138
125
// CSV requeue is handled by the succeeding sync in `annotateCSVs`
139
126
return nil
140
127
}
@@ -185,43 +172,6 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
185
172
return nil
186
173
}
187
174
188
- // pruneEmptyStrings removes any items from a list that are empty
189
- func pruneEmptyStrings (strings []string ) []string {
190
- prunedStrings := []string {}
191
- for _ , str := range strings {
192
- if str != "" {
193
- prunedStrings = append (prunedStrings , str )
194
- }
195
- }
196
- return prunedStrings
197
- }
198
-
199
- // setDifference implements Set Difference: A - B
200
- // https://en.wikipedia.org/wiki/Complement_(set_theory)#In_programming_languages
201
- func setDifference (a , b []string ) (diff []string ) {
202
- if len (a ) == 0 {
203
- return diff
204
- }
205
-
206
- if len (b ) == 0 {
207
- return a
208
- }
209
-
210
- // Find the Set Difference.
211
- m := make (map [string ]bool )
212
-
213
- for _ , item := range b {
214
- m [item ] = true
215
- }
216
-
217
- for _ , item := range a {
218
- if _ , ok := m [item ]; ! ok {
219
- diff = append (diff , item )
220
- }
221
- }
222
- return diff
223
- }
224
-
225
175
func (a * Operator ) operatorGroupDeleted (obj interface {}) {
226
176
op , ok := obj .(* v1.OperatorGroup )
227
177
if ! ok {
@@ -246,52 +196,12 @@ func (a *Operator) operatorGroupDeleted(obj interface{}) {
246
196
}
247
197
}
248
198
249
- // Remove OperatorGroup label from targeNamespaces.
250
- ogLabel := getOperatorGroupLabel (* op )
251
- for _ , namespaceName := range op .Spec .TargetNamespaces {
252
- a .removeNamespaceLabel (namespaceName , ogLabel )
253
- if err != nil {
254
- logger .WithError (err ).Error ("failed to remove OperatorGroup Label from Namespace during garbage collection" )
255
- }
256
- }
257
- }
258
-
259
- func (a * Operator ) updateNamespace (namespaceName string , namespaceUpdateFunc func (* corev1.Namespace )) error {
260
- namespace , err := a .opClient .KubernetesInterface ().CoreV1 ().Namespaces ().Get (namespaceName , metav1.GetOptions {})
261
- if err != nil {
262
- return err
263
- }
264
-
265
- namespaceUpdateFunc (namespace )
266
-
267
- _ , err = a .opClient .KubernetesInterface ().CoreV1 ().Namespaces ().Update (namespace )
268
- if err != nil {
269
- return err
270
- }
271
- return nil
272
- }
273
-
274
- func (a * Operator ) removeNamespaceLabel (namespaceName , key string ) error {
275
- namespaceUpdateFunc := func (namespace * corev1.Namespace ) {
276
- delete (namespace .Labels , key )
277
- }
278
- return a .updateNamespace (namespaceName , namespaceUpdateFunc )
279
- }
280
-
281
- func (a * Operator ) addNamespaceLabel (namespaceName string , key , value string ) error {
282
- namespaceUpdateFunc := func (namespace * corev1.Namespace ) {
283
- if namespace .Labels == nil {
284
- namespace .Labels = make (map [string ]string , 1 )
285
- }
286
- namespace .Labels [key ] = value
199
+ // Trigger a sync on namespaces
200
+ logger .Debug ("OperatorGroup deleted, requeueing out of sync namespaces" )
201
+ for _ , ns := range op .Status .Namespaces {
202
+ logger .WithField ("namespace" , ns ).Debug ("requeueing" )
203
+ a .nsQueueSet .Add (ns )
287
204
}
288
- return a .updateNamespace (namespaceName , namespaceUpdateFunc )
289
- }
290
-
291
- // getOperatorGroupLabel returns a label that is applied to Namespaces to signify that the
292
- // namespace is a part of the OperatorGroup using selectors.
293
- func getOperatorGroupLabel (og v1.OperatorGroup ) string {
294
- return fmt .Sprintf (operatorGroupLabelTemplate , og .GetNamespace (), og .GetName ())
295
205
}
296
206
297
207
func (a * Operator ) annotateCSVs (group * v1.OperatorGroup , targetNamespaces []string , logger * logrus.Entry ) error {
@@ -1075,3 +985,32 @@ func (a *Operator) findCSVsThatProvideAnyOf(provide resolver.APISet) ([]*v1alpha
1075
985
1076
986
return providers , nil
1077
987
}
988
+
989
+ // namespacesAddedOrRemoved returns the union of:
990
+ // - the set of elements in A but not in B
991
+ // - the set of elements in B but not in A
992
+ func namespacesAddedOrRemoved (a , b []string ) []string {
993
+ check := make (map [string ]struct {})
994
+
995
+ for _ , namespace := range a {
996
+ check [namespace ] = struct {}{}
997
+ }
998
+
999
+ for _ , namespace := range b {
1000
+ if _ , ok := check [namespace ]; ! ok {
1001
+ check [namespace ] = struct {}{}
1002
+ } else {
1003
+ delete (check , namespace )
1004
+ }
1005
+ }
1006
+
1007
+ // Remove global namespace name if added
1008
+ delete (check , "" )
1009
+
1010
+ var keys []string
1011
+ for key := range check {
1012
+ keys = append (keys , key )
1013
+ }
1014
+
1015
+ return keys
1016
+ }
0 commit comments