Skip to content

Commit

Permalink
alternative option to resolve cmd-registry-k8s#376
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Tingaikin <[email protected]>
  • Loading branch information
denis-tingaikin committed Mar 18, 2023
1 parent bfbc757 commit 04f3b6a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
11 changes: 10 additions & 1 deletion pkg/registry/etcd/nse_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -105,6 +105,14 @@ func (n *etcdNSERegistryServer) Find(query *registry.NetworkServiceEndpointQuery
if item.Name == "" {
item.Name = list.Items[i].Name
}
if item.ExpirationTime != nil && item.ExpirationTime.AsTime().Local().Before(time.Now()) {
_ = n.client.NetworkservicemeshV1().NetworkServiceEndpoints(n.ns).Delete(n.chainContext, item.Name, metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{
ResourceVersion: &list.Items[i].ResourceVersion,
},
})
continue
}
if matchutils.MatchNetworkServiceEndpoints(query.NetworkServiceEndpoint, item) {
err := s.Send(&registry.NetworkServiceEndpointResponse{NetworkServiceEndpoint: item})
if err != nil {
Expand Down Expand Up @@ -191,6 +199,7 @@ func (n *etcdNSERegistryServer) handleWatcher(
if event.Type == watch.Deleted {
nseResp.Deleted = true
}

if matchutils.MatchNetworkServiceEndpoints(query.NetworkServiceEndpoint, item) {
err := s.Send(nseResp)
if err != nil {
Expand Down
35 changes: 34 additions & 1 deletion pkg/registry/etcd/nse_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -20,11 +20,14 @@ package etcd_test

import (
"context"
"errors"
"io"
"testing"
"time"

"github.com/networkservicemesh/api/pkg/api/registry"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/networkservicemesh/sdk/pkg/registry/core/adapters"
Expand Down Expand Up @@ -94,3 +97,33 @@ func Test_K8sNSERegistry_Find(t *testing.T) {

require.Equal(t, "nse-1", nseResp.NetworkServiceEndpoint.Name)
}

func Test_K8sNSERegistry_Find_ExpiredNSE(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

var myClientset = fake.NewSimpleClientset()
_, err := myClientset.NetworkservicemeshV1().NetworkServiceEndpoints("ns-1").Create(ctx, &v1.NetworkServiceEndpoint{
ObjectMeta: metav1.ObjectMeta{
Name: "nse-1",
},
Spec: v1.NetworkServiceEndpointSpec{
ExpirationTime: timestamppb.New(time.Now().Add(-time.Hour)),
},
}, metav1.CreateOptions{})
require.NoError(t, err)

c := adapters.NetworkServiceEndpointServerToClient(etcd.NewNetworkServiceEndpointRegistryServer(ctx, "ns-1", myClientset))
stream, err := c.Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Name: "nse-1",
},
})
require.NoError(t, err)

_, err = stream.Recv()
require.True(t, errors.Is(err, io.EOF))
resp, err := myClientset.NetworkservicemeshV1().NetworkServiceEndpoints("ns-1").List(ctx, metav1.ListOptions{})
require.NoError(t, err)
require.Len(t, resp.Items, 0)
}

0 comments on commit 04f3b6a

Please sign in to comment.