Skip to content
Permalink
Browse files
feat(images-dependencies): implement images dependencies for stapel b…
…uilder
  • Loading branch information
distorhead committed Jan 26, 2022
1 parent c302c05 commit 5d5f1449dd0791b4f89e60e7f454c13b85c52d7a
@@ -37,17 +37,23 @@ func getImports(imageBaseConfig *config.StapelImageBase, options *getImportsOpti
return imports
}

func newDependenciesStage(imports []*config.Import, name StageName, baseStageOptions *NewBaseStageOptions) *DependenciesStage {
func getDependencies(imageBaseConfig *config.StapelImageBase, options *getImportsOptions) []*config.Dependency {
return nil // TODO(images-dependencies)
}

func newDependenciesStage(imports []*config.Import, dependencies []*config.Dependency, name StageName, baseStageOptions *NewBaseStageOptions) *DependenciesStage {
s := &DependenciesStage{}
s.imports = imports
s.dependencies = dependencies
s.BaseStage = newBaseStage(name, baseStageOptions)
return s
}

type DependenciesStage struct {
*BaseStage

imports []*config.Import
imports []*config.Import
dependencies []*config.Dependency
}

func (s *DependenciesStage) GetDependencies(ctx context.Context, c Conveyor, _, _ container_runtime.LegacyImageInterface) (string, error) {
@@ -68,10 +74,17 @@ func (s *DependenciesStage) GetDependencies(ctx context.Context, c Conveyor, _,
args = append(args, elm.Group, elm.Owner)
}

for _, dep := range s.dependencies {
args = append(args, "Dependency", c.GetImageNameForLastImageStage(dep.ImageName))
for _, imp := range dep.Imports {
args = append(args, "DependencyImport", getDependencyImportID(imp))
}
}

return util.Sha256Hash(args...), nil
}

func (s *DependenciesStage) PrepareImage(ctx context.Context, c Conveyor, _, image container_runtime.LegacyImageInterface) error {
func (s *DependenciesStage) PrepareImage(ctx context.Context, c Conveyor, _, img container_runtime.LegacyImageInterface) error {
for _, elm := range s.imports {
sourceImageName := getSourceImageName(elm)
srv, err := c.GetImportServer(ctx, sourceImageName, elm.Stage)
@@ -80,9 +93,9 @@ func (s *DependenciesStage) PrepareImage(ctx context.Context, c Conveyor, _, ima
}

command := srv.GetCopyCommand(ctx, elm)
image.Container().AddServiceRunCommands(command)
img.Container().AddServiceRunCommands(command)

imageServiceCommitChangeOptions := image.Container().ServiceCommitChangeOptions()
imageServiceCommitChangeOptions := img.Container().ServiceCommitChangeOptions()

labelKey := imagePkg.WerfImportChecksumLabelPrefix + getImportID(elm)

@@ -98,6 +111,37 @@ func (s *DependenciesStage) PrepareImage(ctx context.Context, c Conveyor, _, ima
imageServiceCommitChangeOptions.AddLabel(map[string]string{labelKey: labelValue})
}

for _, dep := range s.dependencies {
depImageServiceOptions := img.Container().ServiceCommitChangeOptions()

depImageName := c.GetImageNameForLastImageStage(dep.ImageName)
depImageID := c.GetImageIDForLastImageStage(dep.ImageName)
depImageRepo, depImageTag := imagePkg.ParseRepositoryAndTag(depImageName)

for _, img := range dep.Imports {
switch img.Type {
case config.ImageRepoImport:
depImageServiceOptions.AddEnv(map[string]string{
img.TargetEnv: depImageRepo,
})
case config.ImageTagImport:
depImageServiceOptions.AddEnv(map[string]string{
img.TargetEnv: depImageTag,
})
case config.ImageNameImport:
depImageServiceOptions.AddEnv(map[string]string{
img.TargetEnv: depImageName,
})
case config.ImageIDImport:
depImageServiceOptions.AddEnv(map[string]string{
img.TargetEnv: depImageID,
})
default:
panic("unexpected configuration")
}
}
}

return nil
}

@@ -238,6 +282,13 @@ func formatIncludeAndExcludePath(path string) string {
return strings.TrimRight(path, "*/")
}

func getDependencyImportID(dependencyImport *config.DependencyImport) string {
return util.Sha256Hash(
"Type", string(dependencyImport.Type),
"TargetEnv", dependencyImport.TargetEnv,
)
}

func getImportID(importElm *config.Import) string {
return util.Sha256Hash(
"ImageName", importElm.ImageName,
@@ -4,16 +4,17 @@ import "github.com/werf/werf/pkg/config"

func GenerateDependenciesAfterInstallStage(imageBaseConfig *config.StapelImageBase, baseStageOptions *NewBaseStageOptions) *DependenciesAfterInstallStage {
imports := getImports(imageBaseConfig, &getImportsOptions{After: Install})
if len(imports) != 0 {
return newDependenciesAfterInstallStage(imports, baseStageOptions)
dependencies := getDependencies(imageBaseConfig, &getImportsOptions{After: Install})
if len(imports)+len(dependencies) > 0 {
return newDependenciesAfterInstallStage(imports, dependencies, baseStageOptions)
}

return nil
}

func newDependenciesAfterInstallStage(imports []*config.Import, baseStageOptions *NewBaseStageOptions) *DependenciesAfterInstallStage {
func newDependenciesAfterInstallStage(imports []*config.Import, dependencies []*config.Dependency, baseStageOptions *NewBaseStageOptions) *DependenciesAfterInstallStage {
s := &DependenciesAfterInstallStage{}
s.DependenciesStage = newDependenciesStage(imports, DependenciesAfterInstall, baseStageOptions)
s.DependenciesStage = newDependenciesStage(imports, dependencies, DependenciesAfterInstall, baseStageOptions)
return s
}

@@ -4,16 +4,17 @@ import "github.com/werf/werf/pkg/config"

func GenerateDependenciesAfterSetupStage(imageBaseConfig *config.StapelImageBase, baseStageOptions *NewBaseStageOptions) *DependenciesAfterSetupStage {
imports := getImports(imageBaseConfig, &getImportsOptions{After: Setup})
if len(imports) != 0 {
return newDependenciesAfterSetupStage(imports, baseStageOptions)
dependencies := getDependencies(imageBaseConfig, &getImportsOptions{After: Setup})
if len(imports)+len(dependencies) > 0 {
return newDependenciesAfterSetupStage(imports, dependencies, baseStageOptions)
}

return nil
}

func newDependenciesAfterSetupStage(imports []*config.Import, baseStageOptions *NewBaseStageOptions) *DependenciesAfterSetupStage {
func newDependenciesAfterSetupStage(imports []*config.Import, dependencies []*config.Dependency, baseStageOptions *NewBaseStageOptions) *DependenciesAfterSetupStage {
s := &DependenciesAfterSetupStage{}
s.DependenciesStage = newDependenciesStage(imports, DependenciesAfterSetup, baseStageOptions)
s.DependenciesStage = newDependenciesStage(imports, dependencies, DependenciesAfterSetup, baseStageOptions)
return s
}

@@ -4,16 +4,17 @@ import "github.com/werf/werf/pkg/config"

func GenerateDependenciesBeforeInstallStage(imageBaseConfig *config.StapelImageBase, baseStageOptions *NewBaseStageOptions) *DependenciesBeforeInstallStage {
imports := getImports(imageBaseConfig, &getImportsOptions{Before: Install})
if len(imports) != 0 {
return newDependenciesBeforeInstallStage(imports, baseStageOptions)
dependencies := getDependencies(imageBaseConfig, &getImportsOptions{Before: Install})
if len(imports)+len(dependencies) > 0 {
return newDependenciesBeforeInstallStage(imports, dependencies, baseStageOptions)
}

return nil
}

func newDependenciesBeforeInstallStage(imports []*config.Import, baseStageOptions *NewBaseStageOptions) *DependenciesBeforeInstallStage {
func newDependenciesBeforeInstallStage(imports []*config.Import, dependencies []*config.Dependency, baseStageOptions *NewBaseStageOptions) *DependenciesBeforeInstallStage {
s := &DependenciesBeforeInstallStage{}
s.DependenciesStage = newDependenciesStage(imports, DependenciesBeforeInstall, baseStageOptions)
s.DependenciesStage = newDependenciesStage(imports, dependencies, DependenciesBeforeInstall, baseStageOptions)
return s
}

@@ -4,16 +4,17 @@ import "github.com/werf/werf/pkg/config"

func GenerateDependenciesBeforeSetupStage(imageBaseConfig *config.StapelImageBase, baseStageOptions *NewBaseStageOptions) *DependenciesBeforeSetupStage {
imports := getImports(imageBaseConfig, &getImportsOptions{Before: Setup})
if len(imports) != 0 {
return newDependenciesBeforeSetupStage(imports, baseStageOptions)
dependencies := getDependencies(imageBaseConfig, &getImportsOptions{Before: Setup})
if len(imports)+len(dependencies) > 0 {
return newDependenciesBeforeSetupStage(imports, dependencies, baseStageOptions)
}

return nil
}

func newDependenciesBeforeSetupStage(imports []*config.Import, baseStageOptions *NewBaseStageOptions) *DependenciesBeforeSetupStage {
func newDependenciesBeforeSetupStage(imports []*config.Import, dependencies []*config.Dependency, baseStageOptions *NewBaseStageOptions) *DependenciesBeforeSetupStage {
s := &DependenciesBeforeSetupStage{}
s.DependenciesStage = newDependenciesStage(imports, DependenciesBeforeSetup, baseStageOptions)
s.DependenciesStage = newDependenciesStage(imports, dependencies, DependenciesBeforeSetup, baseStageOptions)
return s
}

0 comments on commit 5d5f144

Please sign in to comment.