2022-04-20 09:17:20 +00:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-08 16:00:22 +00:00
|
|
|
"errors"
|
2022-04-20 09:17:20 +00:00
|
|
|
"fmt"
|
2022-09-08 16:00:22 +00:00
|
|
|
"sync"
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/handler/middleware"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ns"
|
2022-04-20 09:17:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NNSResolver = "nns"
|
|
|
|
DNSResolver = "dns"
|
|
|
|
)
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
// ErrNoResolvers returns when trying to resolve container without any resolver.
|
|
|
|
var ErrNoResolvers = errors.New("no resolvers")
|
|
|
|
|
2022-12-20 11:01:50 +00:00
|
|
|
// FrostFS represents virtual connection to the FrostFS network.
|
|
|
|
type FrostFS interface {
|
|
|
|
// SystemDNS reads system DNS network parameters of the FrostFS.
|
2022-04-20 09:17:20 +00:00
|
|
|
//
|
|
|
|
// Returns exactly on non-zero value. Returns any error encountered
|
|
|
|
// which prevented the parameter to be read.
|
|
|
|
SystemDNS(context.Context) (string, error)
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
type Settings interface {
|
|
|
|
FormContainerZone(ns string) (zone string, isDefault bool)
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
type Config struct {
|
2022-12-20 11:01:50 +00:00
|
|
|
FrostFS FrostFS
|
2022-04-20 09:17:20 +00:00
|
|
|
RPCAddress string
|
2023-11-28 08:29:08 +00:00
|
|
|
Settings Settings
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerResolver struct {
|
2022-09-08 16:00:22 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
resolvers []*Resolver
|
|
|
|
}
|
|
|
|
|
|
|
|
type Resolver struct {
|
2022-04-20 09:17:20 +00:00
|
|
|
Name string
|
|
|
|
resolve func(context.Context, string) (*cid.ID, error)
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
func (r *Resolver) SetResolveFunc(fn func(context.Context, string) (*cid.ID, error)) {
|
2022-04-20 09:17:20 +00:00
|
|
|
r.resolve = fn
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
func (r *Resolver) Resolve(ctx context.Context, name string) (*cid.ID, error) {
|
|
|
|
return r.resolve(ctx, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContainerResolver(resolverNames []string, cfg *Config) (*ContainerResolver, error) {
|
|
|
|
resolvers, err := createResolvers(resolverNames, cfg)
|
2022-04-20 09:17:20 +00:00
|
|
|
if err != nil {
|
2022-09-08 16:00:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ContainerResolver{
|
|
|
|
resolvers: resolvers,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createResolvers(resolverNames []string, cfg *Config) ([]*Resolver, error) {
|
|
|
|
resolvers := make([]*Resolver, len(resolverNames))
|
|
|
|
for i, name := range resolverNames {
|
|
|
|
cnrResolver, err := newResolver(name, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resolvers[i] = cnrResolver
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolvers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ContainerResolver) Resolve(ctx context.Context, cnrName string) (*cid.ID, error) {
|
|
|
|
r.mu.RLock()
|
|
|
|
defer r.mu.RUnlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, resolver := range r.resolvers {
|
|
|
|
cnrID, resolverErr := resolver.Resolve(ctx, cnrName)
|
|
|
|
if resolverErr != nil {
|
|
|
|
resolverErr = fmt.Errorf("%s: %w", resolver.Name, resolverErr)
|
|
|
|
if err == nil {
|
|
|
|
err = resolverErr
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("%s: %w", err.Error(), resolverErr)
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
2022-09-08 16:00:22 +00:00
|
|
|
continue
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
2022-09-08 16:00:22 +00:00
|
|
|
return cnrID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-20 09:17:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-08 16:00:22 +00:00
|
|
|
|
|
|
|
return nil, ErrNoResolvers
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
func (r *ContainerResolver) UpdateResolvers(resolverNames []string, cfg *Config) error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
if r.equals(resolverNames) {
|
|
|
|
return nil
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
resolvers, err := createResolvers(resolverNames, cfg)
|
2022-04-20 09:17:20 +00:00
|
|
|
if err != nil {
|
2022-09-08 16:00:22 +00:00
|
|
|
return err
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
r.resolvers = resolvers
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ContainerResolver) equals(resolverNames []string) bool {
|
|
|
|
if len(r.resolvers) != len(resolverNames) {
|
|
|
|
return false
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
for i := 0; i < len(resolverNames); i++ {
|
|
|
|
if r.resolvers[i].Name != resolverNames[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
func newResolver(name string, cfg *Config) (*Resolver, error) {
|
2022-04-20 09:17:20 +00:00
|
|
|
switch name {
|
|
|
|
case DNSResolver:
|
2023-11-28 08:29:08 +00:00
|
|
|
return NewDNSResolver(cfg.FrostFS, cfg.Settings)
|
2022-04-20 09:17:20 +00:00
|
|
|
case NNSResolver:
|
2023-11-28 08:29:08 +00:00
|
|
|
return NewNNSResolver(cfg.RPCAddress, cfg.Settings)
|
2022-04-20 09:17:20 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown resolver: %s", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
func NewDNSResolver(frostFS FrostFS, settings Settings) (*Resolver, error) {
|
2022-12-20 11:01:50 +00:00
|
|
|
if frostFS == nil {
|
2022-04-20 09:17:20 +00:00
|
|
|
return nil, fmt.Errorf("pool must not be nil for DNS resolver")
|
|
|
|
}
|
2023-11-28 08:29:08 +00:00
|
|
|
if settings == nil {
|
|
|
|
return nil, fmt.Errorf("resolver settings must not be nil for DNS resolver")
|
|
|
|
}
|
2022-04-20 09:17:20 +00:00
|
|
|
|
|
|
|
var dns ns.DNS
|
|
|
|
|
|
|
|
resolveFunc := func(ctx context.Context, name string) (*cid.ID, error) {
|
2023-11-28 08:29:08 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
namespace, err := middleware.GetNamespace(ctx)
|
2022-04-20 09:17:20 +00:00
|
|
|
if err != nil {
|
2023-11-28 08:29:08 +00:00
|
|
|
return nil, err
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
zone, isDefault := settings.FormContainerZone(namespace)
|
|
|
|
if isDefault {
|
|
|
|
zone, err = frostFS.SystemDNS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read system DNS parameter of the FrostFS: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := name + "." + zone
|
2022-04-20 09:17:20 +00:00
|
|
|
cnrID, err := dns.ResolveContainerName(domain)
|
2023-11-28 08:29:08 +00:00
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't resolve container '%s' as '%s': %w", name, domain, err)
|
|
|
|
}
|
|
|
|
return &cnrID, nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
return &Resolver{
|
|
|
|
Name: DNSResolver,
|
2022-04-20 09:17:20 +00:00
|
|
|
resolve: resolveFunc,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
func NewNNSResolver(rpcAddress string, settings Settings) (*Resolver, error) {
|
|
|
|
if rpcAddress == "" {
|
|
|
|
return nil, fmt.Errorf("rpc address must not be empty for NNS resolver")
|
|
|
|
}
|
|
|
|
if settings == nil {
|
|
|
|
return nil, fmt.Errorf("resolver settings must not be nil for NNS resolver")
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
var nns ns.NNS
|
|
|
|
|
|
|
|
if err := nns.Dial(rpcAddress); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not dial nns: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
resolveFunc := func(ctx context.Context, name string) (*cid.ID, error) {
|
2022-11-08 12:56:36 +00:00
|
|
|
var d container.Domain
|
|
|
|
d.SetName(name)
|
|
|
|
|
2023-11-28 08:29:08 +00:00
|
|
|
namespace, err := middleware.GetNamespace(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
zone, _ := settings.FormContainerZone(namespace)
|
|
|
|
d.SetZone(zone)
|
|
|
|
|
2022-11-08 12:56:36 +00:00
|
|
|
cnrID, err := nns.ResolveContainerDomain(d)
|
2022-04-20 09:17:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't resolve container '%s': %w", name, err)
|
|
|
|
}
|
|
|
|
return &cnrID, nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:00:22 +00:00
|
|
|
return &Resolver{
|
|
|
|
Name: NNSResolver,
|
2022-04-20 09:17:20 +00:00
|
|
|
resolve: resolveFunc,
|
|
|
|
}, nil
|
|
|
|
}
|