Skip to content

Commit 8e4ee01

Browse files
authored
Merge pull request #583 from DefangLabs/eric-add-postgres-support
add Postgres support
2 parents c8ba040 + 043c85e commit 8e4ee01

File tree

8 files changed

+267
-124
lines changed

8 files changed

+267
-124
lines changed

src/pkg/cli/compose/convert.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ func ConvertServices(ctx context.Context, c client.Client, serviceConfigs compos
213213
redis = &defangv1.Redis{}
214214
}
215215

216-
if redis == nil && isStatefulImage(svccfg.Image) {
216+
var postgres *defangv1.Postgres
217+
if _, ok := svccfg.Extensions["x-defang-postgres"]; ok {
218+
postgres = &defangv1.Postgres{}
219+
}
220+
221+
if redis == nil && postgres == nil && isStatefulImage(svccfg.Image) {
217222
term.Warnf("service %q: stateful service will lose data on restart; use a managed service instead", svccfg.Name)
218223
}
219224

@@ -237,6 +242,7 @@ func ConvertServices(ctx context.Context, c client.Client, serviceConfigs compos
237242
DnsRole: dnsRole,
238243
StaticFiles: staticFiles,
239244
Redis: redis,
245+
Postgres: postgres,
240246
})
241247
}
242248
return services, nil

src/pkg/cli/compose/validation.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,17 @@ func ValidateProject(project *compose.Project) error {
262262
}
263263
}
264264

265+
if _, ok := svccfg.Extensions["x-defang-postgres"]; ok {
266+
// Ensure the image is a valid Postgres image
267+
repo := strings.SplitN(svccfg.Image, ":", 2)[0]
268+
if !strings.HasSuffix(repo, "postgres") {
269+
term.Warnf("service %q: managed Postgres service should use a postgres image", svccfg.Name)
270+
}
271+
}
272+
265273
for k := range svccfg.Extensions {
266274
switch k {
267-
case "x-defang-dns-role", "x-defang-static-files", "x-defang-redis":
275+
case "x-defang-dns-role", "x-defang-static-files", "x-defang-redis", "x-defang-postgres":
268276
continue
269277
default:
270278
term.Warnf("service %q: unsupported compose extension: %q", svccfg.Name, k)

src/protos/io/defang/v1/fabric.pb.go

Lines changed: 186 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/protos/io/defang/v1/fabric.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ message Service {
402402
StaticFiles static_files = 15; // x-defang-static-files: use a managed CDN
403403
Network networks = 16; // currently only 1 network is supported
404404
Redis redis = 18; // x-defang-redis: use a managed redis
405+
Postgres postgres = 19; // x-defang-postgres: use a managed
405406
}
406407

407408
message StaticFiles {
@@ -410,6 +411,7 @@ message StaticFiles {
410411
}
411412

412413
message Redis {}
414+
message Postgres {}
413415

414416
message Event {
415417
string specversion = 1; // required (but we don't care)

src/tests/postgres/compose.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
x:
3+
image: postgres
4+
x-defang-postgres:
5+
6+
y:
7+
image: example
8+
x-defang-postgres:
9+
10+
z:
11+
image: postgres
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[
2+
{
3+
"name": "x",
4+
"image": "postgres",
5+
"platform": 2,
6+
"internal": true,
7+
"networks": 1,
8+
"postgres": {}
9+
},
10+
{
11+
"name": "y",
12+
"image": "example",
13+
"platform": 2,
14+
"internal": true,
15+
"networks": 1,
16+
"postgres": {}
17+
},
18+
{
19+
"name": "z",
20+
"image": "postgres",
21+
"platform": 2,
22+
"internal": true,
23+
"networks": 1
24+
}
25+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: postgres
2+
services:
3+
x:
4+
image: postgres
5+
networks:
6+
default: null
7+
x-defang-postgres: null
8+
"y":
9+
image: example
10+
networks:
11+
default: null
12+
x-defang-postgres: null
13+
z:
14+
image: postgres
15+
networks:
16+
default: null
17+
networks:
18+
default:
19+
name: postgres_default
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
! service "x": missing compose directive: restart; assuming 'unless-stopped' (add 'restart' to silence)
2+
! service "x": missing memory reservation; specify deploy.resources.reservations.memory to avoid out-of-memory errors
3+
! service "y": managed Postgres service should use a postgres image
4+
! service "y": missing compose directive: restart; assuming 'unless-stopped' (add 'restart' to silence)
5+
! service "y": missing memory reservation; specify deploy.resources.reservations.memory to avoid out-of-memory errors
6+
! service "z": missing compose directive: restart; assuming 'unless-stopped' (add 'restart' to silence)
7+
! service "z": missing memory reservation; specify deploy.resources.reservations.memory to avoid out-of-memory errors
8+
! service "z": stateful service will lose data on restart; use a managed service instead

0 commit comments

Comments
 (0)