@@ -13,6 +13,7 @@ import (
13
13
corev1 "k8s.io/api/core/v1"
14
14
rbacv1 "k8s.io/api/rbac/v1"
15
15
k8serrors "k8s.io/apimachinery/pkg/api/errors"
16
+ meta "k8s.io/apimachinery/pkg/api/meta"
16
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
18
"k8s.io/apimachinery/pkg/labels"
18
19
"k8s.io/apimachinery/pkg/util/errors"
@@ -64,8 +65,51 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
64
65
"namespace" : op .GetNamespace (),
65
66
})
66
67
68
+ // Query OG in this namespace
69
+ groups , err := a .lister .OperatorsV1 ().OperatorGroupLister ().OperatorGroups (op .GetNamespace ()).List (labels .Everything ())
70
+ if err != nil {
71
+ logger .WithError (err ).Warnf ("failed to list OperatorGroups in the namespace" )
72
+ }
73
+
74
+ // Check if there is a stale multiple OG condition and clear it if existed.
75
+ if len (groups ) == 1 {
76
+ og := groups [0 ]
77
+ if c := meta .FindStatusCondition (og .Status .Conditions , v1 .MutlipleOperatorGroupCondition ); c != nil {
78
+ meta .RemoveStatusCondition (& og .Status .Conditions , v1 .MutlipleOperatorGroupCondition )
79
+ if og .GetName () == op .GetName () {
80
+ meta .RemoveStatusCondition (& op .Status .Conditions , v1 .MutlipleOperatorGroupCondition )
81
+ }
82
+ _ , err = a .client .OperatorsV1 ().OperatorGroups (op .GetNamespace ()).UpdateStatus (context .TODO (), og , metav1.UpdateOptions {})
83
+ if err != nil {
84
+ logger .Warnf ("fail to upgrade operator group status og=%s with condition %+v: %s" , og .GetName (), c , err .Error ())
85
+ }
86
+ }
87
+ } else if len (groups ) > 1 {
88
+ // Add to all OG's status conditions to indicate they're multiple OGs in the
89
+ // same namespace which is not allowed.
90
+ cond := metav1.Condition {
91
+ Type : v1 .MutlipleOperatorGroupCondition ,
92
+ Status : metav1 .ConditionTrue ,
93
+ Reason : v1 .MultipleOperatorGroupsReason ,
94
+ Message : "Multiple OperatorGroup found in the same namespace" ,
95
+ }
96
+ for _ , og := range groups {
97
+ if c := meta .FindStatusCondition (og .Status .Conditions , v1 .MutlipleOperatorGroupCondition ); c != nil {
98
+ continue
99
+ }
100
+ meta .SetStatusCondition (& og .Status .Conditions , cond )
101
+ if og .GetName () == op .GetName () {
102
+ meta .SetStatusCondition (& op .Status .Conditions , cond )
103
+ }
104
+ _ , err = a .client .OperatorsV1 ().OperatorGroups (op .GetNamespace ()).UpdateStatus (context .TODO (), og , metav1.UpdateOptions {})
105
+ if err != nil {
106
+ logger .Warnf ("fail to upgrade operator group status og=%s with condition %+v: %s" , og .GetName (), cond , err .Error ())
107
+ }
108
+ }
109
+ }
110
+
67
111
previousRef := op .Status .ServiceAccountRef .DeepCopy ()
68
- op , err : = a .serviceAccountSyncer .SyncOperatorGroup (op )
112
+ op , err = a .serviceAccountSyncer .SyncOperatorGroup (op )
69
113
if err != nil {
70
114
logger .Errorf ("error updating service account - %v" , err )
71
115
return err
@@ -109,6 +153,7 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
109
153
op .Status = v1.OperatorGroupStatus {
110
154
Namespaces : targetNamespaces ,
111
155
LastUpdated : a .now (),
156
+ Conditions : op .Status .Conditions ,
112
157
}
113
158
114
159
if _ , err = a .client .OperatorsV1 ().OperatorGroups (op .GetNamespace ()).UpdateStatus (context .TODO (), op , metav1.UpdateOptions {}); err != nil && ! k8serrors .IsNotFound (err ) {
0 commit comments