talos/pkg/machinery/client/context.go
Andrey Smirnov 1dee0579e9
feat: add support for proxying one-to-one to apid
This adds a new metadata field `node` which performs always proxying to
a single node without touching any protobuf structs on the way.

So with `node`, we can call APIs which do not conform to the Talos API
proxying standards, but from the UX point of view things will work same
way, but multiplexing will be handled on the client side.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2022-08-08 15:39:22 +04:00

40 lines
1.2 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 client
import (
"context"
"google.golang.org/grpc/metadata"
)
// WithNodes wraps the context with metadata to send request to a set of nodes.
//
// Responses from all nodes are aggregated by the `apid` service and sent back as a single response.
func WithNodes(ctx context.Context, nodes ...string) context.Context {
md, _ := metadata.FromOutgoingContext(ctx)
// overwrite any previous nodes in the context metadata with new value
md = md.Copy()
md.Delete("node")
md.Set("nodes", nodes...)
return metadata.NewOutgoingContext(ctx, md)
}
// WithNode wraps the context with metadata to send request to a single node.
//
// Request will be proxied by the endpoint to the specified node without any further processing.
func WithNode(ctx context.Context, node string) context.Context {
md, _ := metadata.FromOutgoingContext(ctx)
// overwrite any previous nodes in the context metadata with new value
md = md.Copy()
md.Delete("nodes")
md.Set("node", node)
return metadata.NewOutgoingContext(ctx, md)
}