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

Add defaultexpire chain element #1405

Closed
Closed
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: 2 additions & 0 deletions pkg/networkservice/chains/nsmgr/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
registryclientinfo "github.com/networkservicemesh/sdk/pkg/registry/common/clientinfo"
"github.com/networkservicemesh/sdk/pkg/registry/common/clienturl"
registryconnect "github.com/networkservicemesh/sdk/pkg/registry/common/connect"
"github.com/networkservicemesh/sdk/pkg/registry/common/defaultexpire"
"github.com/networkservicemesh/sdk/pkg/registry/common/dial"
"github.com/networkservicemesh/sdk/pkg/registry/common/expire"
"github.com/networkservicemesh/sdk/pkg/registry/common/grpcmetadata"
Expand Down Expand Up @@ -276,6 +277,7 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, options
opts.authorizeNSERegistryServer,
begin.NewNetworkServiceEndpointRegistryServer(),
registryclientinfo.NewNetworkServiceEndpointRegistryServer(),
defaultexpire.NewNetworkServiceEndpointRegistryServer(ctx, time.Minute),
expire.NewNetworkServiceEndpointRegistryServer(ctx),
registryrecvfd.NewNetworkServiceEndpointRegistryServer(), // Allow to receive a passed files
registrysendfd.NewNetworkServiceEndpointRegistryServer(),
Expand Down
2 changes: 2 additions & 0 deletions pkg/networkservice/chains/nsmgr/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ func Test_FailedRegistryAuthorization(t *testing.T) {
registrySupplier := func(
ctx context.Context,
tokenGenerator token.GeneratorFunc,
expiryDuration time.Duration,
proxyRegistryURL *url.URL,
options ...grpc.DialOption) registry.Registry {
registryName := sandbox.UniqueName("registry-memory")
Expand Down Expand Up @@ -688,6 +689,7 @@ func Test_Expire(t *testing.T) {
registrySupplier := func(
ctx context.Context,
tokenGenerator token.GeneratorFunc,
expiryDuration time.Duration,
proxyRegistryURL *url.URL,
options ...grpc.DialOption) registry.Registry {
return memory.NewServer(
Expand Down
12 changes: 12 additions & 0 deletions pkg/registry/chains/memory/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package memory
import (
"context"
"net/url"
"time"

"google.golang.org/grpc"

Expand All @@ -34,6 +35,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/registry/common/clientconn"
"github.com/networkservicemesh/sdk/pkg/registry/common/clienturl"
"github.com/networkservicemesh/sdk/pkg/registry/common/connect"
"github.com/networkservicemesh/sdk/pkg/registry/common/defaultexpire"
"github.com/networkservicemesh/sdk/pkg/registry/common/dial"
"github.com/networkservicemesh/sdk/pkg/registry/common/expire"
"github.com/networkservicemesh/sdk/pkg/registry/common/memory"
Expand All @@ -50,6 +52,7 @@ type serverOptions struct {
authorizeNSERegistryServer registry.NetworkServiceEndpointRegistryServer
authorizeNSRegistryClient registry.NetworkServiceRegistryClient
authorizeNSERegistryClient registry.NetworkServiceEndpointRegistryClient
defaultExpireDuration time.Duration
proxyRegistryURL *url.URL
dialOptions []grpc.DialOption
}
Expand Down Expand Up @@ -97,6 +100,13 @@ func WithAuthorizeNSERegistryClient(authorizeNSERegistryClient registry.NetworkS
}
}

// WithDefaultExpireDuration sets a default expire duration for the server
func WithDefaultExpireDuration(expireDuration time.Duration) Option {
return func(o *serverOptions) {
o.defaultExpireDuration = expireDuration
}
}

// WithProxyRegistryURL sets URL to reach the proxy registry
func WithProxyRegistryURL(proxyRegistryURL *url.URL) Option {
return func(o *serverOptions) {
Expand All @@ -118,6 +128,7 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, options
authorizeNSERegistryServer: registryauthorize.NewNetworkServiceEndpointRegistryServer(registryauthorize.Any()),
authorizeNSRegistryClient: registryauthorize.NewNetworkServiceRegistryClient(registryauthorize.Any()),
authorizeNSERegistryClient: registryauthorize.NewNetworkServiceEndpointRegistryClient(registryauthorize.Any()),
defaultExpireDuration: time.Minute,
proxyRegistryURL: nil,
}
for _, opt := range options {
Expand Down Expand Up @@ -161,6 +172,7 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, options
Condition: func(c context.Context, nse *registry.NetworkServiceEndpoint) bool { return true },
Action: chain.NewNetworkServiceEndpointRegistryServer(
setregistrationtime.NewNetworkServiceEndpointRegistryServer(),
defaultexpire.NewNetworkServiceEndpointRegistryServer(ctx, time.Minute),
expire.NewNetworkServiceEndpointRegistryServer(ctx),
memory.NewNetworkServiceEndpointRegistryServer(),
),
Expand Down
18 changes: 18 additions & 0 deletions pkg/registry/common/defaultexpire/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package defaultexpire provides registry server chain elements for setting the default ExpirationTime if it has not been set.
package defaultexpire
61 changes: 61 additions & 0 deletions pkg/registry/common/defaultexpire/nse_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package defaultexpire

import (
"context"
"time"

"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/clock"
"github.com/networkservicemesh/sdk/pkg/tools/log"
)

type defaultexpireNSEServer struct {
ctx context.Context
defaultNSEExpiration time.Duration
}

// NewNetworkServiceEndpointRegistryServer creates a new NetworkServiceServer chain element that sets the default
// expiration time.
func NewNetworkServiceEndpointRegistryServer(ctx context.Context, defaultNSEExpiration time.Duration) registry.NetworkServiceEndpointRegistryServer {
return &defaultexpireNSEServer{
ctx: ctx,
defaultNSEExpiration: defaultNSEExpiration,
}
}

func (s *defaultexpireNSEServer) Register(ctx context.Context, nse *registry.NetworkServiceEndpoint) (*registry.NetworkServiceEndpoint, error) {
if nse.GetExpirationTime() == nil {
nse.ExpirationTime = timestamppb.New(clock.FromContext(ctx).Now().Add(s.defaultNSEExpiration).Local())
log.FromContext(ctx).Infof("default expiration time %v was set for %v", s.defaultNSEExpiration, nse.GetName())
}
return next.NetworkServiceEndpointRegistryServer(ctx).Register(ctx, nse)
}

func (s *defaultexpireNSEServer) Find(query *registry.NetworkServiceEndpointQuery, server registry.NetworkServiceEndpointRegistry_FindServer) error {
return next.NetworkServiceEndpointRegistryServer(server.Context()).Find(query, server)
}

func (s *defaultexpireNSEServer) Unregister(ctx context.Context, nse *registry.NetworkServiceEndpoint) (*empty.Empty, error) {
return next.NetworkServiceEndpointRegistryServer(ctx).Unregister(ctx, nse)
}
85 changes: 85 additions & 0 deletions pkg/registry/common/defaultexpire/nse_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package defaultexpire_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/common/defaultexpire"
"github.com/networkservicemesh/sdk/pkg/registry/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/clock"
"github.com/networkservicemesh/sdk/pkg/tools/clockmock"
)

func TestDefaultExpireNSEServer(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

ctx = clock.WithClock(ctx, clockmock.New(ctx))

var samples = []struct {
name string
nse *registry.NetworkServiceEndpoint
}{
{
name: "With NSE expiration",
nse: &registry.NetworkServiceEndpoint{
Name: "nse",
NetworkServiceNames: []string{"ns"},
ExpirationTime: timestamppb.New(clock.FromContext(ctx).Now().Add(time.Minute).Local()),
},
},
{
name: "Without NSE expiration",
nse: &registry.NetworkServiceEndpoint{
Name: "nse",
NetworkServiceNames: []string{"ns"},
},
},
}

for _, sample := range samples {
t.Run(sample.name, func(t *testing.T) {
// nolint:scopelint
testDefaultExpireNSEServer(ctx, t, sample.nse)
})
}
}

func testDefaultExpireNSEServer(ctx context.Context, t *testing.T, nse *registry.NetworkServiceEndpoint) {
s := next.NewNetworkServiceEndpointRegistryServer(
defaultexpire.NewNetworkServiceEndpointRegistryServer(ctx, time.Hour),
)

registeredNSE, err := s.Register(ctx, nse.Clone())
require.NoError(t, err)

if nse.GetExpirationTime() != nil {
require.Equal(t, nse.GetExpirationTime(), registeredNSE.GetExpirationTime())
} else {
require.Equal(t, clock.FromContext(ctx).Now().Local().Add(time.Hour), registeredNSE.GetExpirationTime().AsTime().Local())
}
}
39 changes: 25 additions & 14 deletions pkg/tools/sandbox/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/require"
"google.golang.org/grpc"
Expand Down Expand Up @@ -52,36 +53,39 @@ type Builder struct {
supplyRegistryProxy SupplyRegistryProxyFunc
setupNode SetupNodeFunc

name string
dnsResolver dnsresolve.Resolver
generateTokenFunc token.GeneratorFunc
name string
dnsResolver dnsresolve.Resolver
generateTokenFunc token.GeneratorFunc
defaultRegistryExpiryDuration time.Duration

useUnixSockets bool

domain *Domain
}

func newRegistryMemoryServer(ctx context.Context, tokenGenerator token.GeneratorFunc, proxyRegistryURL *url.URL, options ...grpc.DialOption) registry.Registry {
func newRegistryMemoryServer(ctx context.Context, tokenGenerator token.GeneratorFunc, defaultExpiryDuration time.Duration, proxyRegistryURL *url.URL, options ...grpc.DialOption) registry.Registry {
return memory.NewServer(
ctx,
tokenGenerator,
memory.WithDefaultExpireDuration(defaultExpiryDuration),
memory.WithProxyRegistryURL(proxyRegistryURL),
memory.WithDialOptions(options...))
}

// NewBuilder creates new SandboxBuilder
func NewBuilder(ctx context.Context, t *testing.T) *Builder {
b := &Builder{
t: t,
ctx: ctx,
nodesCount: 1,
supplyNSMgr: nsmgr.NewServer,
supplyNSMgrProxy: nsmgrproxy.NewServer,
supplyRegistry: newRegistryMemoryServer,
supplyRegistryProxy: proxydns.NewServer,
name: "cluster.local",
dnsResolver: NewFakeResolver(),
generateTokenFunc: GenerateTestToken,
t: t,
ctx: ctx,
nodesCount: 1,
supplyNSMgr: nsmgr.NewServer,
supplyNSMgrProxy: nsmgrproxy.NewServer,
supplyRegistry: newRegistryMemoryServer,
supplyRegistryProxy: proxydns.NewServer,
name: "cluster.local",
dnsResolver: NewFakeResolver(),
generateTokenFunc: GenerateTestToken,
defaultRegistryExpiryDuration: time.Minute,
}

b.setupNode = func(ctx context.Context, node *Node, _ int) {
Expand Down Expand Up @@ -147,6 +151,12 @@ func (b *Builder) SetTokenGenerateFunc(f token.GeneratorFunc) *Builder {
return b
}

// SetRegistryDefaultExpiryDuration sets default expire
func (b *Builder) SetRegistryDefaultExpiryDuration(duration time.Duration) *Builder {
b.defaultRegistryExpiryDuration = duration
return b
}

// UseUnixSockets sets 1 node and mark it to use unix socket to listen on.
func (b *Builder) UseUnixSockets() *Builder {
require.NotEqual(b.t, "windows", runtime.GOOS, "Unix sockets are not available for windows")
Expand Down Expand Up @@ -265,6 +275,7 @@ func (b *Builder) newRegistry() *RegistryEntry {
entry.Registry = b.supplyRegistry(
ctx,
b.generateTokenFunc,
b.defaultRegistryExpiryDuration,
nsmgrProxyURL,
DialOptions(WithTokenGenerator(b.generateTokenFunc))...,
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/tools/sandbox/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package sandbox
import (
"context"
"net/url"
"time"

registryapi "github.com/networkservicemesh/api/pkg/api/registry"
"google.golang.org/grpc"
Expand All @@ -40,7 +41,7 @@ type SupplyNSMgrProxyFunc func(ctx context.Context, regURL, proxyURL *url.URL, t
type SupplyNSMgrFunc func(ctx context.Context, tokenGenerator token.GeneratorFunc, options ...nsmgr.Option) nsmgr.Nsmgr

// SupplyRegistryFunc supplies Registry
type SupplyRegistryFunc func(ctx context.Context, tokenGenerator token.GeneratorFunc, proxyRegistryURL *url.URL, options ...grpc.DialOption) registry.Registry
type SupplyRegistryFunc func(ctx context.Context, tokenGenerator token.GeneratorFunc, defaultExpiryDuration time.Duration, proxyRegistryURL *url.URL, options ...grpc.DialOption) registry.Registry

// SupplyRegistryProxyFunc supplies registry proxy
type SupplyRegistryProxyFunc func(ctx context.Context, tokenGenerator token.GeneratorFunc, dnsResolver dnsresolve.Resolver, options ...proxydns.Option) registry.Registry
Expand Down