Skip to content

Commit

Permalink
use docker client's native filter
Browse files Browse the repository at this point in the history
  • Loading branch information
shinebayar-g committed Nov 16, 2022
1 parent ec57c1f commit f0adbca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
22 changes: 10 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"log"
"strings"
"time"

"github.com/docker/docker/api/types"
Expand All @@ -27,6 +26,7 @@ func createClient() (*context.Context, *client.Client, error) {
func streamEvents(ctx *context.Context, c *client.Client) (<-chan events.Message, <-chan error) {
filter := filters.NewArgs()
filter.Add("type", "container")
filter.Add("label", "UFW_MANAGED=TRUE")
return c.Events(*ctx, types.EventsOptions{Filters: filter})
}

Expand Down Expand Up @@ -67,18 +67,16 @@ func main() {
for {
select {
case msg := <-messages:
if ufwManaged := msg.Actor.Attributes["UFW_MANAGED"]; strings.ToUpper(ufwManaged) == "TRUE" {
if msg.Action == "start" {
container, err := client.ContainerInspect(*ctx, msg.ID)
if err != nil {
log.Println("ufw-docker-automated: Couldn't inspect container:", err)
continue
}
createChannel <- &container
}
if msg.Action == "die" {
deleteChannel <- msg.ID[:12]
if msg.Action == "start" {
container, err := client.ContainerInspect(*ctx, msg.ID)
if err != nil {
log.Println("ufw-docker-automated: Couldn't inspect container:", err)
continue
}
createChannel <- &container
}
if msg.Action == "die" {
deleteChannel <- msg.ID[:12]
}
case err := <-errors:
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions ufwhandler/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"log"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)

func Sync(createChannel chan *types.ContainerJSON, client *client.Client, ctx *context.Context) {
var filter = filters.NewArgs()
filter.Add("label", "UFW_MANAGED=TRUE")
containers, err := client.ContainerList(*ctx, types.ContainerListOptions{Filters: filter})
containers, err := client.ContainerList(*ctx, types.ContainerListOptions{})
if err != nil {
log.Println(err)
}
Expand Down

0 comments on commit f0adbca

Please sign in to comment.