refactor Cluster gRPC service, add common type converters from/to netip

This commit is contained in:
Pavel Sviderski
2024-09-05 12:15:39 +10:00
parent fd396dd91f
commit aad2ac2085
4 changed files with 811 additions and 141 deletions
File diff suppressed because it is too large Load Diff
+48 -11
View File
@@ -6,30 +6,67 @@ option go_package = "github.com/psviderski/uncloud/internal/machine/cluster/pb";
service Cluster {
rpc AddMachine(AddMachineRequest) returns (AddMachineResponse);
rpc ListMachineEndpoints(ListMachineEndpointsRequest) returns (ListMachineEndpointsResponse);
}
message MachineInfo {
bytes id = 1;
message Machine {
string id = 1;
string name = 2;
IPPrefix subnet = 3;
repeated IPPort endpoints = 4;
bytes publicKey = 5;
NetworkConfig network = 3;
}
message NetworkConfig {
IPPrefix subnet = 1;
IP management_ip = 2;
repeated IPPort endpoints = 3;
bytes publicKey = 4;
}
message MachineEndpoints {
string id = 1;
repeated IPPort endpoints = 2;
}
message ListMachineEndpointsRequest {
string id = 1;
}
message ListMachineEndpointsResponse {
MachineEndpoints endpoints = 1;
}
message User {
NetworkConfig network = 1;
}
message State {
IPPrefix Network = 1;
// The machine configuration in the state is the source of truth set by the administrator.
// The machine itself can't update it.
map<string, Machine> machines = 2;
map<string, MachineEndpoints> endpoints = 3;
repeated User users = 4;
}
message AddMachineRequest {
MachineInfo machine = 1;
string name = 1;
NetworkConfig network = 2;
}
message AddMachineResponse {
MachineInfo machine = 1;
Machine machine = 1;
}
message IP {
bytes ip = 1;
}
message IPPort {
bytes ip = 1;
int32 port = 2;
IP ip = 1;
uint32 port = 2;
}
message IPPrefix {
bytes ip = 1;
int32 bits = 2;
IP ip = 1;
uint32 bits = 2;
}
+41 -3
View File
@@ -2,7 +2,7 @@
// versions:
// - protoc-gen-go-grpc v1.5.1
// - protoc v5.27.3
// source: internal/machine/cluster/pb/cluster.proto
// source: internal/machine/api/pb/cluster.proto
package pb
@@ -19,7 +19,8 @@ import (
const _ = grpc.SupportPackageIsVersion9
const (
Cluster_AddMachine_FullMethodName = "/cluster.Cluster/AddMachine"
Cluster_AddMachine_FullMethodName = "/cluster.Cluster/AddMachine"
Cluster_ListMachineEndpoints_FullMethodName = "/cluster.Cluster/ListMachineEndpoints"
)
// ClusterClient is the client API for Cluster service.
@@ -27,6 +28,7 @@ const (
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type ClusterClient interface {
AddMachine(ctx context.Context, in *AddMachineRequest, opts ...grpc.CallOption) (*AddMachineResponse, error)
ListMachineEndpoints(ctx context.Context, in *ListMachineEndpointsRequest, opts ...grpc.CallOption) (*ListMachineEndpointsResponse, error)
}
type clusterClient struct {
@@ -47,11 +49,22 @@ func (c *clusterClient) AddMachine(ctx context.Context, in *AddMachineRequest, o
return out, nil
}
func (c *clusterClient) ListMachineEndpoints(ctx context.Context, in *ListMachineEndpointsRequest, opts ...grpc.CallOption) (*ListMachineEndpointsResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ListMachineEndpointsResponse)
err := c.cc.Invoke(ctx, Cluster_ListMachineEndpoints_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// ClusterServer is the server API for Cluster service.
// All implementations must embed UnimplementedClusterServer
// for forward compatibility.
type ClusterServer interface {
AddMachine(context.Context, *AddMachineRequest) (*AddMachineResponse, error)
ListMachineEndpoints(context.Context, *ListMachineEndpointsRequest) (*ListMachineEndpointsResponse, error)
mustEmbedUnimplementedClusterServer()
}
@@ -65,6 +78,9 @@ type UnimplementedClusterServer struct{}
func (UnimplementedClusterServer) AddMachine(context.Context, *AddMachineRequest) (*AddMachineResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method AddMachine not implemented")
}
func (UnimplementedClusterServer) ListMachineEndpoints(context.Context, *ListMachineEndpointsRequest) (*ListMachineEndpointsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method ListMachineEndpoints not implemented")
}
func (UnimplementedClusterServer) mustEmbedUnimplementedClusterServer() {}
func (UnimplementedClusterServer) testEmbeddedByValue() {}
@@ -104,6 +120,24 @@ func _Cluster_AddMachine_Handler(srv interface{}, ctx context.Context, dec func(
return interceptor(ctx, in, info, handler)
}
func _Cluster_ListMachineEndpoints_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ListMachineEndpointsRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ClusterServer).ListMachineEndpoints(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Cluster_ListMachineEndpoints_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ClusterServer).ListMachineEndpoints(ctx, req.(*ListMachineEndpointsRequest))
}
return interceptor(ctx, in, info, handler)
}
// Cluster_ServiceDesc is the grpc.ServiceDesc for Cluster service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
@@ -115,7 +149,11 @@ var Cluster_ServiceDesc = grpc.ServiceDesc{
MethodName: "AddMachine",
Handler: _Cluster_AddMachine_Handler,
},
{
MethodName: "ListMachineEndpoints",
Handler: _Cluster_ListMachineEndpoints_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "internal/machine/cluster/pb/cluster.proto",
Metadata: "internal/machine/api/pb/cluster.proto",
}
+49
View File
@@ -0,0 +1,49 @@
package pb
import (
"bytes"
"fmt"
"net/netip"
)
func NewIP(addr netip.Addr) *IP {
// MarshalBinary always returns a nil error.
ip, _ := addr.MarshalBinary()
return &IP{Ip: ip}
}
func (ip *IP) ToAddr() (netip.Addr, error) {
var addr netip.Addr
if err := addr.UnmarshalBinary(ip.Ip); err != nil {
return netip.Addr{}, fmt.Errorf("unmarshal IP: %w", err)
}
return addr, nil
}
func (ip *IP) Equal(other *IP) bool {
return bytes.Equal(ip.Ip, other.Ip)
}
func NewIPPort(ap netip.AddrPort) *IPPort {
return &IPPort{Ip: NewIP(ap.Addr()), Port: uint32(ap.Port())}
}
func (ipp *IPPort) ToAddrPort() (netip.AddrPort, error) {
addr, err := ipp.Ip.ToAddr()
if err != nil {
return netip.AddrPort{}, err
}
return netip.AddrPortFrom(addr, uint16(ipp.Port)), nil
}
func NewIPPrefix(p netip.Prefix) *IPPrefix {
return &IPPrefix{Ip: NewIP(p.Addr()), Bits: uint32(p.Bits())}
}
func (p *IPPrefix) ToPrefix() (netip.Prefix, error) {
addr, err := p.Ip.ToAddr()
if err != nil {
return netip.Prefix{}, err
}
return netip.PrefixFrom(addr, int(p.Bits)), nil
}