Skip to content

Commit 0c0ad3a

Browse files
committed
Allow configuration of multiple MQTT brokers.
This contains the changes from #141 by @sa-wilson with some minor modifications. Closes #141.
1 parent 619d1eb commit 0c0ad3a

File tree

6 files changed

+37
-20
lines changed

6 files changed

+37
-20
lines changed

cmd/chirpstack-gateway-bridge/cmd/configfile.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,13 @@ marshaler="{{ .Integration.Marshaler }}"
220220
221221
# Generic MQTT authentication.
222222
[integration.mqtt.auth.generic]
223-
# MQTT server (e.g. scheme://host:port where scheme is tcp, ssl or ws)
224-
server="{{ .Integration.MQTT.Auth.Generic.Server }}"
223+
# MQTT servers.
224+
#
225+
# Configure one or multiple MQTT server to connect to. Each item must be in
226+
# the following format: scheme://host:port where scheme is tcp, ssl or ws.
227+
servers=[{{ range $index, $elm := .Integration.MQTT.Auth.Generic.Servers }}
228+
"{{ $elm }}",{{ end }}
229+
]
225230
226231
# Connect with the given username (optional)
227232
username="{{ .Integration.MQTT.Auth.Generic.Username }}"

cmd/chirpstack-gateway-bridge/cmd/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func init() {
5757
viper.SetDefault("integration.mqtt.command_topic_template", "gateway/{{ .GatewayID }}/command/#")
5858
viper.SetDefault("integration.mqtt.max_reconnect_interval", 10*time.Minute)
5959

60-
viper.SetDefault("integration.mqtt.auth.generic.server", "tcp://127.0.0.1:1883")
60+
viper.SetDefault("integration.mqtt.auth.generic.servers", []string{"tcp://127.0.0.1:1883"})
6161
viper.SetDefault("integration.mqtt.auth.generic.clean_session", true)
6262

6363
viper.SetDefault("integration.mqtt.auth.gcp_cloud_iot_core.server", "ssl://mqtt.googleapis.com:8883")
@@ -127,6 +127,11 @@ func initConfig() {
127127
config.C.Filters.NetIDs = config.C.Backend.BasicStation.Filters.NetIDs
128128
config.C.Filters.JoinEUIs = config.C.Backend.BasicStation.Filters.JoinEUIs
129129
}
130+
131+
// migrate server to servers
132+
if config.C.Integration.MQTT.Auth.Generic.Server != "" {
133+
config.C.Integration.MQTT.Auth.Generic.Servers = []string{config.C.Integration.MQTT.Auth.Generic.Server}
134+
}
130135
}
131136

132137
func viperBindEnvs(iface interface{}, parts ...string) {

docs/content/install/config.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,13 @@ marshaler="protobuf"
250250

251251
# Generic MQTT authentication.
252252
[integration.mqtt.auth.generic]
253-
# MQTT server (e.g. scheme://host:port where scheme is tcp, ssl or ws)
254-
server="tcp://127.0.0.1:1883"
253+
# MQTT servers.
254+
#
255+
# Configure one or multiple MQTT server to connect to. Each item must be in
256+
# the following format: scheme://host:port where scheme is tcp, ssl or ws.
257+
servers=[
258+
"tcp://127.0.0.1:1883",
259+
]
255260

256261
# Connect with the given username (optional)
257262
username=""

internal/config/config.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ type Config struct {
6262
Type string `mapstructure:"type"`
6363

6464
Generic struct {
65-
Server string `mapstructure:"server"`
66-
Username string `mapstructure:"username"`
67-
Password string `mapstrucure:"password"`
68-
CACert string `mapstructure:"ca_cert"`
69-
TLSCert string `mapstructure:"tls_cert"`
70-
TLSKey string `mapstructure:"tls_key"`
71-
QOS uint8 `mapstructure:"qos"`
72-
CleanSession bool `mapstructure:"clean_session"`
73-
ClientID string `mapstructure:"client_id"`
65+
Server string `mapstructure:"server"`
66+
Servers []string `mapstructure:"servers"`
67+
Username string `mapstructure:"username"`
68+
Password string `mapstrucure:"password"`
69+
CACert string `mapstructure:"ca_cert"`
70+
TLSCert string `mapstructure:"tls_cert"`
71+
TLSKey string `mapstructure:"tls_key"`
72+
QOS uint8 `mapstructure:"qos"`
73+
CleanSession bool `mapstructure:"clean_session"`
74+
ClientID string `mapstructure:"client_id"`
7475
} `mapstructure:"generic"`
7576

7677
GCPCloudIoTCore struct {

internal/integration/mqtt/auth/generic.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// GenericAuthentication implements a generic MQTT authentication.
1414
type GenericAuthentication struct {
15-
server string
15+
servers []string
1616
username string
1717
password string
1818
cleanSession bool
@@ -33,9 +33,8 @@ func NewGenericAuthentication(conf config.Config) (Authentication, error) {
3333
}
3434

3535
return &GenericAuthentication{
36-
tlsConfig: tlsConfig,
37-
38-
server: conf.Integration.MQTT.Auth.Generic.Server,
36+
tlsConfig: tlsConfig,
37+
servers: conf.Integration.MQTT.Auth.Generic.Servers,
3938
username: conf.Integration.MQTT.Auth.Generic.Username,
4039
password: conf.Integration.MQTT.Auth.Generic.Password,
4140
cleanSession: conf.Integration.MQTT.Auth.Generic.CleanSession,
@@ -45,7 +44,9 @@ func NewGenericAuthentication(conf config.Config) (Authentication, error) {
4544

4645
// Init applies the initial configuration.
4746
func (a *GenericAuthentication) Init(opts *mqtt.ClientOptions) error {
48-
opts.AddBroker(a.server)
47+
for _, server := range a.servers {
48+
opts.AddBroker(server)
49+
}
4950
opts.SetUsername(a.username)
5051
opts.SetPassword(a.password)
5152
opts.SetCleanSession(a.cleanSession)

internal/integration/mqtt/backend_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (ts *MQTTBackendTestSuite) SetupSuite() {
5757
conf.Integration.MQTT.EventTopicTemplate = "gateway/{{ .GatewayID }}/event/{{ .EventType }}"
5858
conf.Integration.MQTT.CommandTopicTemplate = "gateway/{{ .GatewayID }}/command/#"
5959
conf.Integration.MQTT.Auth.Type = "generic"
60-
conf.Integration.MQTT.Auth.Generic.Server = server
60+
conf.Integration.MQTT.Auth.Generic.Servers = []string{server}
6161
conf.Integration.MQTT.Auth.Generic.Username = username
6262
conf.Integration.MQTT.Auth.Generic.Password = password
6363
conf.Integration.MQTT.Auth.Generic.CleanSession = true

0 commit comments

Comments
 (0)