@@ -18,7 +18,6 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
- "fmt"
22
21
"strings"
23
22
"time"
24
23
@@ -66,6 +65,10 @@ func (r *NamespaceScopeReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
66
65
return ctrl.Result {}, err
67
66
}
68
67
68
+ if err := r .UpdateConfigMap (instance ); err != nil {
69
+ return ctrl.Result {}, err
70
+ }
71
+
69
72
// Remove NamespaceScopeFinalizer. Once all finalizers have been
70
73
// removed, the object will be deleted.
71
74
controllerutil .RemoveFinalizer (instance , constant .NamespaceScopeFinalizer )
@@ -129,9 +132,13 @@ func (r *NamespaceScopeReconciler) InitConfigMap(instance *operatorv1.NamespaceS
129
132
cm .Namespace = cmNamespace
130
133
cm .Labels = map [string ]string {constant .NamespaceScopeLabel : "true" }
131
134
cm .Data = make (map [string ]string )
132
- cm .Data ["namespaces" ] = strings .Join (instance .Spec .NamespaceMembers , "," )
135
+ nsMembers , err := r .getNamespaceList (instance )
136
+ if err != nil {
137
+ return err
138
+ }
139
+ cm .Data ["namespaces" ] = strings .Join (nsMembers , "," )
133
140
// Set NamespaceScope instance as the owner of the ConfigMap.
134
- if err := controllerutil .SetControllerReference (instance , cm , r .Scheme ); err != nil {
141
+ if err := controllerutil .SetOwnerReference (instance , cm , r .Scheme ); err != nil {
135
142
klog .Errorf ("Failed to set owner reference for ConfigMap %s/%s: %v" , cmNamespace , cmName , err )
136
143
return err
137
144
}
@@ -145,21 +152,6 @@ func (r *NamespaceScopeReconciler) InitConfigMap(instance *operatorv1.NamespaceS
145
152
return err
146
153
}
147
154
148
- ownerRefUIDs := util .GetOwnerReferenceUIDs (cm .GetOwnerReferences ())
149
- if len (ownerRefUIDs ) != 0 {
150
- // ConfigMap OwnerReference UIDs don't contain current NamespaceScope instance UID, means this
151
- // ConfigMap belong to another NamespaceScope instance, stop reconcile.
152
- if ! util .UIDContains (ownerRefUIDs , instance .UID ) {
153
- r .Recorder .Eventf (instance , corev1 .EventTypeWarning , "ConfigMap Name Conflict" , "ConfigMap %s/%s has belong to another NamesapceScope instance, you need to change to a new configmapName" , cmNamespace , cmName )
154
- klog .Errorf ("configMap %s/%s has belong to another NamesapceScope instance, you need to change to a new configmapName" , cmNamespace , cmName )
155
- return fmt .Errorf ("configMap %s/%s has belong to another NamesapceScope instance, you need to change to a new configmapName" , cmNamespace , cmName )
156
- }
157
- } else {
158
- r .Recorder .Eventf (instance , corev1 .EventTypeWarning , "No OwnerReference" , "ConfigMap %s/%s has no owner reference, you need to change to a new configmapName" , cmNamespace , cmName )
159
- klog .Errorf ("configMap %s/%s has no owner reference, you need to change to a new configmapName" , cmNamespace , cmName )
160
- return fmt .Errorf ("configMap %s/%s has no owner reference, you need to change to a new configmapName" , cmNamespace , cmName )
161
- }
162
-
163
155
return nil
164
156
}
165
157
@@ -175,16 +167,35 @@ func (r *NamespaceScopeReconciler) UpdateConfigMap(instance *operatorv1.Namespac
175
167
}
176
168
177
169
// If NamespaceMembers changed, update ConfigMap
178
- if strings .Join (instance .Spec .NamespaceMembers , "," ) != cm .Data ["namespaces" ] {
179
- cm .Data ["namespaces" ] = strings .Join (instance .Spec .NamespaceMembers , "," )
170
+ nsMembers , err := r .getNamespaceList (instance )
171
+ if err != nil {
172
+ return err
173
+ }
174
+
175
+ // Get owner uids
176
+ ownerRefUIDs := util .GetOwnerReferenceUIDs (cm .GetOwnerReferences ())
177
+
178
+ if util .CheckListDifference (nsMembers , strings .Split (cm .Data ["namespaces" ], "," )) || ! util .UIDContains (ownerRefUIDs , instance .UID ) {
179
+ restartpod := util .CheckListDifference (nsMembers , strings .Split (cm .Data ["namespaces" ], "," ))
180
+ if restartpod {
181
+ cm .Data ["namespaces" ] = strings .Join (nsMembers , "," )
182
+ }
183
+
184
+ if err := controllerutil .SetOwnerReference (instance , cm , r .Scheme ); err != nil {
185
+ klog .Errorf ("Failed to set owner reference for ConfigMap %s/%s: %v" , cm .Namespace , cm .Name , err )
186
+ return err
187
+ }
188
+
180
189
if err := r .Update (ctx , cm ); err != nil {
181
190
klog .Errorf ("Failed to update ConfigMap %s : %v" , cmKey .String (), err )
182
191
return err
183
192
}
184
193
185
194
// When the configmap updated, restart all the pods with the RestartLabels
186
- if err := r .RestartPods (instance .Spec .RestartLabels , instance .Namespace ); err != nil {
187
- return err
195
+ if restartpod {
196
+ if err := r .RestartPods (instance .Spec .RestartLabels , instance .Namespace ); err != nil {
197
+ return err
198
+ }
188
199
}
189
200
}
190
201
return nil
@@ -197,7 +208,7 @@ func (r *NamespaceScopeReconciler) PushRbacToNamespace(instance *operatorv1.Name
197
208
return err
198
209
}
199
210
labels := map [string ]string {
200
- "projectedfrom " : instance .Namespace + "-" + instance .Name ,
211
+ "namespace-scope-configmap " : instance .Namespace + "-" + instance .Spec . ConfigmapName ,
201
212
}
202
213
203
214
for _ , toNs := range instance .Spec .NamespaceMembers {
@@ -229,10 +240,13 @@ func (r *NamespaceScopeReconciler) DeleteRbacFromUnmanagedNamespace(instance *op
229
240
if cm .Data ["namespaces" ] != "" {
230
241
nsInCm = strings .Split (cm .Data ["namespaces" ], "," )
231
242
}
232
- nsInCr := instance .Spec .NamespaceMembers
243
+ nsInCr , err := r .getNamespaceList (instance )
244
+ if err != nil {
245
+ return err
246
+ }
233
247
unmanagedNss := util .GetListDifference (nsInCm , nsInCr )
234
248
labels := map [string ]string {
235
- "projectedfrom " : instance .Namespace + "-" + instance .Name ,
249
+ "namespace-scope-configmap " : instance .Namespace + "-" + instance .Spec . ConfigmapName ,
236
250
}
237
251
for _ , toNs := range unmanagedNss {
238
252
if err := r .DeleteRoleBinding (labels , toNs ); err != nil {
@@ -255,8 +269,9 @@ func (r *NamespaceScopeReconciler) DeleteRbacFromUnmanagedNamespace(instance *op
255
269
256
270
// When delete NamespaceScope instance, cleanup all RBAC resources
257
271
func (r * NamespaceScopeReconciler ) DeleteAllRbac (instance * operatorv1.NamespaceScope ) error {
272
+ instance = setDefaults (instance )
258
273
labels := map [string ]string {
259
- "projectedfrom " : instance .Namespace + "-" + instance .Name ,
274
+ "namespace-scope-configmap " : instance .Namespace + "-" + instance .Spec . ConfigmapName ,
260
275
}
261
276
for _ , toNs := range instance .Spec .NamespaceMembers {
262
277
if err := r .DeleteRoleBinding (labels , toNs ); err != nil {
@@ -303,7 +318,7 @@ func (r *NamespaceScopeReconciler) GetServiceAccountFromNamespace(labels map[str
303
318
}
304
319
305
320
func (r * NamespaceScopeReconciler ) CreateRole (labels map [string ]string , toNs string ) error {
306
- name := constant .NamespaceScopeManagedRoleName + labels ["projectedfrom " ]
321
+ name := constant .NamespaceScopeManagedRoleName + labels ["namespace-scope-configmap " ]
307
322
namespace := toNs
308
323
role := & rbacv1.Role {
309
324
ObjectMeta : metav1.ObjectMeta {
@@ -344,7 +359,7 @@ func (r *NamespaceScopeReconciler) DeleteRole(labels map[string]string, toNs str
344
359
}
345
360
346
361
func (r * NamespaceScopeReconciler ) CreateUpdateRoleBinding (labels map [string ]string , saNames []string , fromNs , toNs string ) error {
347
- name := constant .NamespaceScopeManagedRoleBindingName + labels ["projectedfrom " ]
362
+ name := constant .NamespaceScopeManagedRoleBindingName + labels ["namespace-scope-configmap " ]
348
363
namespace := toNs
349
364
subjects := []rbacv1.Subject {}
350
365
for _ , saName := range saNames {
@@ -364,7 +379,7 @@ func (r *NamespaceScopeReconciler) CreateUpdateRoleBinding(labels map[string]str
364
379
Subjects : subjects ,
365
380
RoleRef : rbacv1.RoleRef {
366
381
Kind : "Role" ,
367
- Name : constant .NamespaceScopeManagedRoleName + labels ["projectedfrom " ],
382
+ Name : constant .NamespaceScopeManagedRoleName + labels ["namespace-scope-configmap " ],
368
383
APIGroup : "rbac.authorization.k8s.io" ,
369
384
},
370
385
}
@@ -431,10 +446,37 @@ func setDefaults(instance *operatorv1.NamespaceScope) *operatorv1.NamespaceScope
431
446
if instance .Spec .ConfigmapName == "" {
432
447
instance .Spec .ConfigmapName = constant .NamespaceScopeConfigmapName
433
448
}
449
+ if len (instance .Spec .RestartLabels ) == 0 {
450
+ instance .Spec .RestartLabels = map [string ]string {
451
+ constant .DefaultRestartLabelsKey : constant .DefaultRestartLabelsValue ,
452
+ }
453
+ }
434
454
435
455
return instance
436
456
}
437
457
458
+ func (r * NamespaceScopeReconciler ) getNamespaceList (instance * operatorv1.NamespaceScope ) ([]string , error ) {
459
+ // List the instance using the same configmap
460
+ crList := & operatorv1.NamespaceScopeList {}
461
+ namespaceMembersList := util .MakeSet ([]string {})
462
+ if err := r .List (ctx , crList , & client.ListOptions {Namespace : instance .Namespace }); err != nil {
463
+ klog .Errorf ("Cannot list namespacescope with in namespace %s: %v" , instance .Namespace , err )
464
+ return nil , err
465
+ }
466
+ for _ , cr := range crList .Items {
467
+ cr := setDefaults (& cr )
468
+ if ! cr .GetDeletionTimestamp ().IsZero () {
469
+ continue
470
+ }
471
+ if instance .Spec .ConfigmapName == cr .Spec .ConfigmapName {
472
+ for _ , ns := range cr .Spec .NamespaceMembers {
473
+ namespaceMembersList .Add (ns )
474
+ }
475
+ }
476
+ }
477
+ return util .ToStringSlice (namespaceMembersList ), nil
478
+ }
479
+
438
480
func (r * NamespaceScopeReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
439
481
return ctrl .NewControllerManagedBy (mgr ).
440
482
Owns (& corev1.ConfigMap {}).
0 commit comments