Skip to content
Draft
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
pkglib: put real pkg-build-arg-hash into pkg hash
when using
```
buildArgs:
  - REL_HASH_%=@lkt:pkgs:../*
```
then exactly this string is put into the pkg hash, but
if one of these packages are updated, the pkg hash is
not updated, as the string is static.

Instead put in the real pkg-build-arg-hash so that even if
a pkg is updated, it's descendants end up with a new hash.

Signed-off-by: Christoph Ostarek <christoph@zededa.com>
  • Loading branch information
christoph-zededa committed Sep 22, 2025
commit 6e329b9ed2916be05402b132a856bd5db3acdbfd
2 changes: 1 addition & 1 deletion src/cmd/linuxkit/pkg_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func pkgBuildCmd() *cobra.Command {
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// check if the value is a special linuxkit value
buildArg, err := pkglib.TransformBuildArgValue(line, filename)
buildArg, err := pkglib.TransformBuildArgValue("", line, filename)
if err != nil {
return fmt.Errorf("error transforming build arg %s: %v", line, err)
}
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/linuxkit/pkglib/buildarg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
)

// TransformBuildArgValue transforms a build arg pair whose value starts with the special linuxkit prefix.
func TransformBuildArgValue(line, anchorFile string) ([]string, error) {
func TransformBuildArgValue(ownPkgName string, line, anchorFile string) ([]string, error) {
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid build-arg, must be in format 'arg=value': %s", line)
Expand Down Expand Up @@ -80,6 +80,9 @@ func TransformBuildArgValue(line, anchorFile string) ([]string, error) {
if !info.IsDir() {
continue
}
if match == ownPkgName {
continue
}
if strings.HasPrefix(info.Name(), ".") {
continue
}
Expand Down
42 changes: 28 additions & 14 deletions src/cmd/linuxkit/pkglib/pkglib.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,7 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) {
tagTmpl = "{{.Hash}}"
}

// calculate the tag to use based on the template and the pkgHash
tmpl, err := template.New("tag").Parse(tagTmpl)
if err != nil {
return nil, fmt.Errorf("invalid tag template: %v", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, map[string]string{"Hash": pkgHash}); err != nil {
return nil, fmt.Errorf("failed to execute tag template: %v", err)
}
tag := buf.String()
pkgs = append(pkgs, Pkg{
pkg := Pkg{
image: pi.Image,
org: pi.Org,
hash: pkgHash,
Expand All @@ -299,8 +289,32 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) {
buildYML: buildYmlFile,
dockerfile: pi.Dockerfile,
git: git,
tag: tag,
})
}
err = pkg.ProcessBuildArgs()
if err != nil {
return pkgs, fmt.Errorf("processing build-args failed: %w", err)
}
if pkg.buildArgs != nil {
for _, buildArg := range *pkg.buildArgs {
fmt.Printf("add buildArg '%s' to hash\n", buildArg)

buildArgKey := fmt.Sprintf("buildArg=%s", buildArg)
pkgHash += fmt.Sprintf("%x", sha1.Sum([]byte(buildArgKey)))
pkgHash = fmt.Sprintf("%x", sha1.Sum([]byte(pkgHash)))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should be optimized, f.e. filter out duplicates if needed and sort alphabetically.

}
}
// calculate the tag to use based on the template and the pkgHash
tmpl, err := template.New("tag").Parse(tagTmpl)
if err != nil {
return nil, fmt.Errorf("invalid tag template: %v", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, map[string]string{"Hash": pkgHash}); err != nil {
return nil, fmt.Errorf("failed to execute tag template: %v", err)
}
tag := buf.String()
pkg.tag = tag
pkgs = append(pkgs, pkg)
}
return pkgs, nil
}
Expand Down Expand Up @@ -374,7 +388,7 @@ func (p *Pkg) ProcessBuildArgs() error {
}
var buildArgs []string
for _, arg := range *p.buildArgs {
transformedLine, err := TransformBuildArgValue(arg, p.buildYML)
transformedLine, err := TransformBuildArgValue(p.path, arg, p.buildYML)
if err != nil {
return fmt.Errorf("error processing build arg %q: %v", arg, err)
}
Expand Down
Loading