mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-18 12:37:05 +02:00
This provides interface to consume (watch) events in the same way (with the same API) as server code does. So some processing might be moved back and forth between client and server without much code change. Also moved peer methods out of helpers to the main client package, they might be useful for any client, not only talosctl. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
33 lines
752 B
Go
33 lines
752 B
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"
|
|
"net"
|
|
|
|
"google.golang.org/grpc/peer"
|
|
)
|
|
|
|
// RemotePeer parses remote peer address from grpc stream context.
|
|
func RemotePeer(ctx context.Context) (peerHost string) {
|
|
peerHost = "unknown"
|
|
|
|
remote, ok := peer.FromContext(ctx)
|
|
if ok {
|
|
peerHost = AddrFromPeer(remote)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// AddrFromPeer extracts peer address from grpc Peer.
|
|
func AddrFromPeer(remote *peer.Peer) (peerHost string) {
|
|
peerHost = remote.Addr.String()
|
|
peerHost, _, _ = net.SplitHostPort(peerHost) //nolint: errcheck
|
|
|
|
return peerHost
|
|
}
|