From 4c66c7fc68619f2fd8d60dd54cd6d223ae39f81b Mon Sep 17 00:00:00 2001 From: denis-tingaikin Date: Thu, 2 Jun 2022 18:25:02 +0300 Subject: [PATCH 1/2] add vl3 dns support for vl3 endpoints Signed-off-by: denis-tingaikin --- .golangci.yml | 2 +- go.mod | 4 ++++ go.sum | 10 ++++++++ main.go | 65 +++++++++++++++++++++++++++++++++------------------ 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 6f9d23b..c185881 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -30,7 +30,7 @@ linters-settings: goimports: local-prefixes: github.com/networkservicemesh gocyclo: - min-complexity: 20 + min-complexity: 25 maligned: suggest-new: true dupl: diff --git a/go.mod b/go.mod index a7655d0..76cfa09 100644 --- a/go.mod +++ b/go.mod @@ -38,6 +38,7 @@ require ( github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe // indirect + github.com/miekg/dns v1.1.49 // indirect github.com/networkservicemesh/sdk-kernel v0.0.0-20220602005025-aeb8e5a41690 // indirect github.com/open-policy-agent/opa v0.16.1 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect @@ -59,9 +60,12 @@ require ( go.opentelemetry.io/otel/trace v1.3.0 // indirect go.opentelemetry.io/proto/otlp v0.11.0 // indirect golang.org/x/crypto v0.0.0-20220307211146-efcb8507fb70 // indirect + golang.org/x/mod v0.4.2 // indirect golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 // indirect golang.org/x/text v0.3.7 // indirect + golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200609130330-bd2cb7843e1b // indirect google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12 // indirect gopkg.in/fsnotify.v1 v1.4.7 // indirect diff --git a/go.sum b/go.sum index 4b00621..d90903b 100644 --- a/go.sum +++ b/go.sum @@ -130,6 +130,8 @@ github.com/mdlayher/genetlink v1.0.0/go.mod h1:0rJ0h4itni50A86M2kHcgS85ttZazNt7a github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA= github.com/mdlayher/netlink v1.0.0/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M= github.com/mdlayher/netlink v1.1.0/go.mod h1:H4WCitaheIsdF9yOYu8CFmCgQthAPIWZmcKp9uZHgmY= +github.com/miekg/dns v1.1.49 h1:qe0mQU3Z/XpFeE+AEBo2rqaS1IPBJ3anmqZ4XiZJVG8= +github.com/miekg/dns v1.1.49/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME= github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721/go.mod h1:Ickgr2WtCLZ2MDGd4Gr0geeCH5HybhRJbonOgQpvSxc= github.com/mna/pigeon v0.0.0-20180808201053-bb0192cfc2ae/go.mod h1:Iym28+kJVnC1hfQvv5MUtI6AiFFzvQjHcvI4RFTG/04= github.com/networkservicemesh/api v1.3.2-0.20220516230921-edaa6f46d6ab h1:hV6T7Kvze0qQphqi6g5ZKFoJ0m08OKdWETUINo3tuaA= @@ -235,6 +237,7 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -250,6 +253,7 @@ golang.org/x/net v0.0.0-20191007182048-72f939374954/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -259,6 +263,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -278,8 +283,10 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200610111108-226ff32320da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 h1:8IVLkfbr2cLhv0a/vKq4UFUcJym8RmDoDboxCFWEjYE= golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -287,6 +294,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -299,6 +307,8 @@ golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 h1:BonxutuHCTL0rBDnZlKjpGIQFTjyUVTexFOdWkB6Fg0= +golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 11e52e9..54073c8 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ package main import ( "context" "crypto/tls" + "fmt" "io/ioutil" "net/url" "os" @@ -45,6 +46,7 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/common/null" "github.com/networkservicemesh/sdk/pkg/networkservice/common/onidle" "github.com/networkservicemesh/sdk/pkg/networkservice/common/retry" + "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext/vl3dns" "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/ipcontext/vl3" registrysendfd "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd" "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" @@ -96,6 +98,8 @@ type Config struct { RegisterService bool `default:"true" desc:"if true then registers network service on startup" split_words:"true"` OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint"` PrefixServerURL url.URL `default:"vl3-ipam:5006" desc:"URL to VL3 IPAM server"` + DomainNamingTemplates []string `default:"{{ index .Labels \"podName\" }}.{{ .NetworkService }}" desc:"Represents domain naming templates in go-template format. Gets on input networkservice.Connection"` + LogLevel string `default:"INFO" desc:"Log level" split_words:"true"` } // Process prints and processes env to config @@ -112,7 +116,7 @@ func (c *Config) Process() error { func startListenPrefixes(ctx context.Context, c *Config, tlsClientConfig *tls.Config, subscriptions []chan *ipam.PrefixResponse) { var previousResponse *ipam.PrefixResponse go func() { - for ctx.Err() == nil { + for ; ctx.Err() == nil; time.Sleep(time.Millisecond * 200) { cc, err := grpc.DialContext(ctx, grpcutils.URLToTarget(&c.PrefixServerURL), grpc.WithTransportCredentials( credentials.NewTLS( tlsClientConfig, @@ -120,14 +124,12 @@ func startListenPrefixes(ctx context.Context, c *Config, tlsClientConfig *tls.Co )) if err != nil { logrus.Error(err.Error()) - time.Sleep(time.Millisecond * 200) continue } managePrefixClient, err := ipam.NewIPAMClient(cc).ManagePrefixes(ctx) if err != nil { logrus.Error(err.Error()) - time.Sleep(time.Millisecond * 200) continue } @@ -139,7 +141,6 @@ func startListenPrefixes(ctx context.Context, c *Config, tlsClientConfig *tls.Co err = managePrefixClient.Send(request) if err != nil { - time.Sleep(time.Millisecond * 200) continue } @@ -167,8 +168,6 @@ func main() { // ******************************************************************************** // setup logging // ******************************************************************************** - logrus.SetLevel(logrus.TraceLevel) - logrus.SetFormatter(&nested.Formatter{}) ctx = log.WithLog(ctx, logruslogger.New(ctx, map[string]interface{}{"cmd": os.Args[0]})) if err := debug.Self(); err != nil { @@ -188,6 +187,13 @@ func main() { logrus.Fatal(err.Error()) } + level, err := logrus.ParseLevel(config.LogLevel) + if err != nil { + logrus.Fatalf("invalid log level %s", config.LogLevel) + } + logrus.SetLevel(level) + logrus.SetFormatter(&nested.Formatter{}) + // ******************************************************************************** // Configure Open Telemetry // ******************************************************************************** @@ -197,7 +203,7 @@ func main() { metricExporter := opentelemetry.InitMetricExporter(ctx, collectorAddress) o := opentelemetry.Init(ctx, spanExporter, metricExporter, config.Name) defer func() { - if err := o.Close(); err != nil { + if err = o.Close(); err != nil { logrus.Error(err.Error()) } }() @@ -304,11 +310,30 @@ func main() { // ******************************************************************************** var subscribedChannels []chan *ipam.PrefixResponse + subscribedChannels = append(subscribedChannels, make(chan *ipam.PrefixResponse, 1)) var closeAll = func() { close(subscribedChannels[0]) } - server := createVl3Endpoint(ctx, cancel, config, vppConn, tlsServerConfig, source, loopOptions, vrfOptions, subscribedChannels[0]) + + nseStream, err := nseRegistryClient.Find(ctx, ®istryapi.NetworkServiceEndpointQuery{ + NetworkServiceEndpoint: ®istryapi.NetworkServiceEndpoint{ + NetworkServiceNames: config.ServiceNames, + }, + }) + + if err != nil { + log.FromContext(ctx).Fatalf("error getting nses: %+v", err) + } + nseList := registryapi.ReadNetworkServiceEndpointList(nseStream) + + for i := 0; i < len(nseList); i++ { + subscribedChannels = append(subscribedChannels, make(chan *ipam.PrefixResponse, 1)) + } + + var initialDNSFanoutList = make([]url.URL, len(nseList)) + + server := createVl3Endpoint(ctx, cancel, config, vppConn, tlsServerConfig, source, loopOptions, vrfOptions, subscribedChannels[0], initialDNSFanoutList) srvErrCh := grpcutils.ListenAndServe(ctx, listenOn, server) exitOnErr(ctx, cancel, srvErrCh) @@ -330,20 +355,6 @@ func main() { log.FromContext(ctx).Fatalf("unable to register nse %+v", err) } - nseStream, err := nseRegistryClient.Find(ctx, ®istryapi.NetworkServiceEndpointQuery{ - NetworkServiceEndpoint: ®istryapi.NetworkServiceEndpoint{ - NetworkServiceNames: config.ServiceNames, - }, - }) - if err != nil { - log.FromContext(ctx).Fatalf("error getting nses: %+v", err) - } - nseList := registryapi.ReadNetworkServiceEndpointList(nseStream) - - for i := 0; i < len(nseList); i++ { - subscribedChannels = append(subscribedChannels, make(chan *ipam.PrefixResponse, 1)) - } - startListenPrefixes(ctx, config, tlsClientConfig, subscribedChannels) for i, nse := range nseList { @@ -374,6 +385,13 @@ func main() { continue } + for _, config := range conn.Context.GetDnsContext().GetConfigs() { + for _, nameserverAddress := range config.DnsServerIps { + initialDNSFanoutList[i] = url.URL{Scheme: "tcp", Host: fmt.Sprintf("%v:53", nameserverAddress)} + log.FromContext(ctx).Infof("Added dns server to fanout: %v", initialDNSFanoutList[i]) + } + } + prevClose := closeAll closeAll = func() { close(subscribedChannels[index]) @@ -434,7 +452,7 @@ func createVl3Client(ctx context.Context, config *Config, vppConn vpphelper.Conn } func createVl3Endpoint(ctx context.Context, cancel context.CancelFunc, config *Config, vppConn vpphelper.Connection, tlsServerConfig *tls.Config, - source x509svid.Source, loopOpts []loopback.Option, vrfOpts []vrf.Option, prefixCh <-chan *ipam.PrefixResponse) *grpc.Server { + source x509svid.Source, loopOpts []loopback.Option, vrfOpts []vrf.Option, prefixCh <-chan *ipam.PrefixResponse, initialDNSFanoutList []url.URL) *grpc.Server { vl3Endpoint := endpoint.NewServer(ctx, spiffejwt.TokenGeneratorFunc(source, config.MaxTokenLifetime), endpoint.WithName(config.Name), @@ -442,6 +460,7 @@ func createVl3Endpoint(ctx context.Context, cancel context.CancelFunc, config *C endpoint.WithAdditionalFunctionality( onidle.NewServer(ctx, cancel, config.IdleTimeout), vl3.NewServer(ctx, prefixCh), + vl3dns.NewServer(ctx, vl3dns.WithDomainSchemes(config.DomainNamingTemplates...), vl3dns.WithInitialFanoutList(initialDNSFanoutList)), up.NewServer(ctx, vppConn, up.WithLoadSwIfIndex(loopback.Load)), ipaddress.NewServer(vppConn, ipaddress.WithLoadSwIfIndex(loopback.Load)), unnumbered.NewServer(vppConn, loopback.Load), From 7a0578fd0b2b3f648a7e0f9d239bd13a8d2986ed Mon Sep 17 00:00:00 2001 From: denis-tingaikin Date: Thu, 2 Jun 2022 18:28:28 +0300 Subject: [PATCH 2/2] fix go generate Signed-off-by: denis-tingaikin --- internal/imports/imports_linux.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/imports/imports_linux.go b/internal/imports/imports_linux.go index 7234fa2..f6785dd 100644 --- a/internal/imports/imports_linux.go +++ b/internal/imports/imports_linux.go @@ -4,6 +4,7 @@ package imports import ( _ "context" _ "crypto/tls" + _ "fmt" _ "github.com/antonfisher/nested-logrus-formatter" _ "github.com/edwarnicke/grpcfd" _ "github.com/edwarnicke/vpphelper" @@ -30,6 +31,7 @@ import ( _ "github.com/networkservicemesh/sdk/pkg/networkservice/common/null" _ "github.com/networkservicemesh/sdk/pkg/networkservice/common/onidle" _ "github.com/networkservicemesh/sdk/pkg/networkservice/common/retry" + _ "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext/vl3dns" _ "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/ipcontext/vl3" _ "github.com/networkservicemesh/sdk/pkg/networkservice/core/chain" _ "github.com/networkservicemesh/sdk/pkg/registry/chains/client"