Skip to content

Commit

Permalink
Use NSM_IPV6_PREFIX variable in case of IPv6 CIDR only (no IPv4) #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 Feb 23, 2022
1 parent 1af5210 commit b255453
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
19 changes: 9 additions & 10 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ const (

// Config holds configuration parameters from environment variables
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"`
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"`

Services []ServiceConfig `default:"" desc:"list of supported services"`
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:"IPv4 CIDR Prefix to assign IPs from" split_words:"true"`
Ipv6Prefix string `default:"" desc:"IPv6 CIDR Prefix to assign IPs 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"`
Services []ServiceConfig `default:"" desc:"list of supported services"`
}

// Process prints and processes env to config
Expand Down
59 changes: 43 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import (
"net"
"net/url"
"os"
"strings"
"time"

nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/edwarnicke/grpcfd"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/workloadapi"
Expand Down Expand Up @@ -135,25 +137,12 @@ func main() {
log.FromContext(ctx).Infof("executing phase 3: parsing network prefixes for ipam")
// ********************************************************************************

_, ipNet1, err := net.ParseCIDR(cfg.CidrPrefix)
ipamChain, err := getIPAMChain(cfg.CidrPrefix, cfg.Ipv6Prefix)
if err != nil {
logrus.Fatalf("Could not parse cidr %s; %+v", cfg.CidrPrefix, err)
logger.Fatalf("error parsing CIDR from config: %v", err.Error())
}
log.FromContext(ctx).Infof("network prefixes parsed successfully")

var ipamChain networkservice.NetworkServiceServer

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 @@ -306,3 +295,41 @@ func getNseEndpoint(listenOn *url.URL, cfg *config.Config) *registryapi.NetworkS
}
return nse
}

func getIPAMChain(ip4CIDR, ip6CIDR string) (networkservice.NetworkServiceServer, error) {
var ipNet1 *net.IPNet
var ipNet2 *net.IPNet

if ip4CIDR != "" {
var parseErr error
_, ipNet1, parseErr = net.ParseCIDR(ip4CIDR)
if parseErr != nil {
logrus.Fatalf("Could not parse IPv4 CIDR %s; %+v", ip4CIDR, parseErr)
}
// accept IPv6 address also for backward compatibility reason
}
if ip6CIDR != "" {
var parseErr error
_, ipNet2, parseErr = net.ParseCIDR(ip6CIDR)
if parseErr != nil {
logrus.Fatalf("Could not parse IPv6 CIDR: %s; %+v", ip6CIDR, parseErr)
}
if !strings.Contains(ip6CIDR, ":") {
return nil, errors.New("not ipv6 address is set")
}
}

switch {
case ipNet1 != nil && ipNet2 != nil:
return chain.NewNetworkServiceServer(
singlepointipam.NewServer(ipNet1),
singlepointipam.NewServer(ipNet2),
), nil
case ipNet2 != nil:
return chain.NewNetworkServiceServer(singlepointipam.NewServer(ipNet2)), nil
case ipNet1 != nil:
return chain.NewNetworkServiceServer(singlepointipam.NewServer(ipNet1)), nil
default:
return nil, errors.New("can not set CIDR")
}
}

0 comments on commit b255453

Please sign in to comment.