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 8 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
37 changes: 37 additions & 0 deletions loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,24 @@ 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: []types.LabelFile{
{
Path: filepath.Join(workingDir, "example1.label"),
},
{
Path: filepath.Join(workingDir, "example2.label"),
},
},
Links: []string{
"db",
"db:database",
Expand Down Expand Up @@ -770,9 +784,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 +1081,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 +1407,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 +1742,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: []types.LabelFile{
{Path: 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: []types.LabelFile{
{Path: "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
14 changes: 14 additions & 0 deletions override/uncity.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
unique["services.*.env_file"] = envFileIndexer
unique["services.*.expose"] = exposeIndexer
unique["services.*.labels"] = keyValueIndexer
unique["services.*.label_file"] = labelFileIndexer
unique["services.*.links"] = keyValueIndexer
unique["services.*.networks.*.aliases"] = keyValueIndexer
unique["services.*.networks.*.link_local_ips"] = keyValueIndexer
Expand Down Expand Up @@ -227,3 +228,16 @@ func envFileIndexer(y any, p tree.Path) (string, error) {
}
return "", nil
}

func labelFileIndexer(y any, p tree.Path) (string, error) {
switch value := y.(type) {
case string:
return value, nil
case map[string]any:
Copy link
Collaborator

Choose a reason for hiding this comment

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

not relevant anymore AFAICT

if pathValue, ok := value["path"]; ok {
return pathValue.(string), nil
}
return "", fmt.Errorf("label path attribute %s is missing", p)
}
return "", nil
}
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.*.path": r.absPath,
"services.*.extends.file": r.absExtendsPath,
"services.*.develop.watch.*.path": r.absSymbolicLink,
"services.*.volumes.*": r.absVolumeMount,
Expand Down
14 changes: 11 additions & 3 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 @@ -866,9 +867,6 @@
"path": {
"type": "string"
},
"format": {
"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.

is this removal intentional ?

Copy link
Contributor Author

@idsulik idsulik Nov 27, 2024

Choose a reason for hiding this comment

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

yes, you wrote to make it string or []string, so I did it).

Copy link
Collaborator

Choose a reason for hiding this comment

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

sure, but here you removed format attribute from env_file defintion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, accidentally deleted it, put it back

"required": {
"type": ["boolean", "string"],
"default": true
Expand All @@ -884,6 +882,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
1 change: 1 addition & 0 deletions transform/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
transformers["services.*.build.additional_contexts"] = transformKeyValue
transformers["services.*.depends_on"] = transformDependsOn
transformers["services.*.env_file"] = transformEnvFile
transformers["services.*.label_file"] = transformLabelFile
transformers["services.*.extends"] = transformExtends
transformers["services.*.networks"] = transformServiceNetworks
transformers["services.*.volumes.*"] = transformVolumeMount
Expand Down
55 changes: 55 additions & 0 deletions transform/labelfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2020 The Compose Specification Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package transform

import (
"fmt"

"github.com/compose-spec/compose-go/v2/tree"
)

func transformLabelFile(data any, p tree.Path, _ bool) (any, error) {
switch v := data.(type) {
case string:
return []any{
transformLabelFileValue(v),
}, nil
case []any:
for i, e := range v {
v[i] = transformLabelFileValue(e)
}
return v, nil
default:
return nil, fmt.Errorf("%s: invalid type %T for label_file", p, v)
}
}

func transformLabelFileValue(data any) any {
Copy link
Collaborator

Choose a reason for hiding this comment

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

not relevant anymore

switch v := data.(type) {
case string:
return map[string]any{
"path": v,
"required": true,
}
case map[string]any:
if _, ok := v["required"]; !ok {
v["required"] = true
}
return v
}
return nil
}
30 changes: 30 additions & 0 deletions types/derived.gen.go

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

36 changes: 36 additions & 0 deletions types/labelfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2020 The Compose Specification Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

import (
"encoding/json"
)

type LabelFile struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

unused ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, thank you, removed

Path string `yaml:"path,omitempty" json:"path,omitempty"`
Format string `yaml:"format,omitempty" json:"format,omitempty"`
}

// MarshalYAML makes LabelFile implement yaml.Marshaler
func (e LabelFile) MarshalYAML() (interface{}, error) {
return e.Path, nil
}

// MarshalJSON makes LabelFile implement json.Marshaler
func (e *LabelFile) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Path)
}
Loading