diff --git a/.golangci.yml b/.golangci.yml index 71298c9e..c4b36e40 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,6 +6,7 @@ linters: - depguard - exhaustruct - tagliatelle + - wsl settings: errcheck: check-type-assertions: true diff --git a/entry_groups.go b/entry_groups.go index 8ea18523..90fb33f7 100644 --- a/entry_groups.go +++ b/entry_groups.go @@ -40,6 +40,7 @@ func (e *entryGroups) get(containerID string) (*avahi.EntryGroup, func(), error) } e.mutex.Lock() + if _, ok := e.groups[containerID]; !ok { entryGroup, err := e.avahiServer.EntryGroupNew() if err != nil { diff --git a/go.mod b/go.mod index 17a26df2..b37b13eb 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( require ( github.com/carlmjohnson/versioninfo v0.22.5 github.com/coreos/go-systemd/v22 v22.6.0 + github.com/google/gops v0.3.28 ) require ( diff --git a/go.sum b/go.sum index ec8ee0c2..8f49dbe6 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gops v0.3.28 h1:2Xr57tqKAmQYRAfG12E+yLcoa2Y42UJo2lOrUFL9ark= +github.com/google/gops v0.3.28/go.mod h1:6f6+Nl8LcHrzJwi8+p0ii+vmBFSlB4f8cOOkTJ7sk4c= github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms= github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg= github.com/holoplot/go-avahi v1.0.1 h1:XcqR2keL4qWRnlxHD5CAOdWpLFZJ+EOUK0vEuylfvvk= diff --git a/main.go b/main.go index 3449ba7b..401c1e40 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/coreos/go-systemd/v22/daemon" "github.com/docker/docker/client" "github.com/godbus/dbus/v5" + "github.com/google/gops/agent" "github.com/holoplot/go-avahi" "github.com/kelseyhightower/envconfig" "ldddns.arnested.dk/internal/log" @@ -29,6 +30,7 @@ var ( // //nolint:lll type Config struct { + Gops bool `default:"false" json:"Gops" split_words:"true"` HostnameLookup []string `default:"env:VIRTUAL_HOST,containerName" json:"HostnameLookup" split_words:"true"` IgnoreDockerComposeOneoff bool `default:"true" json:"IgnoreDockerComposeOneoff" split_words:"true"` } @@ -53,6 +55,8 @@ func main() { panic(fmt.Errorf("could not read environment config: %w", err)) } + gops(config.Gops) + docker, err := client.NewClientWithOpts( client.FromEnv, client.WithAPIVersionNegotiation(), @@ -132,3 +136,17 @@ func getVersion() string { return version } + +// gops starts the gops agent if the config option is set. +func gops(start bool) { + if !start { + return + } + + err := agent.Listen(agent.Options{ + ShutdownCleanup: true, // automatically closes on os.Interrupt + }) + if err != nil { + panic(fmt.Errorf("could not start gops agent: %w", err)) + } +}