-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dnsresolve client chain elements
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
- Loading branch information
1 parent
241ef75
commit 24b6529
Showing
10 changed files
with
314 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dnsresolve | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/registry" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/registry/core/next" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/tools/clienturlctx" | ||
"github.com/networkservicemesh/sdk/pkg/tools/interdomain" | ||
) | ||
|
||
type dnsNSResolveClient struct { | ||
resolver Resolver | ||
registryService string | ||
} | ||
|
||
// NewNetworkServiceRegistryClient creates new NetworkServiceRegistryClient that can resolve passed domain to clienturl | ||
func NewNetworkServiceRegistryClient(opts ...Option) registry.NetworkServiceRegistryClient { | ||
var clientOptions = &options{ | ||
resolver: net.DefaultResolver, | ||
registryService: DefaultRegistryService, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(clientOptions) | ||
} | ||
|
||
r := &dnsNSResolveClient{ | ||
resolver: clientOptions.resolver, | ||
registryService: clientOptions.registryService, | ||
} | ||
|
||
return r | ||
} | ||
|
||
func (d *dnsNSResolveClient) Register(ctx context.Context, ns *registry.NetworkService, opts ...grpc.CallOption) (*registry.NetworkService, error) { | ||
domain := interdomain.Domain(ns.Name) | ||
url, err := resolveDomain(ctx, d.registryService, domain, d.resolver) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ctx = clienturlctx.WithClientURL(ctx, url) | ||
ns.Name = interdomain.Target(ns.Name) | ||
resp, err := next.NetworkServiceRegistryClient(ctx).Register(ctx, ns, opts...) | ||
|
||
resp.Name = interdomain.Join(resp.Name, domain) | ||
|
||
return resp, err | ||
} | ||
|
||
type dnsNSResolveFindClient struct { | ||
registry.NetworkServiceRegistry_FindClient | ||
domain string | ||
} | ||
|
||
func (c *dnsNSResolveFindClient) Recv() (*registry.NetworkServiceResponse, error) { | ||
resp, err := c.NetworkServiceRegistry_FindClient.Recv() | ||
if err != nil { | ||
return resp, err | ||
} | ||
resp.NetworkService.Name = interdomain.Join(resp.NetworkService.Name, c.domain) | ||
|
||
return resp, err | ||
} | ||
|
||
func (d *dnsNSResolveClient) Find(ctx context.Context, q *registry.NetworkServiceQuery, opts ...grpc.CallOption) (registry.NetworkServiceRegistry_FindClient, error) { | ||
domain := interdomain.Domain(q.NetworkService.Name) | ||
if domain == "" { | ||
return nil, errors.New("domain cannot be empty") | ||
} | ||
url, err := resolveDomain(ctx, d.registryService, domain, d.resolver) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ctx = clienturlctx.WithClientURL(ctx, url) | ||
q.NetworkService.Name = interdomain.Target(q.NetworkService.Name) | ||
|
||
resp, err := next.NetworkServiceRegistryClient(ctx).Find(ctx, q, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &dnsNSResolveFindClient{ | ||
NetworkServiceRegistry_FindClient: resp, | ||
domain: domain, | ||
}, nil | ||
} | ||
|
||
func (d *dnsNSResolveClient) Unregister(ctx context.Context, ns *registry.NetworkService, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
domain := interdomain.Domain(ns.Name) | ||
url, err := resolveDomain(ctx, d.registryService, domain, d.resolver) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ctx = clienturlctx.WithClientURL(ctx, url) | ||
ns.Name = interdomain.Target(ns.Name) | ||
defer func() { | ||
ns.Name = interdomain.Join(ns.Name, domain) | ||
}() | ||
return next.NetworkServiceRegistryClient(ctx).Unregister(ctx, ns, opts...) | ||
} |
Oops, something went wrong.