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

Change nse URL in localbypass if nse was restarted #1225

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/registry/common/localbypass/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewNetworkServiceEndpointRegistryServer(nsmgrURL string) registry.NetworkSe

func (s *localBypassNSEServer) Register(ctx context.Context, nse *registry.NetworkServiceEndpoint) (reg *registry.NetworkServiceEndpoint, err error) {
u, loaded := s.nseURLs.Load(nse.Name)
if !loaded {
if !loaded || u.String() != nse.Url {
u, err = url.Parse(nse.Url)
if err != nil {
return nil, errors.Wrapf(err, "cannot register NSE with passed URL: %s", nse.Url)
Expand Down
55 changes: 54 additions & 1 deletion pkg/registry/common/localbypass/server_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -157,6 +157,59 @@ func TestLocalBypassNSEServer_Restart(t *testing.T) {
require.Equal(t, nseURL, nseResp.NetworkServiceEndpoint.Url)
}

func TestLocalBypassNSEServer_RegisterNSESameName(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

mem := memory.NewNetworkServiceEndpointRegistryServer()

server := next.NewNetworkServiceEndpointRegistryServer(
localbypass.NewNetworkServiceEndpointRegistryServer(nsmgrURL),
mem,
)

// 1. Register
nse, err := server.Register(ctx, &registry.NetworkServiceEndpoint{
Name: "nse",
Url: nseURL,
})
require.NoError(t, err)
require.Equal(t, nseURL, nse.Url)

// 2. NSE restarted with the same name but different URL
nseRestartedURL := "tcp://1.1.1.2"
nse, err = server.Register(ctx, &registry.NetworkServiceEndpoint{
Name: "nse",
Url: nseRestartedURL,
})
require.NoError(t, err)
require.Equal(t, nseRestartedURL, nse.Url)

// 3. Find
stream, err := adapters.NetworkServiceEndpointServerToClient(server).Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint),
})
require.NoError(t, err)

nseResp, err := stream.Recv()
require.NoError(t, err)
require.Equal(t, nseRestartedURL, nseResp.NetworkServiceEndpoint.Url)

// 4. Unregister
_, err = server.Unregister(ctx, nse)
require.NoError(t, err)

stream, err = adapters.NetworkServiceEndpointServerToClient(mem).Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: new(registry.NetworkServiceEndpoint),
})
require.NoError(t, err)

_, err = stream.Recv()
require.Error(t, err)
}

func TestLocalBypassNSEServer_SlowRegistryFind(t *testing.T) {
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())

Expand Down