From cede43eae9f99e489b21c82c56ed61ce8a0fb30c Mon Sep 17 00:00:00 2001 From: "christian.werz" Date: Mon, 24 Feb 2025 15:49:07 +0100 Subject: [PATCH 1/3] add docker swarm support --- app/discovery/events.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/discovery/events.go b/app/discovery/events.go index 83cb66e..08ce123 100644 --- a/app/discovery/events.go +++ b/app/discovery/events.go @@ -136,6 +136,10 @@ func (e *EventNotif) emitRunningContainers() error { for _, c := range containers { containerName := strings.TrimPrefix(c.Names[0], "/") + if taskID, ok := c.Labels["com.docker.swarm.task.id"]; ok && taskID != "" { + containerName = strings.Replace(containerName, "."+taskID, "", -1) + log.Printf("[INFO] Container %+s is part of swarm. so use this name %+s", strings.TrimPrefix(c.Names[0], "/"), containerName) + } if !e.isAllowed(containerName) { log.Printf("[INFO] container %s excluded", containerName) continue From 3a4fa49a9649e450acd23035e03df3fd1a811d5e Mon Sep 17 00:00:00 2001 From: "christian.werz" Date: Mon, 24 Feb 2025 16:51:16 +0100 Subject: [PATCH 2/3] add APP_UID to handle permission denied of /var/run/docker.sock --- Dockerfile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 38443d1..9fead49 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM umputun/baseimage:buildgo-latest as build +FROM umputun/baseimage:buildgo-latest AS build ARG GIT_BRANCH ARG GITHUB_SHA @@ -6,6 +6,7 @@ ARG CI ENV CGO_ENABLED=0 + ADD . /build WORKDIR /build @@ -19,7 +20,7 @@ RUN \ FROM umputun/baseimage:app-latest LABEL org.opencontainers.image.source="https://github.com/umputun/docker-logger" - +ENV APP_UID=995 COPY --from=build /build/docker-logger /srv/docker-logger RUN \ chown -R app:app /srv && \ From 9d37af69edca2538e6baeb2965409dd1ddcf47bf Mon Sep 17 00:00:00 2001 From: "christian.werz" Date: Mon, 24 Feb 2025 21:59:38 +0100 Subject: [PATCH 3/3] add labels support and refactor also event --- app/discovery/events.go | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/app/discovery/events.go b/app/discovery/events.go index 08ce123..d814944 100644 --- a/app/discovery/events.go +++ b/app/discovery/events.go @@ -36,6 +36,7 @@ type DockerClient interface { } var reGroup = regexp.MustCompile(`/(.*?)/`) +var reSwarm = regexp.MustCompile(`(?m)(.*)\.(\d+)\.(.*)`) // NewEventNotif makes EventNotif publishing all changes to eventsCh func NewEventNotif(dockerClient DockerClient, excludes, includes []string, includesPattern, excludesPattern string) (*EventNotif, error) { @@ -106,8 +107,8 @@ func (e *EventNotif) activate(client DockerClient) { } log.Printf("[DEBUG] api event %+v", dockerEvent) - containerName := strings.TrimPrefix(dockerEvent.Actor.Attributes["name"], "/") - + containerName := buildContainerName(dockerEvent.Actor.Attributes, strings.TrimPrefix(dockerEvent.Actor.Attributes["name"], "/")) + groupName := buildGroupName(dockerEvent.Actor.Attributes, e.group(dockerEvent.From)) if !e.isAllowed(containerName) { log.Printf("[INFO] container %s excluded", containerName) continue @@ -118,7 +119,7 @@ func (e *EventNotif) activate(client DockerClient) { ContainerName: containerName, Status: contains(dockerEvent.Status, upStatuses), TS: time.Unix(dockerEvent.Time/1000, dockerEvent.TimeNano), - Group: e.group(dockerEvent.From), + Group: groupName, } log.Printf("[INFO] new event %+v", event) e.eventsCh <- event @@ -135,11 +136,8 @@ func (e *EventNotif) emitRunningContainers() error { log.Printf("[DEBUG] total containers = %d", len(containers)) for _, c := range containers { - containerName := strings.TrimPrefix(c.Names[0], "/") - if taskID, ok := c.Labels["com.docker.swarm.task.id"]; ok && taskID != "" { - containerName = strings.Replace(containerName, "."+taskID, "", -1) - log.Printf("[INFO] Container %+s is part of swarm. so use this name %+s", strings.TrimPrefix(c.Names[0], "/"), containerName) - } + containerName := buildContainerName(c.Labels, strings.TrimPrefix(c.Names[0], "/")) + groupName := buildGroupName(c.Labels, e.group(c.Image)) if !e.isAllowed(containerName) { log.Printf("[INFO] container %s excluded", containerName) continue @@ -149,7 +147,7 @@ func (e *EventNotif) emitRunningContainers() error { ContainerName: containerName, ContainerID: c.ID, TS: time.Unix(c.Created/1000, 0), - Group: e.group(c.Image), + Group: groupName, } log.Printf("[DEBUG] running container added, %+v", event) e.eventsCh <- event @@ -191,3 +189,25 @@ func contains(e string, s []string) bool { } return false } + +func buildContainerName(labels map[string]string, containerName string) string { + result := []string{} + if r := reSwarm.FindStringSubmatch(containerName); len(r) == 4 { + result = append(result, r[1]) // service name + result = append(result, r[2]) // replica number + } else if labelName, ok := labels["logger.container.name"]; ok && labelName != "" { + result = append(result, labelName) + } + if len(result) > 0 { + return strings.Join(result, "-") + } + return containerName +} + +func buildGroupName(labels map[string]string, defaultValue string) string { + if labelGroup, ok := labels["logger.group.name"]; ok && labelGroup != "" { + return labelGroup + } + + return defaultValue +}