Skip to content

Commit 3c5a7d1

Browse files
authored
Merge pull request #416 from arnested/harden-entrygroups
harden entrygroups
2 parents ebd2971 + 8d871eb commit 3c5a7d1

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

container.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func handleContainer(
2424
docker *client.Client,
2525
containerID string,
2626
egs *entryGroups,
27-
status string,
27+
status events.Action,
2828
config Config,
2929
) error {
3030
entryGroup, commit, err := egs.get(containerID)
@@ -140,7 +140,7 @@ func listen(ctx context.Context, config Config, docker *client.Client, egs *entr
140140
case err := <-errs:
141141
panic(fmt.Errorf("go error reading docker events: %w", err))
142142
case msg := <-msgs:
143-
err := handleContainer(ctx, docker, msg.ID, egs, msg.Status, config)
143+
err := handleContainer(ctx, docker, msg.Actor.ID, egs, msg.Action, config)
144144
if err != nil {
145145
log.Logf(log.PriErr, "handling container: %v", err)
146146
}

entry_groups.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func newEntryGroups(avahiServer *avahi.Server) *entryGroups {
2424

2525
func (e *entryGroups) get(containerID string) (*avahi.EntryGroup, func(), error) {
2626
commit := func() {
27+
defer e.mutex.Unlock()
28+
2729
empty, err := e.groups[containerID].IsEmpty()
2830
if err != nil {
2931
log.Logf(log.PriErr, "checking whether Avahi entry group is empty: %v", err)
@@ -35,18 +37,18 @@ func (e *entryGroups) get(containerID string) (*avahi.EntryGroup, func(), error)
3537
log.Logf(log.PriErr, "error committing: %v", err)
3638
}
3739
}
38-
39-
e.mutex.Unlock()
4040
}
4141

4242
e.mutex.Lock()
4343
if _, ok := e.groups[containerID]; !ok {
44-
eg, err := e.avahiServer.EntryGroupNew()
44+
entryGroup, err := e.avahiServer.EntryGroupNew()
4545
if err != nil {
46-
return nil, commit, fmt.Errorf("error creating new entry group: %w", err)
46+
e.mutex.Unlock()
47+
48+
return nil, func() {}, fmt.Errorf("error creating new entry group: %w", err)
4749
}
4850

49-
e.groups[containerID] = eg
51+
e.groups[containerID] = entryGroup
5052
}
5153

5254
return e.groups[containerID], commit, nil

internal/container/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c Container) Services() map[string]uint16 {
5656
continue
5757
}
5858

59-
//nolint:mnd,gomnd
59+
//nolint:mnd
6060
if portNumber > 65535 {
6161
log.Logf(log.PriErr, "Port number %d is too large", portNumber)
6262

internal/container/container_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"os"
88
"testing"
99

10-
"github.com/docker/docker/api/types"
10+
docker_container "github.com/docker/docker/api/types/container"
1111
"ldddns.arnested.dk/internal/container"
1212
)
1313

14-
func containerJSON() (*types.ContainerJSON, error) {
14+
func containerJSON() (*docker_container.InspectResponse, error) {
1515
jsonFile, err := os.Open("../../testdata/container.json")
1616
if err != nil {
1717
return nil, fmt.Errorf("opening JSON test data: %w", err)
@@ -25,7 +25,7 @@ func containerJSON() (*types.ContainerJSON, error) {
2525
}
2626

2727
// we initialize our Users array
28-
var containerJSON *types.ContainerJSON
28+
var containerJSON *docker_container.InspectResponse
2929

3030
err = json.Unmarshal(byteValue, &containerJSON)
3131
if err != nil {

internal/hostname/hostname_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"os"
88
"testing"
99

10-
"github.com/docker/docker/api/types"
10+
docker_container "github.com/docker/docker/api/types/container"
1111
"ldddns.arnested.dk/internal/container"
1212
"ldddns.arnested.dk/internal/hostname"
1313
)
1414

15-
func containerJSON() (*types.ContainerJSON, error) {
15+
func containerJSON() (*docker_container.InspectResponse, error) {
1616
jsonFile, err := os.Open("../../testdata/container.json")
1717
if err != nil {
1818
return nil, fmt.Errorf("opening JSON test data: %w", err)
@@ -26,7 +26,7 @@ func containerJSON() (*types.ContainerJSON, error) {
2626
}
2727

2828
// we initialize our Users array
29-
var containerJSON *types.ContainerJSON
29+
var containerJSON *docker_container.InspectResponse
3030

3131
err = json.Unmarshal(byteValue, &containerJSON)
3232
if err != nil {

0 commit comments

Comments
 (0)