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

Fix absolute path substitution in configs imported as dependencies. #5389

Merged
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
24 changes: 17 additions & 7 deletions cmd/skaffold/app/cmd/parse_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func getConfigs(cfgOpts configOpts, opts config.SkaffoldOptions, r *record) ([]*
if len(parsed) == 0 {
return nil, fmt.Errorf("skaffold config file %s is empty", opts.ConfigurationFile)
}
logrus.Debugf("parsed %d configs from configuration file %s", len(parsed), cfgOpts.file)

var configs []*latest.SkaffoldConfig
for i, cfg := range parsed {
Expand Down Expand Up @@ -144,7 +145,8 @@ func processEachConfig(config *latest.SkaffoldConfig, cfgOpts configOpts, opts c

var configs []*latest.SkaffoldConfig
for _, d := range config.Dependencies {
depConfigs, err := processEachDependency(d, cfgOpts.file, required, profiles, opts, r)
newOpts := configOpts{file: cfgOpts.file, profiles: filterActiveProfiles(d, profiles), isRequired: required, isDependency: cfgOpts.isDependency}
depConfigs, err := processEachDependency(d, newOpts, opts, r)
if err != nil {
return nil, err
}
Expand All @@ -157,8 +159,8 @@ func processEachConfig(config *latest.SkaffoldConfig, cfgOpts configOpts, opts c
return configs, nil
}

// processEachDependency parses a config dependency with the calculated set of activated profiles.
func processEachDependency(d latest.ConfigDependency, fPath string, required bool, profiles []string, opts config.SkaffoldOptions, r *record) ([]*latest.SkaffoldConfig, error) {
// filterActiveProfiles selects the set of profiles to activate in the dependency config based on the current set of active profiles.
func filterActiveProfiles(d latest.ConfigDependency, profiles []string) []string {
var depProfiles []string
for _, ap := range d.ActiveProfiles {
if len(ap.ActivatedBy) == 0 {
Expand All @@ -172,6 +174,11 @@ func processEachDependency(d latest.ConfigDependency, fPath string, required boo
}
}
}
return depProfiles
}

// processEachDependency parses a config dependency with the calculated set of activated profiles.
func processEachDependency(d latest.ConfigDependency, cfgOpts configOpts, opts config.SkaffoldOptions, r *record) ([]*latest.SkaffoldConfig, error) {
path := d.Path

if d.GitRepo != nil {
Expand All @@ -184,19 +191,22 @@ func processEachDependency(d latest.ConfigDependency, fPath string, required boo

if path == "" {
// empty path means configs in the same file
path = fPath
path = cfgOpts.file
}
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(errors.Unwrap(err)) {
return nil, fmt.Errorf("could not find skaffold config %s that is referenced as a dependency in config %s", path, fPath)
return nil, fmt.Errorf("could not find skaffold config %s that is referenced as a dependency in config %s", path, cfgOpts.file)
}
return nil, fmt.Errorf("parsing dependencies for skaffold config %s: %w", fPath, err)
return nil, fmt.Errorf("parsing dependencies for skaffold config %s: %w", cfgOpts.file, err)
}
if fi.IsDir() {
path = filepath.Join(path, "skaffold.yaml")
}
depConfigs, err := getConfigs(configOpts{file: path, selection: d.Names, profiles: depProfiles, isRequired: required, isDependency: path != fPath}, opts, r)
cfgOpts.isDependency = cfgOpts.isDependency || path != cfgOpts.file
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
cfgOpts.file = path
cfgOpts.selection = d.Names
depConfigs, err := getConfigs(cfgOpts, opts, r)
if err != nil {
return nil, err
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/skaffold/app/cmd/parse_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ requires:
createCfg("cfg00", "image00", ".", []latest.ConfigDependency{{Names: []string{"cfg01"}}, {Path: "doc2", Names: []string{"cfg21"}}}),
},
},
{
description: "dependencies in same file",
documents: []document{
{path: "skaffold.yaml", configs: []mockCfg{{name: "cfg00", requiresStanza: `
requires:
- path: doc1
`}}},
{path: "doc1/skaffold.yaml", configs: []mockCfg{{name: "cfg10", requiresStanza: `
requires:
- configs: [cfg11]
`}, {name: "cfg11", requiresStanza: ""}}},
},
expected: []*latest.SkaffoldConfig{
createCfg("cfg11", "image11", "doc1", nil),
createCfg("cfg10", "image10", "doc1", []latest.ConfigDependency{{Names: []string{"cfg11"}}}),
createCfg("cfg00", "image00", ".", []latest.ConfigDependency{{Path: "doc1"}}),
},
},
{
description: "looped dependencies",
documents: []document{
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ type ConfigDependency struct {
Names []string `yaml:"configs,omitempty"`

// Path describes the path to the file containing the required configs.
Path string `yaml:"path" skaffold:"filepath" yamltags:"oneOf=paths"`
Path string `yaml:"path,omitempty" skaffold:"filepath" yamltags:"oneOf=paths"`

// GitRepo describes a remote git repository containing the required configs.
GitRepo *GitInfo `yaml:"git" yamltags:"oneOf=paths"`
GitRepo *GitInfo `yaml:"git,omitempty" yamltags:"oneOf=paths"`

// ActiveProfiles describes the list of profiles to activate when resolving the required configs. These profiles must exist in the imported config.
ActiveProfiles []ProfileDependency `yaml:"activeProfiles,omitempty"`
Expand Down