Don't crash on not working database connection
If the connection to a mongo instance fails, the controller hard crashed like this:
{"level":"error","msg":"Error reading in gNMI subscription paths, can not watch network elements automati
cally: open configs/gNMISubscriptions.txt: no such file or directory","time":"2024-06-06T22:38:20+02:00"}
{"level":"info","msg":"will try to connect to rabbitmq: amqp://guest:guest@localhost:5672/","time":"2024-
06-06T22:38:20+02:00"}
{"level":"info","msg":"Connected to RabbitMQ","time":"2024-06-06T22:38:20+02:00"}
{"level":"info","msg":"Could not connect to database","time":"2024-06-06T22:38:25+02:00"}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x1048fa4b0]
goroutine 1 [running]:
go.mongodb.org/mongo-driver/mongo.newCollection(0x0, {0x104c5f20d, 0xf}, {0x0?, 0x0?, 0x0?})
/Users/aps/go/pkg/mod/go.mongodb.org/mongo-driver@v1.15.0/mongo/collection.go:74 +0xa0
go.mongodb.org/mongo-driver/mongo.(*Database).Collection(...)
/Users/aps/go/pkg/mod/go.mongodb.org/mongo-driver@v1.15.0/mongo/database.go:109
code.fbi.h-da.de/danet/gosdn/controller/topology/nodes.NewDatabaseNodeStore(...)
/Users/aps/code/hda/gosdn/controller/topology/nodes/databaseNodeStore.go:38
code.fbi.h-da.de/danet/gosdn/controller/topology/nodes.NewNodeStore(0x105a6cba0?)
/Users/aps/code/hda/gosdn/controller/topology/nodes/store.go:16 +0x58
code.fbi.h-da.de/danet/gosdn/controller.initialize()
/Users/aps/code/hda/gosdn/controller/controller.go:103 +0xa4
code.fbi.h-da.de/danet/gosdn/controller.Run.func1()
/Users/aps/code/hda/gosdn/controller/controller.go:384 +0x24
sync.(*Once).doSlow(0x7?, 0x140000c2100?)
/usr/local/go/src/sync/once.go:74 +0x100
sync.(*Once).Do(...)
/usr/local/go/src/sync/once.go:65
code.fbi.h-da.de/danet/gosdn/controller.Run({0x1051851f0, 0x140000c8050})
/Users/aps/code/hda/gosdn/controller/controller.go:383 +0x68
code.fbi.h-da.de/danet/gosdn/controller/cmd.init.func1(0x140000ec800?, {0x104c52846?, 0x4?, 0x104c5280a?}
)
/Users/aps/code/hda/gosdn/controller/cmd/root.go:65 +0x48
github.com/spf13/cobra.(*Command).execute(0x105a72800, {0x14000130040, 0x0, 0x0})
/Users/aps/go/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:983 +0x840
github.com/spf13/cobra.(*Command).ExecuteC(0x105a72800)
/Users/aps/go/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:1115 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
/Users/aps/go/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:1039
code.fbi.h-da.de/danet/gosdn/controller/cmd.Execute()
/Users/aps/code/hda/gosdn/controller/cmd/root.go:72 +0x2c
main.main()
/Users/aps/code/hda/gosdn/controller/cmd/gosdn/main.go:48 +0x4c
With this change it will gracefully "crash" without the segmentation fault.
{"level":"error","msg":"Error reading in gNMI subscription paths, can not watch network elements automatically: open configs/gNMISubscriptions.txt: no such file or directory","time":"2024-06-06T22:42:58+02:00"}
{"level":"info","msg":"will try to connect to rabbitmq: amqp://guest:guest@localhost:5672/","time":"2024-06-06T22:42:58+02:00"}
{"level":"info","msg":"Connected to RabbitMQ","time":"2024-06-06T22:42:58+02:00"}
{"level":"info","msg":"Could not connect to database","time":"2024-06-06T22:43:03+02:00"}
{"level":"error","msg":"failed to connect to database: server selection error: context deadline exceeded, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }","time":"2024-06-06T22:43:03+02:00"}
Error: failed to connect to database: server selection error: context deadline exceeded, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
Usage:
gosdn [flags]
gosdn [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
version returns information about the controllers version
Flags:
--config string config file (default is ./configs/gosdn.toml)
--csbi-orchestrator string csbi orchestrator address
-h, --help help for gosdn
-l, --log-level string log level 'debug' or 'trace'
--plugin-folder string folder holding all goSDN specific plugins
--plugin-registry string plugin registry address
-s, --security string security level 'secure' or 'insecure'
--socket string port for the controllers gRPC northbound interface
Use "gosdn [command] --help" for more information about a command.
{"level":"error","msg":"failed to connect to database: server selection error: context deadline exceeded, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }","time":"2024-06-06T22:43:03+02:00"}