-
Notifications
You must be signed in to change notification settings - Fork 9
Base Configs Support - Phase 1 #455
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
Open
elainezhao1
wants to merge
7
commits into
main
Choose a base branch
from
user/elaine/poc1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package imagecustomizerlib | ||
|
||
import ( | ||
"fmt" | ||
|
||
"path/filepath" | ||
|
||
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagecustomizerapi" | ||
) | ||
|
||
func resolveBaseConfigs(cfg *imagecustomizerapi.Config, baseDir string) (*ResolvedConfig, error) { | ||
visited := make(map[string]bool) | ||
pathStack := []string{} | ||
|
||
configChain, err := BuildInheritanceChain(cfg, baseDir, visited, pathStack) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resolved := NewResolvedConfig(configChain) | ||
|
||
return resolved, nil | ||
} | ||
|
||
func BuildInheritanceChain(cfg *imagecustomizerapi.Config, baseDir string, visited map[string]bool, pathStack []string) ([]*imagecustomizerapi.Config, error) { | ||
if cfg.BaseConfigs != nil { | ||
for i, base := range cfg.BaseConfigs { | ||
if err := base.IsValid(); err != nil { | ||
return nil, fmt.Errorf("invalid baseConfig item at index %d:\n%w", i, err) | ||
} | ||
} | ||
} | ||
|
||
var chain []*imagecustomizerapi.Config | ||
|
||
for _, base := range cfg.BaseConfigs { | ||
basePath := filepath.Join(baseDir, base.Path) | ||
absPath, err := filepath.Abs(basePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if visited[absPath] { | ||
return nil, fmt.Errorf("cycle detected in baseConfigs: %v -> %s", pathStack, absPath) | ||
} | ||
|
||
visited[absPath] = true | ||
pathStack = append(pathStack, absPath) | ||
|
||
// Load base file into struct | ||
var baseCfg imagecustomizerapi.Config | ||
if err := imagecustomizerapi.UnmarshalYamlFile(absPath, &baseCfg); err != nil { | ||
return nil, fmt.Errorf("failed to load base config %s: %w", absPath, err) | ||
} | ||
|
||
// Recurse into base config | ||
subChain, err := BuildInheritanceChain(&baseCfg, filepath.Dir(absPath), visited, pathStack) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
chain = append(chain, subChain...) | ||
} | ||
|
||
// Add the current config at the end | ||
chain = append(chain, cfg) | ||
|
||
return chain, nil | ||
} | ||
|
||
func resolveOverrideFields(chain []*imagecustomizerapi.Config, target *ResolvedConfig) { | ||
for _, config := range chain { | ||
if config.Input.Image != (imagecustomizerapi.InputImage{}) && config.Input.Image.Path != "" { | ||
// .input.image.path | ||
target.InputImagePath = config.Input.Image.Path | ||
} | ||
|
||
if config.Output != (imagecustomizerapi.Output{}) { | ||
// .output.image.path | ||
if config.Output.Image != (imagecustomizerapi.OutputImage{}) && config.Output.Image.Path != "" { | ||
target.OutputImagePath = config.Output.Image.Path | ||
} | ||
// .output.image.format | ||
if config.Output.Image != (imagecustomizerapi.OutputImage{}) && config.Output.Image.Format != "" { | ||
target.OutputImageFormat = config.Output.Image.Format | ||
} | ||
// .output.image.artifacts.path | ||
if config.Output.Artifacts != nil && config.Output.Artifacts.Path != "" { | ||
target.OutputArtifactsPath = config.Output.Artifacts.Path | ||
} | ||
} | ||
} | ||
} | ||
|
||
func resolveMergeFields(chain []*imagecustomizerapi.Config, target *ResolvedConfig) { | ||
for _, cfg := range chain { | ||
// .output.artifacts.items | ||
if cfg.Output != (imagecustomizerapi.Output{}) && cfg.Output.Artifacts != nil && len(cfg.Output.Artifacts.Items) > 0 { | ||
target.OutputArtifactsItems = mergeOutputArtifactTypes( | ||
target.OutputArtifactsItems, cfg.Output.Artifacts.Items, | ||
) | ||
} | ||
} | ||
} | ||
|
||
func mergeOutputArtifactTypes(base, current []imagecustomizerapi.OutputArtifactsItemType) []imagecustomizerapi.OutputArtifactsItemType { | ||
seen := make(map[imagecustomizerapi.OutputArtifactsItemType]bool) | ||
var merged []imagecustomizerapi.OutputArtifactsItemType | ||
|
||
// Add base items first | ||
for _, item := range base { | ||
if !seen[item] { | ||
merged = append(merged, item) | ||
seen[item] = true | ||
} | ||
} | ||
|
||
// Add current items | ||
for _, item := range current { | ||
if !seen[item] { | ||
merged = append(merged, item) | ||
seen[item] = true | ||
} | ||
} | ||
|
||
return merged | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package imagecustomizerlib_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagecustomizerapi" | ||
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/pkg/imagecustomizerlib" | ||
) | ||
|
||
func TestBaseConfigsInput(t *testing.T) { | ||
base := &imagecustomizerapi.Config{ | ||
Input: imagecustomizerapi.Input{ | ||
Image: imagecustomizerapi.InputImage{ | ||
Path: "base-image-1.vhdx", | ||
}, | ||
}, | ||
} | ||
|
||
current := &imagecustomizerapi.Config{ | ||
Input: imagecustomizerapi.Input{ | ||
Image: imagecustomizerapi.InputImage{ | ||
Path: "base-image-2.vhdx", | ||
}, | ||
}, | ||
} | ||
|
||
chain := []*imagecustomizerapi.Config{base, current} | ||
|
||
resolved := imagecustomizerlib.NewResolvedConfig(chain) | ||
|
||
if resolved.InputImagePath != "base-image-2.vhdx" { | ||
t.Errorf("expected input image path is base-image-2.vhdx, got %s", resolved.InputImagePath) | ||
} | ||
} | ||
|
||
func TestBaseConfigsOutput(t *testing.T) { | ||
base := &imagecustomizerapi.Config{ | ||
Output: imagecustomizerapi.Output{ | ||
Image: imagecustomizerapi.OutputImage{ | ||
Path: "output-image-1.vhdx", | ||
}, | ||
Artifacts: &imagecustomizerapi.Artifacts{ | ||
Path: "./artifacts-1", | ||
Items: []imagecustomizerapi.OutputArtifactsItemType{ | ||
imagecustomizerapi.OutputArtifactsItemUkis, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
current := &imagecustomizerapi.Config{ | ||
Output: imagecustomizerapi.Output{ | ||
Image: imagecustomizerapi.OutputImage{ | ||
Path: "output-image-2.vhdx", | ||
}, | ||
Artifacts: &imagecustomizerapi.Artifacts{ | ||
Path: "./artifacts-2", | ||
Items: []imagecustomizerapi.OutputArtifactsItemType{ | ||
imagecustomizerapi.OutputArtifactsItemShim, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
chain := []*imagecustomizerapi.Config{base, current} | ||
|
||
resolved := imagecustomizerlib.NewResolvedConfig(chain) | ||
|
||
if resolved.OutputImagePath != "output-image-2.vhdx" { | ||
t.Errorf("expected output path is output-image-2.vhdx, got %s", resolved.OutputImagePath) | ||
} | ||
|
||
if resolved.OutputArtifactsPath != "./artifacts-2" { | ||
t.Errorf("expected artifacts path is ./artifacts-2, got %s", resolved.OutputArtifactsPath) | ||
} | ||
|
||
expectedItems := []imagecustomizerapi.OutputArtifactsItemType{ | ||
imagecustomizerapi.OutputArtifactsItemUkis, | ||
imagecustomizerapi.OutputArtifactsItemShim, | ||
} | ||
got := resolved.OutputArtifactsItems | ||
if len(got) != len(expectedItems) { | ||
t.Errorf("expected output artifacts length is %d, got %d", len(expectedItems), len(got)) | ||
} | ||
|
||
for _, item := range expectedItems { | ||
found := false | ||
for _, g := range got { | ||
if g == item { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
t.Errorf("expected artifact item %q not found in result: %v", item, got) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package imagecustomizerlib | ||
|
||
import ( | ||
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagecustomizerapi" | ||
) | ||
|
||
// ResolvedConfig represents resolved fields and a chain of base configs. | ||
type ResolvedConfig struct { | ||
InputImagePath string | ||
OutputImagePath string | ||
OutputImageFormat imagecustomizerapi.ImageFormatType | ||
OutputArtifactsPath string | ||
OutputArtifactsItems []imagecustomizerapi.OutputArtifactsItemType | ||
|
||
InheritanceChain []*imagecustomizerapi.Config | ||
} | ||
|
||
func NewResolvedConfig(chain []*imagecustomizerapi.Config) *ResolvedConfig { | ||
elainezhao1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolved := &ResolvedConfig{ | ||
InheritanceChain: chain, | ||
} | ||
|
||
resolveOverrideFields(chain, resolved) | ||
resolveMergeFields(chain, resolved) | ||
|
||
return resolved | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try creating some configs without the artifacts path field the test would fail with null pointer exception at ResolveOverrideFields_