Skip to content

Commit

Permalink
remove duplicated IPAM code
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Bayandinov <wazsone@ya.ru>
  • Loading branch information
wazsone committed Jan 11, 2023
1 parent 61dbb57 commit 8a807d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
4 changes: 2 additions & 2 deletions internal/pkg/imports/imports_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/common/onidle"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/ipam/point2pointipam"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/ipam/groupipam"
_ "github.com/networkservicemesh/sdk/pkg/registry/chains/client"
_ "github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
_ "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
_ "github.com/networkservicemesh/sdk/pkg/tools/cidr"
_ "github.com/networkservicemesh/sdk/pkg/tools/clientinfo"
_ "github.com/networkservicemesh/sdk/pkg/tools/debug"
_ "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
Expand Down
59 changes: 20 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Xored Software Inc and others.
// Copyright (c) 2023 Xored Software Inc and others.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -23,7 +23,6 @@ import (
"context"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"os/signal"
Expand Down Expand Up @@ -57,11 +56,11 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/onidle"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/ipam/point2pointipam"
"github.com/networkservicemesh/sdk/pkg/networkservice/ipam/groupipam"
registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client"
registryauthorize "github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
registrysendfd "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
"github.com/networkservicemesh/sdk/pkg/tools/cidr"
"github.com/networkservicemesh/sdk/pkg/tools/clientinfo"
"github.com/networkservicemesh/sdk/pkg/tools/debug"
"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
Expand All @@ -84,7 +83,7 @@ type Config struct {
ServiceNames []string `default:"istio-proxy-responder" desc:"Name of provided services" split_words:"true"`
Labels map[string]string `default:"" desc:"Endpoint labels"`
DNSConfigs dnsconfig.Decoder `default:"[]" desc:"DNSConfigs represents array of DNSConfig in json format. See at model definition: https://github.com/networkservicemesh/api/blob/main/pkg/api/networkservice/connectioncontext.pb.go#L426-L435" split_words:"true"`
CidrPrefix []string `default:"169.254.0.0/16" desc:"List of CIDR Prefix to assign IPv4 and IPv6 addresses from" split_words:"true"`
CidrPrefix cidr.Groups `default:"169.254.0.0/16" desc:"List of CIDR Prefix to assign IPv4 and IPv6 addresses from" split_words:"true"`
IdleTimeout time.Duration `default:"0" desc:"timeout for automatic shutdown when there were no requests for specified time. Set 0 to disable auto-shutdown." split_words:"true"`
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint"`
Expand Down Expand Up @@ -132,10 +131,10 @@ func main() {
log.FromContext(ctx).Infof("the phases include:")
log.FromContext(ctx).Infof("1: get config from environment")
log.FromContext(ctx).Infof("2: retrieve spiffe svid")
log.FromContext(ctx).Infof("3: create server ipam")
log.FromContext(ctx).Infof("4: create server nse")
log.FromContext(ctx).Infof("5: create grpc and mount nse")
log.FromContext(ctx).Infof("6: register nse with nsm")
log.FromContext(ctx).Infof("3: create server nse")
log.FromContext(ctx).Infof("4: create grpc and mount nse")
log.FromContext(ctx).Infof("5: register nse with nsm")
log.FromContext(ctx).Infof("6: run DNS server")
log.FromContext(ctx).Infof("a final success message with start time duration")

starttime := time.Now()
Expand All @@ -148,14 +147,16 @@ func main() {
logrus.Fatal(err.Error())
}

// TODO Fix for multiple clients
if len(config.CidrPrefix) != 1 {
logrus.Fatal("Only one CIDR prefix expected")
logrus.Fatal("Only one CIDR prefix group expected")
}
ip, _, err := net.ParseCIDR(config.CidrPrefix[0])
if err != nil {
logrus.Fatalf("parsing CIDR error: %s", err.Error())

// TODO Fix for multiple clients
if len(config.CidrPrefix[0]) != 1 {
logrus.Fatal("Only one CIDR prefix expected")
}

ip := config.CidrPrefix[0][0].IP
if ip.To4() == nil {
logrus.Fatal("expected CIDR ipv4")
}
Expand Down Expand Up @@ -199,14 +200,7 @@ func main() {
log.FromContext(ctx).Infof("SVID: %q", svid.ID)

// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 3: creating server ipam")
// ********************************************************************************
ipamChain := getIPAMChain(ctx, config.CidrPrefix)

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

// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 4: create network service endpoint")
log.FromContext(ctx).Infof("executing phase 3: create network service endpoint")
// ********************************************************************************
rules := getIPTablesRules(ctx, config.RulesConfigPath)

Expand All @@ -220,7 +214,7 @@ func main() {
endpoint.WithAuthorizeServer(authorize.NewServer()),
endpoint.WithAdditionalFunctionality(
onidle.NewServer(ctx, cancel, config.IdleTimeout),
ipamChain,
groupipam.NewServer(config.CidrPrefix),
recvfd.NewServer(),
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{
kernelmech.MECHANISM: kernel.NewServer(),
Expand All @@ -232,7 +226,7 @@ func main() {
),
)
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 5: create grpc server and register icmp-server")
log.FromContext(ctx).Infof("executing phase 4: create grpc server and register icmp-server")
// ********************************************************************************
options := append(
tracing.WithTracing(),
Expand All @@ -257,7 +251,7 @@ func main() {
log.FromContext(ctx).Infof("grpc server started")

// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 6: register nse with nsm")
log.FromContext(ctx).Infof("executing phase 5: register nse with nsm")
// ********************************************************************************
clientOptions := append(
tracing.WithTracingDial(),
Expand Down Expand Up @@ -295,7 +289,7 @@ func main() {
}

// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 7: run DNS server")
log.FromContext(ctx).Infof("executing phase 6: run DNS server")
// ********************************************************************************
dnsServer := &dns.ProxyRewriteServer{
RewriteTO: ip,
Expand Down Expand Up @@ -380,16 +374,3 @@ func exitOnErr(ctx context.Context, cancel context.CancelFunc, errCh <-chan erro
cancel()
}(ctx, errCh)
}

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, point2pointipam.NewServer(ipNet))
}
return chain.NewNetworkServiceServer(ipamchain...)
}

0 comments on commit 8a807d1

Please sign in to comment.