diff --git a/internal/controllers/networking/network_peering_controller.go b/internal/controllers/networking/network_peering_controller.go index 8ef4ce795..55be6fc59 100644 --- a/internal/controllers/networking/network_peering_controller.go +++ b/internal/controllers/networking/network_peering_controller.go @@ -60,11 +60,9 @@ func (r *NetworkPeeringReconciler) reconcile(ctx context.Context, log logr.Logge } } - if len(peeringClaimRefs) > 0 { - log.V(1).Info("Peering claim refs require network spec update") - if err := r.updateSpec(ctx, log, network, peeringClaimRefs); err != nil { - return ctrl.Result{}, fmt.Errorf("error updating network spec: %w", err) - } + log.V(1).Info("Peering claim refs require network spec update") + if err := r.updateSpec(ctx, log, network, peeringClaimRefs); err != nil { + return ctrl.Result{}, fmt.Errorf("error updating network spec: %w", err) } log.V(1).Info("Reconciled") diff --git a/internal/controllers/networking/network_peering_controller_test.go b/internal/controllers/networking/network_peering_controller_test.go index a1479e8fc..7904c4708 100644 --- a/internal/controllers/networking/network_peering_controller_test.go +++ b/internal/controllers/networking/network_peering_controller_test.go @@ -577,4 +577,100 @@ var _ = Describe("NetworkPeeringController", func() { Eventually(Get(network1)).Should(Satisfy(apierrors.IsNotFound)) Eventually(Get(network3)).Should(Satisfy(apierrors.IsNotFound)) }) + + It("should remove incoming peerings if no matching peering found", func(ctx SpecContext) { + By("creating a network network-1") + network1 := &networkingv1alpha1.Network{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: ns.Name, + Name: "network-1", + }, + Spec: networkingv1alpha1.NetworkSpec{ + Peerings: []networkingv1alpha1.NetworkPeering{ + { + Name: "peering-1", + NetworkRef: networkingv1alpha1.NetworkPeeringNetworkRef{ + Name: "network-2", + Namespace: ns.Name, + }, + }, + }, + }, + } + Expect(k8sClient.Create(ctx, network1)).To(Succeed()) + + By("creating a network network-2") + network2 := &networkingv1alpha1.Network{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: ns.Name, + Name: "network-2", + }, + Spec: networkingv1alpha1.NetworkSpec{ + Peerings: []networkingv1alpha1.NetworkPeering{ + { + Name: "peering-1", + NetworkRef: networkingv1alpha1.NetworkPeeringNetworkRef{ + Name: "network-1", + Namespace: ns.Name, + }, + }, + }, + }, + } + Expect(k8sClient.Create(ctx, network2)).To(Succeed()) + + By("patching networks as available") + baseNetwork1 := network1.DeepCopy() + network1.Status.State = networkingv1alpha1.NetworkStateAvailable + Expect(k8sClient.Status().Patch(ctx, network1, client.MergeFrom(baseNetwork1))).To(Succeed()) + + baseNetwork2 := network2.DeepCopy() + network2.Status.State = networkingv1alpha1.NetworkStateAvailable + Expect(k8sClient.Status().Patch(ctx, network2, client.MergeFrom(baseNetwork2))).To(Succeed()) + + By("waiting for networks to reference each other") + Eventually(Object(network1)). + Should(SatisfyAll( + HaveField("Spec.PeeringClaimRefs", ConsistOf(networkingv1alpha1.NetworkPeeringClaimRef{ + Namespace: network2.Namespace, + Name: network2.Name, + UID: network2.UID, + })), + HaveField("Status.State", Equal(networkingv1alpha1.NetworkStateAvailable)), + )) + + Eventually(Object(network2)). + Should(SatisfyAll( + HaveField("Spec.PeeringClaimRefs", ConsistOf(networkingv1alpha1.NetworkPeeringClaimRef{ + Namespace: network1.Namespace, + Name: network1.Name, + UID: network1.UID, + })), + HaveField("Status.State", Equal(networkingv1alpha1.NetworkStateAvailable)), + )) + + By("removing peerings from network-1") + baseNetwork1 = network1.DeepCopy() + network1.Spec.Peerings = []networkingv1alpha1.NetworkPeering{} + Expect(k8sClient.Patch(ctx, network1, client.MergeFrom(baseNetwork1))).To(Succeed()) + + By("ensuring incoming peerings are also removed from network-1 and network-2") + Eventually(Object(network1)).Should(SatisfyAll( + HaveField("Spec.Peerings", BeNil()), + HaveField("Spec.PeeringClaimRefs", BeNil()), + )) + + Eventually(Object(network2)).Should(SatisfyAll( + HaveField("Spec.Peerings", Not(BeNil())), + HaveField("Spec.PeeringClaimRefs", BeNil()), + )) + + By("deleting the networks") + Expect(k8sClient.Delete(ctx, network1)).To(Succeed()) + Expect(k8sClient.Delete(ctx, network2)).To(Succeed()) + + By("waiting for networks to be gone") + Eventually(Get(network1)).Should(Satisfy(apierrors.IsNotFound)) + Eventually(Get(network2)).Should(Satisfy(apierrors.IsNotFound)) + }) })