talos/pkg/client/peer.go
Andrey Smirnov b9b9aa9104 feat: implement simplified client method to consume events
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>
2020-05-25 10:15:39 -07:00

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
}