From 4a79aebb10957f23c7fc82378b4498686bb6fe73 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Fri, 31 Jan 2025 19:49:02 +0400 Subject: [PATCH] feat: make variant in `pkg.yaml` explicit Fixes #183 Note: this is a breaking change. Also highlight alpine variant in the graph. Signed-off-by: Andrey Smirnov --- .../testdata/arch-amd64/final/pkg.yaml | 1 + .../testdata/arch-arm64/final/pkg.yaml | 1 + .../testdata/cache/check-cache/pkg.yaml | 1 + .../testdata/cache/fill-cache/pkg.yaml | 1 + .../testdata/platform-override/final/pkg.yaml | 1 + .../testdata/platform-override/onlyarm/pkg.yaml | 1 + .../pkg/integration/testdata/simple/go/pkg.yaml | 1 + .../integration/testdata/stages/final/pkg.yaml | 1 + .../integration/testdata/stages/stage-a/pkg.yaml | 1 + .../integration/testdata/stages/stage-b/pkg.yaml | 1 + .../testdata/stages/stage-d/stage-c/pkg.yaml | 1 + .../testdata/stages/stage-d/stage-d/pkg.yaml | 1 + .../testdata/variables/final/pkg.yaml | 1 + .../testdata/variables/global-vars/pkg.yaml | 1 + .../testdata/variables/local-vars/pkg.yaml | 1 + .../testdata/variables/override/pkg.yaml | 1 + .../testdata/variables/std-vars/pkg.yaml | 1 + .../testdata/variables/vars-yaml/inner/pkg.yaml | 1 + .../testdata/variables/vars-yaml/pkg.yaml | 1 + internal/pkg/solver/graph.go | 16 +++++++++++++++- internal/pkg/types/v1alpha2/pkg.go | 5 ++++- internal/pkg/types/v1alpha2/variant.go | 6 ++++-- 22 files changed, 42 insertions(+), 4 deletions(-) diff --git a/internal/pkg/integration/testdata/arch-amd64/final/pkg.yaml b/internal/pkg/integration/testdata/arch-amd64/final/pkg.yaml index 9ac3629..e8d69e3 100644 --- a/internal/pkg/integration/testdata/arch-amd64/final/pkg.yaml +++ b/internal/pkg/integration/testdata/arch-amd64/final/pkg.yaml @@ -1,5 +1,6 @@ --- name: final +variant: alpine dependencies: # this arm64 variant of the image only contains the u-boot binary, # so this is a good test to ensure that we can copy the binary from diff --git a/internal/pkg/integration/testdata/arch-arm64/final/pkg.yaml b/internal/pkg/integration/testdata/arch-arm64/final/pkg.yaml index ef44865..bafcdd1 100644 --- a/internal/pkg/integration/testdata/arch-arm64/final/pkg.yaml +++ b/internal/pkg/integration/testdata/arch-arm64/final/pkg.yaml @@ -1,5 +1,6 @@ --- name: final +variant: alpine steps: - test: - test "${BUILD:-x}" = "aarch64-linux-musl" diff --git a/internal/pkg/integration/testdata/cache/check-cache/pkg.yaml b/internal/pkg/integration/testdata/cache/check-cache/pkg.yaml index 8bca23f..d66d6bf 100644 --- a/internal/pkg/integration/testdata/cache/check-cache/pkg.yaml +++ b/internal/pkg/integration/testdata/cache/check-cache/pkg.yaml @@ -1,4 +1,5 @@ name: check-cache +variant: alpine dependencies: - stage: fill-cache steps: diff --git a/internal/pkg/integration/testdata/cache/fill-cache/pkg.yaml b/internal/pkg/integration/testdata/cache/fill-cache/pkg.yaml index 7df82f7..e7558e8 100644 --- a/internal/pkg/integration/testdata/cache/fill-cache/pkg.yaml +++ b/internal/pkg/integration/testdata/cache/fill-cache/pkg.yaml @@ -1,4 +1,5 @@ name: fill-cache +variant: alpine steps: - cachePaths: - /.cache diff --git a/internal/pkg/integration/testdata/platform-override/final/pkg.yaml b/internal/pkg/integration/testdata/platform-override/final/pkg.yaml index 5db1475..c00f657 100644 --- a/internal/pkg/integration/testdata/platform-override/final/pkg.yaml +++ b/internal/pkg/integration/testdata/platform-override/final/pkg.yaml @@ -1,5 +1,6 @@ --- name: final +variant: scratch dependencies: - stage: onlyarm platform: linux/arm64 diff --git a/internal/pkg/integration/testdata/platform-override/onlyarm/pkg.yaml b/internal/pkg/integration/testdata/platform-override/onlyarm/pkg.yaml index 17e9898..777de15 100644 --- a/internal/pkg/integration/testdata/platform-override/onlyarm/pkg.yaml +++ b/internal/pkg/integration/testdata/platform-override/onlyarm/pkg.yaml @@ -1,5 +1,6 @@ --- name: onlyarm +variant: alpine steps: - test: - test "${BUILD:-x}" = "aarch64-linux-musl" diff --git a/internal/pkg/integration/testdata/simple/go/pkg.yaml b/internal/pkg/integration/testdata/simple/go/pkg.yaml index ba98dab..66867c3 100644 --- a/internal/pkg/integration/testdata/simple/go/pkg.yaml +++ b/internal/pkg/integration/testdata/simple/go/pkg.yaml @@ -1,4 +1,5 @@ name: go +variant: alpine steps: - sources: - url: https://dl.google.com/go/go1.12.5.src.tar.gz diff --git a/internal/pkg/integration/testdata/stages/final/pkg.yaml b/internal/pkg/integration/testdata/stages/final/pkg.yaml index 83a0c76..fd87552 100644 --- a/internal/pkg/integration/testdata/stages/final/pkg.yaml +++ b/internal/pkg/integration/testdata/stages/final/pkg.yaml @@ -1,4 +1,5 @@ name: final +variant: alpine dependencies: - stage: stage-a - stage: stage-b diff --git a/internal/pkg/integration/testdata/stages/stage-a/pkg.yaml b/internal/pkg/integration/testdata/stages/stage-a/pkg.yaml index 61b03da..f03721a 100644 --- a/internal/pkg/integration/testdata/stages/stage-a/pkg.yaml +++ b/internal/pkg/integration/testdata/stages/stage-a/pkg.yaml @@ -1,4 +1,5 @@ name: stage-a +variant: alpine steps: - prepare: - mkdir -p /root diff --git a/internal/pkg/integration/testdata/stages/stage-b/pkg.yaml b/internal/pkg/integration/testdata/stages/stage-b/pkg.yaml index 804c8dd..d4eb3d5 100644 --- a/internal/pkg/integration/testdata/stages/stage-b/pkg.yaml +++ b/internal/pkg/integration/testdata/stages/stage-b/pkg.yaml @@ -1,4 +1,5 @@ name: stage-b +variant: alpine steps: - prepare: - mkdir -p /root diff --git a/internal/pkg/integration/testdata/stages/stage-d/stage-c/pkg.yaml b/internal/pkg/integration/testdata/stages/stage-d/stage-c/pkg.yaml index 08aa005..dedf369 100644 --- a/internal/pkg/integration/testdata/stages/stage-d/stage-c/pkg.yaml +++ b/internal/pkg/integration/testdata/stages/stage-d/stage-c/pkg.yaml @@ -1,4 +1,5 @@ name: stage-c +variant: alpine steps: - prepare: - mkdir -p /root diff --git a/internal/pkg/integration/testdata/stages/stage-d/stage-d/pkg.yaml b/internal/pkg/integration/testdata/stages/stage-d/stage-d/pkg.yaml index a52dd0c..2905ef5 100644 --- a/internal/pkg/integration/testdata/stages/stage-d/stage-d/pkg.yaml +++ b/internal/pkg/integration/testdata/stages/stage-d/stage-d/pkg.yaml @@ -1,4 +1,5 @@ name: stage-d +variant: alpine dependencies: - stage: stage-c steps: diff --git a/internal/pkg/integration/testdata/variables/final/pkg.yaml b/internal/pkg/integration/testdata/variables/final/pkg.yaml index 47ce66f..d3becac 100644 --- a/internal/pkg/integration/testdata/variables/final/pkg.yaml +++ b/internal/pkg/integration/testdata/variables/final/pkg.yaml @@ -1,4 +1,5 @@ name: final +variant: alpine dependencies: - stage: global-vars - stage: std-vars diff --git a/internal/pkg/integration/testdata/variables/global-vars/pkg.yaml b/internal/pkg/integration/testdata/variables/global-vars/pkg.yaml index 6adb93b..7998061 100644 --- a/internal/pkg/integration/testdata/variables/global-vars/pkg.yaml +++ b/internal/pkg/integration/testdata/variables/global-vars/pkg.yaml @@ -1,4 +1,5 @@ name: global-vars +variant: alpine steps: - prepare: - mkdir -p /root diff --git a/internal/pkg/integration/testdata/variables/local-vars/pkg.yaml b/internal/pkg/integration/testdata/variables/local-vars/pkg.yaml index 7bb68cd..d6cef82 100644 --- a/internal/pkg/integration/testdata/variables/local-vars/pkg.yaml +++ b/internal/pkg/integration/testdata/variables/local-vars/pkg.yaml @@ -1,4 +1,5 @@ name: local-vars +variant: alpine steps: - prepare: - mkdir -p /root diff --git a/internal/pkg/integration/testdata/variables/override/pkg.yaml b/internal/pkg/integration/testdata/variables/override/pkg.yaml index f2b7389..cf477a7 100644 --- a/internal/pkg/integration/testdata/variables/override/pkg.yaml +++ b/internal/pkg/integration/testdata/variables/override/pkg.yaml @@ -1,4 +1,5 @@ name: override +variant: alpine steps: - prepare: - mkdir -p /root diff --git a/internal/pkg/integration/testdata/variables/std-vars/pkg.yaml b/internal/pkg/integration/testdata/variables/std-vars/pkg.yaml index bf9ea5d..322a128 100644 --- a/internal/pkg/integration/testdata/variables/std-vars/pkg.yaml +++ b/internal/pkg/integration/testdata/variables/std-vars/pkg.yaml @@ -1,4 +1,5 @@ name: std-vars +variant: alpine steps: - prepare: diff --git a/internal/pkg/integration/testdata/variables/vars-yaml/inner/pkg.yaml b/internal/pkg/integration/testdata/variables/vars-yaml/inner/pkg.yaml index 6abfb29..698f1de 100644 --- a/internal/pkg/integration/testdata/variables/vars-yaml/inner/pkg.yaml +++ b/internal/pkg/integration/testdata/variables/vars-yaml/inner/pkg.yaml @@ -1,4 +1,5 @@ name: vars-yaml-inner +variant: alpine steps: - prepare: diff --git a/internal/pkg/integration/testdata/variables/vars-yaml/pkg.yaml b/internal/pkg/integration/testdata/variables/vars-yaml/pkg.yaml index 9fe351f..9ef6f10 100644 --- a/internal/pkg/integration/testdata/variables/vars-yaml/pkg.yaml +++ b/internal/pkg/integration/testdata/variables/vars-yaml/pkg.yaml @@ -1,4 +1,5 @@ name: vars-yaml-0 +variant: alpine dependencies: - stage: vars-yaml-inner from: /result diff --git a/internal/pkg/solver/graph.go b/internal/pkg/solver/graph.go index 1feec6e..74efed1 100644 --- a/internal/pkg/solver/graph.go +++ b/internal/pkg/solver/graph.go @@ -6,6 +6,7 @@ package solver import ( "fmt" + "strings" "github.com/emicklei/dot" @@ -41,7 +42,11 @@ func (node *PackageNode) DumpDot(g *dot.Graph) dot.Node { if dep.IsInternal() { depNode = g.Node(dep.Stage) } else { - depNode = g.Node(dep.Image) + imageRef := dep.Image + // cut the digest + imageRef, _, _ = strings.Cut(imageRef, "@") + + depNode = g.Node(imageRef) depNode.Box() depNode.Attr("fillcolor", "lemonchiffon") depNode.Attr("style", "filled") @@ -55,6 +60,15 @@ func (node *PackageNode) DumpDot(g *dot.Graph) dot.Node { } } + if node.Pkg.Variant == v1alpha2.Alpine { + packageNode := g.Node("alpine") + packageNode.Box() + packageNode.Attr("fillcolor", "aquamarine") + packageNode.Attr("style", "filled") + + packageNode.Edge(n) + } + for _, dep := range node.Pkg.Install { packageNode := g.Node("Alpine: " + dep) packageNode.Box() diff --git a/internal/pkg/types/v1alpha2/pkg.go b/internal/pkg/types/v1alpha2/pkg.go index 5736131..a7189d0 100644 --- a/internal/pkg/types/v1alpha2/pkg.go +++ b/internal/pkg/types/v1alpha2/pkg.go @@ -37,7 +37,6 @@ func NewPkg(baseDir, fileName string, contents []byte, vars types.Variables) (*P BaseDir: baseDir, FileName: fileName, Shell: "/bin/sh", - Variant: Alpine, Context: vars.Copy(), } @@ -72,6 +71,10 @@ func (p *Pkg) Validate() error { multiErr = multierror.Append(multiErr, errors.New("package name can't be empty")) } + if p.Variant == Unset { + multiErr = multierror.Append(multiErr, errors.New("variant should be set")) + } + if len(p.Steps) > 0 && len(p.Finalize) == 0 { multiErr = multierror.Append(multiErr, errors.New("finalize steps are missing, this is going to lead to empty build")) } diff --git a/internal/pkg/types/v1alpha2/variant.go b/internal/pkg/types/v1alpha2/variant.go index 2e7e55c..1a2a201 100644 --- a/internal/pkg/types/v1alpha2/variant.go +++ b/internal/pkg/types/v1alpha2/variant.go @@ -12,14 +12,16 @@ import "fmt" type Variant int const ( + // Unset is a variant that is not set. + Unset Variant = iota // Alpine variant uses Alpine as base image for the build. - Alpine Variant = iota + Alpine // Scratch variant uses scratch image as base image for the build. Scratch ) func (v Variant) String() string { - return []string{"alpine", "scratch"}[v] + return []string{"unset", "alpine", "scratch"}[v] } // UnmarshalYAML implements yaml.Unmarshaller interface.