Skip to content

Commit

Permalink
Update network spec with no peering claim refs (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasabe28 authored Aug 30, 2024
1 parent 032223a commit 29e085e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
8 changes: 3 additions & 5 deletions internal/controllers/networking/network_peering_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
96 changes: 96 additions & 0 deletions internal/controllers/networking/network_peering_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})

0 comments on commit 29e085e

Please sign in to comment.