Skip to content

Commit 53c5494

Browse files
NiccoloFeigbartolinimnencia
authored
feat: enable specification of environment variables (#165)
Allow specifying env variables for an extension in its metadata. Related to cloudnative-pg/cloudnative-pg#10372 Closes #162 Signed-off-by: Niccolò Fei <niccolo.fei@enterprisedb.com> Signed-off-by: Gabriele Bartolini <gabriele.bartolini@enterprisedb.com> Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com> Co-authored-by: Gabriele Bartolini <gabriele.bartolini@enterprisedb.com> Co-authored-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
1 parent 6ffd1f7 commit 53c5494

File tree

9 files changed

+61
-13
lines changed

9 files changed

+61
-13
lines changed

dagger/maintenance/catalogs.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"path/filepath"
88
"slices"
9+
"sort"
910

1011
"go.yaml.in/yaml/v3"
1112

@@ -22,13 +23,19 @@ type ImageVolumeSource struct {
2223
PullPolicy string `yaml:"pullPolicy,omitempty"`
2324
}
2425

26+
type ExtensionEnvVar struct {
27+
Name string `yaml:"name"`
28+
Value string `yaml:"value"`
29+
}
30+
2531
type ExtensionConfiguration struct {
2632
Name string `yaml:"name"`
2733
ImageVolumeSource ImageVolumeSource `yaml:"image"`
2834
ExtensionControlPath []string `yaml:"extension_control_path,omitempty"`
2935
DynamicLibraryPath []string `yaml:"dynamic_library_path,omitempty"`
3036
LdLibraryPath []string `yaml:"ld_library_path,omitempty"`
3137
BinPath []string `yaml:"bin_path,omitempty"`
38+
Env []ExtensionEnvVar `yaml:"env,omitempty"`
3239
}
3340

3441
type ImageCatalog struct {
@@ -111,3 +118,14 @@ func writeCatalogToDir(catalog *ImageCatalog, outDir *dagger.Directory) (*dagger
111118

112119
return outDir.WithNewFile(outName, buf.String()), nil
113120
}
121+
122+
func envMapToSlice(env map[string]string) []ExtensionEnvVar {
123+
result := make([]ExtensionEnvVar, 0, len(env))
124+
for name, value := range env {
125+
result = append(result, ExtensionEnvVar{Name: name, Value: value})
126+
}
127+
sort.Slice(result, func(i, j int) bool {
128+
return result[i].Name < result[j].Name
129+
})
130+
return result
131+
}

dagger/maintenance/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ func (m *Maintenance) GenerateCatalogs(
515515
DynamicLibraryPath: metadata.DynamicLibraryPath,
516516
LdLibraryPath: metadata.LdLibraryPath,
517517
BinPath: metadata.BinPath,
518+
Env: envMapToSlice(metadata.Env),
518519
}
519520

520521
img.Extensions = append(img.Extensions, extensionsConfig)

dagger/maintenance/parse.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ type extensionVersion struct {
2525
type versionMap map[string]map[string]extensionVersion
2626

2727
type extensionMetadata struct {
28-
Name string `hcl:"name" cty:"name"`
29-
SQLName string `hcl:"sql_name" cty:"sql_name"`
30-
ImageName string `hcl:"image_name" cty:"image_name"`
31-
SharedPreloadLibraries []string `hcl:"shared_preload_libraries" cty:"shared_preload_libraries"`
32-
ExtensionControlPath []string `hcl:"extension_control_path" cty:"extension_control_path"`
33-
DynamicLibraryPath []string `hcl:"dynamic_library_path" cty:"dynamic_library_path"`
34-
LdLibraryPath []string `hcl:"ld_library_path" cty:"ld_library_path"`
35-
BinPath []string `hcl:"bin_path" cty:"bin_path"`
36-
AutoUpdateOsLibs bool `hcl:"auto_update_os_libs" cty:"auto_update_os_libs"`
37-
RequiredExtensions []string `hcl:"required_extensions" cty:"required_extensions"`
38-
CreateExtension bool `hcl:"create_extension" cty:"create_extension"`
39-
Versions versionMap `hcl:"versions" cty:"versions"`
40-
Remain hcl.Body `hcl:",remain"`
28+
Name string `hcl:"name" cty:"name"`
29+
SQLName string `hcl:"sql_name" cty:"sql_name"`
30+
ImageName string `hcl:"image_name" cty:"image_name"`
31+
SharedPreloadLibraries []string `hcl:"shared_preload_libraries" cty:"shared_preload_libraries"`
32+
ExtensionControlPath []string `hcl:"extension_control_path" cty:"extension_control_path"`
33+
DynamicLibraryPath []string `hcl:"dynamic_library_path" cty:"dynamic_library_path"`
34+
LdLibraryPath []string `hcl:"ld_library_path" cty:"ld_library_path"`
35+
BinPath []string `hcl:"bin_path" cty:"bin_path"`
36+
Env map[string]string `hcl:"env" cty:"env"`
37+
AutoUpdateOsLibs bool `hcl:"auto_update_os_libs" cty:"auto_update_os_libs"`
38+
RequiredExtensions []string `hcl:"required_extensions" cty:"required_extensions"`
39+
CreateExtension bool `hcl:"create_extension" cty:"create_extension"`
40+
Versions versionMap `hcl:"versions" cty:"versions"`
41+
Remain hcl.Body `hcl:",remain"`
4142
}
4243

4344
const (

dagger/maintenance/testingvalues.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func generateExtensionConfiguration(metadata *extensionMetadata, extensionImage
109109
DynamicLibraryPath: metadata.DynamicLibraryPath,
110110
LdLibraryPath: metadata.LdLibraryPath,
111111
BinPath: metadata.BinPath,
112+
Env: envMapToSlice(metadata.Env),
112113
}, nil
113114
}
114115

pg-crash/metadata.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ metadata = {
1010
dynamic_library_path = []
1111
ld_library_path = []
1212
bin_path = []
13+
env = {}
1314
auto_update_os_libs = false
1415
required_extensions = []
1516
create_extension = false

pgaudit/metadata.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ metadata = {
88
dynamic_library_path = []
99
ld_library_path = []
1010
bin_path = []
11+
env = {}
1112
auto_update_os_libs = false
1213
required_extensions = []
1314
create_extension = true

pgvector/metadata.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ metadata = {
88
dynamic_library_path = []
99
ld_library_path = []
1010
bin_path = []
11+
env = {}
1112
auto_update_os_libs = false
1213
required_extensions = []
1314
create_extension = true

postgis/metadata.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ metadata = {
99
dynamic_library_path = []
1010
ld_library_path = ["system"]
1111
bin_path = []
12+
env = {}
1213
auto_update_os_libs = true
1314
required_extensions = []
1415
create_extension = true

templates/metadata.hcl.tmpl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ metadata = {
5858
# Postgres process. Used in tests and to generate image catalogs.
5959
bin_path = []
6060

61+
# TODO: Remove this comment block after customizing the file.
62+
# `env`: Optional map of environment variables to be injected into the
63+
# PostgreSQL process for this extension.
64+
#
65+
# NOTE: Both HCL and the CNPG operator use `${...}` for placeholder
66+
# expansion. In both systems, `$${...}` is the escape that produces a
67+
# literal `${...}` in the output (`$$` without a following `{` is kept
68+
# as-is).
69+
#
70+
# 1. CNPG Placeholders: Use `$${...}` to pass a placeholder through HCL
71+
# to the operator, which then expands it.
72+
# Example: { "LIB_PATH" = "$${image_root}/lib" }
73+
# HCL output: ${image_root}/lib -> Operator expands to the mount path
74+
#
75+
# 2. Literal `${...}`: Use `$$${...}` so that HCL produces `$${...}`,
76+
# which the operator then treats as a literal `${...}`.
77+
# Example: { "TOKEN_FORMAT" = "$$${value}" }
78+
# HCL output: $${value} -> Operator produces literal: ${value}
79+
#
80+
# 3. Static Values: No special escaping needed.
81+
# Example: { "DEBUG" = "true" }
82+
env = {}
83+
6184
# TODO: Remove this comment block after customizing the file.
6285
# `auto_update_os_libs`: set to true to allow the maintenance tooling
6386
# to update OS libraries automatically; look at the `postgis` example.

0 commit comments

Comments
 (0)