Skip to content

Commit

Permalink
Increase the idempotence of the tunnel endpoint creator
Browse files Browse the repository at this point in the history
  • Loading branch information
giorio94 authored and adamjensenbot committed Sep 23, 2021
1 parent 63e4df4 commit 392024e
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 202 deletions.
1 change: 0 additions & 1 deletion cmd/liqonet/network-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func runNetworkManager(commonFlags *liqonetCommonFlags, managerFlags *networkMan
tec := &tunnelendpointcreator.TunnelEndpointCreator{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
DynClient: dynClient,
IPManager: ipam,
}

Expand Down
8 changes: 0 additions & 8 deletions deployments/liqo/files/liqo-network-manager-ClusterRole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,4 @@ rules:
- patch
- update
- watch
- apiGroups:
- net.liqo.io
resources:
- tunnelendpoints/status
verbs:
- get
- patch
- update

22 changes: 22 additions & 0 deletions internal/liqonet/network-manager/netcfgcreator/networkconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ func GetLocalNetworkConfig(ctx context.Context, c client.Client, clusterID, name
}
}

// GetRemoteNetworkConfig returns the remote NetworkConfig associated with a given cluster ID.
func GetRemoteNetworkConfig(ctx context.Context, c client.Client, clusterID, namespace string) (*netv1alpha1.NetworkConfig, error) {
networkConfigList := &netv1alpha1.NetworkConfigList{}
labels := client.MatchingLabels{crdreplicator.RemoteLabelSelector: clusterID}

if err := c.List(ctx, networkConfigList, labels, client.InNamespace(namespace)); err != nil {
klog.Errorf("An error occurred while listing NetworkConfigs: %v", err)
return nil, err
}

switch len(networkConfigList.Items) {
case 0:
return nil, kerrors.NewNotFound(netv1alpha1.NetworkConfigGroupResource,
fmt.Sprintf("Remote NetworkConfig for cluster: %v", clusterID))
case 1:
return &networkConfigList.Items[0], nil
default:
// Multiple NetworkConfigs for the same cluster have been detected.
return nil, fmt.Errorf("found multiple instances of remote NetworkConfigs for remote cluster %v", clusterID)
}
}

// filterDuplicateNetworkConfig filters a list of NetworkConfigs, and selects the duplicated to be deleted.
func filterDuplicateNetworkConfig(items []netv1alpha1.NetworkConfig) (netcfg *netv1alpha1.NetworkConfig, duplicates []netv1alpha1.NetworkConfig) {
// Sort the elements by creation timestamp and, if equal, by UID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,52 @@ var _ = Describe("Network config functions", func() {
})
})

Describe("The GetRemoteNetworkConfig function", func() {
var (
netcfg *netv1alpha1.NetworkConfig
err error
)

JustBeforeEach(func() {
netcfg, err = GetRemoteNetworkConfig(ctx, fcw.Client, clusterID, namespace)
})

When("the network config with the given cluster ID does not exist", func() {
It("should return a not found error", func() {
Expect(err).To(HaveOccurred())
Expect(kerrors.IsNotFound(err)).To(BeTrue())
})
It("should return a nil network config", func() { Expect(netcfg).To(BeNil()) })
})

When("the network config with the given cluster ID does exist", func() {
var existing *netv1alpha1.NetworkConfig

BeforeEach(func() {
existing = &netv1alpha1.NetworkConfig{ObjectMeta: metav1.ObjectMeta{
Name: "foo", Namespace: namespace, Labels: map[string]string{crdreplicator.RemoteLabelSelector: clusterID},
}}
clientBuilder.WithObjects(existing)
})

It("should succeed", func() { Expect(err).ToNot(HaveOccurred()) })
It("should return the expected network config", func() { Expect(netcfg).To(Equal(existing)) })
})

When("two network configs with the given cluster ID do exist", func() {
BeforeEach(func() {
clientBuilder.WithObjects(&netv1alpha1.NetworkConfig{ObjectMeta: metav1.ObjectMeta{
Name: "foo", Namespace: namespace, Labels: map[string]string{crdreplicator.RemoteLabelSelector: clusterID},
}}, &netv1alpha1.NetworkConfig{ObjectMeta: metav1.ObjectMeta{
Name: "bar", Namespace: namespace, Labels: map[string]string{crdreplicator.RemoteLabelSelector: clusterID},
}})
})

It("should fail with an error", func() { Expect(err).To(HaveOccurred()) })
It("should return a nil network config", func() { Expect(netcfg).To(BeNil()) })
})
})

Describe("The Enforce* functions", func() {
var (
fc *discoveryv1alpha1.ForeignCluster
Expand Down

0 comments on commit 392024e

Please sign in to comment.