Add specific field to resolve final images

This commit starts deprecation of `join` keyword in favor of
`requires_final_images` as boolean in the compilation spec.

The change is driven by two reasons: syntax and guarantee unique hashes.

- the hashtree when computing a hash it analizes the requires field of
  each spec, ignoring the join field
- the join field doesn't add much value. Having it separate suggests
  that a spec can contain both `requires` and `join`, but that's not
  actually true. We just act differently on the same list.

Signed-off-by: Ettore Di Giacinto <mudler@sabayon.org>
This commit is contained in:
Ettore Di Giacinto 2021-07-09 09:31:37 +02:00
parent 64ab3711ca
commit 2efb17a06c
2 changed files with 47 additions and 35 deletions

View File

@ -774,22 +774,31 @@ func (cs *LuetCompiler) getSpecHash(pkgs pkg.DefaultPackages, salt string) (stri
return fmt.Sprintf("%x", h.Sum(nil)), nil return fmt.Sprintf("%x", h.Sum(nil)), nil
} }
func (cs *LuetCompiler) resolveJoinImages(concurrency int, keepPermissions bool, p *compilerspec.LuetCompilationSpec) error { func (cs *LuetCompiler) resolveFinalImages(concurrency int, keepPermissions bool, p *compilerspec.LuetCompilationSpec) error {
joinTag := ">:loop: join<" joinTag := ">:loop: final images<"
if len(p.Join) != 0 { var fromPackages pkg.DefaultPackages
Info(joinTag, "Generating a joint parent image from final packages")
if len(p.Join) > 0 {
fromPackages = p.Join
Warning(joinTag, `
Attention! the 'join' keyword is going to be deprecated in Luet >=0.18.x.
Use 'requires_final_images: true' instead in the build.yaml file`)
} else if p.RequiresFinalImages {
Info(joinTag, "Generating a parent image from final packages")
fromPackages = p.Package.GetRequires()
} else { } else {
// No source image to resolve
return nil return nil
} }
// First compute a hash and check if image is available. if it is, then directly consume that // First compute a hash and check if image is available. if it is, then directly consume that
overallFp, err := cs.getSpecHash(p.Join, "join") overallFp, err := cs.getSpecHash(fromPackages, "join")
if err != nil { if err != nil {
return errors.Wrap(err, "could not generate image hash") return errors.Wrap(err, "could not generate image hash")
} }
Info(joinTag, "Searching existing image with hash ", overallFp) Info(joinTag, "Searching existing image with hash", overallFp)
image := cs.findImageHash(overallFp, p) image := cs.findImageHash(overallFp, p)
if image != "" { if image != "" {
@ -811,12 +820,12 @@ func (cs *LuetCompiler) resolveJoinImages(concurrency int, keepPermissions bool,
} }
defer os.RemoveAll(joinDir) // clean up defer os.RemoveAll(joinDir) // clean up
for _, p := range p.Join { //highly dependent on the order for _, p := range fromPackages {
Info(joinTag, ":arrow_right_hook:", p.HumanReadableString(), ":leaves:") Info(joinTag, ":arrow_right_hook:", p.HumanReadableString(), ":leaves:")
} }
current := 0 current := 0
for _, c := range p.Join { for _, c := range fromPackages {
current++ current++
if c != nil && c.Name != "" && c.Version != "" { if c != nil && c.Name != "" && c.Version != "" {
joinTag2 := fmt.Sprintf("%s %d/%d ⤑ :hammer: build %s", joinTag, current, len(p.Join), c.HumanReadableString()) joinTag2 := fmt.Sprintf("%s %d/%d ⤑ :hammer: build %s", joinTag, current, len(p.Join), c.HumanReadableString())
@ -924,7 +933,7 @@ func (cs *LuetCompiler) compile(concurrency int, keepPermissions bool, generateF
//Before multistage : join - same as multistage, but keep artifacts, join them, create a new one and generate a final image. //Before multistage : join - same as multistage, but keep artifacts, join them, create a new one and generate a final image.
// When the image is there, use it as a source here, in place of GetImage(). // When the image is there, use it as a source here, in place of GetImage().
if err := cs.resolveJoinImages(concurrency, keepPermissions, p); err != nil { if err := cs.resolveFinalImages(concurrency, keepPermissions, p); err != nil {
return nil, errors.Wrap(err, "while resolving join images") return nil, errors.Wrap(err, "while resolving join images")
} }
@ -1028,7 +1037,7 @@ func (cs *LuetCompiler) compile(concurrency int, keepPermissions bool, generateF
Assert: assertion, Assert: assertion,
}) })
if err := cs.resolveJoinImages(concurrency, keepPermissions, compileSpec); err != nil { if err := cs.resolveFinalImages(concurrency, keepPermissions, compileSpec); err != nil {
return nil, errors.Wrap(err, "while resolving join images") return nil, errors.Wrap(err, "while resolving join images")
} }

View File

@ -115,39 +115,42 @@ type LuetCompilationSpec struct {
Copy []CopyField `json:"copy"` Copy []CopyField `json:"copy"`
Join pkg.DefaultPackages `json:"join"` Join pkg.DefaultPackages `json:"join"`
RequiresFinalImages bool `json:"requires_final_images" yaml:"requires_final_images"`
} }
// Signature is a portion of the spec that yields a signature for the hash // Signature is a portion of the spec that yields a signature for the hash
type Signature struct { type Signature struct {
Image string Image string
Steps []string Steps []string
PackageDir string PackageDir string
Prelude []string Prelude []string
Seed string Seed string
Env []string Env []string
Retrieve []string Retrieve []string
Unpack bool Unpack bool
Includes []string Includes []string
Excludes []string Excludes []string
Copy []CopyField Copy []CopyField
Join pkg.DefaultPackages Join pkg.DefaultPackages
RequiresFinalImages bool
} }
func (cs *LuetCompilationSpec) signature() Signature { func (cs *LuetCompilationSpec) signature() Signature {
return Signature{ return Signature{
Image: cs.Image, Image: cs.Image,
Steps: cs.Steps, Steps: cs.Steps,
PackageDir: cs.PackageDir, PackageDir: cs.PackageDir,
Prelude: cs.Prelude, Prelude: cs.Prelude,
Seed: cs.Seed, Seed: cs.Seed,
Env: cs.Env, Env: cs.Env,
Retrieve: cs.Retrieve, Retrieve: cs.Retrieve,
Unpack: cs.Unpack, Unpack: cs.Unpack,
Includes: cs.Includes, Includes: cs.Includes,
Excludes: cs.Excludes, Excludes: cs.Excludes,
Copy: cs.Copy, Copy: cs.Copy,
Join: cs.Join, Join: cs.Join,
RequiresFinalImages: cs.RequiresFinalImages,
} }
} }