diff --git a/README.md b/README.md index 2cfd306..609acdb 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,3 @@ auto-elects in the event of a current master failure. Non-node peers are also s can only access the network but do not provide routing capabilities. ![Heimdall](docs/overview.png) - -# diff --git a/api/v1/heimdall.pb.go b/api/v1/heimdall.pb.go index c1530ab..90e875d 100644 --- a/api/v1/heimdall.pb.go +++ b/api/v1/heimdall.pb.go @@ -32,6 +32,7 @@ type Master struct { ID string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` GRPCAddress string `protobuf:"bytes,2,opt,name=grpc_address,json=grpcAddress,proto3" json:"grpc_address,omitempty"` RedisURL string `protobuf:"bytes,3,opt,name=redis_url,json=redisUrl,proto3" json:"redis_url,omitempty"` + Peers []*Peer `protobuf:"bytes,5,rep,name=peers,proto3" json:"peers,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -82,9 +83,20 @@ func (m *Master) GetRedisURL() string { return "" } +func (m *Master) GetPeers() []*Peer { + if m != nil { + return m.Peers + } + return nil +} + type JoinRequest struct { ID string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` ClusterKey string `protobuf:"bytes,2,opt,name=cluster_key,json=clusterKey,proto3" json:"cluster_key,omitempty"` + GRPCAddress string `protobuf:"bytes,3,opt,name=grpc_address,json=grpcAddress,proto3" json:"grpc_address,omitempty"` + EndpointIP string `protobuf:"bytes,4,opt,name=endpoint_ip,json=endpointIp,proto3" json:"endpoint_ip,omitempty"` + EndpointPort uint64 `protobuf:"varint,5,opt,name=endpoint_port,json=endpointPort,proto3" json:"endpoint_port,omitempty"` + InterfaceName string `protobuf:"bytes,6,opt,name=interface_name,json=interfaceName,proto3" json:"interface_name,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -128,8 +140,38 @@ func (m *JoinRequest) GetClusterKey() string { return "" } +func (m *JoinRequest) GetGRPCAddress() string { + if m != nil { + return m.GRPCAddress + } + return "" +} + +func (m *JoinRequest) GetEndpointIP() string { + if m != nil { + return m.EndpointIP + } + return "" +} + +func (m *JoinRequest) GetEndpointPort() uint64 { + if m != nil { + return m.EndpointPort + } + return 0 +} + +func (m *JoinRequest) GetInterfaceName() string { + if m != nil { + return m.InterfaceName + } + return "" +} + type JoinResponse struct { Master *Master `protobuf:"bytes,1,opt,name=master,proto3" json:"master,omitempty"` + Node *Node `protobuf:"bytes,2,opt,name=node,proto3" json:"node,omitempty"` + Peers []*Peer `protobuf:"bytes,3,rep,name=peers,proto3" json:"peers,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -166,6 +208,20 @@ func (m *JoinResponse) GetMaster() *Master { return nil } +func (m *JoinResponse) GetNode() *Node { + if m != nil { + return m.Node + } + return nil +} + +func (m *JoinResponse) GetPeers() []*Peer { + if m != nil { + return m.Peers + } + return nil +} + type ConnectRequest struct { ID string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` @@ -464,6 +520,7 @@ type Node struct { EndpointPort uint64 `protobuf:"varint,5,opt,name=endpoint_port,json=endpointPort,proto3" json:"endpoint_port,omitempty"` GatewayIP string `protobuf:"bytes,6,opt,name=gateway_ip,json=gatewayIp,proto3" json:"gateway_ip,omitempty"` Updated time.Time `protobuf:"bytes,7,opt,name=updated,proto3,stdtime" json:"updated"` + InterfaceName string `protobuf:"bytes,8,opt,name=interface_name,json=interfaceName,proto3" json:"interface_name,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -542,6 +599,13 @@ func (m *Node) GetUpdated() time.Time { return time.Time{} } +func (m *Node) GetInterfaceName() string { + if m != nil { + return m.InterfaceName + } + return "" +} + type NodesRequest struct { XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -967,71 +1031,75 @@ func init() { } var fileDescriptor_b6184fc395da86b1 = []byte{ - // 1014 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x57, 0x5d, 0x6f, 0xe3, 0x44, - 0x17, 0x7e, 0x9d, 0xef, 0x1c, 0x27, 0x8d, 0x34, 0x5a, 0xf5, 0xf5, 0x06, 0x21, 0x17, 0xef, 0x05, - 0x59, 0xb4, 0xb2, 0x9b, 0x00, 0xe2, 0x4b, 0x8b, 0x68, 0x9a, 0xdd, 0xc5, 0x14, 0xaa, 0xec, 0x2c, - 0x7b, 0x83, 0x56, 0x0a, 0x6e, 0x3c, 0xa4, 0x6e, 0x9d, 0xcc, 0x60, 0x4f, 0x5a, 0x85, 0x1b, 0x6e, - 0xb9, 0xe4, 0xff, 0x20, 0x71, 0x0d, 0xff, 0x80, 0xab, 0x20, 0xe5, 0x97, 0xa0, 0x19, 0x8f, 0x4d, - 0xd2, 0x36, 0xaa, 0xbb, 0xec, 0x5d, 0xe6, 0xcc, 0x79, 0xce, 0x39, 0x7e, 0xce, 0x73, 0x8e, 0x1d, - 0xf8, 0x78, 0x12, 0xf0, 0xd3, 0xf9, 0x89, 0x3d, 0xa6, 0x53, 0x27, 0xe6, 0x24, 0x0c, 0xbd, 0x88, - 0x45, 0xf4, 0x8c, 0x8c, 0xb9, 0x73, 0x4a, 0x82, 0xa9, 0xef, 0x85, 0xa1, 0xe3, 0xb1, 0xc0, 0xb9, - 0xe8, 0x66, 0x67, 0x9b, 0x45, 0x94, 0x53, 0xf4, 0x4e, 0x40, 0xed, 0x4d, 0x84, 0x9d, 0x79, 0x78, - 0x2c, 0xb0, 0x2f, 0xba, 0xed, 0x7b, 0x13, 0x3a, 0xa1, 0xd2, 0xdb, 0x11, 0xbf, 0x12, 0x60, 0xfb, - 0xad, 0x09, 0xa5, 0x93, 0x90, 0x38, 0xf2, 0x74, 0x32, 0xff, 0xc1, 0x21, 0x53, 0xc6, 0x17, 0xea, - 0xd2, 0xbc, 0x7a, 0xc9, 0x83, 0x29, 0x89, 0xb9, 0x37, 0x65, 0x89, 0x83, 0xf5, 0x33, 0x54, 0xbe, - 0xf1, 0x62, 0x4e, 0x22, 0xb4, 0x0b, 0x85, 0xc0, 0x37, 0xb4, 0x3d, 0xad, 0x53, 0xef, 0x57, 0x56, - 0x4b, 0xb3, 0xe0, 0x0e, 0x70, 0x21, 0xf0, 0x51, 0x0f, 0x1a, 0x93, 0x88, 0x8d, 0x47, 0x9e, 0xef, - 0x47, 0x24, 0x8e, 0x8d, 0x82, 0xf4, 0x68, 0xad, 0x96, 0xa6, 0xfe, 0x0c, 0x0f, 0x0f, 0x0f, 0x12, - 0x33, 0xd6, 0x85, 0x93, 0x3a, 0xa0, 0x87, 0x50, 0x8f, 0x88, 0x1f, 0xc4, 0xa3, 0x79, 0x14, 0x1a, - 0x45, 0x09, 0x68, 0xac, 0x96, 0x66, 0x0d, 0x0b, 0xe3, 0x4b, 0xfc, 0x35, 0xae, 0xc9, 0xeb, 0x97, - 0x51, 0x68, 0x3d, 0x05, 0xfd, 0x2b, 0x1a, 0xcc, 0x30, 0xf9, 0x71, 0x4e, 0x62, 0xbe, 0xb5, 0x0a, - 0x13, 0xf4, 0x71, 0x38, 0x17, 0x85, 0x8e, 0xce, 0xc9, 0x22, 0x29, 0x02, 0x83, 0x32, 0x1d, 0x91, - 0x85, 0xf5, 0x1c, 0x1a, 0x49, 0x9c, 0x98, 0xd1, 0x59, 0x4c, 0xd0, 0x01, 0x54, 0xa6, 0xf2, 0xc1, - 0x64, 0x30, 0xbd, 0xf7, 0xd0, 0xbe, 0x95, 0x60, 0x3b, 0x61, 0x02, 0x2b, 0xa0, 0xd5, 0x81, 0x9d, - 0x43, 0x3a, 0x9b, 0x91, 0x31, 0xbf, 0xa5, 0x3a, 0xeb, 0x2f, 0x0d, 0x5a, 0x99, 0xab, 0x2a, 0xe0, - 0x39, 0x54, 0xcf, 0xc9, 0x82, 0x79, 0x41, 0x5a, 0xc1, 0x7b, 0x39, 0x2a, 0x38, 0x22, 0x8b, 0xa1, - 0x17, 0x44, 0x7d, 0x7d, 0xb5, 0x34, 0xab, 0xea, 0x80, 0xd3, 0x38, 0xc8, 0x80, 0xea, 0x46, 0x17, - 0x70, 0x7a, 0x44, 0x8f, 0xa1, 0xcc, 0x08, 0x89, 0x62, 0xa3, 0xb8, 0x57, 0xec, 0xe8, 0xbd, 0x77, - 0x73, 0xa4, 0x1a, 0x12, 0x12, 0xe1, 0x04, 0x85, 0xee, 0x43, 0xd1, 0x9f, 0xc5, 0x46, 0x69, 0xaf, - 0xd8, 0xa9, 0xf7, 0xab, 0xab, 0xa5, 0x59, 0x1c, 0x1c, 0xbf, 0xc0, 0xc2, 0x66, 0xd9, 0x70, 0xef, - 0x60, 0xce, 0x4f, 0x69, 0x14, 0xfc, 0x44, 0x24, 0xe4, 0x16, 0x2a, 0xf6, 0x61, 0x77, 0x40, 0xbc, - 0xbb, 0x20, 0x0c, 0xd8, 0xcd, 0x32, 0xf8, 0x02, 0x10, 0x2b, 0x84, 0xf5, 0x01, 0xfc, 0xff, 0xda, - 0x8d, 0x62, 0xf7, 0x3e, 0x14, 0x03, 0x3f, 0x36, 0xb4, 0x7f, 0x2b, 0x76, 0x07, 0x31, 0x16, 0x36, - 0xcb, 0x85, 0x94, 0x39, 0xa1, 0x1a, 0x16, 0x05, 0x17, 0x1e, 0x27, 0x52, 0x35, 0x5a, 0xa2, 0x1a, - 0x65, 0x3a, 0x22, 0x0b, 0xf4, 0x36, 0x00, 0x9b, 0x9f, 0x84, 0xc1, 0x78, 0x4d, 0x55, 0xf5, 0xc4, - 0x22, 0x44, 0xf5, 0x67, 0x01, 0x4a, 0xc7, 0xd4, 0x27, 0x5b, 0x65, 0x89, 0xa0, 0x24, 0x5a, 0xa0, - 0x90, 0xf2, 0xf7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 0xd7, 0x81, 0x2a, - 0x4a, 0x27, 0x50, 0x12, 0x5f, 0x09, 0xc8, 0xce, 0x11, 0x63, 0xed, 0xb3, 0xa4, 0xed, 0xe4, 0xf6, - 0x57, 0x89, 0xa6, 0x50, 0x49, 0xe4, 0x88, 0xf6, 0xf3, 0xca, 0x2e, 0x7b, 0xc0, 0xee, 0x1d, 0x10, - 0x2a, 0xdd, 0x2b, 0xd0, 0xd7, 0x66, 0x12, 0x7d, 0x98, 0x47, 0x6d, 0xd7, 0x66, 0x78, 0x6b, 0x03, - 0x5f, 0x81, 0xbe, 0x36, 0x9c, 0xb9, 0xa2, 0x5f, 0x1f, 0xe6, 0xad, 0xd1, 0xcf, 0xa0, 0x2c, 0x5f, - 0x04, 0xc8, 0xc9, 0xb9, 0xf1, 0x33, 0xa2, 0xf6, 0xf3, 0x03, 0x14, 0x4f, 0x67, 0x50, 0x4e, 0xf4, - 0xe7, 0xe4, 0x5c, 0x9e, 0x77, 0xca, 0xb5, 0xa1, 0xb5, 0xbe, 0xfd, 0xdd, 0xa3, 0xdc, 0xff, 0x06, - 0x3e, 0xbb, 0xe8, 0x9e, 0x54, 0x24, 0x2f, 0xef, 0xff, 0x13, 0x00, 0x00, 0xff, 0xff, 0xcd, 0x85, - 0x12, 0x11, 0x44, 0x0c, 0x00, 0x00, + // 1076 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x57, 0x5f, 0x6f, 0xe3, 0x44, + 0x10, 0xc7, 0xf9, 0x9f, 0x49, 0xd3, 0x4a, 0xab, 0x53, 0xf1, 0x05, 0x21, 0x97, 0x9c, 0x10, 0x39, + 0x74, 0xb2, 0xdb, 0x02, 0xe2, 0xcf, 0xe9, 0x10, 0x4d, 0x73, 0x1c, 0xa6, 0x50, 0x85, 0x3d, 0xee, + 0x05, 0x9d, 0x14, 0xdc, 0x78, 0x2e, 0x75, 0xeb, 0xd8, 0x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b, 0x00, 0x6c, 0x72, 0xe4, 0x7b, + 0xc3, 0x85, 0x19, 0xac, 0x27, 0x96, 0x03, 0x9c, 0xb6, 0xff, 0x29, 0x40, 0x49, 0xf4, 0xd3, 0xca, + 0x21, 0x26, 0x50, 0x12, 0x12, 0x28, 0xa4, 0xfc, 0x7b, 0x51, 0xf8, 0xe2, 0x6b, 0x12, 0xfe, 0xff, + 0x19, 0xeb, 0x07, 0x00, 0x23, 0x87, 0xe3, 0xb9, 0x33, 0x15, 0x41, 0xe5, 0x48, 0x77, 0x9b, 0xf3, + 0x99, 0x51, 0x7f, 0x92, 0x58, 0xed, 0x3e, 0xad, 0x2b, 0x07, 0x9b, 0x91, 0xcf, 0xa1, 0x3a, 0x61, + 0xae, 0xc3, 0xd1, 0xd5, 0xab, 0xb2, 0xac, 0x96, 0x99, 0xec, 0x72, 0x33, 0xdd, 0xe5, 0xe6, 0xf7, + 0xe9, 0x2e, 0xef, 0xd6, 0xfe, 0x98, 0x19, 0x6f, 0xfc, 0xfa, 0xb7, 0xa1, 0xd1, 0x14, 0x74, 0xc5, + 0x12, 0xa9, 0x5d, 0xb5, 0x44, 0xd6, 0x61, 0x4d, 0x30, 0x9e, 0xf5, 0xc0, 0x21, 0x34, 0xd5, 0x59, + 0x29, 0xff, 0x08, 0xca, 0x62, 0xb8, 0x13, 0xed, 0x6f, 0xb0, 0x12, 0x12, 0x94, 0x78, 0x5f, 0x4a, + 0xa2, 0x95, 0x56, 0x4a, 0xba, 0x20, 0x5f, 0xe1, 0xf5, 0xc9, 0xe7, 0xf8, 0x7e, 0x78, 0x8e, 0xee, + 0xc0, 0x63, 0xc9, 0x8c, 0x2a, 0xf9, 0xf6, 0x12, 0xb3, 0xdd, 0x8f, 0x29, 0x28, 0x17, 0x9b, 0xc5, + 0xa4, 0x05, 0xb5, 0x54, 0xa9, 0x44, 0x6c, 0x9a, 0x9d, 0x05, 0x41, 0x4b, 0x43, 0x72, 0x08, 0xcd, + 0xe5, 0xd1, 0x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x5a, 0x56, 0x6e, 0x7f, 0x95, 0x68, + 0x0c, 0x95, 0xa4, 0x1d, 0xc9, 0x76, 0xde, 0xb6, 0xcb, 0x0a, 0xdc, 0xb9, 0x01, 0x42, 0xa5, 0x7b, + 0x0e, 0x8d, 0x85, 0x99, 0x24, 0x1f, 0xe5, 0xe9, 0xb6, 0x4b, 0x33, 0xbc, 0x52, 0xc0, 0xe7, 0xd0, + 0x58, 0x18, 0xce, 0x5c, 0xd1, 0x2f, 0x0f, 0xf3, 0xca, 0xe8, 0x27, 0x50, 0x96, 0x0f, 0x01, 0xb1, + 0x72, 0x6e, 0xfc, 0x8c, 0xa8, 0xed, 0xfc, 0x00, 0xc5, 0xd3, 0x09, 0x94, 0x93, 0xfe, 0xb3, 0x72, + 0x2e, 0xcf, 0x1b, 0xe5, 0x5a, 0xea, 0xb5, 0xae, 0xf9, 0xc3, 0x83, 0xdc, 0xff, 0xa3, 0x3d, 0x3c, + 0xdb, 0x39, 0xaa, 0x48, 0x5e, 0x3e, 0xf8, 0x2f, 0x00, 0x00, 0xff, 0xff, 0x58, 0x29, 0xda, 0xfa, + 0xda, 0x0d, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/api/v1/heimdall.proto b/api/v1/heimdall.proto index 50e5a9d..97d0bdf 100644 --- a/api/v1/heimdall.proto +++ b/api/v1/heimdall.proto @@ -25,15 +25,22 @@ message Master { string id = 1 [(gogoproto.customname) = "ID"]; string grpc_address = 2 [(gogoproto.customname) = "GRPCAddress"]; string redis_url = 3 [(gogoproto.customname) = "RedisURL"]; + repeated Peer peers = 5; } message JoinRequest { string id = 1 [(gogoproto.customname) = "ID"]; string cluster_key = 2; + string grpc_address = 3 [(gogoproto.customname) = "GRPCAddress"]; + string endpoint_ip = 4 [(gogoproto.customname) = "EndpointIP"]; + uint64 endpoint_port = 5; + string interface_name = 6; } message JoinResponse { Master master = 1; + Node node = 2; + repeated Peer peers = 3; } message ConnectRequest { @@ -74,6 +81,7 @@ message Node { uint64 endpoint_port = 5; string gateway_ip = 6 [(gogoproto.customname) = "GatewayIP"]; google.protobuf.Timestamp updated = 7 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false]; + string interface_name = 8; } message NodesRequest {} diff --git a/client/join.go b/client/join.go index ad18ce6..39159d6 100644 --- a/client/join.go +++ b/client/join.go @@ -28,15 +28,7 @@ import ( ) // Join attempts to connect to the peer and returns the master info -func (c *Client) Join(key string) (*v1.Master, error) { +func (c *Client) Join(req *v1.JoinRequest) (*v1.JoinResponse, error) { ctx := context.Background() - resp, err := c.heimdallClient.Join(ctx, &v1.JoinRequest{ - ID: c.id, - ClusterKey: key, - }) - if err != nil { - return nil, err - } - - return resp.Master, nil + return c.heimdallClient.Join(ctx, req) } diff --git a/cmd/heimdall/main.go b/cmd/heimdall/main.go index dc4f779..af7f20c 100644 --- a/cmd/heimdall/main.go +++ b/cmd/heimdall/main.go @@ -60,6 +60,12 @@ func main() { Value: fmt.Sprintf("tcp://%s:%d", heimdall.GetIP(), defaultGRPCPort), EnvVar: "HEIMDALL_GRPC_ADDR", }, + cli.StringFlag{ + Name: "advertise-grpc-address", + Usage: "public advertise grpc address", + Value: fmt.Sprintf("tcp://%s:%d", heimdall.GetIP(), defaultGRPCPort), + EnvVar: "HEIMDALL_ADVERTISE_GRPC_ADDR", + }, cli.StringFlag{ Name: "redis-url, r", Usage: "uri for datastore backend", diff --git a/cmd/heimdall/run.go b/cmd/heimdall/run.go index 1eccbdf..8c51e94 100644 --- a/cmd/heimdall/run.go +++ b/cmd/heimdall/run.go @@ -41,17 +41,18 @@ import ( func runServer(cx *cli.Context) error { cfg := &heimdall.Config{ - ID: cx.String("id"), - GRPCAddress: cx.String("addr"), - GRPCPeerAddress: cx.String("peer"), - ClusterKey: cx.String("cluster-key"), - NodeNetwork: cx.String("node-network"), - PeerNetwork: cx.String("peer-network"), - EndpointIP: cx.String("endpoint-ip"), - EndpointPort: cx.Int("endpoint-port"), - InterfaceName: cx.String("interface-name"), - RedisURL: cx.String("redis-url"), - AdvertiseRedisURL: cx.String("advertise-redis-url"), + ID: cx.String("id"), + GRPCAddress: cx.String("addr"), + AdvertiseGRPCAddress: cx.String("advertise-grpc-address"), + GRPCPeerAddress: cx.String("peer"), + ClusterKey: cx.String("cluster-key"), + NodeNetwork: cx.String("node-network"), + PeerNetwork: cx.String("peer-network"), + EndpointIP: cx.String("endpoint-ip"), + EndpointPort: cx.Int("endpoint-port"), + InterfaceName: cx.String("interface-name"), + RedisURL: cx.String("redis-url"), + AdvertiseRedisURL: cx.String("advertise-redis-url"), } errCh := make(chan error, 1) diff --git a/config.go b/config.go index c8915cd..76c1dd5 100644 --- a/config.go +++ b/config.go @@ -29,6 +29,8 @@ type Config struct { ID string // GRPCAddress is the address for the grpc server GRPCAddress string + // AdvertiseGRPCAddress is the public address for the grpc server + AdvertiseGRPCAddress string // GRPCPeerAddress is the peer address to join GRPCPeerAddress string // ClusterKey is a preshared key for cluster peers diff --git a/server/join.go b/server/join.go index 5de5666..8bbfa82 100644 --- a/server/join.go +++ b/server/join.go @@ -60,7 +60,35 @@ func (s *Server) Join(ctx context.Context, req *v1.JoinRequest) (*v1.JoinRespons return nil, err } + peers, err := s.getPeers(ctx) + if err != nil { + return nil, err + } + + if err := s.ensureNetworkSubnet(ctx, req.ID); err != nil { + return nil, err + } + + node, err := s.getNode(ctx, req.ID) + if err != nil { + if err != redis.ErrNil { + return nil, err + } + n, err := s.createNode(ctx, req) + if err != nil { + return nil, err + } + + if err := s.updatePeerInfo(ctx, req.ID); err != nil { + return nil, err + } + + node = n + } + return &v1.JoinResponse{ Master: &master, + Node: node, + Peers: peers, }, nil } diff --git a/server/net.go b/server/net.go index 20449a9..a766069 100644 --- a/server/net.go +++ b/server/net.go @@ -29,6 +29,7 @@ import ( "strings" "github.com/gomodule/redigo/redis" + "github.com/pkg/errors" ) type subnetRange struct { @@ -37,8 +38,8 @@ type subnetRange struct { Subnet *net.IPNet } -func (s *Server) updateNodeNetwork(ctx context.Context, subnet string) error { - if _, err := s.master(ctx, "SET", s.getNodeNetworkKey(s.cfg.ID), subnet); err != nil { +func (s *Server) updateNodeNetwork(ctx context.Context, id string, subnet string) error { + if _, err := s.master(ctx, "SET", s.getNodeNetworkKey(id), subnet); err != nil { return err } return nil @@ -70,7 +71,7 @@ func (s *Server) getOrAllocatePeerIP(ctx context.Context, id string) (net.IP, *n func (s *Server) getNodeIP(ctx context.Context, id string) (net.IP, *net.IPNet, error) { subnet, err := redis.String(s.local(ctx, "GET", s.getNodeNetworkKey(id))) if err != nil { - return nil, nil, err + return nil, nil, errors.Wrap(err, "error getting node ip") } r, err := parseSubnetRange(subnet) if err != nil { diff --git a/server/node.go b/server/node.go index e8ceba4..c624a35 100644 --- a/server/node.go +++ b/server/node.go @@ -23,6 +23,7 @@ package server import ( "context" + "fmt" "net/url" "sort" "strings" @@ -82,24 +83,15 @@ func (s *Server) getNode(ctx context.Context, id string) (*v1.Node, error) { func (s *Server) configureNode() error { ctx := context.Background() - nodeKeys, err := redis.Strings(s.local(ctx, "KEYS", s.getNodeKey("*"))) + nodes, err := s.getNodes(ctx) if err != nil { return err } // attempt to connect to existing - if len(nodeKeys) > 0 { - for _, nodeKey := range nodeKeys { - nodeData, err := redis.Bytes(s.local(ctx, "GET", nodeKey)) - if err != nil { - logrus.Warn(err) - continue - } - var node v1.Node - if err := proto.Unmarshal(nodeData, &node); err != nil { - return err - } + if len(nodes) > 0 { + for _, node := range nodes { // ignore self - if node.Addr == s.cfg.GRPCAddress { + if node.ID == s.cfg.ID { continue } @@ -109,14 +101,28 @@ func (s *Server) configureNode() error { logrus.Warn(err) continue } - m, err := c.Join(s.cfg.ClusterKey) + r, err := c.Join(&v1.JoinRequest{ + ID: s.cfg.ID, + ClusterKey: s.cfg.ClusterKey, + GRPCAddress: s.cfg.GRPCAddress, + EndpointIP: s.cfg.EndpointIP, + EndpointPort: uint64(s.cfg.EndpointPort), + InterfaceName: s.cfg.InterfaceName, + }) if err != nil { c.Close() logrus.Warn(err) continue } - if err := s.joinMaster(m); err != nil { + // TODO: start tunnel + if err := s.updatePeerConfig(ctx, r.Node, r.Peers); err != nil { + return err + } + // TODO: wait for tunnel to come up + time.Sleep(time.Second * 20) + + if err := s.joinMaster(r.Master); err != nil { c.Close() logrus.Warn(err) continue @@ -154,6 +160,8 @@ func (s *Server) configureNode() error { return err } + // TODO: start tunnel + if err := s.joinMaster(&master); err != nil { return err } @@ -163,14 +171,19 @@ func (s *Server) configureNode() error { return nil } -func (s *Server) disableReplica() { - s.wpool = getPool(s.cfg.RedisURL) +func (s *Server) disableReplica() error { + p, err := getPool(s.cfg.RedisURL) + if err != nil { + return err + } + s.wpool = p // signal replica monitor to stop if started as a peer close(s.replicaCh) // unset peer s.cfg.GRPCPeerAddress = "" + return nil } func (s *Server) replicaMonitor() { @@ -187,7 +200,7 @@ func (s *Server) replicaMonitor() { logrus.Error(err) continue } - if n.ID != s.cfg.ID { + if n == nil || n.ID != s.cfg.ID { logrus.Debugf("waiting for new master to initialize: %s", n.ID) continue } @@ -242,12 +255,15 @@ func (s *Server) masterHeartbeat() { func (s *Server) joinMaster(m *v1.Master) error { // configure replica logrus.Infof("configuring node as replica of %+v", m.ID) - conn, err := redis.DialURL(s.cfg.RedisURL) + pool, err := getPool(s.cfg.RedisURL) if err != nil { - return errors.Wrap(err, "unable to connect to redis") + return err } + + conn := pool.Get() defer conn.Close() + logrus.Debugf("configuring redis as slave of %s", m.RedisURL) u, err := url.Parse(m.RedisURL) if err != nil { return errors.Wrap(err, "error parsing master redis url") @@ -258,15 +274,11 @@ func (s *Server) joinMaster(m *v1.Master) error { if _, err := conn.Do("REPLICAOF", host, port); err != nil { return err } - // auth - auth, ok := u.User.Password() - if ok { - if _, err := conn.Do("CONFIG", "SET", "MASTERAUTH", auth); err != nil { - return errors.Wrap(err, "error authenticating to redis") - } - } - s.wpool = getPool(m.RedisURL) + s.wpool, err = getPool(m.RedisURL) + if err != nil { + return err + } return nil } @@ -275,10 +287,21 @@ func (s *Server) updateMasterInfo(ctx context.Context) error { if _, err := s.master(ctx, "SET", clusterKey, s.cfg.ClusterKey); err != nil { return err } + // build redis url with gateway ip + gatewayIP, _, err := s.getNodeIP(ctx, s.cfg.ID) + if err != nil { + return err + } + u, err := url.Parse(s.cfg.RedisURL) + if err != nil { + return err + } + // update master redis url to gateway to serve over wireguard + u.Host = fmt.Sprintf("%s:%s", gatewayIP.String(), u.Port()) m := &v1.Master{ ID: s.cfg.ID, - GRPCAddress: s.cfg.GRPCAddress, - RedisURL: s.cfg.AdvertiseRedisURL, + GRPCAddress: s.cfg.AdvertiseGRPCAddress, + RedisURL: u.String(), } data, err := proto.Marshal(m) if err != nil { @@ -292,48 +315,91 @@ func (s *Server) updateMasterInfo(ctx context.Context) error { if _, err := s.master(ctx, "EXPIRE", masterKey, int(masterHeartbeatInterval.Seconds())); err != nil { return errors.Wrap(err, "error setting expire for master info") } + return nil } -func (s *Server) nodeHeartbeat(ctx context.Context) { +func (s *Server) updateNodeInfo(ctx context.Context) { logrus.Debugf("starting node heartbeat: ttl=%s", nodeHeartbeatInterval) t := time.NewTicker(nodeHeartbeatInterval) - key := s.getNodeKey(s.cfg.ID) for range t.C { - keyPair, err := s.getOrCreateKeyPair(ctx, s.cfg.ID) - if err != nil { - logrus.Error(err) - continue - } - nodeIP, _, err := s.getNodeIP(ctx, s.cfg.ID) - if err != nil { - logrus.Error(err) - continue - } - node := &v1.Node{ - Updated: time.Now(), - ID: s.cfg.ID, - Addr: s.cfg.GRPCAddress, - KeyPair: keyPair, - EndpointIP: s.cfg.EndpointIP, - EndpointPort: uint64(s.cfg.EndpointPort), - GatewayIP: nodeIP.String(), - } - - data, err := proto.Marshal(node) - if err != nil { - logrus.Error(err) - continue - } - - if _, err := s.master(ctx, "SET", key, data); err != nil { - logrus.Error(err) - continue - } - - if _, err := s.master(ctx, "EXPIRE", key, nodeHeartbeatExpiry); err != nil { + if err := s.updateLocalNodeInfo(ctx); err != nil { logrus.Error(err) continue } } } + +func (s *Server) updateLocalNodeInfo(ctx context.Context) error { + key := s.getNodeKey(s.cfg.ID) + keyPair, err := s.getOrCreateKeyPair(ctx, s.cfg.ID) + if err != nil { + return err + } + nodeIP, _, err := s.getNodeIP(ctx, s.cfg.ID) + if err != nil { + return err + } + node := &v1.Node{ + Updated: time.Now(), + ID: s.cfg.ID, + Addr: s.cfg.GRPCAddress, + KeyPair: keyPair, + EndpointIP: s.cfg.EndpointIP, + EndpointPort: uint64(s.cfg.EndpointPort), + GatewayIP: nodeIP.String(), + InterfaceName: s.cfg.InterfaceName, + } + + data, err := proto.Marshal(node) + if err != nil { + return err + } + + if _, err := s.master(ctx, "SET", key, data); err != nil { + return err + } + + if _, err := s.master(ctx, "EXPIRE", key, nodeHeartbeatExpiry); err != nil { + return err + } + + return nil +} + +func (s *Server) createNode(ctx context.Context, req *v1.JoinRequest) (*v1.Node, error) { + key := s.getNodeKey(req.ID) + keyPair, err := s.getOrCreateKeyPair(ctx, req.ID) + if err != nil { + return nil, errors.Wrapf(err, "error getting/creating keypair for %s", req.ID) + } + nodeIP, _, err := s.getNodeIP(ctx, req.ID) + if err != nil { + return nil, errors.Wrapf(err, "error getting node ip for %s", req.ID) + } + node := &v1.Node{ + Updated: time.Now(), + ID: req.ID, + Addr: req.GRPCAddress, + KeyPair: keyPair, + EndpointIP: req.EndpointIP, + EndpointPort: uint64(req.EndpointPort), + GatewayIP: nodeIP.String(), + InterfaceName: req.InterfaceName, + } + + data, err := proto.Marshal(node) + if err != nil { + return nil, err + } + + if _, err := s.master(ctx, "SET", key, data); err != nil { + return nil, err + } + + if _, err := s.master(ctx, "EXPIRE", key, nodeHeartbeatExpiry); err != nil { + return nil, err + } + + return node, nil +} diff --git a/server/peer.go b/server/peer.go index c2c8b72..e57aa4d 100644 --- a/server/peer.go +++ b/server/peer.go @@ -69,10 +69,9 @@ func (s *Server) getPeers(ctx context.Context) ([]*v1.Peer, error) { func (s *Server) peerUpdater(ctx context.Context) { logrus.Debugf("starting peer config updater: ttl=%s", peerConfigUpdateInterval) - t := time.NewTicker(peerConfigUpdateInterval) - for range t.C { + logrus.Debug("peer config update") uctx, cancel := context.WithTimeout(ctx, peerConfigUpdateInterval) if err := s.updatePeerInfo(uctx, s.cfg.ID); err != nil { logrus.Errorf("updateLocalPeerInfo: %s", err) @@ -80,7 +79,23 @@ func (s *Server) peerUpdater(ctx context.Context) { continue } - if err := s.updatePeerConfig(uctx); err != nil { + peers, err := s.getPeers(ctx) + if err != nil { + logrus.Error(err) + cancel() + continue + } + + logrus.Debugf("peer update: peers %+v", peers) + + node, err := s.getNode(ctx, s.cfg.ID) + if err != nil { + logrus.Error(err) + cancel() + continue + } + + if err := s.updatePeerConfig(uctx, node, peers); err != nil { logrus.Errorf("updatePeerConfig: %s", err) cancel() continue @@ -208,47 +223,29 @@ func (s *Server) getPeerInfo(ctx context.Context, id string) (*v1.Peer, error) { return &peer, nil } -func (s *Server) updatePeerConfig(ctx context.Context) error { - peerKeys, err := redis.Strings(s.local(ctx, "KEYS", s.getPeerKey("*"))) - if err != nil { - return err - } - var peers []*v1.Peer - for _, peerKey := range peerKeys { - peerData, err := redis.Bytes(s.local(ctx, "GET", peerKey)) - if err != nil { - return err - } - var p v1.Peer - if err := proto.Unmarshal(peerData, &p); err != nil { - return err - } - +func (s *Server) updatePeerConfig(ctx context.Context, node *v1.Node, peers []*v1.Peer) error { + var nodePeers []*v1.Peer + for _, peer := range peers { // do not add self as a peer - if p.ID == s.cfg.ID { + if peer.ID == node.ID { continue } - peers = append(peers, &p) + nodePeers = append(nodePeers, peer) } - keyPair, err := s.getOrCreateKeyPair(ctx, s.cfg.ID) + keyPair, err := s.getOrCreateKeyPair(ctx, node.ID) if err != nil { return err } - gatewayIP, gatewayNet, err := s.getNodeIP(ctx, s.cfg.ID) - if err != nil { - return err - } - - size, _ := gatewayNet.Mask.Size() + //size, _ := gatewayNet.Mask.Size() wireguardCfg := &wg.Config{ - Iface: s.cfg.InterfaceName, + Iface: node.InterfaceName, PrivateKey: keyPair.PrivateKey, - ListenPort: s.cfg.EndpointPort, - Address: fmt.Sprintf("%s/%d", gatewayIP.To4().String(), size), - Peers: peers, + ListenPort: int(node.EndpointPort), + Address: fmt.Sprintf("%s/%d", node.GatewayIP, 16), + Peers: nodePeers, } wireguardConfigPath := s.getWireguardConfigPath() diff --git a/server/server.go b/server/server.go index ec6b087..10b1d60 100644 --- a/server/server.go +++ b/server/server.go @@ -25,6 +25,7 @@ import ( "context" "fmt" "io/ioutil" + "net/url" "path/filepath" "runtime" "runtime/pprof" @@ -81,7 +82,10 @@ type Server struct { // NewServer returns a new Heimdall server func NewServer(cfg *heimdall.Config) (*Server, error) { - pool := getPool(cfg.RedisURL) + pool, err := getPool(cfg.RedisURL) + if err != nil { + return nil, err + } return &Server{ cfg: cfg, rpool: pool, @@ -121,13 +125,28 @@ func (s *Server) Run() error { } defer c.Close() - master, err := c.Join(s.cfg.ClusterKey) + r, err := c.Join(&v1.JoinRequest{ + ID: s.cfg.ID, + ClusterKey: s.cfg.ClusterKey, + GRPCAddress: s.cfg.GRPCAddress, + EndpointIP: s.cfg.EndpointIP, + EndpointPort: uint64(s.cfg.EndpointPort), + InterfaceName: s.cfg.InterfaceName, + }) if err != nil { return err } - logrus.Debugf("master info received: %+v", master) - if err := s.joinMaster(master); err != nil { + logrus.Debugf("response: %+v", r) + // start tunnel + if err := s.updatePeerConfig(ctx, r.Node, r.Peers); err != nil { + return errors.Wrap(err, "error updating peer config") + } + // TODO: wait for tunnel to come up + time.Sleep(time.Second * 20) + + logrus.Debugf("master info received: %+v", r) + if err := s.joinMaster(r.Master); err != nil { return err } @@ -144,12 +163,17 @@ func (s *Server) Run() error { } // ensure node network subnet - if err := s.ensureNetworkSubnet(ctx); err != nil { + if err := s.ensureNetworkSubnet(ctx, s.cfg.ID); err != nil { + return err + } + + // initial node update + if err := s.updateLocalNodeInfo(ctx); err != nil { return err } // start node heartbeat to update in redis - go s.nodeHeartbeat(ctx) + go s.updateNodeInfo(ctx) // initial peer info update if err := s.updatePeerInfo(ctx, s.cfg.ID); err != nil { @@ -157,7 +181,15 @@ func (s *Server) Run() error { } // initial config update - if err := s.updatePeerConfig(ctx); err != nil { + node, err := s.getNode(ctx, s.cfg.ID) + if err != nil { + return err + } + peers, err := s.getPeers(ctx) + if err != nil { + return err + } + if err := s.updatePeerConfig(ctx, node, peers); err != nil { return err } @@ -184,7 +216,7 @@ func (s *Server) Run() error { } }() - err := <-errCh + err = <-errCh return err } @@ -194,20 +226,33 @@ func (s *Server) Stop() error { return nil } -func getPool(u string) *redis.Pool { +func getPool(redisUrl string) (*redis.Pool, error) { pool := redis.NewPool(func() (redis.Conn, error) { - conn, err := redis.DialURL(u) + conn, err := redis.DialURL(redisUrl) if err != nil { return nil, errors.Wrap(err, "unable to connect to redis") } + + u, err := url.Parse(redisUrl) + if err != nil { + return nil, err + } + + auth, ok := u.User.Password() + if ok { + logrus.Debug("setting masterauth for redis") + if _, err := conn.Do("CONFIG", "SET", "MASTERAUTH", auth); err != nil { + return nil, errors.Wrap(err, "error authenticating to redis") + } + } return conn, nil }, 10) - return pool + return pool, nil } -func (s *Server) ensureNetworkSubnet(ctx context.Context) error { - network, err := redis.String(s.local(ctx, "GET", s.getNodeNetworkKey(s.cfg.ID))) +func (s *Server) ensureNetworkSubnet(ctx context.Context, id string) error { + network, err := redis.String(s.local(ctx, "GET", s.getNodeNetworkKey(id))) if err != nil { if err != redis.ErrNil { return err @@ -222,7 +267,6 @@ func (s *Server) ensureNetworkSubnet(ctx context.Context) error { if err != nil { return err } - logrus.Debug(nodeNetworkKeys) lookup := map[string]struct{}{} for _, netKey := range nodeNetworkKeys { n, err := redis.String(s.local(ctx, "GET", netKey)) @@ -244,8 +288,8 @@ func (s *Server) ensureNetworkSubnet(ctx context.Context) error { subnet = n continue } - logrus.Debugf("allocated network %s for %s", n.String(), s.cfg.ID) - if err := s.updateNodeNetwork(ctx, n.String()); err != nil { + logrus.Debugf("allocated network %s for %s", n.String(), id) + if err := s.updateNodeNetwork(ctx, id, n.String()); err != nil { return err } break @@ -253,7 +297,7 @@ func (s *Server) ensureNetworkSubnet(ctx context.Context) error { return nil } - logrus.Debugf("node network: %s", network) + logrus.Debugf("node network for %s: %s", id, network) return nil }