Andrey Smirnov a068acfbe4 feat: split routerd from apid
New service `routerd` performs exactly single task: based on incoming
API call service name, it routes the requests to the appropriate Talos
service (`networkd`, `osd`, etc.) Service `routerd` listens of file
socket and routes requests to file sockets.

Service `apid` now does single task as well:

* it either fans out request to other `apid` services running on other
nodes and aggregates responses
* or it forwards requests to local `routerd` (when request destination
is local node)

Cons:

* one more proxying layer on request path

Pros:

* more clear service roles
* `routerd` is part of core Talos, services should register with it to
expose their API; no auth in the service (not exposed to the world)
* `apid` might be replaced with other implementation, it depends on TLS infra,
auth, etc.
* `apid` is better segregated from other Talos services (can only access
`routerd`, can't talk to other Talos services directly, so less exposure
in case of a bug)

This change is no-op to the end users.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2020-03-05 22:05:56 +03:00

73 lines
1.6 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package backend
import (
"context"
"sync"
"github.com/talos-systems/grpc-proxy/proxy"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// Local implements local backend (proxying one2one to local service)
type Local struct {
name string
socketPath string
mu sync.Mutex
conn *grpc.ClientConn
}
// NewLocal builds new Local backend
func NewLocal(name, socketPath string) *Local {
return &Local{
name: name,
socketPath: socketPath,
}
}
func (l *Local) String() string {
return l.name
}
// GetConnection returns a grpc connection to the backend.
func (l *Local) GetConnection(ctx context.Context) (context.Context, *grpc.ClientConn, error) {
l.mu.Lock()
defer l.mu.Unlock()
// copy metadata
outCtx := ctx
if md, ok := metadata.FromIncomingContext(ctx); ok {
outCtx = metadata.NewOutgoingContext(ctx, md)
}
if l.conn != nil {
return outCtx, l.conn, nil
}
var err error
l.conn, err = grpc.DialContext(
ctx,
"unix:"+l.socketPath,
grpc.WithInsecure(),
grpc.WithCodec(proxy.Codec()), //nolint: staticcheck
)
return outCtx, l.conn, err
}
// AppendInfo is called to enhance response from the backend with additional data.
func (l *Local) AppendInfo(streaming bool, resp []byte) ([]byte, error) {
return resp, nil
}
// BuildError is called to convert error from upstream into response field.
func (l *Local) BuildError(streaming bool, err error) ([]byte, error) {
return nil, nil
}