Skip to content

Commit 4bfc0b3

Browse files
Merge pull request #592 from lmiccini/bgp-default-openshift-frr-k8s-namespace
Change BGPConfiguration default namespace to openshift-frr-k8s and add legacy cleanup
2 parents a515b63 + 58a81a6 commit 4bfc0b3

6 files changed

Lines changed: 124 additions & 10 deletions

File tree

apis/bases/network.openstack.org_bgpconfigurations.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ spec:
4444
description: BGPConfigurationSpec defines the desired state of BGPConfiguration
4545
properties:
4646
frrConfigurationNamespace:
47-
default: metallb-system
47+
default: openshift-frr-k8s
4848
description: FRRConfigurationNamespace - namespace where to create
49-
the FRRConfiguration. Defaults to metallb-system.
49+
the FRRConfiguration. Defaults to openshift-frr-k8s.
5050
type: string
5151
frrNodeConfigurationSelector:
5252
description: |-

apis/network/v1beta1/bgpconfiguration_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ type FRRNodeConfigurationSelectorType struct {
3535
// BGPConfigurationSpec defines the desired state of BGPConfiguration
3636
type BGPConfigurationSpec struct {
3737
// +kubebuilder:validation:Optional
38-
// +kubebuilder:default="metallb-system"
39-
// FRRConfigurationNamespace - namespace where to create the FRRConfiguration. Defaults to metallb-system.
38+
// +kubebuilder:default="openshift-frr-k8s"
39+
// FRRConfigurationNamespace - namespace where to create the FRRConfiguration. Defaults to openshift-frr-k8s.
4040
FRRConfigurationNamespace string `json:"frrConfigurationNamespace"`
4141

4242
// +kubebuilder:validation:Optional

config/crd/bases/network.openstack.org_bgpconfigurations.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ spec:
4444
description: BGPConfigurationSpec defines the desired state of BGPConfiguration
4545
properties:
4646
frrConfigurationNamespace:
47-
default: metallb-system
47+
default: openshift-frr-k8s
4848
description: FRRConfigurationNamespace - namespace where to create
49-
the FRRConfiguration. Defaults to metallb-system.
49+
the FRRConfiguration. Defaults to openshift-frr-k8s.
5050
type: string
5151
frrNodeConfigurationSelector:
5252
description: |-

internal/bgp/funcs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func GetNodesRunningPods(podNetworkDetailList []PodDetail) []string {
7575
// OpenShiftFRRNamespace is the FRR namespace used in OpenShift 4.20+
7676
const OpenShiftFRRNamespace = "openshift-frr-k8s"
7777

78+
// LegacyMetalLBNamespace is the FRR namespace used before OpenShift 4.20
79+
const LegacyMetalLBNamespace = "metallb-system"
80+
7881
// GetFRRConfigurationNamespace checks whether migrationNamespace exists and returns it,
7982
// otherwise falls back to defaultNamespace.
8083
func GetFRRConfigurationNamespace(ctx context.Context, c client.Client, migrationNamespace, defaultNamespace string) (string, error) {

internal/controller/network/bgpconfiguration_controller.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ import (
5656
// BGPConfigurationReconciler reconciles a BGPConfiguration object
5757
type BGPConfigurationReconciler struct {
5858
client.Client
59-
Kclient kubernetes.Interface
60-
Scheme *runtime.Scheme
61-
FRRMigrationNamespace string
59+
Kclient kubernetes.Interface
60+
Scheme *runtime.Scheme
61+
FRRMigrationNamespace string
62+
LegacyMetalLBNamespace string
6263
}
6364

6465
// GetLogger returns a logger object with a prefix of "controller.name" and additional controller context fields
@@ -73,6 +74,13 @@ func (r *BGPConfigurationReconciler) getFRRMigrationNamespace() string {
7374
return bgp.OpenShiftFRRNamespace
7475
}
7576

77+
func (r *BGPConfigurationReconciler) getLegacyMetalLBNamespace() string {
78+
if r.LegacyMetalLBNamespace != "" {
79+
return r.LegacyMetalLBNamespace
80+
}
81+
return bgp.LegacyMetalLBNamespace
82+
}
83+
7684
//+kubebuilder:rbac:groups=network.openstack.org,resources=bgpconfigurations,verbs=get;list;watch;create;update;patch;delete
7785
//+kubebuilder:rbac:groups=network.openstack.org,resources=bgpconfigurations/status,verbs=get;update;patch
7886
//+kubebuilder:rbac:groups=network.openstack.org,resources=bgpconfigurations/finalizers,verbs=update
@@ -388,6 +396,13 @@ func (r *BGPConfigurationReconciler) reconcileDelete(ctx context.Context, instan
388396
}
389397
}
390398

399+
legacyNS := r.getLegacyMetalLBNamespace()
400+
if frrNamespace != legacyNS {
401+
if err := r.cleanupOldNamespaceFRRConfigurations(ctx, instance, legacyNS); err != nil {
402+
return ctrl.Result{}, fmt.Errorf("error cleaning up FRRConfigurations from legacy namespace %s: %w", legacyNS, err)
403+
}
404+
}
405+
391406
// Service is deleted so remove the finalizer.
392407
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
393408
Log.Info("Reconciled Service delete successfully")
@@ -508,6 +523,14 @@ func (r *BGPConfigurationReconciler) reconcileNormal(ctx context.Context, instan
508523
}
509524
}
510525

526+
// Clean up FRRConfigurations from the legacy metallb-system namespace (pre-OpenShift 4.20)
527+
legacyNS := r.getLegacyMetalLBNamespace()
528+
if frrNamespace != legacyNS {
529+
if err := r.cleanupOldNamespaceFRRConfigurations(ctx, instance, legacyNS); err != nil {
530+
Log.Error(err, "Failed to cleanup legacy FRRConfigurations from namespace", "namespace", legacyNS)
531+
}
532+
}
533+
511534
Log.Info("Reconciled Service successfully")
512535
return ctrl.Result{}, nil
513536
}

test/functional/bgpconfiguration_controller_test.go

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
var _ = Describe("BGPConfiguration controller", func() {
3434
var bgpcfgName types.NamespacedName
3535
var meallbFRRCfgName types.NamespacedName
36-
frrCfgNamespace := "metallb-system"
36+
frrCfgNamespace := "openshift-frr-k8s"
3737

3838
When("a default BGPConfiguration gets created", func() {
3939
BeforeEach(func() {
@@ -720,4 +720,92 @@ var _ = Describe("BGPConfiguration controller", func() {
720720
}, timeout, interval).Should(Succeed())
721721
})
722722
})
723+
724+
When("legacy FRRConfigurations in metallb-system are cleaned up after default namespace change", func() {
725+
var podFrrName types.NamespacedName
726+
var podName types.NamespacedName
727+
var legacyNS *corev1.Namespace
728+
729+
BeforeEach(func() {
730+
// Create the new default namespace (openshift-frr-k8s)
731+
defaultNS := th.CreateNamespace(frrCfgNamespace + "-" + namespace)
732+
733+
defaultFRRCfgName := types.NamespacedName{Namespace: defaultNS.Name, Name: "worker-0"}
734+
defaultFRRCfg := CreateFRRConfiguration(defaultFRRCfgName, GetMetalLBFRRConfigurationSpec("worker-0"))
735+
Expect(defaultFRRCfg).To(Not(BeNil()))
736+
737+
// Create the legacy metallb-system namespace with an owned FRRConfiguration
738+
legacyNS = th.CreateNamespace("metallb-system-" + namespace)
739+
bgpReconciler.LegacyMetalLBNamespace = legacyNS.Name
740+
DeferCleanup(func() {
741+
bgpReconciler.LegacyMetalLBNamespace = ""
742+
})
743+
744+
nad := th.CreateNAD(types.NamespacedName{Namespace: namespace, Name: "internalapi"}, GetNADSpec())
745+
746+
bgpcfg := CreateBGPConfiguration(namespace, GetBGPConfigurationSpec(defaultNS.Name))
747+
bgpcfgName.Name = bgpcfg.GetName()
748+
bgpcfgName.Namespace = bgpcfg.GetNamespace()
749+
750+
podName = types.NamespacedName{Namespace: namespace, Name: uuid.New().String()}
751+
th.CreatePod(podName, GetPodAnnotation(namespace), GetPodSpec("worker-0"))
752+
th.SimulatePodPhaseRunning(podName)
753+
754+
podFrrName.Name = podName.Namespace + "-" + podName.Name
755+
podFrrName.Namespace = defaultNS.Name
756+
757+
DeferCleanup(th.DeleteInstance, bgpcfg)
758+
DeferCleanup(th.DeleteInstance, nad)
759+
DeferCleanup(th.DeleteInstance, defaultFRRCfg)
760+
})
761+
762+
It("should clean up owned FRRConfigurations from the legacy metallb-system namespace", func() {
763+
// Wait for the FRRConfiguration to be created in the new default namespace
764+
Eventually(func(g Gomega) {
765+
frr := GetFRRConfiguration(types.NamespacedName{Namespace: podFrrName.Namespace, Name: podFrrName.Name})
766+
g.Expect(frr).To(Not(BeNil()))
767+
g.Expect(frr.Spec.BGP.Routers[0].Prefixes[0]).To(Equal("172.17.0.40/32"))
768+
}, timeout, interval).Should(Succeed())
769+
770+
// Create an owned FRRConfiguration in the legacy namespace to simulate a pre-upgrade state
771+
legacyFRR := CreateFRRConfiguration(
772+
types.NamespacedName{Namespace: legacyNS.Name, Name: podFrrName.Name},
773+
GetMetalLBFRRConfigurationSpec("worker-0"),
774+
)
775+
Expect(legacyFRR).To(Not(BeNil()))
776+
777+
// Add owner labels matching the BGPConfiguration so cleanup will target it
778+
frr := &frrk8sv1.FRRConfiguration{}
779+
Eventually(func(g Gomega) {
780+
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: legacyNS.Name, Name: podFrrName.Name}, frr)).Should(Succeed())
781+
}, timeout, interval).Should(Succeed())
782+
783+
bgpcfg := GetBGPConfiguration(bgpcfgName)
784+
frr.Labels = map[string]string{
785+
"bgpconfiguration.openstack.org/name": bgpcfg.Name,
786+
"bgpconfiguration.openstack.org/namespace": bgpcfg.Namespace,
787+
}
788+
Expect(k8sClient.Update(ctx, frr)).Should(Succeed())
789+
790+
// Trigger reconcile
791+
pod := th.GetPod(podName)
792+
if pod.Annotations == nil {
793+
pod.Annotations = map[string]string{}
794+
}
795+
pod.Annotations["trigger-reconcile"] = "true"
796+
Eventually(func(g Gomega) {
797+
g.Expect(k8sClient.Update(ctx, pod)).Should(Succeed())
798+
}, timeout, interval).Should(Succeed())
799+
800+
// Verify the legacy FRRConfiguration is cleaned up
801+
Eventually(func(g Gomega) {
802+
err := k8sClient.Get(ctx, types.NamespacedName{
803+
Namespace: legacyNS.Name,
804+
Name: podFrrName.Name,
805+
}, &frrk8sv1.FRRConfiguration{})
806+
g.Expect(err).To(HaveOccurred())
807+
g.Expect(k8s_errors.IsNotFound(err)).To(BeTrue())
808+
}, timeout, interval).Should(Succeed())
809+
})
810+
})
723811
})

0 commit comments

Comments
 (0)