@@ -438,18 +438,30 @@ func (r *rbac) createResources(ctx context.Context) error {
438438 }
439439 }
440440 if ! withError {
441- logger .Infof ("namespace %s sucessfully reconciled. Adding label namespace-reconcile-version to mark it as reconciled" , ns .Name )
442- // Add `openshift-pipelines.tekton.dev/namespace-reconcile-version` label to namespace
443- // so that rbac won't loop on it again
444- nsLabels := ns .GetLabels ()
445- if len (nsLabels ) == 0 {
441+ logger .Infof ("add label namespace-reconcile-version to mark namespace '%s' as reconciled" , ns .Name )
442+
443+ // Re-fetch the namespace to ensure we have the latest version
444+ updatedNS , err := r .kubeClientSet .CoreV1 ().Namespaces ().Get (ctx , ns .Name , metav1.GetOptions {})
445+ if err != nil {
446+ return fmt .Errorf ("failed to re-fetch namespace %s: %v" , ns .Name , err )
447+ }
448+
449+ // Get the current labels, or initialize if none exist
450+ nsLabels := updatedNS .GetLabels ()
451+ if nsLabels == nil {
446452 nsLabels = map [string ]string {}
447453 }
454+
455+ // Add `openshift-pipelines.tekton.dev/namespace-reconcile-version` label to namespace
456+ // so that rbac won't loop on it again
448457 nsLabels [namespaceVersionLabel ] = r .version
449- ns .SetLabels (nsLabels )
450- if _ , err := r .kubeClientSet .CoreV1 ().Namespaces ().Update (ctx , & ns , metav1.UpdateOptions {}); err != nil {
451- return fmt .Errorf ("failed to update namespace %s with label %s, %v" , ns .Name , namespaceVersionLabel , err )
458+ updatedNS .SetLabels (nsLabels )
459+
460+ // Update the namespace with set labels
461+ if _ , err = r .kubeClientSet .CoreV1 ().Namespaces ().Update (ctx , updatedNS , metav1.UpdateOptions {}); err != nil {
462+ return fmt .Errorf ("failed to update namespace '%s' with label %s, %v" , ns .Name , namespaceVersionLabel , err )
452463 }
464+ logger .Infof ("namespace '%s' sucessfully reconciled" , ns .Name )
453465 }
454466 }
455467
0 commit comments