Skip to content

A self-hosted dashboard that puts all your feeds in one place

Apply docker containers category filter to parents only

ethanstoner8c432705b12c
diff --git a/internal/glance/widget-docker-containers-category_test.go b/internal/glance/widget-docker-containers-category_test.go
new file mode 100644
index 0000000..3e33282
--- /dev/null
+++ b/internal/glance/widget-docker-containers-category_test.go
@@ -0,0 +1,92 @@
+package glance
+
+import (
+ "reflect"
+ "sort"
+ "testing"
+)
+
+func TestGroupDockerContainerChildrenCategoryFilter(t *testing.T) {
+ container := func(name string, labels dockerContainerLabels) dockerContainerJsonResponse {
+ return dockerContainerJsonResponse{Names: []string{"/" + name}, Labels: labels}
+ }
+
+ containers := []dockerContainerJsonResponse{
+ container("gitea", dockerContainerLabels{
+ dockerContainerLabelID: "gitea",
+ dockerContainerLabelCategory: "external",
+ }),
+ container("gitea-db", dockerContainerLabels{
+ dockerContainerLabelParent: "gitea",
+ }),
+ container("gitea-hidden", dockerContainerLabels{
+ dockerContainerLabelParent: "gitea",
+ dockerContainerLabelHide: "true",
+ }),
+ container("grafana", dockerContainerLabels{
+ dockerContainerLabelCategory: "internal",
+ }),
+ container("uncategorized", nil),
+ }
+
+ tests := []struct {
+ name string
+ category string
+ expected map[string][]string
+ }{
+ {
+ name: "no category filter includes all containers",
+ category: "",
+ expected: map[string][]string{
+ "gitea": {"gitea-db"},
+ "grafana": nil,
+ "uncategorized": nil,
+ },
+ },
+ {
+ name: "child without category is included via matching parent",
+ category: "external",
+ expected: map[string][]string{
+ "gitea": {"gitea-db"},
+ },
+ },
+ {
+ name: "child is excluded when parent does not match",
+ category: "internal",
+ expected: map[string][]string{
+ "grafana": nil,
+ },
+ },
+ {
+ name: "no containers match",
+ category: "nonexistent",
+ expected: map[string][]string{},
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ parents, children := groupDockerContainerChildren(containers, false, test.category)
+
+ // Mirrors how fetchDockerContainers attaches children to their parents
+ actual := make(map[string][]string, len(parents))
+ for i := range parents {
+ parent := &parents[i]
+ var childNames []string
+
+ if id := parent.Labels.getOrDefault(dockerContainerLabelID, ""); id != "" {
+ for j := range children[id] {
+ childNames = append(childNames, deriveDockerContainerName(&children[id][j], false))
+ }
+ }
+
+ sort.Strings(childNames)
+ actual[deriveDockerContainerName(parent, false)] = childNames
+ }
+
+ if !reflect.DeepEqual(actual, test.expected) {
+ t.Errorf("expected %v, got %v", test.expected, actual)
+ }
+ })
+ }
+}
diff --git a/internal/glance/widget-docker-containers.go b/internal/glance/widget-docker-containers.go
index 1dec239..b69ab0f 100644
--- a/internal/glance/widget-docker-containers.go
+++ b/internal/glance/widget-docker-containers.go
@@ -162,12 +162,12 @@ func fetchDockerContainers(
formatNames bool,
labelOverrides map[string]map[string]string,
) (dockerContainerList, error) {
- containers, err := fetchDockerContainersFromSource(socketPath, category, runningOnly, labelOverrides)
+ containers, err := fetchDockerContainersFromSource(socketPath, runningOnly, labelOverrides)
if err != nil {
return nil, fmt.Errorf("fetching containers: %w", err)
}
- containers, children := groupDockerContainerChildren(containers, hideByDefault)
+ containers, children := groupDockerContainerChildren(containers, hideByDefault, category)
dockerContainers := make(dockerContainerList, 0, len(containers))
for i := range containers {
@@ -247,6 +247,7 @@ func deriveDockerContainerName(container *dockerContainerJsonResponse, formatNam
func groupDockerContainerChildren(
containers []dockerContainerJsonResponse,
hideByDefault bool,
+ category string,
) (
[]dockerContainerJsonResponse,
map[string][]dockerContainerJsonResponse,
@@ -265,10 +266,18 @@ func groupDockerContainerChildren(
parent := container.Labels.getOrDefault(dockerContainerLabelParent, "")
if !isParent && parent != "" {
+ // Children inherit the category of their parent, so they're not filtered here
children[parent] = append(children[parent], *container)
- } else {
- parents = append(parents, *container)
+ continue
+ }
+
+ // We have to filter here instead of using the `filters` parameter of Docker's API
+ // because the user may define a category override within their config
+ if category != "" && container.Labels.getOrDefault(dockerContainerLabelCategory, "") != category {
+ continue
}
+
+ parents = append(parents, *container)
}
return parents, children
@@ -284,7 +293,6 @@ func isDockerContainerHidden(container *dockerContainerJsonResponse, hideByDefau
func fetchDockerContainersFromSource(
source string,
- category string,
runningOnly bool,
labelOverrides map[string]map[string]string,
) ([]dockerContainerJsonResponse, error) {
@@ -355,22 +363,6 @@ func fetchDockerContainersFromSource(
}
}
- // We have to filter here instead of using the `filters` parameter of Docker's API
- // because the user may define a category override within their config
- if category != "" {
- filtered := make([]dockerContainerJsonResponse, 0, len(containers))
-
- for i := range containers {
- container := &containers[i]
-
- if container.Labels.getOrDefault(dockerContainerLabelCategory, "") == category {
- filtered = append(filtered, *container)
- }
- }
-
- containers = filtered
- }
-
return containers, nil
}