Skip to content

Modern Docker Management, Designed for Everyone

feat: add subnet and gateway columns to the networks table (#3942)

Kyle Mendell146b4b9658d1
diff --git a/backend/internal/network/handler.go b/backend/internal/network/handler.go
index e08c91fb5..3bc0f6e0a 100644
--- a/backend/internal/network/handler.go
+++ b/backend/internal/network/handler.go
@@ -26,6 +26,7 @@ import (
"github.com/getarcaneapp/arcane/types/v2/base"
networktypes "github.com/getarcaneapp/arcane/types/v2/network"
dockernetwork "github.com/moby/moby/api/types/network"
+ "github.com/moby/moby/client"
"github.com/samber/mo"
)
@@ -227,8 +228,13 @@ func (h *NetworkHandler) CreateNetwork(ctx context.Context, input *CreateNetwork
return nil, err
}
- // Convert to Docker SDK options
- dockerOptions := input.Body.Options.ToDockerCreateOptions()
+ dockerOptions, err := mapper.MapOne[networktypes.CreateOptions, client.NetworkCreateOptions](input.Body.Options)
+ if err != nil {
+ return nil, huma.Error400BadRequest(errors.WithMessage(err, "Invalid network options").Error())
+ }
+ if !input.Body.Options.EnableIPv6 {
+ dockerOptions.EnableIPv6 = nil
+ }
var response *dockernetwork.CreateResponse
runtimeCtx := utils.ActivityRuntimeContext(ctx, h.appCtx)
diff --git a/backend/internal/network/service.go b/backend/internal/network/service.go
index c4f0dbf36..df02d71b2 100644
--- a/backend/internal/network/service.go
+++ b/backend/internal/network/service.go
@@ -7,6 +7,7 @@ import (
"fmt"
"log/slog"
"net/netip"
+ "slices"
"sort"
"strings"
@@ -18,6 +19,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/mapper"
networktypes "github.com/getarcaneapp/arcane/types/v2/network"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/network"
@@ -369,7 +371,10 @@ func (s *NetworkService) ListNetworksPaginated(ctx context.Context, params pagin
}
rawNets := networkList.Items
- items := s.convertToNetworkSummaries(rawNets, inUseByID, inUseByName)
+ items, err := s.convertToNetworkSummaries(rawNets, inUseByID, inUseByName)
+ if err != nil {
+ return nil, pagination.Response{}, networktypes.UsageCounts{}, err
+ }
config := s.buildNetworkPaginationConfig()
result := config.SearchOrderAndPaginate(items, params)
counts := s.calculateNetworkUsageCounts(items)
@@ -400,15 +405,18 @@ func buildTopologyContainerInfoInternal(containers []container.Summary) map[stri
return infoByID
}
-func (s *NetworkService) convertToNetworkSummaries(rawNets []network.Summary, inUseByID, inUseByName map[string]bool) []networktypes.Summary {
+func (s *NetworkService) convertToNetworkSummaries(rawNets []network.Summary, inUseByID, inUseByName map[string]bool) ([]networktypes.Summary, error) {
items := make([]networktypes.Summary, 0, len(rawNets))
for _, n := range rawNets {
- netDto := networktypes.NewSummary(n)
+ netDto, err := mapper.MapOne[network.Summary, networktypes.Summary](n)
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to map network")
+ }
netDto.InUse = inUseByID[netDto.ID] || inUseByName[netDto.Name]
netDto.IsDefault = dockerutil.IsDefaultNetwork(netDto.Name)
items = append(items, netDto)
}
- return items
+ return items, nil
}
func (s *NetworkService) buildNetworkPaginationConfig() pagination.Config[networktypes.Summary] {
@@ -418,6 +426,10 @@ func (s *NetworkService) buildNetworkPaginationConfig() pagination.Config[networ
func(n networktypes.Summary) (string, error) { return n.Driver, nil },
func(n networktypes.Summary) (string, error) { return n.Scope, nil },
func(n networktypes.Summary) (string, error) { return n.ID, nil },
+ func(n networktypes.Summary) (string, error) {
+ subnets, gateways := ipamValuesInternal(n)
+ return strings.Join(append(subnets, gateways...), " "), nil
+ },
},
SortBindings: s.buildNetworkSortBindings(),
FilterAccessors: s.buildNetworkFilterAccessors(),
@@ -453,7 +465,35 @@ func (s *NetworkService) buildNetworkSortBindings() []pagination.SortBinding[net
Key: "inUse",
Fn: s.compareNetworkInUse,
},
+ {
+ Key: "subnet",
+ Fn: func(a, b networktypes.Summary) int {
+ subnetsA, _ := ipamValuesInternal(a)
+ subnetsB, _ := ipamValuesInternal(b)
+ return slices.Compare(subnetsA, subnetsB)
+ },
+ },
+ {
+ Key: "gateway",
+ Fn: func(a, b networktypes.Summary) int {
+ _, gatewaysA := ipamValuesInternal(a)
+ _, gatewaysB := ipamValuesInternal(b)
+ return slices.Compare(gatewaysA, gatewaysB)
+ },
+ },
+ }
+}
+
+func ipamValuesInternal(n networktypes.Summary) (subnets, gateways []string) {
+ for _, cfg := range n.IPAM.Config {
+ if cfg.Subnet != "" {
+ subnets = append(subnets, cfg.Subnet)
+ }
+ if cfg.Gateway != "" {
+ gateways = append(gateways, cfg.Gateway)
+ }
}
+ return subnets, gateways
}
func (s *NetworkService) compareNetworkCreated(a, b networktypes.Summary) int {
diff --git a/backend/pkg/utils/mapper/mapper.go b/backend/pkg/utils/mapper/mapper.go
index 6fde831be..9e398c135 100644
--- a/backend/pkg/utils/mapper/mapper.go
+++ b/backend/pkg/utils/mapper/mapper.go
@@ -3,12 +3,43 @@ package mapper
import (
"fmt"
"net/netip"
+ "strings"
"emperror.dev/errors"
"github.com/jinzhu/copier"
)
var typeConverters = []copier.TypeConverter{
+ {
+ SrcType: "",
+ DstType: netip.Prefix{},
+ Fn: func(src any) (any, error) {
+ raw, ok := src.(string)
+ if !ok {
+ return nil, fmt.Errorf("expected string, got %T", src)
+ }
+ if strings.TrimSpace(raw) == "" {
+ return netip.Prefix{}, nil
+ }
+
+ return netip.ParsePrefix(strings.TrimSpace(raw))
+ },
+ },
+ {
+ SrcType: "",
+ DstType: netip.Addr{},
+ Fn: func(src any) (any, error) {
+ raw, ok := src.(string)
+ if !ok {
+ return nil, fmt.Errorf("expected string, got %T", src)
+ }
+ if strings.TrimSpace(raw) == "" {
+ return netip.Addr{}, nil
+ }
+
+ return netip.ParseAddr(strings.TrimSpace(raw))
+ },
+ },
{
SrcType: netip.Prefix{},
DstType: "",
diff --git a/frontend/src/lib/types/docker.ts b/frontend/src/lib/types/docker.ts
index fbb8ea114..2f9e604f5 100644
--- a/frontend/src/lib/types/docker.ts
+++ b/frontend/src/lib/types/docker.ts
@@ -742,6 +742,7 @@ export interface NetworkSummaryDto {
created: string;
options?: Record<string, string> | null;
labels?: Record<string, string> | null;
+ ipam?: IPAMDto;
inUse: boolean;
isDefault?: boolean;
}
diff --git a/frontend/src/routes/(app)/networks/network-table.svelte b/frontend/src/routes/(app)/networks/network-table.svelte
index c7eb6a652..726334108 100644
--- a/frontend/src/routes/(app)/networks/network-table.svelte
+++ b/frontend/src/routes/(app)/networks/network-table.svelte
@@ -132,16 +132,36 @@
{ accessorKey: 'name', title: m.common_name(), sortable: true, cell: NameCell },
{ accessorKey: 'inUse', title: m.common_status(), sortable: true, cell: StatusCell },
{ accessorKey: 'driver', title: m.common_driver(), sortable: true, cell: DriverCell },
- { accessorKey: 'scope', title: m.common_scope(), sortable: true, cell: ScopeCell }
+ { accessorKey: 'scope', title: m.common_scope(), sortable: true, cell: ScopeCell },
+ {
+ id: 'subnet',
+ accessorFn: (row) => ipamValues(row, 'subnet').join(' '),
+ title: m.common_subnet(),
+ sortable: true,
+ cell: SubnetCell
+ },
+ {
+ id: 'gateway',
+ accessorFn: (row) => ipamValues(row, 'gateway').join(' '),
+ title: m.common_gateway(),
+ sortable: true,
+ cell: GatewayCell
+ }
] satisfies ColumnSpec<NetworkSummaryDto>[];
const mobileFields = [
{ id: 'id', label: m.common_id(), defaultVisible: false },
{ id: 'inUse', label: m.common_status(), defaultVisible: true },
{ id: 'driver', label: m.common_driver(), defaultVisible: true },
- { id: 'scope', label: m.common_scope(), defaultVisible: true }
+ { id: 'scope', label: m.common_scope(), defaultVisible: true },
+ { id: 'subnet', label: m.common_subnet(), defaultVisible: true },
+ { id: 'gateway', label: m.common_gateway(), defaultVisible: true }
];
+ function ipamValues(item: NetworkSummaryDto, key: 'subnet' | 'gateway'): string[] {
+ return (item.ipam?.config ?? []).map((c) => c[key]).filter((v): v is string => !!v);
+ }
+
const bulkActions = $derived.by<BulkAction[]>(() => [
{
id: 'remove',
@@ -184,6 +204,32 @@
<Badge variant={item.scope === 'local' ? 'green' : 'amber'} minWidth="20">{capitalizeFirstLetter(item.scope)}</Badge>
{/snippet}
+{#snippet SubnetCell({ item }: { item: NetworkSummaryDto })}
+ {@const subnets = ipamValues(item, 'subnet')}
+ {#if subnets.length === 0}
+ <span class="text-muted-foreground">—</span>
+ {:else}
+ <div class="flex flex-col font-mono text-sm">
+ {#each subnets as subnet (subnet)}
+ <span>{subnet}</span>
+ {/each}
+ </div>
+ {/if}
+{/snippet}
+
+{#snippet GatewayCell({ item }: { item: NetworkSummaryDto })}
+ {@const gateways = ipamValues(item, 'gateway')}
+ {#if gateways.length === 0}
+ <span class="text-muted-foreground">—</span>
+ {:else}
+ <div class="flex flex-col font-mono text-sm">
+ {#each gateways as gateway (gateway)}
+ <span>{gateway}</span>
+ {/each}
+ </div>
+ {/if}
+{/snippet}
+
{#snippet StatusCell({ item }: { item: NetworkSummaryDto })}
{#if item.isDefault}
<Badge variant="sky" minWidth="20">{m.networks_predefined()}</Badge>
@@ -235,6 +281,22 @@
type: 'badge' as const,
badgeVariant: item.scope === 'local' ? ('green' as const) : ('amber' as const),
show: mobileFieldVisibility['scope'] ?? true
+ },
+ {
+ label: m.common_subnet(),
+ getValue: (item: NetworkSummaryDto) => ipamValues(item, 'subnet').join(', ') || '—',
+ icon: NetworksIcon,
+ iconVariant: 'gray' as const,
+ type: 'mono' as const,
+ show: mobileFieldVisibility['subnet'] ?? true
+ },
+ {
+ label: m.common_gateway(),
+ getValue: (item: NetworkSummaryDto) => ipamValues(item, 'gateway').join(', ') || '—',
+ icon: GlobeIcon,
+ iconVariant: 'gray' as const,
+ type: 'mono' as const,
+ show: mobileFieldVisibility['gateway'] ?? true
}
]}
rowActions={RowActions}
diff --git a/types/network/network.go b/types/network/network.go
index b5d6dcd4a..1d605c996 100644
--- a/types/network/network.go
+++ b/types/network/network.go
@@ -1,12 +1,9 @@
package network
import (
- "net/netip"
- "strings"
"time"
"github.com/moby/moby/api/types/network"
- "github.com/moby/moby/client"
)
type Summary struct {
@@ -45,6 +42,11 @@ type Summary struct {
// Required: true
Labels map[string]string `json:"labels"`
+ // IPAM contains IP address management configuration.
+ //
+ // Required: true
+ IPAM IPAM `json:"ipam"`
+
// InUse indicates if the network is currently in use by a container.
//
// Required: true
@@ -265,105 +267,6 @@ type CreateOptions struct {
EnableIPv6 bool `json:"enableIPv6,omitempty" doc:"Enable IPv6 networking"`
}
-// ToDockerCreateOptions converts to Docker SDK CreateOptions.
-func (o CreateOptions) ToDockerCreateOptions() client.NetworkCreateOptions {
- opts := client.NetworkCreateOptions{
- Driver: o.Driver,
- Internal: o.Internal,
- Attachable: o.Attachable,
- Ingress: o.Ingress,
- EnableIPv6: boolPtrIfTrue(o.EnableIPv6),
- Options: o.Options,
- Labels: o.Labels,
- }
-
- if o.IPAM != nil {
- opts.IPAM = toDockerIPAM(o.IPAM)
- }
-
- return opts
-}
-
-func boolPtrIfTrue(v bool) *bool {
- if !v {
- return nil
- }
-
- return &v
-}
-
-func toDockerIPAM(src *IPAM) *network.IPAM {
- dockerIPAM := &network.IPAM{
- Driver: src.Driver,
- Options: src.Options,
- }
-
- for _, cfg := range src.Config {
- if dockerCfg, ok := toDockerIPAMConfig(cfg); ok {
- dockerIPAM.Config = append(dockerIPAM.Config, dockerCfg)
- }
- }
-
- return dockerIPAM
-}
-
-func toDockerIPAMConfig(cfg IPAMConfig) (network.IPAMConfig, bool) {
- dockerCfg := network.IPAMConfig{}
- hasAny := false
-
- if parsed, ok := parsePrefix(cfg.Subnet); ok {
- dockerCfg.Subnet = parsed
- hasAny = true
- }
- if parsed, ok := parseAddr(cfg.Gateway); ok {
- dockerCfg.Gateway = parsed
- hasAny = true
- }
- if parsed, ok := parsePrefix(cfg.IPRange); ok {
- dockerCfg.IPRange = parsed
- hasAny = true
- }
- if aux := parseAuxAddress(cfg.AuxAddress); len(aux) > 0 {
- dockerCfg.AuxAddress = aux
- hasAny = true
- }
-
- return dockerCfg, hasAny
-}
-
-func parsePrefix(raw string) (netip.Prefix, bool) {
- prefix, err := netip.ParsePrefix(strings.TrimSpace(raw))
- if err != nil {
- return netip.Prefix{}, false
- }
-
- return prefix, true
-}
-
-func parseAddr(raw string) (netip.Addr, bool) {
- addr, err := netip.ParseAddr(strings.TrimSpace(raw))
- if err != nil {
- return netip.Addr{}, false
- }
-
- return addr, true
-}
-
-func parseAuxAddress(auxAddress map[string]string) map[string]netip.Addr {
- if len(auxAddress) == 0 {
- return nil
- }
-
- aux := make(map[string]netip.Addr, len(auxAddress))
- for key, rawAddr := range auxAddress {
- if parsed, ok := parseAddr(rawAddr); ok {
- aux[key] = parsed
- }
- }
-
- return aux
-}
-
// ConnectContainerRequest contains the parameters for connecting a container
// to a network.
type ConnectContainerRequest struct {
@@ -418,21 +321,3 @@ type PruneReport struct {
// Required: false
ActivityID *string `json:"activityId,omitempty"`
}
-
-// NewSummary creates a Summary from a docker network.Summary, calculating InUse and IsDefault fields.
-func NewSummary(s network.Summary) Summary {
- return Summary{
- ID: s.ID,
- Name: s.Name,
- Driver: s.Driver,
- Scope: s.Scope,
- Created: s.Created,
- Options: s.Options,
- Labels: s.Labels,
- // InUse is computed by service-layer usage checks that include container attachments.
- InUse: false,
- // IsDefault is set to true if the network is one of the default Docker networks
- // (bridge, host, or none), false otherwise.
- IsDefault: s.Name == "bridge" || s.Name == "host" || s.Name == "none",
- }
-}