Skip to content

Commit

Permalink
update network controller in apinetlet to handle peerings
Browse files Browse the repository at this point in the history
  • Loading branch information
kasabe28 committed Mar 26, 2024
1 parent 20b0e5f commit 701c85a
Show file tree
Hide file tree
Showing 19 changed files with 417 additions and 1,094 deletions.
24 changes: 24 additions & 0 deletions api/core/v1alpha1/network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ type NetworkSpec struct {
}

type NetworkStatus struct {
// Peerings contains the states of the network peerings for the network.
Peerings []NetworkPeeringStatus `json:"peerings,omitempty"`
}

// NetworkState is the state of a network.
// +enum
type NetworkState string

// NetworkPeeringState is the state a NetworkPeering can be in
type NetworkPeeringState string

const (
// NetworkPeeringStatePending signals that the network peering is not applied.
NetworkPeeringStatePending NetworkPeeringState = "Pending"
// NetworkPeeringStateApplied signals that the network peering is applied.
NetworkPeeringStateApplied NetworkPeeringState = "Applied"
)

// NetworkPeeringStatus is the status of a network peering.
type NetworkPeeringStatus struct {
// Name is the name of the network peering.
Name string `json:"name"`
// State represents the network peering state
State NetworkPeeringState `json:"state,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
23 changes: 22 additions & 1 deletion api/core/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions apinetlet/controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,6 @@ func SetupTest(apiNetNamespace *corev1.Namespace) *corev1.Namespace {
APINetNamespace: apiNetNamespace.Name,
}).SetupWithManager(k8sManager, k8sManager.GetCache())).To(Succeed())

Expect((&NetworkPeeringReconciler{
Client: k8sManager.GetClient(),
APINetClient: k8sManager.GetClient(),
APINetNamespace: apiNetNamespace.Name,
}).SetupWithManager(k8sManager, k8sManager.GetCache())).To(Succeed())

Expect((&NetworkInterfaceReconciler{
Client: k8sManager.GetClient(),
APINetClient: k8sManager.GetClient(),
Expand Down
22 changes: 22 additions & 0 deletions apinetlet/controllers/network_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ func (r *NetworkReconciler) applyAPINetNetwork(ctx context.Context, log logr.Log
},
}

var peeredIDs []string
for _, peeringClaimRef := range network.Spec.PeeringClaimRefs {
log.V(1).Info("Get apinet network for target network")
targetApinetNetwork := &apinetv1alpha1.Network{}
if err := r.APINetClient.Get(ctx, client.ObjectKey{Namespace: r.APINetNamespace, Name: string(peeringClaimRef.UID)}, targetApinetNetwork); err != nil {
log.V(1).Info("target apinet network is not created yet")
break
}
peeredIDs = append(peeredIDs, targetApinetNetwork.Spec.ID)
}
apiNetNetwork.Spec.PeeredIDs = peeredIDs

var peerings []apinetv1alpha1.NetworkPeeringStatus
for _, peering := range network.Status.Peerings {
peerings = append(peerings, apinetv1alpha1.NetworkPeeringStatus{
Name: peering.Name,
// TODO remove below comment after merging ironcore PR #1026
// State: peering.State,
})
}
apiNetNetwork.Status.Peerings = peerings

log.V(1).Info("Applying APINet network")
if err := r.APINetClient.Patch(ctx, apiNetNetwork, client.Apply, fieldOwner, client.ForceOwnership); err != nil {
return nil, fmt.Errorf("error applying apinet network: %w", err)
Expand Down
123 changes: 119 additions & 4 deletions apinetlet/controllers/network_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package controllers

import (
"github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
apinetletclient "github.com/ironcore-dev/ironcore-net/apinetlet/client"
"github.com/ironcore-dev/ironcore-net/apinetlet/provider"
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
Expand All @@ -19,6 +19,7 @@ import (

var _ = Describe("NetworkController", func() {
ns := SetupNamespace(&k8sClient)
ns1 := SetupNamespace(&k8sClient)
apiNetNs := SetupNamespace(&k8sClient)
SetupTest(apiNetNs)

Expand All @@ -33,7 +34,7 @@ var _ = Describe("NetworkController", func() {
Expect(k8sClient.Create(ctx, network)).To(Succeed())

By("waiting for the corresponding APINet network to be created")
apiNetNetwork := &v1alpha1.Network{
apiNetNetwork := &apinetv1alpha1.Network{
ObjectMeta: metav1.ObjectMeta{
Namespace: apiNetNs.Name,
Name: string(network.UID),
Expand Down Expand Up @@ -71,7 +72,7 @@ var _ = Describe("NetworkController", func() {

It("should clean up dangling apinet networks", func(ctx SpecContext) {
By("creating a apinet network")
apiNetNetwork := &v1alpha1.Network{
apiNetNetwork := &apinetv1alpha1.Network{
ObjectMeta: metav1.ObjectMeta{
Namespace: apiNetNs.Name,
GenerateName: "apinet-network-",
Expand All @@ -85,11 +86,125 @@ var _ = Describe("NetworkController", func() {
},
),
},
Spec: v1alpha1.NetworkSpec{},
Spec: apinetv1alpha1.NetworkSpec{},
}
Expect(k8sClient.Create(ctx, apiNetNetwork)).To(Succeed())

By("waiting for the apinet network to be gone")
Eventually(Get(apiNetNetwork)).Should(Satisfy(apierrors.IsNotFound))
})

It("should update peeredIDs if two networks from different namespaces peers each other correctly", 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: ns1.Name,
},
},
},
},
}
Expect(k8sClient.Create(ctx, network1)).To(Succeed())

By("creating a network network-2")
network2 := &networkingv1alpha1.Network{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns1.Name,
Name: "network-2",
},
Spec: networkingv1alpha1.NetworkSpec{
Peerings: []networkingv1alpha1.NetworkPeering{
{
Name: "peering-2",
NetworkRef: networkingv1alpha1.NetworkPeeringNetworkRef{
Name: "network-1",
Namespace: ns.Name,
},
},
},
},
}
Expect(k8sClient.Create(ctx, network2)).To(Succeed())

By("waiting for the corresponding APINet networks to be created")
apiNetNetwork1 := &apinetv1alpha1.Network{
ObjectMeta: metav1.ObjectMeta{
Namespace: apiNetNs.Name,
Name: string(network1.UID),
},
}
Eventually(Get(apiNetNetwork1)).Should(Succeed())

apiNetNetwork2 := &apinetv1alpha1.Network{
ObjectMeta: metav1.ObjectMeta{
Namespace: apiNetNs.Name,
Name: string(network2.UID),
},
}
Eventually(Get(apiNetNetwork2)).Should(Succeed())

By("inspecting the created apinet networks")
Expect(apiNetNetwork1.Labels).To(Equal(apinetletclient.SourceLabels(k8sClient.Scheme(), k8sClient.RESTMapper(), network1)))
Expect(apiNetNetwork1.Spec.ID).NotTo(BeEmpty())

Expect(apiNetNetwork2.Labels).To(Equal(apinetletclient.SourceLabels(k8sClient.Scheme(), k8sClient.RESTMapper(), network2)))
Expect(apiNetNetwork2.Spec.ID).NotTo(BeEmpty())

By("patching networks with peeringClaimRefs")
baseNetwork1 := network1.DeepCopy()
network1.Spec.PeeringClaimRefs = []networkingv1alpha1.NetworkPeeringClaimRef{{
Name: network2.Name,
Namespace: network2.Namespace,
UID: network2.UID,
}}
network1.Status.Peerings = []networkingv1alpha1.NetworkPeeringStatus{{
Name: network1.Spec.Peerings[0].Name,
// TODO remove below comment after merging ironcore PR #1026
// State: networkingv1alpha1.NetworkPeeringStatePending,
}}
Expect(k8sClient.Patch(ctx, network1, client.MergeFrom(baseNetwork1))).To(Succeed())

baseNetwork2 := network2.DeepCopy()
network2.Spec.PeeringClaimRefs = []networkingv1alpha1.NetworkPeeringClaimRef{{
Name: network1.Name,
Namespace: network1.Namespace,
UID: network1.UID,
}}
network2.Status.Peerings = []networkingv1alpha1.NetworkPeeringStatus{{
Name: network2.Spec.Peerings[0].Name,
// TODO remove below comment after merging ironcore PR #1026
// State: networkingv1alpha1.NetworkPeeringStatePending,
}}
Expect(k8sClient.Patch(ctx, network2, client.MergeFrom(baseNetwork2))).To(Succeed())

By("ensuring apinet network peeredIDs are updated")
Eventually(Object(apiNetNetwork1)).Should(SatisfyAll(
HaveField("Spec.PeeredIDs", ConsistOf(apiNetNetwork2.Spec.ID)),
))

Eventually(Object(apiNetNetwork2)).Should(SatisfyAll(
HaveField("Spec.PeeredIDs", ConsistOf(apiNetNetwork1.Spec.ID)),
))

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))

By("asserting the corresponding apinet network is gone as well")
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(apiNetNetwork1), apiNetNetwork1)).To(Satisfy(apierrors.IsNotFound))
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(apiNetNetwork2), apiNetNetwork2)).To(Satisfy(apierrors.IsNotFound))
})
})
Loading

0 comments on commit 701c85a

Please sign in to comment.