refactor(labels): replace raw gordon.* strings with domain constants
- commit
- 3f8d9833c7d7db393e2f6d65785b62da23b29b0f
- author
- bnema · · 2026-02-24 21:46 UTC
- parent
- b86c85a
- signature
- unsigned
4 files · +43 -14
M internal/adapters/out/docker/runtime.go +3 -3
| @@ -1016,8 +1016,8 @@ func (r *Runtime) CreateVolume(ctx context.Context, volumeName string) error { | |||
| 1016 | 1016 | _, err := r.client.VolumeCreate(ctx, volume.CreateOptions{ | |
| 1017 | 1017 | Name: volumeName, | |
| 1018 | 1018 | Labels: map[string]string{ | |
| 1019 | - | "gordon.managed": "true", | |
| 1020 | - | "gordon.created": "auto", | |
| 1019 | + | domain.LabelManaged: "true", | |
| 1020 | + | domain.LabelCreated: "auto", | |
| 1021 | 1021 | }, | |
| 1022 | 1022 | }) | |
| 1023 | 1023 | if err != nil { | |
| @@ -1142,7 +1142,7 @@ func (r *Runtime) CreateNetwork(ctx context.Context, name string, options map[st | |||
| 1142 | 1142 | createOptions := network.CreateOptions{ | |
| 1143 | 1143 | Driver: driver, | |
| 1144 | 1144 | Labels: map[string]string{ | |
| 1145 | - | "gordon.managed": "true", | |
| 1145 | + | domain.LabelManaged: "true", | |
| 1146 | 1146 | }, | |
| 1147 | 1147 | } | |
| 1148 | 1148 | ||
| @@ -1016,8 +1016,8 @@ func (r *Runtime) CreateVolume(ctx context.Context, volumeName string) error { | |||||
| 1016 | _, err := r.client.VolumeCreate(ctx, volume.CreateOptions{ | 1016 | _, err := r.client.VolumeCreate(ctx, volume.CreateOptions{ | ||
| 1017 | Name: volumeName, | 1017 | Name: volumeName, | ||
| 1018 | Labels: map[string]string{ | 1018 | Labels: map[string]string{ | ||
| 1019 | - | "gordon.managed": "true", | 1019 | + | domain.LabelManaged: "true", |
| 1020 | - | "gordon.created": "auto", | 1020 | + | domain.LabelCreated: "auto", |
| 1021 | }, | 1021 | }, | ||
| 1022 | }) | 1022 | }) | ||
| 1023 | if err != nil { | 1023 | if err != nil { | ||
| @@ -1142,7 +1142,7 @@ func (r *Runtime) CreateNetwork(ctx context.Context, name string, options map[st | |||||
| 1142 | createOptions := network.CreateOptions{ | 1142 | createOptions := network.CreateOptions{ | ||
| 1143 | Driver: driver, | 1143 | Driver: driver, | ||
| 1144 | Labels: map[string]string{ | 1144 | Labels: map[string]string{ | ||
| 1145 | - | "gordon.managed": "true", | 1145 | + | domain.LabelManaged: "true", |
| 1146 | }, | 1146 | }, | ||
| 1147 | } | 1147 | } | ||
| 1148 | 1148 | ||||
A internal/domain/labels_test.go +29 -0
| @@ -0,0 +1,29 @@ | |||
| 1 | + | package domain_test | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "testing" | |
| 5 | + | ||
| 6 | + | "github.com/bnema/gordon/internal/domain" | |
| 7 | + | ) | |
| 8 | + | ||
| 9 | + | // TestLabelConstantsValues guards against accidental value changes that | |
| 10 | + | // would silently break container discovery. | |
| 11 | + | func TestLabelConstantsValues(t *testing.T) { | |
| 12 | + | tests := []struct { | |
| 13 | + | constant string | |
| 14 | + | expected string | |
| 15 | + | }{ | |
| 16 | + | {domain.LabelDomain, "gordon.domain"}, | |
| 17 | + | {domain.LabelImage, "gordon.image"}, | |
| 18 | + | {domain.LabelManaged, "gordon.managed"}, | |
| 19 | + | {domain.LabelRoute, "gordon.route"}, | |
| 20 | + | {domain.LabelAttachment, "gordon.attachment"}, | |
| 21 | + | {domain.LabelAttachedTo, "gordon.attached-to"}, | |
| 22 | + | {domain.LabelCreated, "gordon.created"}, | |
| 23 | + | } | |
| 24 | + | for _, tt := range tests { | |
| 25 | + | if tt.constant != tt.expected { | |
| 26 | + | t.Errorf("constant value changed: got %q, want %q", tt.constant, tt.expected) | |
| 27 | + | } | |
| 28 | + | } | |
| 29 | + | } | |
| @@ -0,0 +1,29 @@ | |||||
| 1 | + | package domain_test | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "testing" | |||
| 5 | + | ||||
| 6 | + | "github.com/bnema/gordon/internal/domain" | |||
| 7 | + | ) | |||
| 8 | + | ||||
| 9 | + | // TestLabelConstantsValues guards against accidental value changes that | |||
| 10 | + | // would silently break container discovery. | |||
| 11 | + | func TestLabelConstantsValues(t *testing.T) { | |||
| 12 | + | tests := []struct { | |||
| 13 | + | constant string | |||
| 14 | + | expected string | |||
| 15 | + | }{ | |||
| 16 | + | {domain.LabelDomain, "gordon.domain"}, | |||
| 17 | + | {domain.LabelImage, "gordon.image"}, | |||
| 18 | + | {domain.LabelManaged, "gordon.managed"}, | |||
| 19 | + | {domain.LabelRoute, "gordon.route"}, | |||
| 20 | + | {domain.LabelAttachment, "gordon.attachment"}, | |||
| 21 | + | {domain.LabelAttachedTo, "gordon.attached-to"}, | |||
| 22 | + | {domain.LabelCreated, "gordon.created"}, | |||
| 23 | + | } | |||
| 24 | + | for _, tt := range tests { | |||
| 25 | + | if tt.constant != tt.expected { | |||
| 26 | + | t.Errorf("constant value changed: got %q, want %q", tt.constant, tt.expected) | |||
| 27 | + | } | |||
| 28 | + | } | |||
| 29 | + | } | |||
M internal/usecase/container/events.go +2 -2
| @@ -111,7 +111,7 @@ func (h *ConfigReloadHandler) Handle(ctx context.Context, event domain.Event) er | |||
| 111 | 111 | ||
| 112 | 112 | activeRoutes := make(map[string]*domain.Container) | |
| 113 | 113 | for _, container := range currentContainers { | |
| 114 | - | if route, exists := container.Labels["gordon.route"]; exists { | |
| 114 | + | if route, exists := container.Labels[domain.LabelRoute]; exists { | |
| 115 | 115 | activeRoutes[route] = container | |
| 116 | 116 | } | |
| 117 | 117 | } | |
| @@ -119,7 +119,7 @@ func (h *ConfigReloadHandler) Handle(ctx context.Context, event domain.Event) er | |||
| 119 | 119 | routes := h.configSvc.GetRoutes(ctx) | |
| 120 | 120 | for _, route := range routes { | |
| 121 | 121 | if container, exists := activeRoutes[route.Domain]; exists { | |
| 122 | - | currentImage := container.Labels["gordon.image"] | |
| 122 | + | currentImage := container.Labels[domain.LabelImage] | |
| 123 | 123 | if currentImage != route.Image { | |
| 124 | 124 | log.Info(). | |
| 125 | 125 | Str("domain", route.Domain). | |
| @@ -111,7 +111,7 @@ func (h *ConfigReloadHandler) Handle(ctx context.Context, event domain.Event) er | |||||
| 111 | 111 | ||||
| 112 | activeRoutes := make(map[string]*domain.Container) | 112 | activeRoutes := make(map[string]*domain.Container) | ||
| 113 | for _, container := range currentContainers { | 113 | for _, container := range currentContainers { | ||
| 114 | - | if route, exists := container.Labels["gordon.route"]; exists { | 114 | + | if route, exists := container.Labels[domain.LabelRoute]; exists { |
| 115 | activeRoutes[route] = container | 115 | activeRoutes[route] = container | ||
| 116 | } | 116 | } | ||
| 117 | } | 117 | } | ||
| @@ -119,7 +119,7 @@ func (h *ConfigReloadHandler) Handle(ctx context.Context, event domain.Event) er | |||||
| 119 | routes := h.configSvc.GetRoutes(ctx) | 119 | routes := h.configSvc.GetRoutes(ctx) | ||
| 120 | for _, route := range routes { | 120 | for _, route := range routes { | ||
| 121 | if container, exists := activeRoutes[route.Domain]; exists { | 121 | if container, exists := activeRoutes[route.Domain]; exists { | ||
| 122 | - | currentImage := container.Labels["gordon.image"] | 122 | + | currentImage := container.Labels[domain.LabelImage] |
| 123 | if currentImage != route.Image { | 123 | if currentImage != route.Image { | ||
| 124 | log.Info(). | 124 | log.Info(). | ||
| 125 | Str("domain", route.Domain). | 125 | Str("domain", route.Domain). | ||
M internal/usecase/container/service.go +9 -9
| @@ -219,10 +219,10 @@ func (s *Service) buildContainerConfig(containerDomain, image, actualImageRef st | |||
| 219 | 219 | NetworkMode: networkName, | |
| 220 | 220 | Hostname: containerDomain, | |
| 221 | 221 | Labels: map[string]string{ | |
| 222 | - | "gordon.domain": containerDomain, | |
| 223 | - | "gordon.image": image, | |
| 224 | - | "gordon.managed": "true", | |
| 225 | - | "gordon.route": containerDomain, | |
| 222 | + | domain.LabelDomain: containerDomain, | |
| 223 | + | domain.LabelImage: image, | |
| 224 | + | domain.LabelManaged: "true", | |
| 225 | + | domain.LabelRoute: containerDomain, | |
| 226 | 226 | }, | |
| 227 | 227 | AutoRemove: false, | |
| 228 | 228 | } | |
| @@ -905,7 +905,7 @@ func (s *Service) SyncContainers(ctx context.Context) error { | |||
| 905 | 905 | managed := make(map[string]*domain.Container) | |
| 906 | 906 | for _, c := range allContainers { | |
| 907 | 907 | if c.Labels != nil { | |
| 908 | - | if d, ok := c.Labels["gordon.domain"]; ok && c.Labels["gordon.managed"] == "true" { | |
| 908 | + | if d, ok := c.Labels[domain.LabelDomain]; ok && c.Labels[domain.LabelManaged] == "true" { | |
| 909 | 909 | managed[d] = c | |
| 910 | 910 | } | |
| 911 | 911 | } | |
| @@ -1875,10 +1875,10 @@ func (s *Service) deployAttachedService(ctx context.Context, ownerDomain, servic | |||
| 1875 | 1875 | Volumes: volumes, | |
| 1876 | 1876 | NetworkMode: networkName, // Same network as main app | |
| 1877 | 1877 | Labels: map[string]string{ | |
| 1878 | - | "gordon.managed": "true", | |
| 1879 | - | "gordon.attachment": "true", | |
| 1880 | - | "gordon.attached-to": ownerDomain, | |
| 1881 | - | "gordon.image": serviceImage, | |
| 1878 | + | domain.LabelManaged: "true", | |
| 1879 | + | domain.LabelAttachment: "true", | |
| 1880 | + | domain.LabelAttachedTo: ownerDomain, | |
| 1881 | + | domain.LabelImage: serviceImage, | |
| 1882 | 1882 | }, | |
| 1883 | 1883 | } | |
| 1884 | 1884 | ||
| @@ -219,10 +219,10 @@ func (s *Service) buildContainerConfig(containerDomain, image, actualImageRef st | |||||
| 219 | NetworkMode: networkName, | 219 | NetworkMode: networkName, | ||
| 220 | Hostname: containerDomain, | 220 | Hostname: containerDomain, | ||
| 221 | Labels: map[string]string{ | 221 | Labels: map[string]string{ | ||
| 222 | - | "gordon.domain": containerDomain, | 222 | + | domain.LabelDomain: containerDomain, |
| 223 | - | "gordon.image": image, | 223 | + | domain.LabelImage: image, |
| 224 | - | "gordon.managed": "true", | 224 | + | domain.LabelManaged: "true", |
| 225 | - | "gordon.route": containerDomain, | 225 | + | domain.LabelRoute: containerDomain, |
| 226 | }, | 226 | }, | ||
| 227 | AutoRemove: false, | 227 | AutoRemove: false, | ||
| 228 | } | 228 | } | ||
| @@ -905,7 +905,7 @@ func (s *Service) SyncContainers(ctx context.Context) error { | |||||
| 905 | managed := make(map[string]*domain.Container) | 905 | managed := make(map[string]*domain.Container) | ||
| 906 | for _, c := range allContainers { | 906 | for _, c := range allContainers { | ||
| 907 | if c.Labels != nil { | 907 | if c.Labels != nil { | ||
| 908 | - | if d, ok := c.Labels["gordon.domain"]; ok && c.Labels["gordon.managed"] == "true" { | 908 | + | if d, ok := c.Labels[domain.LabelDomain]; ok && c.Labels[domain.LabelManaged] == "true" { |
| 909 | managed[d] = c | 909 | managed[d] = c | ||
| 910 | } | 910 | } | ||
| 911 | } | 911 | } | ||
| @@ -1875,10 +1875,10 @@ func (s *Service) deployAttachedService(ctx context.Context, ownerDomain, servic | |||||
| 1875 | Volumes: volumes, | 1875 | Volumes: volumes, | ||
| 1876 | NetworkMode: networkName, // Same network as main app | 1876 | NetworkMode: networkName, // Same network as main app | ||
| 1877 | Labels: map[string]string{ | 1877 | Labels: map[string]string{ | ||
| 1878 | - | "gordon.managed": "true", | 1878 | + | domain.LabelManaged: "true", |
| 1879 | - | "gordon.attachment": "true", | 1879 | + | domain.LabelAttachment: "true", |
| 1880 | - | "gordon.attached-to": ownerDomain, | 1880 | + | domain.LabelAttachedTo: ownerDomain, |
| 1881 | - | "gordon.image": serviceImage, | 1881 | + | domain.LabelImage: serviceImage, |
| 1882 | }, | 1882 | }, | ||
| 1883 | } | 1883 | } | ||
| 1884 | 1884 | ||||