From d171aeb45e8fec53802682aa18dba293535fc621 Mon Sep 17 00:00:00 2001 From: Alex Gustafsson Date: Mon, 13 Jan 2025 15:53:43 +0100 Subject: [PATCH] Add support for Docker Compose labels --- cmd/cupdate/main.go | 2 +- internal/platform/docker/platform.go | 14 +++++++++++++- internal/platform/docker/resources.go | 6 ++++++ internal/worker/worker.go | 2 ++ web/graph.tsx | 2 ++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/cmd/cupdate/main.go b/cmd/cupdate/main.go index 412c0c0..ed25b75 100644 --- a/cmd/cupdate/main.go +++ b/cmd/cupdate/main.go @@ -323,7 +323,7 @@ func main() { Type: string(n.Kind()), Name: n.Name(), } - if node.Type() == "docker/"+docker.ResourceKindSwarmNamespace { + if node.Type() == "docker/"+docker.ResourceKindSwarmNamespace || node.Type() == "docker/"+docker.ResourceKindComposeProject { namespaceNode = &node } case platform.ImageNode: diff --git a/internal/platform/docker/platform.go b/internal/platform/docker/platform.go index d1a56f8..f1c3c0d 100644 --- a/internal/platform/docker/platform.go +++ b/internal/platform/docker/platform.go @@ -240,7 +240,7 @@ func (p *Platform) Graph(ctx context.Context) (*graph.Graph[platform.Node], erro }, } - // Add graph nodes for Docker Swarm, if available + // Add graph nodes for Docker Swarm and Compose, if available if container.Labels != nil { if taskID, ok := container.Labels["com.docker.swarm.task.id"]; ok { taskName, ok := container.Labels["com.docker.swarm.task.name"] @@ -266,6 +266,12 @@ func (p *Platform) Graph(ctx context.Context) (*graph.Graph[platform.Node], erro id: fmt.Sprintf("docker/swarm/service/%s", serviceID), name: serviceName, }) + } else if service, ok := container.Labels["com.docker.compose.service"]; ok { + tree = append(tree, resource{ + kind: ResourceKindComposeService, + id: fmt.Sprintf("docker/compose/service/%s", service), + name: service, + }) } if namespace, ok := container.Labels["com.docker.stack.namespace"]; ok { @@ -274,6 +280,12 @@ func (p *Platform) Graph(ctx context.Context) (*graph.Graph[platform.Node], erro id: fmt.Sprintf("docker/swarm/namespace/%s", namespace), name: namespace, }) + } else if project, ok := container.Labels["com.docker.compose.project"]; ok { + tree = append(tree, resource{ + kind: ResourceKindComposeProject, + id: fmt.Sprintf("docker/compose/project/%s", project), + name: project, + }) } } diff --git a/internal/platform/docker/resources.go b/internal/platform/docker/resources.go index 43ef82b..8148c48 100644 --- a/internal/platform/docker/resources.go +++ b/internal/platform/docker/resources.go @@ -13,6 +13,8 @@ const ( ResourceKindSwarmTask = "swarm/task" ResourceKindSwarmService = "swarm/service" ResourceKindSwarmNamespace = "swarm/namespace" + ResourceKindComposeProject = "compose/project" + ResourceKindComposeService = "compose/service" ) type Resource interface { @@ -58,6 +60,10 @@ func TagName(kind ResourceKind) string { return "service" case ResourceKindSwarmNamespace: return "namespace" + case ResourceKindComposeProject: + return "project" + case ResourceKindComposeService: + return "service" default: // Panic as missing entries would be a programming issue, not runtime // bug diff --git a/internal/worker/worker.go b/internal/worker/worker.go index 191c271..9539abd 100644 --- a/internal/worker/worker.go +++ b/internal/worker/worker.go @@ -142,6 +142,8 @@ func (w *Worker) ProcessRawImage(ctx context.Context, reference oci.Reference) e case "docker": if node.Type == docker.ResourceKindSwarmNamespace { data.InsertTag("namespace:" + node.Name) + } else if node.Type == docker.ResourceKindComposeProject { + data.InsertTag("project:" + node.Name) } } } diff --git a/web/graph.tsx b/web/graph.tsx index f248729..01050c9 100644 --- a/web/graph.tsx +++ b/web/graph.tsx @@ -50,6 +50,8 @@ const titles: Record | undefined> = { 'swarm/task': 'Task', 'swarm/service': 'Service', 'swarm/namespace': 'Namespace', + 'compose/service': 'Service', + 'compose/project': 'Project', }, }