Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase the idempotence of the tunnel endpoint creator #897

Merged
merged 1 commit into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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