From 7d77538ca4dad73594bc20d11b7f85dc0693d7a5 Mon Sep 17 00:00:00 2001 From: aditsachde <23707194+aditsachde@users.noreply.github.com> Date: Wed, 2 Oct 2019 05:29:27 -0400 Subject: [PATCH] patch dendrite microservices with bind config (#795) This PR adds a block in the dendrite config for the services to bind to. The microservices should bind to the addresses in the bind block, and will be contacted at the address in the listen block. This fixes an issue with the microservices and kubernetes services. --- cmd/dendrite-appservice-server/main.go | 3 ++- cmd/dendrite-client-api-server/main.go | 3 ++- cmd/dendrite-federation-api-server/main.go | 3 ++- cmd/dendrite-federation-sender-server/main.go | 3 ++- cmd/dendrite-media-api-server/main.go | 3 ++- cmd/dendrite-public-rooms-api-server/main.go | 3 ++- cmd/dendrite-room-server/main.go | 3 ++- cmd/dendrite-sync-api-server/main.go | 3 ++- cmd/dendrite-typing-server/main.go | 3 ++- common/basecomponent/base.go | 11 ++++++++++- common/config/config.go | 14 ++++++++++++++ common/test/config.go | 10 ++++++++++ 12 files changed, 52 insertions(+), 10 deletions(-) diff --git a/cmd/dendrite-appservice-server/main.go b/cmd/dendrite-appservice-server/main.go index dcaea5138..f203969f4 100644 --- a/cmd/dendrite-appservice-server/main.go +++ b/cmd/dendrite-appservice-server/main.go @@ -35,5 +35,6 @@ func main() { base, accountDB, deviceDB, federation, alias, query, cache, ) - base.SetupAndServeHTTP(string(base.Cfg.Listen.FederationSender)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.AppServiceAPI), string(base.Cfg.Listen.AppServiceAPI)) + } diff --git a/cmd/dendrite-client-api-server/main.go b/cmd/dendrite-client-api-server/main.go index e273ecadb..2bde0f4cf 100644 --- a/cmd/dendrite-client-api-server/main.go +++ b/cmd/dendrite-client-api-server/main.go @@ -45,5 +45,6 @@ func main() { alias, input, query, typingInputAPI, asQuery, transactions.New(), fedSenderAPI, ) - base.SetupAndServeHTTP(string(base.Cfg.Listen.ClientAPI)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.ClientAPI), string(base.Cfg.Listen.ClientAPI)) + } diff --git a/cmd/dendrite-federation-api-server/main.go b/cmd/dendrite-federation-api-server/main.go index 014ed3343..c83845d25 100644 --- a/cmd/dendrite-federation-api-server/main.go +++ b/cmd/dendrite-federation-api-server/main.go @@ -39,5 +39,6 @@ func main() { alias, input, query, asQuery, ) - base.SetupAndServeHTTP(string(base.Cfg.Listen.FederationAPI)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.FederationAPI), string(base.Cfg.Listen.FederationAPI)) + } diff --git a/cmd/dendrite-federation-sender-server/main.go b/cmd/dendrite-federation-sender-server/main.go index 59b98e5bb..71fc0b015 100644 --- a/cmd/dendrite-federation-sender-server/main.go +++ b/cmd/dendrite-federation-sender-server/main.go @@ -32,5 +32,6 @@ func main() { base, federation, query, ) - base.SetupAndServeHTTP(string(base.Cfg.Listen.FederationSender)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.FederationSender), string(base.Cfg.Listen.FederationSender)) + } diff --git a/cmd/dendrite-media-api-server/main.go b/cmd/dendrite-media-api-server/main.go index 718bb6f1b..a818db73a 100644 --- a/cmd/dendrite-media-api-server/main.go +++ b/cmd/dendrite-media-api-server/main.go @@ -28,5 +28,6 @@ func main() { mediaapi.SetupMediaAPIComponent(base, deviceDB) - base.SetupAndServeHTTP(string(base.Cfg.Listen.MediaAPI)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.MediaAPI), string(base.Cfg.Listen.MediaAPI)) + } diff --git a/cmd/dendrite-public-rooms-api-server/main.go b/cmd/dendrite-public-rooms-api-server/main.go index 63e1f40b5..b60eed92a 100644 --- a/cmd/dendrite-public-rooms-api-server/main.go +++ b/cmd/dendrite-public-rooms-api-server/main.go @@ -28,5 +28,6 @@ func main() { publicroomsapi.SetupPublicRoomsAPIComponent(base, deviceDB) - base.SetupAndServeHTTP(string(base.Cfg.Listen.PublicRoomsAPI)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.PublicRoomsAPI), string(base.Cfg.Listen.PublicRoomsAPI)) + } diff --git a/cmd/dendrite-room-server/main.go b/cmd/dendrite-room-server/main.go index a5942544d..41b705755 100644 --- a/cmd/dendrite-room-server/main.go +++ b/cmd/dendrite-room-server/main.go @@ -28,5 +28,6 @@ func main() { roomserver.SetupRoomServerComponent(base) - base.SetupAndServeHTTP(string(base.Cfg.Listen.RoomServer)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.RoomServer), string(base.Cfg.Listen.RoomServer)) + } diff --git a/cmd/dendrite-sync-api-server/main.go b/cmd/dendrite-sync-api-server/main.go index 343d3567d..1c47ec525 100644 --- a/cmd/dendrite-sync-api-server/main.go +++ b/cmd/dendrite-sync-api-server/main.go @@ -31,5 +31,6 @@ func main() { syncapi.SetupSyncAPIComponent(base, deviceDB, accountDB, query) - base.SetupAndServeHTTP(string(base.Cfg.Listen.SyncAPI)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.SyncAPI), string(base.Cfg.Listen.SyncAPI)) + } diff --git a/cmd/dendrite-typing-server/main.go b/cmd/dendrite-typing-server/main.go index 4eb0823a9..461eb7144 100644 --- a/cmd/dendrite-typing-server/main.go +++ b/cmd/dendrite-typing-server/main.go @@ -32,5 +32,6 @@ func main() { typingserver.SetupTypingServerComponent(base, cache.NewTypingCache()) - base.SetupAndServeHTTP(string(base.Cfg.Listen.TypingServer)) + base.SetupAndServeHTTP(string(base.Cfg.Bind.TypingServer), string(base.Cfg.Listen.TypingServer)) + } diff --git a/common/basecomponent/base.go b/common/basecomponent/base.go index 503134b23..b05ec42db 100644 --- a/common/basecomponent/base.go +++ b/common/basecomponent/base.go @@ -157,7 +157,16 @@ func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationCli // SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on // ApiMux under /api/ and adds a prometheus handler under /metrics. -func (b *BaseDendrite) SetupAndServeHTTP(addr string) { +func (b *BaseDendrite) SetupAndServeHTTP(bindaddr string, listenaddr string) { + // If a separate bind address is defined, listen on that. Otherwise use + // the listen address + var addr string + if bindaddr != "" { + addr = bindaddr + } else { + addr = listenaddr + } + common.SetupHTTPAPI(http.DefaultServeMux, common.WrapHandlerInCORS(b.APIMux)) logrus.Infof("Starting %s server on %s", b.componentName, addr) diff --git a/common/config/config.go b/common/config/config.go index 16cf26408..0332d0358 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -196,6 +196,20 @@ type Dendrite struct { // The internal addresses the components will listen on. // These should not be exposed externally as they expose metrics and debugging APIs. + // Falls back to addresses listed in Listen if not specified + Bind struct { + MediaAPI Address `yaml:"media_api"` + ClientAPI Address `yaml:"client_api"` + FederationAPI Address `yaml:"federation_api"` + AppServiceAPI Address `yaml:"appservice_api"` + SyncAPI Address `yaml:"sync_api"` + RoomServer Address `yaml:"room_server"` + FederationSender Address `yaml:"federation_sender"` + PublicRoomsAPI Address `yaml:"public_rooms_api"` + TypingServer Address `yaml:"typing_server"` + } `yaml:"bind"` + + // The addresses for talking to other microservices. Listen struct { MediaAPI Address `yaml:"media_api"` ClientAPI Address `yaml:"client_api"` diff --git a/common/test/config.go b/common/test/config.go index 08a1b398f..668e9a26e 100644 --- a/common/test/config.go +++ b/common/test/config.go @@ -106,6 +106,16 @@ func MakeConfig(configDir, kafkaURI, database, host string, startPort int) (*con cfg.Listen.PublicRoomsAPI = assignAddress() cfg.Listen.TypingServer = assignAddress() + // Bind to the same address as the listen address + // All microservices are run on the same host in testing + cfg.Bind.ClientAPI = cfg.Listen.ClientAPI + cfg.Bind.FederationAPI = cfg.Listen.FederationAPI + cfg.Bind.MediaAPI = cfg.Listen.MediaAPI + cfg.Bind.RoomServer = cfg.Listen.RoomServer + cfg.Bind.SyncAPI = cfg.Listen.SyncAPI + cfg.Bind.PublicRoomsAPI = cfg.Listen.PublicRoomsAPI + cfg.Bind.TypingServer = cfg.Listen.TypingServer + return &cfg, port, nil }