Skip to content

Commit

Permalink
remove extends from model after serice has been extended
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Mar 23, 2023
1 parent 5713643 commit ed19dca
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 38 deletions.
27 changes: 15 additions & 12 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,16 +589,17 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
}

if serviceConfig.Extends != nil && !opts.SkipExtends {
baseServiceName := *serviceConfig.Extends["service"]
baseServiceName := serviceConfig.Extends.Service
var baseService *types.ServiceConfig
if file := serviceConfig.Extends["file"]; file == nil {
file := serviceConfig.Extends.File
if file == "" {
baseService, err = loadServiceWithExtends(filename, baseServiceName, servicesDict, workingDir, lookupEnv, opts, ct)
if err != nil {
return nil, err
}
} else {
// Resolve the path to the imported file, and load it.
baseFilePath := absPath(workingDir, *file)
baseFilePath := absPath(workingDir, file)

b, err := os.ReadFile(baseFilePath)
if err != nil {
Expand All @@ -617,10 +618,10 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
}

// Make paths relative to the importing Compose file. Note that we
// make the paths relative to `*file` rather than `baseFilePath` so
// that the resulting paths won't be absolute if `*file` isn't an
// make the paths relative to `file` rather than `baseFilePath` so
// that the resulting paths won't be absolute if `file` isn't an
// absolute path.
baseFileParent := filepath.Dir(*file)
baseFileParent := filepath.Dir(file)
if baseService.Build != nil {
baseService.Build.Context = resolveBuildContextPath(baseFileParent, baseService.Build.Context)
}
Expand All @@ -641,6 +642,7 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
if err != nil {
return nil, err
}
serviceConfig.Extends = nil
}

return serviceConfig, nil
Expand Down Expand Up @@ -1048,14 +1050,15 @@ var transformDependsOnConfig TransformerFunc = func(data interface{}) (interface
}
}

var transformExtendsConfig TransformerFunc = func(data interface{}) (interface{}, error) {
switch data.(type) {
var transformExtendsConfig TransformerFunc = func(value interface{}) (interface{}, error) {
switch value.(type) {
case string:
data = map[string]interface{}{
"service": data,
}
return map[string]interface{}{"service": value}, nil
case map[string]interface{}:
return value, nil
default:
return value, errors.Errorf("invalid type %T for extends", value)
}
return transformMappingOrListFunc("=", true)(data)
}

var transformServiceVolumeConfig TransformerFunc = func(data interface{}) (interface{}, error) {
Expand Down
21 changes: 1 addition & 20 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1934,19 +1934,10 @@ func TestLoadWithExtends(t *testing.T) {

expectedEnvFilePath := filepath.Join(extendsDir, "extra.env")

expectedExtendsPath := filepath.Join(
extendsDir,
"compose-test-extends-imported.yaml",
)

expServices := types.Services{
{
Name: "importer",
Image: "nginx",
Extends: types.ExtendsConfig{
"file": &expectedExtendsPath,
"service": strPtr("imported"),
},
Environment: types.MappingWithEquals{
"SOURCE": strPtr("extends"),
},
Expand Down Expand Up @@ -1979,24 +1970,13 @@ func TestLoadWithExtendsWithContextUrl(t *testing.T) {
actual, err := Load(configDetails)
assert.NilError(t, err)

expectedExtendsPath, err := filepath.Abs(
filepath.Join(
"testdata",
"compose-test-extends-with-context-url-imported.yaml",
),
)
assert.NilError(t, err)
expServices := types.Services{
{
Name: "importer-with-https-url",
Build: &types.BuildConfig{
Context: "https://github.com/docker/compose.git",
Dockerfile: "Dockerfile",
},
Extends: types.ExtendsConfig{
"file": &expectedExtendsPath,
"service": strPtr("imported-with-https-url"),
},
Environment: types.MappingWithEquals{},
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
Scale: 1,
Expand Down Expand Up @@ -2304,6 +2284,7 @@ volumes:

func TestLoadServiceExtension(t *testing.T) {
dict := `
name: test
services:
extension: # this name should be allowed
image: web
Expand Down
5 changes: 2 additions & 3 deletions loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ func Normalize(project *types.Project, resolvePaths bool) error {
}
s.Environment = s.Environment.Resolve(fn)

if extendFile := s.Extends["file"]; extendFile != nil && *extendFile != "" {
p := absPath(project.WorkingDir, *extendFile)
s.Extends["file"] = &p
if s.Extends != nil && s.Extends.File != "" {
s.Extends.File = absPath(project.WorkingDir, s.Extends.File)
}

for _, link := range s.Links {
Expand Down
2 changes: 1 addition & 1 deletion loader/testdata/subdir/compose-test-extends-imported.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ services:
imported:
image: nginx
env_file:
- extra.env
- ./extra.env # expected to be loaded relative to this file, not the extending one
volumes:
- /opt/data:/var/lib/mysql
7 changes: 5 additions & 2 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type ServiceConfig struct {
Environment MappingWithEquals `yaml:",omitempty" json:"environment,omitempty"`
EnvFile StringList `mapstructure:"env_file" yaml:"env_file,omitempty" json:"env_file,omitempty"`
Expose StringOrNumberList `yaml:",omitempty" json:"expose,omitempty"`
Extends ExtendsConfig `yaml:"extends,omitempty" json:"extends,omitempty"`
Extends *ExtendsConfig `yaml:"extends,omitempty" json:"extends,omitempty"`
ExternalLinks []string `mapstructure:"external_links" yaml:"external_links,omitempty" json:"external_links,omitempty"`
ExtraHosts HostsList `mapstructure:"extra_hosts" yaml:"extra_hosts,omitempty" json:"extra_hosts,omitempty"`
GroupAdd []string `mapstructure:"group_add" yaml:"group_add,omitempty" json:"group_add,omitempty"`
Expand Down Expand Up @@ -1008,7 +1008,10 @@ type ServiceDependency struct {
Extensions Extensions `mapstructure:"#extensions" yaml:",inline" json:"-"`
}

type ExtendsConfig MappingWithEquals
type ExtendsConfig struct {
File string `yaml:",omitempty" json:"file,omitempty"`
Service string `yaml:",omitempty" json:"service,omitempty"`
}

// SecretConfig for a secret
type SecretConfig FileObjectConfig
Expand Down

0 comments on commit ed19dca

Please sign in to comment.