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 d69f584
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 33 deletions.
5 changes: 4 additions & 1 deletion internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// Copyright (c) 2024 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -26,6 +28,7 @@ import (
"time"

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

Expand All @@ -44,7 +47,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
39 changes: 8 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand All @@ -50,7 +49,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 +98,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 +154,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 +192,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 +306,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 d69f584

Please sign in to comment.