Skip to content

Commit

Permalink
Add support for Docker Compose labels
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGustafsson committed Jan 14, 2025
1 parent 862a0bb commit d171aeb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/cupdate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 13 additions & 1 deletion internal/platform/docker/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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 {
Expand All @@ -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,
})
}
}

Expand Down
6 changes: 6 additions & 0 deletions internal/platform/docker/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
ResourceKindSwarmTask = "swarm/task"
ResourceKindSwarmService = "swarm/service"
ResourceKindSwarmNamespace = "swarm/namespace"
ResourceKindComposeProject = "compose/project"
ResourceKindComposeService = "compose/service"
)

type Resource interface {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions web/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const titles: Record<string, Record<string, string | undefined> | undefined> = {
'swarm/task': 'Task',
'swarm/service': 'Service',
'swarm/namespace': 'Namespace',
'compose/service': 'Service',
'compose/project': 'Project',
},
}

Expand Down

0 comments on commit d171aeb

Please sign in to comment.