@@ -56,19 +56,28 @@ import (
5656// BGPConfigurationReconciler reconciles a BGPConfiguration object
5757type BGPConfigurationReconciler struct {
5858 client.Client
59- Kclient kubernetes.Interface
60- Scheme * runtime.Scheme
59+ Kclient kubernetes.Interface
60+ Scheme * runtime.Scheme
61+ FRRMigrationNamespace string
6162}
6263
6364// GetLogger returns a logger object with a prefix of "controller.name" and additional controller context fields
6465func (r * BGPConfigurationReconciler ) GetLogger (ctx context.Context ) logr.Logger {
6566 return log .FromContext (ctx ).WithName ("Controllers" ).WithName ("BGPConfiguration" )
6667}
6768
69+ func (r * BGPConfigurationReconciler ) getFRRMigrationNamespace () string {
70+ if r .FRRMigrationNamespace != "" {
71+ return r .FRRMigrationNamespace
72+ }
73+ return bgp .OpenShiftFRRNamespace
74+ }
75+
6876//+kubebuilder:rbac:groups=network.openstack.org,resources=bgpconfigurations,verbs=get;list;watch;create;update;patch;delete
6977//+kubebuilder:rbac:groups=network.openstack.org,resources=bgpconfigurations/status,verbs=get;update;patch
7078//+kubebuilder:rbac:groups=network.openstack.org,resources=bgpconfigurations/finalizers,verbs=update
7179//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;update;patch
80+ //+kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch
7281//+kubebuilder:rbac:groups=frrk8s.metallb.io,resources=frrconfigurations,verbs=get;list;watch;create;update;patch;delete;deletecollection
7382//+kubebuilder:rbac:groups=k8s.cni.cncf.io,resources=network-attachment-definitions,verbs=get;list;watch
7483
@@ -353,12 +362,17 @@ func (r *BGPConfigurationReconciler) reconcileDelete(ctx context.Context, instan
353362 Log := r .GetLogger (ctx )
354363 Log .Info ("Reconciling Service delete" )
355364
356- // Delete all FRRConfiguration in the Spec.FRRConfigurationNamespace namespace,
365+ frrNamespace , err := bgp .GetFRRConfigurationNamespace (ctx , r .Client , r .getFRRMigrationNamespace (), instance .Spec .FRRConfigurationNamespace )
366+ if err != nil {
367+ return ctrl.Result {}, fmt .Errorf ("unable to determine FRR namespace: %w" , err )
368+ }
369+
370+ // Delete all FRRConfiguration in the determined namespace,
357371 // which have the correct owner and ownernamespace label
358- err : = r .DeleteAllOf (
372+ err = r .DeleteAllOf (
359373 ctx ,
360374 & frrk8sv1.FRRConfiguration {},
361- client .InNamespace (instance . Spec . FRRConfigurationNamespace ),
375+ client .InNamespace (frrNamespace ),
362376 client.MatchingLabels {
363377 labels .GetOwnerNameLabelSelector (labels .GetGroupLabel ("bgpconfiguration" )): instance .Name ,
364378 labels .GetOwnerNameSpaceLabelSelector (labels .GetGroupLabel ("bgpconfiguration" )): instance .Namespace ,
@@ -368,6 +382,12 @@ func (r *BGPConfigurationReconciler) reconcileDelete(ctx context.Context, instan
368382 return ctrl.Result {}, fmt .Errorf ("error DeleteAllOf FRRConfiguration: %w" , err )
369383 }
370384
385+ if frrNamespace != instance .Spec .FRRConfigurationNamespace {
386+ if err := r .cleanupOldNamespaceFRRConfigurations (ctx , instance , instance .Spec .FRRConfigurationNamespace ); err != nil {
387+ Log .Error (err , "Failed to cleanup FRRConfigurations from old namespace" , "namespace" , instance .Spec .FRRConfigurationNamespace )
388+ }
389+ }
390+
371391 // Service is deleted so remove the finalizer.
372392 controllerutil .RemoveFinalizer (instance , helper .GetFinalizer ())
373393 Log .Info ("Reconciled Service delete successfully" )
@@ -379,6 +399,12 @@ func (r *BGPConfigurationReconciler) reconcileNormal(ctx context.Context, instan
379399 Log := r .GetLogger (ctx )
380400 Log .Info ("Reconciling Service" )
381401
402+ frrNamespace , err := bgp .GetFRRConfigurationNamespace (ctx , r .Client , r .getFRRMigrationNamespace (), instance .Spec .FRRConfigurationNamespace )
403+ if err != nil {
404+ return ctrl.Result {}, fmt .Errorf ("unable to determine FRR namespace: %w" , err )
405+ }
406+ Log .Info ("Using FRR namespace" , "namespace" , frrNamespace )
407+
382408 // Get a list of pods which are in the same namespace as the ctlplane
383409 // to verify if a FRRConfiguration needs to be created for.
384410 podList := & corev1.PodList {}
@@ -398,7 +424,7 @@ func (r *BGPConfigurationReconciler) reconcileNormal(ctx context.Context, instan
398424 // get all frrconfigs
399425 frrConfigList := & frrk8sv1.FRRConfigurationList {}
400426 listOpts = []client.ListOption {
401- client .InNamespace (instance . Spec . FRRConfigurationNamespace ), // defaults to metallb-system
427+ client .InNamespace (frrNamespace ),
402428 }
403429 if err := r .List (ctx , frrConfigList , listOpts ... ); err != nil {
404430 return ctrl.Result {}, fmt .Errorf ("unable to retrieve FRRConfigurationList %w" , err )
@@ -462,6 +488,7 @@ func (r *BGPConfigurationReconciler) reconcileNormal(ctx context.Context, instan
462488 map [string ]string {
463489 groupLabel + "/pod-name" : podNetworkDetail .Name ,
464490 }),
491+ frrNamespace ,
465492 )
466493 if err != nil {
467494 return ctrl.Result {}, err
@@ -474,6 +501,13 @@ func (r *BGPConfigurationReconciler) reconcileNormal(ctx context.Context, instan
474501 return ctrl.Result {}, err
475502 }
476503
504+ // Clean up old FRRConfigurations from the old namespace if we've migrated to a new one
505+ if frrNamespace != instance .Spec .FRRConfigurationNamespace {
506+ if err := r .cleanupOldNamespaceFRRConfigurations (ctx , instance , instance .Spec .FRRConfigurationNamespace ); err != nil {
507+ Log .Error (err , "Failed to cleanup old FRRConfigurations from namespace" , "namespace" , instance .Spec .FRRConfigurationNamespace )
508+ }
509+ }
510+
477511 Log .Info ("Reconciled Service successfully" )
478512 return ctrl.Result {}, nil
479513}
@@ -500,6 +534,30 @@ func (r *BGPConfigurationReconciler) deleteStaleFRRConfigurations(ctx context.Co
500534 return nil
501535}
502536
537+ func (r * BGPConfigurationReconciler ) cleanupOldNamespaceFRRConfigurations (
538+ ctx context.Context ,
539+ instance * networkv1.BGPConfiguration ,
540+ oldNamespace string ,
541+ ) error {
542+ Log := r .GetLogger (ctx )
543+ Log .Info ("Cleaning up old FRRConfigurations" , "namespace" , oldNamespace )
544+
545+ err := r .DeleteAllOf (
546+ ctx ,
547+ & frrk8sv1.FRRConfiguration {},
548+ client .InNamespace (oldNamespace ),
549+ client.MatchingLabels {
550+ labels .GetOwnerNameLabelSelector (labels .GetGroupLabel ("bgpconfiguration" )): instance .Name ,
551+ labels .GetOwnerNameSpaceLabelSelector (labels .GetGroupLabel ("bgpconfiguration" )): instance .Namespace ,
552+ },
553+ )
554+ if err != nil && ! k8s_errors .IsNotFound (err ) {
555+ return fmt .Errorf ("error DeleteAllOf FRRConfiguration in old namespace %s: %w" , oldNamespace , err )
556+ }
557+
558+ return nil
559+ }
560+
503561// getPodNetworkDetails - returns the podDetails for a list of pods in status.phase: Running
504562// where the pod has the multus k8s_networkv1.NetworkAttachmentAnnot annotation
505563// and its value is not '[]' OR has a predictableip label
@@ -650,6 +708,7 @@ func (r *BGPConfigurationReconciler) createOrPatchFRRConfiguration(
650708 podDtl * bgp.PodDetail ,
651709 nodeFRRCfgs map [string ]frrk8sv1.FRRConfiguration ,
652710 frrLabels map [string ]string ,
711+ frrNamespace string ,
653712) error {
654713 Log := r .GetLogger (ctx )
655714 Log .Info ("Reconciling createOrUpdateFRRConfiguration" )
@@ -662,7 +721,7 @@ func (r *BGPConfigurationReconciler) createOrPatchFRRConfiguration(
662721 frrConfig := & frrk8sv1.FRRConfiguration {
663722 ObjectMeta : metav1.ObjectMeta {
664723 Name : instance .Namespace + "-" + podDtl .Name ,
665- Namespace : instance . Spec . FRRConfigurationNamespace ,
724+ Namespace : frrNamespace ,
666725 },
667726 }
668727
0 commit comments