Skip to content

Commit

Permalink
feat(staged-dockerfile): refactor calculateBuildContextGlobsChecksum
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov authored and distorhead committed Oct 27, 2022
1 parent fca82fe commit e558e1e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
37 changes: 36 additions & 1 deletion pkg/build/image/build_context_archive.go
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"sort"

"github.com/containers/buildah/copier"

"github.com/werf/logboek"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/context_manager"
Expand Down Expand Up @@ -111,7 +113,40 @@ func (a *BuildContextArchive) CleanupExtractedDir(ctx context.Context) {
}
}

func (a *BuildContextArchive) CalculatePathsChecksum(ctx context.Context, paths []string) (string, error) {
func (a *BuildContextArchive) CalculateGlobsChecksum(ctx context.Context, globs []string, checkForArchives bool) (string, error) {
contextDir, err := a.ExtractOrGetExtractedDir(ctx)
if err != nil {
return "", fmt.Errorf("unable to get build context dir: %w", err)
}

globStats, err := copier.Stat(contextDir, contextDir, copier.StatOptions{CheckForArchives: checkForArchives}, globs)
if err != nil {
return "", fmt.Errorf("unable to stat globs: %w", err)
}
if len(globStats) == 0 {
return "", fmt.Errorf("no glob matches for globs: %v", globs)
}

var matches []string
for _, globStat := range globStats {
if globStat.Error != "" {
return "", fmt.Errorf("unable to stat glob %q: %s", globStat.Glob, globStat.Error)
}

for _, match := range globStat.Globbed {
matches = append(matches, match)
}
}

pathsChecksum, err := a.calculatePathsChecksum(ctx, matches)
if err != nil {
return "", fmt.Errorf("unable to calculate build context paths checksum: %w", err)
}

return pathsChecksum, nil
}

func (a *BuildContextArchive) calculatePathsChecksum(ctx context.Context, paths []string) (string, error) {
sort.Strings(paths)
paths = util.UniqStrings(paths)

Expand Down
37 changes: 1 addition & 36 deletions pkg/build/stage/instruction/add.go
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"strings"

"github.com/containers/buildah/copier"

"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
Expand Down Expand Up @@ -45,7 +43,7 @@ func (stg *Add) GetDependencies(ctx context.Context, c stage.Conveyor, cb contai
}

if len(fileGlobSrc) > 0 {
if srcChecksum, err := calculateBuildContextGlobsChecksum(ctx, fileGlobSrc, true, buildContextArchive); err != nil {
if srcChecksum, err := buildContextArchive.CalculateGlobsChecksum(ctx, fileGlobSrc, true); err != nil {
return "", fmt.Errorf("unable to calculate build context globs checksum: %w", err)
} else {
args = append(args, "SrcChecksum", srcChecksum)
Expand All @@ -59,36 +57,3 @@ func (stg *Add) GetDependencies(ctx context.Context, c stage.Conveyor, cb contai

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

func calculateBuildContextGlobsChecksum(ctx context.Context, fileGlobs []string, checkForArchives bool, buildContextArchive container_backend.BuildContextArchiver) (string, error) {
contextDir, err := buildContextArchive.ExtractOrGetExtractedDir(ctx)
if err != nil {
return "", fmt.Errorf("unable to get build context dir: %w", err)
}

globStats, err := copier.Stat(contextDir, contextDir, copier.StatOptions{CheckForArchives: checkForArchives}, fileGlobs)
if err != nil {
return "", fmt.Errorf("unable to stat globs: %w", err)
}
if len(globStats) == 0 {
return "", fmt.Errorf("no glob matches for globs: %v", fileGlobs)
}

var matches []string
for _, globStat := range globStats {
if globStat.Error != "" {
return "", fmt.Errorf("unable to stat glob %q: %s", globStat.Glob, globStat.Error)
}

for _, match := range globStat.Globbed {
matches = append(matches, match)
}
}

pathsChecksum, err := buildContextArchive.CalculatePathsChecksum(ctx, matches)
if err != nil {
return "", fmt.Errorf("unable to calculate build context paths checksum: %w", err)
}

return pathsChecksum, nil
}
2 changes: 1 addition & 1 deletion pkg/build/stage/instruction/copy.go
Expand Up @@ -47,7 +47,7 @@ func (stg *Copy) GetDependencies(ctx context.Context, c stage.Conveyor, cb conta
args = append(args, "ExpandedFrom", stg.backendInstruction.From)

if stg.UsesBuildContext() {
if srcChecksum, err := calculateBuildContextGlobsChecksum(ctx, stg.instruction.Data.Src, false, buildContextArchive); err != nil {
if srcChecksum, err := buildContextArchive.CalculateGlobsChecksum(ctx, stg.instruction.Data.Src, false); err != nil {
return "", fmt.Errorf("unable to calculate build context globs checksum: %w", err)
} else {
args = append(args, "SrcChecksum", srcChecksum)
Expand Down
2 changes: 1 addition & 1 deletion pkg/container_backend/archive_interface.go
Expand Up @@ -6,7 +6,7 @@ type BuildContextArchiver interface {
Create(ctx context.Context, opts BuildContextArchiveCreateOptions) error
Path() string
ExtractOrGetExtractedDir(ctx context.Context) (string, error)
CalculatePathsChecksum(ctx context.Context, paths []string) (string, error)
CalculateGlobsChecksum(ctx context.Context, globs []string, checkForArchive bool) (string, error)
CleanupExtractedDir(ctx context.Context)
}

Expand Down

0 comments on commit e558e1e

Please sign in to comment.