Skip to content

Commit

Permalink
Deprecate NSM_IPV6_PREFIX variable and use NSM_CIDR_PREFIX #12
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Apr 27, 2022
1 parent 7f37f42 commit 701e506
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
3 changes: 1 addition & 2 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ type Config struct {
Name string `default:"vlan-server" desc:"Name of the endpoint"`
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"`
CidrPrefix string `default:"169.254.0.0/16" desc:"CIDR Prefix to assign IPs from" split_words:"true"`
Ipv6Prefix string `default:"" desc:"Ipv6 Prefix for dual-stack" 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"`
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
33 changes: 16 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net"
"net/url"
"os"
"strings"
"os/signal"
"syscall"
"time"
Expand Down Expand Up @@ -148,25 +149,10 @@ func main() {
log.FromContext(ctx).Infof("executing phase 3: parsing network prefixes for ipam")
// ********************************************************************************

_, ipNet1, err := net.ParseCIDR(cfg.CidrPrefix)
if err != nil {
logrus.Fatalf("Could not parse cidr %s; %+v", cfg.CidrPrefix, err)
}
ipamChain := getIPAMChain(ctx, cfg.CidrPrefix)

var ipamChain networkservice.NetworkServiceServer
log.FromContext(ctx).Infof("network prefixes parsed successfully")

if cfg.Ipv6Prefix != "" {
_, ipNet2, parseErr := net.ParseCIDR(cfg.Ipv6Prefix)
if parseErr != nil {
log.FromContext(ctx).Fatalf("error parsing cidr: %+v", err)
}
ipamChain = chain.NewNetworkServiceServer(
singlepointipam.NewServer(ipNet1),
singlepointipam.NewServer(ipNet2),
)
} else {
ipamChain = chain.NewNetworkServiceServer(singlepointipam.NewServer(ipNet1))
}
// ********************************************************************************
logger.Infof("executing phase 4: create network service endpoint")
// ********************************************************************************
Expand Down Expand Up @@ -319,3 +305,16 @@ func getNseEndpoint(listenOn *url.URL, cfg *config.Config) *registryapi.NetworkS
}
return nse
}

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 701e506

Please sign in to comment.