Skip to content

Commit 7f9ab4e

Browse files
committed
refactor(operatorgroup): account for TargetNamespace field in
updateNamespaceList(...)
1 parent c14a1fd commit 7f9ab4e

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

deploy/chart/templates/0000_30_13-operatorgroup.crd.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ spec:
6363
type: array
6464
items:
6565
type: string
66+
pattern: ^\S+$
6667
serviceAccountName:
6768
type: string
6869
required:

pkg/api/apis/operators/v1alpha1/clusterserviceversion_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ type ClusterServiceVersionSpec struct {
136136
Links []AppLink `json:"links,omitempty"`
137137
Icon []Icon `json:"icon,omitempty"`
138138

139-
// InstallModes
139+
// InstallModes specify supported installation types
140+
// +optional
140141
InstallModes []InstallMode `json:"installModes,omitempty"`
141142

142143
// The name of a CSV this one replaces. Should match the `metadata.Name` field of the old CSV.

pkg/controller/operators/olm/operator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ func (a *Operator) syncClusterServiceVersion(obj interface{}) (syncError error)
477477
func (a *Operator) operatorGroupForActiveCSV(logger *logrus.Entry, csv *v1alpha1.ClusterServiceVersion) *v1alpha2.OperatorGroup {
478478
annotations := csv.GetAnnotations()
479479

480+
// TODO: Only return OperatorGroup if InstallModes allow the CSV to be part of the group or no installmodes are declared
481+
480482
// not part of a group yet
481483
if annotations == nil {
482484
logger.Info("not part of any operatorgroup, no annotations")

pkg/controller/operators/olm/operator_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ func TestSyncOperatorGroups(t *testing.T) {
22462246
ownerutil.AddNonBlockingOwner(ownedDeployment, operatorCSV)
22472247

22482248
annotatedDeployment := ownedDeployment.DeepCopy()
2249-
annotatedDeployment.Spec.Template.SetAnnotations(map[string]string{v1alpha2.OperatorGroupTargetsAnnotationKey: targetNamespace + "," + operatorNamespace, v1alpha2.OperatorGroupAnnotationKey: "operator-group-1", v1alpha2.OperatorGroupNamespaceAnnotationKey: operatorNamespace})
2249+
annotatedDeployment.Spec.Template.SetAnnotations(map[string]string{v1alpha2.OperatorGroupTargetsAnnotationKey: operatorNamespace + "," + targetNamespace, v1alpha2.OperatorGroupAnnotationKey: "operator-group-1", v1alpha2.OperatorGroupNamespaceAnnotationKey: operatorNamespace})
22502250
annotatedDeployment.SetLabels(map[string]string{
22512251
"olm.owner": "csv1",
22522252
"olm.owner.namespace": "operator-ns",
@@ -2365,7 +2365,7 @@ func TestSyncOperatorGroups(t *testing.T) {
23652365
k8sObjs: namespaces,
23662366
},
23672367
expectedStatus: v1alpha2.OperatorGroupStatus{
2368-
Namespaces: []string{targetNamespace, operatorNamespace},
2368+
Namespaces: []string{operatorNamespace, targetNamespace},
23692369
LastUpdated: timeNow(),
23702370
},
23712371
},
@@ -2390,12 +2390,12 @@ func TestSyncOperatorGroups(t *testing.T) {
23902390
crds: []runtime.Object{crd},
23912391
},
23922392
expectedStatus: v1alpha2.OperatorGroupStatus{
2393-
Namespaces: []string{targetNamespace, operatorNamespace},
2393+
Namespaces: []string{operatorNamespace, targetNamespace},
23942394
LastUpdated: timeNow(),
23952395
},
23962396
final: final{objects: map[string][]runtime.Object{
23972397
operatorNamespace: {
2398-
withAnnotations(operatorCSVFinal.DeepCopy(), map[string]string{v1alpha2.OperatorGroupTargetsAnnotationKey: targetNamespace + "," + operatorNamespace, v1alpha2.OperatorGroupAnnotationKey: "operator-group-1", v1alpha2.OperatorGroupNamespaceAnnotationKey: operatorNamespace}),
2398+
withAnnotations(operatorCSVFinal.DeepCopy(), map[string]string{v1alpha2.OperatorGroupTargetsAnnotationKey: operatorNamespace + "," + targetNamespace, v1alpha2.OperatorGroupAnnotationKey: "operator-group-1", v1alpha2.OperatorGroupNamespaceAnnotationKey: operatorNamespace}),
23992399
annotatedDeployment,
24002400
},
24012401
targetNamespace: {

pkg/controller/operators/olm/operatorgroup.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package olm
33
import (
44
"fmt"
55
"reflect"
6+
"sort"
67
"strings"
78

89
corev1 "k8s.io/api/core/v1"
@@ -400,45 +401,56 @@ func (a *Operator) updateNamespaceList(op *v1alpha2.OperatorGroup) ([]string, er
400401
return nil, err
401402
}
402403

403-
namespaceList := []string{}
404+
namespaceSet := make(map[string]struct{})
405+
namespaceAll := false
404406
if op.Spec.TargetNamespaces != nil && len(op.Spec.TargetNamespaces) > 0 {
405-
// TODO: Ensure namespaceList is a set
406-
namespaceList = append(namespaceList, op.Spec.TargetNamespaces...)
407-
} else if selector.Empty() || selector == nil {
408-
namespaceList = append(namespaceList, corev1.NamespaceAll)
407+
for _, ns := range op.Spec.TargetNamespaces {
408+
if ns == corev1.NamespaceAll {
409+
return nil, fmt.Errorf("TargetNamespaces cannot contain NamespaceAll: %v", op.Spec.TargetNamespaces)
410+
}
411+
namespaceSet[ns] = struct{}{}
412+
}
413+
} else if selector == nil || selector.Empty() {
414+
namespaceSet[corev1.NamespaceAll] = struct{}{}
415+
namespaceAll = true
409416
} else {
410417
matchedNamespaces, err := a.lister.CoreV1().NamespaceLister().List(selector)
411418
if err != nil {
412419
return nil, err
413420
}
414421

415-
operatorGroupNamespaceSelected := false
416422
for _, ns := range matchedNamespaces {
417-
namespaceList = append(namespaceList, ns.GetName())
418-
if ns.GetName() == op.GetNamespace() {
419-
operatorGroupNamespaceSelected = true
420-
}
423+
namespaceSet[ns.GetName()] = struct{}{}
421424
}
425+
}
422426

423-
// always include the operatorgroup namespace as a target namespace
424-
if !operatorGroupNamespaceSelected {
425-
namespaceList = append(namespaceList, op.GetNamespace())
426-
}
427+
if !namespaceAll {
428+
// Add operator namespace to the set
429+
namespaceSet[op.GetNamespace()] = struct{}{}
427430
}
428431

432+
namespaceList := []string{}
433+
for ns := range namespaceSet {
434+
namespaceList = append(namespaceList, ns)
435+
}
436+
sort.StringSlice(namespaceList).Sort()
437+
429438
if !namespacesChanged(namespaceList, op.Status.Namespaces) {
430439
// status is current with correct namespaces, so no further updates required
431440
return namespaceList, nil
432441
}
442+
433443
a.Log.Debugf("Namespace change detected, found: %v", namespaceList)
434444
op.Status = v1alpha2.OperatorGroupStatus{
435445
Namespaces: namespaceList,
436446
LastUpdated: timeNow(),
437447
}
448+
438449
_, err = a.client.OperatorsV1alpha2().OperatorGroups(op.GetNamespace()).UpdateStatus(op)
439450
if err != nil {
440451
return namespaceList, err
441452
}
453+
442454
return namespaceList, nil
443455
}
444456

0 commit comments

Comments
 (0)