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
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
25 changes: 25 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,31 @@ 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(), Required: true},
},
},
},
}
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 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
30 changes: 29 additions & 1 deletion 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,7 +867,34 @@
"path": {
"type": "string"
},
"format": {
"required": {
"type": ["boolean", "string"],
"default": true
}
},
"required": [
"path"
]
}
]
}
}
]
},

"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": {
"oneOf": [
{"type": "string"},
{
"type": "object",
"additionalProperties": false,
"properties": {
"path": {
"type": "string"
},
"required": {
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.

47 changes: 47 additions & 0 deletions types/labelfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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"`
Required bool `yaml:"required" json:"required"`
Format string `yaml:"format,omitempty" json:"format,omitempty"`
}

// MarshalYAML makes LabelFile implement yaml.Marshaler
func (e LabelFile) MarshalYAML() (interface{}, error) {
if e.Required {
return e.Path, nil
}
return map[string]any{
"path": e.Path,
"required": e.Required,
}, nil
}

// MarshalJSON makes LabelFile implement json.Marshaler
func (e *LabelFile) MarshalJSON() ([]byte, error) {
if e.Required {
return json.Marshal(e.Path)
}
// Pass as a value to avoid re-entering this method and use the default implementation
return json.Marshal(*e)
}
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
Loading