Skip to content

Commit 37c8d64

Browse files
committed
fix(compose): remove stale labels for disabled domains
1 parent 63144e6 commit 37c8d64

2 files changed

Lines changed: 92 additions & 2 deletions

File tree

‎apps/dokploy/__test__/compose/domain/enabled-filter.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
66
// addDomainToCompose reads the compose file from disk through loadDockerCompose
77
// (existsSync + readFileSync). Mock node:fs so the function runs its real
88
// label-generation logic against an in-memory compose spec.
9-
const composeYaml = `
9+
const baseComposeYaml = `
1010
services:
1111
frigate:
1212
image: frigate
1313
`;
14+
let composeYaml = baseComposeYaml;
1415

1516
vi.mock("node:fs", async (importOriginal) => {
1617
const actual = await importOriginal<typeof import("node:fs")>();
@@ -62,6 +63,7 @@ const serviceLabels = (
6263
describe("addDomainToCompose enabled filtering", () => {
6364
beforeEach(() => {
6465
vi.clearAllMocks();
66+
composeYaml = baseComposeYaml;
6567
});
6668

6769
it("generates traefik labels for an enabled domain", async () => {
@@ -88,6 +90,55 @@ describe("addDomainToCompose enabled filtering", () => {
8890
);
8991
});
9092

93+
it.each([
94+
[
95+
"docker-compose",
96+
`services:
97+
frigate:
98+
image: frigate
99+
labels:
100+
- traefik.enable=true
101+
- traefik.http.routers.test-app-1-web.rule=Host(\`frigate.example.com\`)
102+
- traefik.http.services.test-app-1-web.loadbalancer.server.port=8971
103+
- traefik.http.middlewares.stripprefix-test-app-1.stripprefix.prefixes=/api
104+
- custom.label=preserved
105+
`,
106+
],
107+
[
108+
"stack",
109+
`services:
110+
frigate:
111+
image: frigate
112+
deploy:
113+
labels:
114+
- traefik.enable=true
115+
- traefik.http.routers.test-app-1-web.rule=Host(\`frigate.example.com\`)
116+
- traefik.http.services.test-app-1-web.loadbalancer.server.port=8971
117+
- traefik.http.middlewares.stripprefix-test-app-1.stripprefix.prefixes=/api
118+
- custom.label=preserved
119+
`,
120+
],
121+
] as const)(
122+
"removes stale labels for a disabled domain from %s rebuilds",
123+
async (composeType, staleComposeYaml) => {
124+
composeYaml = staleComposeYaml;
125+
126+
const result = await addDomainToCompose({ ...baseCompose, composeType }, [
127+
{ ...baseDomain, enabled: false },
128+
]);
129+
130+
const service = result?.services?.frigate;
131+
const labels =
132+
composeType === "docker-compose"
133+
? service?.labels
134+
: service?.deploy?.labels;
135+
expect(labels).toContain("custom.label=preserved");
136+
expect(
137+
(labels as string[]).some((label) => label.includes("test-app-1")),
138+
).toBe(false);
139+
},
140+
);
141+
91142
it("emits labels only for the enabled domain when both are present", async () => {
92143
const result = await addDomainToCompose(baseCompose, [
93144
{ ...baseDomain, host: "enabled.example.com", enabled: true },

‎packages/server/src/utils/docker/domain.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ export const applyComposeFilePatch = async (
166166
}
167167
};
168168

169+
const removeDomainLabels = (
170+
labels: DefinitionsService["labels"],
171+
appName: string,
172+
uniqueConfigKey: number,
173+
) => {
174+
if (!Array.isArray(labels)) return labels;
175+
176+
const prefixes = [
177+
`traefik.http.routers.${appName}-${uniqueConfigKey}-`,
178+
`traefik.http.services.${appName}-${uniqueConfigKey}-`,
179+
`traefik.http.middlewares.stripprefix-${appName}-${uniqueConfigKey}.`,
180+
`traefik.http.middlewares.addprefix-${appName}-${uniqueConfigKey}.`,
181+
];
182+
183+
return labels.filter(
184+
(label) => !prefixes.some((prefix) => label.startsWith(prefix)),
185+
);
186+
};
187+
169188
export const addDomainToCompose = async (
170189
compose: Compose,
171190
domains: Domain[],
@@ -198,7 +217,27 @@ export const addDomainToCompose = async (
198217
result = randomized;
199218
}
200219

201-
// Disabled domains keep their config but must not produce any traefik labels.
220+
for (const domain of domains.filter((d) => !d.enabled)) {
221+
if (!domain.serviceName) continue;
222+
223+
const service = result.services?.[domain.serviceName];
224+
if (!service) continue;
225+
226+
if (compose.composeType === "docker-compose") {
227+
service.labels = removeDomainLabels(
228+
service.labels,
229+
appName,
230+
domain.uniqueConfigKey,
231+
);
232+
} else if (service.deploy) {
233+
service.deploy.labels = removeDomainLabels(
234+
service.deploy.labels,
235+
appName,
236+
domain.uniqueConfigKey,
237+
);
238+
}
239+
}
240+
202241
for (const domain of domains.filter((d) => d.enabled)) {
203242
const { serviceName, https } = domain;
204243
if (!serviceName) {

0 commit comments

Comments
 (0)