work around mongo database uri parsing

This commit is contained in:
rasmus 2022-11-06 02:16:54 +02:00
parent 908ac5c52a
commit 20d867bfcb
1 changed files with 12 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"path"
@ -61,7 +62,7 @@ var App = &cli.App{
// &cli.BoolFlag{Name: "parse-json"}, //TODO:
&cli.StringFlag{Category: "k8s metadata", Name: "pod-namespace", EnvVars: []string{"KUBE_POD_NAMESPACE"}}, // TODO:
&cli.StringFlag{Category: "k8s metadata", Name: "node-name", EnvVars: []string{"KUBE_NODE_NAME"}, Required: true},
&cli.StringFlag{Category: "secrets", Name: "mongo-uri", EnvVars: []string{"MONGO_URI"}, Usage: "mongodb://foo:bar@host:27017", Required: true},
&cli.StringFlag{Category: "secrets", Name: "mongo-uri", EnvVars: []string{"MONGO_URI"}, Usage: "mongodb://foo:bar@host:27017/database", Required: true},
},
Action: func(ctx *cli.Context) error {
@ -111,7 +112,15 @@ var App = &cli.App{
l.Fatal("connecting to mongo", zap.String("uri", dbOpt.GetURI()), zap.Error(err))
}
state.db = dbClient.Database(dbOpt.Auth.AuthSource).Collection("logs")
uriParsed, err := url.ParseRequestURI(ctx.String("mongo-uri"))
if err != nil {
l.Fatal("parsing URI for mongo database name", zap.Error(err))
}
if uriParsed.Path == "" {
l.Fatal("mongo database name must be set in mongo URI")
}
state.db = dbClient.Database(uriParsed.Path).Collection("logs")
state.hostInfo, err = getHostInfo(ctx.String("node-name"))
if err != nil {
@ -193,7 +202,7 @@ func getHostInfo(nodeName string) (h HostInfo, err error) {
h.name = strings.TrimSpace(nodeName)
id, errL := os.ReadFile(MACHINEID)
err = errAppend(err, fmt.Errorf("id: %w", errL))
err = errAppend(err, fmt.Errorf("id: %w", errL))
h.id = strings.TrimSpace(string(id))