Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add label_file support to service #713

Merged
merged 11 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions loader/example1.label
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# passed through
FOO=foo_from_label_file
LABEL.WITH.DOT=ok
LABEL_WITH_UNDERSCORE=ok

# overridden in example2.label
BAR=bar_from_label_file

# overridden in full-example.yml
BAZ=baz_from_label_file
4 changes: 4 additions & 0 deletions loader/example2.label
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BAR=bar_from_label_file_2

# overridden in configDetails.Labels
QUX=quz_from_label_file_2
4 changes: 4 additions & 0 deletions loader/full-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ services:
# - "com.example.number=42"
# - "com.example.empty-label"

label_file:
- ./example1.label
- ./example2.label

links:
- db
- db:database
Expand Down
33 changes: 33 additions & 0 deletions loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,20 @@ func services(workingDir, homeDir string) types.Services {
Ipc: "host",
Uts: "host",
Labels: map[string]string{
"FOO": "foo_from_label_file",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this huge test we inherited from legacy compose parser in docker/cli.
As it is painful to maintain, we prefer to have individual test checking specific attribute is well supported (which you also provided 🥳)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I revert these changes or keep them?

"BAR": "bar_from_label_file_2",
"BAZ": "baz_from_label_file",
"QUX": "quz_from_label_file_2",
"LABEL.WITH.DOT": "ok",
"LABEL_WITH_UNDERSCORE": "ok",
"com.example.description": "Accounting webapp",
"com.example.number": "42",
"com.example.empty-label": "",
},
LabelFiles: []string{
filepath.Join(workingDir, "example1.label"),
filepath.Join(workingDir, "example2.label"),
},
Links: []string{
"db",
"db:database",
Expand Down Expand Up @@ -770,9 +780,18 @@ services:
image: redis
ipc: host
labels:
BAR: bar_from_label_file_2
BAZ: baz_from_label_file
FOO: foo_from_label_file
LABEL.WITH.DOT: ok
LABEL_WITH_UNDERSCORE: ok
QUX: quz_from_label_file_2
com.example.description: Accounting webapp
com.example.empty-label: ""
com.example.number: "42"
label_file:
- %s
- %s
links:
- db
- db:database
Expand Down Expand Up @@ -1058,6 +1077,8 @@ x-nested:
filepath.Join(workingDir, "bar"),
filepath.Join(workingDir, "example1.env"),
filepath.Join(workingDir, "example2.env"),
filepath.Join(workingDir, "example1.label"),
filepath.Join(workingDir, "example2.label"),
workingDir,
filepath.Join(workingDir, "static"),
filepath.Join(homeDir, "configs"),
Expand Down Expand Up @@ -1382,10 +1403,20 @@ func fullExampleJSON(workingDir, homeDir string) string {
"image": "redis",
"ipc": "host",
"labels": {
"BAR": "bar_from_label_file_2",
"BAZ": "baz_from_label_file",
"FOO": "foo_from_label_file",
"LABEL.WITH.DOT": "ok",
"LABEL_WITH_UNDERSCORE": "ok",
"QUX": "quz_from_label_file_2",
"com.example.description": "Accounting webapp",
"com.example.empty-label": "",
"com.example.number": "42"
},
"label_file": [
"%s",
"%s"
],
"links": [
"db",
"db:database",
Expand Down Expand Up @@ -1707,6 +1738,8 @@ func fullExampleJSON(workingDir, homeDir string) string {
toPath(workingDir, "bar"),
toPath(workingDir, "example1.env"),
toPath(workingDir, "example2.env"),
toPath(workingDir, "example1.label"),
toPath(workingDir, "example2.label"),
toPath(workingDir),
toPath(workingDir, "static"),
toPath(homeDir, "configs"),
Expand Down
6 changes: 6 additions & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,12 @@ func modelToProject(dict map[string]interface{}, opts *Options, configDetails ty
return nil, err
}
}

project, err = project.WithServicesLabelsResolved(opts.discardEnvFiles)
if err != nil {
return nil, err
}

return project, nil
}

Expand Down
40 changes: 40 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,46 @@ func TestLoadServiceWithEnvFile(t *testing.T) {
assert.Equal(t, "YES", *service.Environment["HALLO"])
}

func TestLoadServiceWithLabelFile(t *testing.T) {
file, err := os.CreateTemp("", "test-compose-go")
assert.NilError(t, err)
defer os.Remove(file.Name())

_, err = file.Write([]byte("MY_LABEL=MY_VALUE"))
assert.NilError(t, err)

p := &types.Project{
Services: types.Services{
"test": {
Name: "test",
LabelFiles: []string{
file.Name(),
},
},
},
}
p, err = p.WithServicesLabelsResolved(false)
assert.NilError(t, err)
service, err := p.GetService("test")
assert.NilError(t, err)
assert.Equal(t, "MY_VALUE", service.Labels["MY_LABEL"])
}

func TestLoadServiceWithLabelFile_NotExists(t *testing.T) {
p := &types.Project{
Services: types.Services{
"test": {
Name: "test",
LabelFiles: []string{
"test",
},
},
},
}
p, err := p.WithServicesLabelsResolved(false)
assert.ErrorContains(t, err, "label file test not found")
}

func TestLoadNoSSHInBuildConfig(t *testing.T) {
actual, err := loadYAML(`
name: load-no-ssh-in-build-config
Expand Down
1 change: 1 addition & 0 deletions override/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func init() {
mergeSpecials["services.*.dns_search"] = mergeToSequence
mergeSpecials["services.*.entrypoint"] = override
mergeSpecials["services.*.env_file"] = mergeToSequence
mergeSpecials["services.*.label_file"] = mergeToSequence
mergeSpecials["services.*.environment"] = mergeToSequence
mergeSpecials["services.*.extra_hosts"] = mergeExtraHosts
mergeSpecials["services.*.healthcheck.test"] = override
Expand Down
1 change: 1 addition & 0 deletions paths/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func ResolveRelativePaths(project map[string]any, base string, remotes []RemoteR
"services.*.build.context": r.absContextPath,
"services.*.build.additional_contexts.*": r.absContextPath,
"services.*.env_file.*.path": r.absPath,
"services.*.label_file.*": r.absPath,
"services.*.extends.file": r.absExtendsPath,
"services.*.develop.watch.*.path": r.absSymbolicLink,
"services.*.volumes.*": r.absVolumeMount,
Expand Down
11 changes: 11 additions & 0 deletions schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
"domainname": {"type": "string"},
"entrypoint": {"$ref": "#/definitions/command"},
"env_file": {"$ref": "#/definitions/env_file"},
"label_file": {"$ref": "#/definitions/label_file"},
"environment": {"$ref": "#/definitions/list_or_dict"},

"expose": {
Expand Down Expand Up @@ -884,6 +885,16 @@
]
},

"label_file": {
"oneOf": [
{"type": "string"},
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds to me this is a premature feature to add support for required here. label_file should be declared at most a string|array of string for user convenience

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof agree, fixed

"type": "array",
"items": {"type": "string"}
}
]
},

"string_or_list": {
"oneOf": [
{"type": "string"},
Expand Down
32 changes: 32 additions & 0 deletions types/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions types/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ import (
// Labels is a mapping type for labels
type Labels map[string]string

func NewLabelsFromMappingWithEquals(mapping MappingWithEquals) Labels {
labels := Labels{}
for k, v := range mapping {
if v != nil {
labels[k] = *v
}
}
return labels
}

func (l Labels) Add(key, value string) Labels {
if l == nil {
l = Labels{}
Expand All @@ -42,6 +52,15 @@ func (l Labels) AsList() []string {
return s
}

func (l Labels) ToMappingWithEquals() MappingWithEquals {
mapping := MappingWithEquals{}
for k, v := range l {
v := v
mapping[k] = &v
}
return mapping
}

// label value can be a string | number | boolean | null (empty)
func labelValue(e interface{}) string {
if e == nil {
Expand Down
57 changes: 54 additions & 3 deletions types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,22 +663,73 @@ func (p Project) WithServicesEnvironmentResolved(discardEnvFiles bool) (*Project
return newProject, nil
}

// WithServicesLabelsResolved parses label_files set for services to resolve the actual label map for services
// It returns a new Project instance with the changes and keep the original Project unchanged
func (p Project) WithServicesLabelsResolved(discardLabelFiles bool) (*Project, error) {
newProject := p.deepCopy()
for i, service := range newProject.Services {
labels := MappingWithEquals{}
// resolve variables based on other files we already parsed
var resolve dotenv.LookupFn = func(s string) (string, bool) {
v, ok := labels[s]
if ok && v != nil {
return *v, ok
}
return "", false
}

for _, labelFile := range service.LabelFiles {
vars, err := loadLabelFile(labelFile, resolve)
if err != nil {
return nil, err
}
labels.OverrideBy(vars.ToMappingWithEquals())
}

labels = labels.OverrideBy(service.Labels.ToMappingWithEquals())
if len(labels) == 0 {
labels = nil
} else {
service.Labels = NewLabelsFromMappingWithEquals(labels)
}

if discardLabelFiles {
service.LabelFiles = nil
}
newProject.Services[i] = service
}
return newProject, nil
}

func loadEnvFile(envFile EnvFile, resolve dotenv.LookupFn) (Mapping, error) {
if _, err := os.Stat(envFile.Path); os.IsNotExist(err) {
if envFile.Required {
return nil, fmt.Errorf("env file %s not found: %w", envFile.Path, err)
}
return nil, nil
}
file, err := os.Open(envFile.Path)

return loadMappingFile(envFile.Path, envFile.Format, resolve)
}

func loadLabelFile(labelFile string, resolve dotenv.LookupFn) (Mapping, error) {
if _, err := os.Stat(labelFile); os.IsNotExist(err) {
return nil, fmt.Errorf("label file %s not found: %w", labelFile, err)
}

return loadMappingFile(labelFile, "", resolve)
}

func loadMappingFile(path string, format string, resolve dotenv.LookupFn) (Mapping, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close() //nolint:errcheck

var fileVars map[string]string
if envFile.Format != "" {
fileVars, err = dotenv.ParseWithFormat(file, envFile.Path, resolve, envFile.Format)
if format != "" {
fileVars, err = dotenv.ParseWithFormat(file, path, resolve, format)
} else {
fileVars, err = dotenv.ParseWithLookup(file, resolve)
}
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type ServiceConfig struct {
Ipc string `yaml:"ipc,omitempty" json:"ipc,omitempty"`
Isolation string `yaml:"isolation,omitempty" json:"isolation,omitempty"`
Labels Labels `yaml:"labels,omitempty" json:"labels,omitempty"`
LabelFiles []string `yaml:"label_file,omitempty" json:"label_file,omitempty"`
CustomLabels Labels `yaml:"-" json:"-"`
Links []string `yaml:"links,omitempty" json:"links,omitempty"`
Logging *LoggingConfig `yaml:"logging,omitempty" json:"logging,omitempty"`
Expand Down