diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 0803d21..f7f3332 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -1,4 +1,4 @@ -// Copyright (c) 2021-2022 Nordix Foundation. +// Copyright (c) 2021-2023 Nordix Foundation. // // SPDX-License-Identifier: Apache-2.0 // @@ -25,6 +25,8 @@ import ( "github.com/kelseyhightower/envconfig" "github.com/pkg/errors" + + "github.com/networkservicemesh/sdk/pkg/tools/cidr" ) const ( @@ -42,7 +44,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"` diff --git a/internal/pkg/imports/imports_linux.go b/internal/pkg/imports/imports_linux.go index c53d0fe..b930af6 100644 --- a/internal/pkg/imports/imports_linux.go +++ b/internal/pkg/imports/imports_linux.go @@ -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" diff --git a/main.go b/main.go index 500c4c4..dbaf786 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,5 @@ -// Copyright (c) 2021-2022 Doc.ai and/or its affiliates. -// Copyright (c) 2021-2022 Nordix and/or its affiliates. +// Copyright (c) 2021-2023 Doc.ai and/or its affiliates. +// Copyright (c) 2021-2023 Nordix and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -26,7 +26,6 @@ import ( "net/url" "os" "os/signal" - "strings" "syscall" "time" @@ -48,7 +47,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" @@ -97,10 +96,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() @@ -112,6 +110,10 @@ func main() { logrus.Fatal(err.Error()) } + if len(cfg.CidrPrefix) != 1 { + logrus.Fatal("Only one CIDR prefix group expected") + } + l, errLog := logrus.ParseLevel(cfg.LogLevel) if errLog != nil { logrus.Fatalf("invalid log level %s", cfg.LogLevel) @@ -154,22 +156,14 @@ 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), @@ -177,7 +171,7 @@ func main() { 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( @@ -200,7 +194,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( @@ -314,16 +308,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...) -}