Skip to content

Commit

Permalink
do not choose endpoints that doesn't match with the network service (#…
Browse files Browse the repository at this point in the history
…1516)

Signed-off-by: Denis Tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin authored Oct 2, 2023
1 parent 256d2cc commit f2a585e
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
98 changes: 98 additions & 0 deletions pkg/networkservice/common/discover/server_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -36,6 +38,7 @@ import (
registryadapters "github.com/networkservicemesh/sdk/pkg/registry/core/adapters"
registrynext "github.com/networkservicemesh/sdk/pkg/registry/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/clienturlctx"
"github.com/networkservicemesh/sdk/pkg/tools/matchutils"
)

const (
Expand Down Expand Up @@ -543,3 +546,98 @@ func TestDiscoverCandidatesServer_NoEndpointOnClose(t *testing.T) {

require.True(t, closed)
}

func Test_Discover_Scale_FromZero_vL3(t *testing.T) {
var ns = &registry.NetworkService{
Name: "ns-1",
Payload: "IP",
Matches: []*registry.Match{
{
Fallthrough: true,
Routes: []*registry.Destination{
{
DestinationSelector: map[string]string{
"app": "nse-vl3-vpp",
"nodeName": "{{.nodeName}}",
},
},
},
},
{
Fallthrough: true,
SourceSelector: map[string]string{
"capability": "vl3",
},
Routes: []*registry.Destination{
{
DestinationSelector: map[string]string{
"capability": "vl3",
},
},
},
},
{
Fallthrough: true,
Routes: []*registry.Destination{
{
DestinationSelector: map[string]string{
"app": "nse-autoscaler",
},
},
},
},
},
}

var nseA = &registry.NetworkServiceEndpoint{
Name: "nse-A",
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"ns-1": {Labels: map[string]string{
"capability": "vl3",
"nodeName": "node-1",
"app": "nse-vl3-vpp",
}},
},
}

var nseB = &registry.NetworkServiceEndpoint{
Name: "nse-B",
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"ns-1": {Labels: map[string]string{
"capability": "vl3",
"nodeName": "node-2",
"app": "nse-vl3-vpp",
}},
},
}

var nseC = &registry.NetworkServiceEndpoint{
Name: "nse-C",
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"ns-1": {Labels: map[string]string{
"app": "nse-autoscaler",
}},
},
}

var nseD = &registry.NetworkServiceEndpoint{
Name: "nse-D",
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"ns-1": {Labels: map[string]string{
"app": "unknown",
}},
},
}

require.Empty(t, matchutils.MatchEndpoint(map[string]string{}, ns, nseD), "there are no matches for nse-D")
require.Empty(t, matchutils.MatchEndpoint(map[string]string{"app": "nse-autoscaler"}, ns, nseD), "there are no matches for nse-D")

require.Equal(t, []*registry.NetworkServiceEndpoint{nseC}, matchutils.MatchEndpoint(map[string]string{}, ns, nseC, nseD), "by match №3")
require.Equal(t, []*registry.NetworkServiceEndpoint{nseC}, matchutils.MatchEndpoint(map[string]string{"app": "nse-autoscaler"}, ns, nseC, nseD), "by match №3")

require.Equal(t, []*registry.NetworkServiceEndpoint{nseB}, matchutils.MatchEndpoint(map[string]string{"app": "nse-vl3-vpp", "nodeName": "node-2"}, ns, nseC, nseA, nseB, nseD), "by match №1")
require.Equal(t, []*registry.NetworkServiceEndpoint{nseA}, matchutils.MatchEndpoint(map[string]string{"app": "nse-vl3-vpp", "nodeName": "node-1"}, ns, nseC, nseA, nseD, nseB), "by match №1")

require.Equal(t, []*registry.NetworkServiceEndpoint{nseA}, matchutils.MatchEndpoint(map[string]string{"capability": "vl3", "app": "nse-vl3-vpp", "nodeName": "node-2"}, ns, nseC, nseA, nseD), "by match №2")
require.Equal(t, []*registry.NetworkServiceEndpoint{nseB}, matchutils.MatchEndpoint(map[string]string{"capability": "vl3", "app": "nse-vl3-vpp", "nodeName": "node-1"}, ns, nseC, nseD, nseB), "by match №2")
}
7 changes: 5 additions & 2 deletions pkg/tools/matchutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
// MatchEndpoint filters input nses by network service configuration.
// Returns the same nses list if no matches are declared in the network service.
func MatchEndpoint(nsLabels map[string]string, ns *registry.NetworkService, nses ...*registry.NetworkServiceEndpoint) []*registry.NetworkServiceEndpoint {
if len(ns.Matches) == 0 {
return nses
}
for _, match := range ns.GetMatches() {
// All match source selector labels should be present in the requested labels map
if !IsSubset(nsLabels, match.GetSourceSelector(), nsLabels) {
Expand All @@ -60,13 +63,13 @@ func MatchEndpoint(nsLabels map[string]string, ns *registry.NetworkService, nses
}

if match.GetMetadata() != nil && len(match.Routes) == 0 && len(nseCandidates) == 0 {
break
return nses
}

return nseCandidates
}

return nses
return nil
}

// MatchNetworkServices returns true if two network services are matched
Expand Down

0 comments on commit f2a585e

Please sign in to comment.