Skip to content

Commit

Permalink
Remove duplicated IPAM code for NSE applications
Browse files Browse the repository at this point in the history
NSM issue link: networkservicemesh/sdk#1409

Signed-off-by: Botond Szirtes <botond.szirtes@est.tech>
  • Loading branch information
bszirtes committed Mar 26, 2024
1 parent 7b4e000 commit 5c3c046
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
5 changes: 3 additions & 2 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022 Nordix Foundation.
// Copyright (c) 2021-2024 Nordix Foundation.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
Expand Down Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/kelseyhightower/envconfig"
"github.com/networkservicemesh/sdk/pkg/tools/cidr"
"github.com/pkg/errors"
)

Expand All @@ -44,7 +45,7 @@ type Config struct {
ConnectTo url.URL `default:"nsm-registry-svc:5002" desc:"url of registry service to connect to" split_words:"true"`
MaxTokenLifetime time.Duration `default:"24h" desc:"maximum lifetime of tokens" split_words:"true"`
RegistryClientPolicies []string `default:"etc/nsm/opa/common/.*.rego,etc/nsm/opa/registry/.*.rego,etc/nsm/opa/client/.*.rego" desc:"paths to files and directories that contain registry client policies" split_words:"true"`
CidrPrefix []string `default:"169.254.0.0/16" desc:"CIDR Prefix to assign IPs (IPv4 and/or IPv6) from" split_words:"true"`
CidrPrefix cidr.Groups `default:"169.254.0.0/16" desc:"CIDR Prefix to assign IPs (IPv4 and/or IPv6) from" split_words:"true"`
RegisterService bool `default:"true" desc:"if true then registers network service on startup" split_words:"true"`
ListenOn url.URL `default:"tcp://:5003" desc:"tcp:// url to be listen on. It will be used as public to register NSM" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint"`
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/imports/imports_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/ipam/groupipam"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/ipam/singlepointipam"
_ "github.com/networkservicemesh/sdk/pkg/registry/chains/client"
_ "github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
_ "github.com/networkservicemesh/sdk/pkg/registry/common/clientinfo"
_ "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
_ "github.com/networkservicemesh/sdk/pkg/tools/cidr"
_ "github.com/networkservicemesh/sdk/pkg/tools/debug"
_ "github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
_ "github.com/networkservicemesh/sdk/pkg/tools/listenonurl"
Expand Down
42 changes: 10 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Nordix and/or its affiliates.
//
// Copyright (c) 2021-2024 Nordix and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
Expand Down Expand Up @@ -28,7 +29,6 @@ import (
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand All @@ -50,7 +50,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/ipam/groupipam"
"github.com/networkservicemesh/sdk/pkg/networkservice/ipam/singlepointipam"
registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client"
registryauthorize "github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
Expand Down Expand Up @@ -99,10 +99,9 @@ func main() {
logger.Infof("the phases include:")
logger.Infof("1: get config from environment")
logger.Infof("2: retrieve spiffe svid")
logger.Infof("3: parse network prefixes for ipam")
logger.Infof("4: create network service endpoint")
logger.Infof("5: create grpc server and register the server")
logger.Infof("6: register nse with nsm")
logger.Infof("3: create network service endpoint")
logger.Infof("4: create grpc server and register the server")
logger.Infof("5: register nse with nsm")
logger.Infof("a final success message with start time duration")
starttime := time.Now()

Expand Down Expand Up @@ -156,30 +155,22 @@ func main() {
tlsServerConfig.MinVersion = tls.VersionTLS12

// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 3: parsing network prefixes for ipam")
// ********************************************************************************

ipamChain := getIPAMChain(ctx, cfg.CidrPrefix)

log.FromContext(ctx).Infof("network prefixes parsed successfully")

// ********************************************************************************
logger.Infof("executing phase 4: create network service endpoint")
logger.Infof("executing phase 3: create network service endpoint")
// ********************************************************************************
responderEndpoint := endpoint.NewServer(ctx,
spiffejwt.TokenGeneratorFunc(source, cfg.MaxTokenLifetime),
endpoint.WithName(cfg.Name),
endpoint.WithAuthorizeServer(authorize.NewServer()),
endpoint.WithAdditionalFunctionality(
ipamChain,
groupipam.NewServer(cfg.CidrPrefix, groupipam.WithCustomIPAMServer(singlepointipam.NewServer)),
recvfd.NewServer(),
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{
vlanmech.MECHANISM: vlanmapserver.NewServer(cfg),
}),
sendfd.NewServer()))

// ********************************************************************************
logger.Infof("executing phase 5: create grpc server and register the server")
logger.Infof("executing phase 4: create grpc server and register the server")
// ********************************************************************************
serverCreds := grpc.Creds(
grpcfd.TransportCredentials(
Expand All @@ -202,7 +193,7 @@ func main() {
logger.Infof("grpc server started")

// ********************************************************************************
logger.Infof("executing phase 6: register nse with nsm")
logger.Infof("executing phase 5: register nse with nsm")
// ********************************************************************************

clientOptions := append(
Expand Down Expand Up @@ -316,16 +307,3 @@ func genPublishableURL(listenOn *url.URL, logger log.Logger) *url.URL {
}
return listenonurl.GetPublicURL(addrs, listenOn)
}

func getIPAMChain(ctx context.Context, cIDRs []string) networkservice.NetworkServiceServer {
var ipamchain []networkservice.NetworkServiceServer
for _, cidr := range cIDRs {
var parseErr error
_, ipNet, parseErr := net.ParseCIDR(strings.TrimSpace(cidr))
if parseErr != nil {
log.FromContext(ctx).Fatalf("Could not parse CIDR %s; %+v", cidr, parseErr)
}
ipamchain = append(ipamchain, singlepointipam.NewServer(ipNet))
}
return chain.NewNetworkServiceServer(ipamchain...)
}

0 comments on commit 5c3c046

Please sign in to comment.