Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field renames per spec review #1170

Merged
merged 3 commits into from
Aug 3, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
os = "linux"
arch = "amd64"
arch-variant = "some-variant"
[run-image.target.distribution]
[run-image.target.distro]
name = "ubuntu"
version = "some-cute-version"

14 changes: 7 additions & 7 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,14 @@ func testAnalyzer(platformAPI string) func(t *testing.T, when spec.G, it spec.S)

h.AssertEq(t, md.RunImage.Reference, "s0m3D1g3sT")
})
it("populates Target metadata from the run image", func() {
h.AssertNil(t, image.SetLabel("io.buildpacks.id", "id software"))
it("populates target metadata from the run image", func() {
h.AssertNil(t, image.SetLabel("io.buildpacks.base.id", "id software"))
h.AssertNil(t, image.SetOS("windows"))
h.AssertNil(t, image.SetOSVersion("95"))
h.AssertNil(t, image.SetArchitecture("Pentium"))
h.AssertNil(t, image.SetVariant("MMX"))
h.AssertNil(t, image.SetLabel("io.buildpacks.distribution.name", "moobuntu"))
h.AssertNil(t, image.SetLabel("io.buildpacks.distribution.version", "Helpful Holstein"))
h.AssertNil(t, image.SetLabel("io.buildpacks.distro.name", "moobuntu"))
h.AssertNil(t, image.SetLabel("io.buildpacks.distro.version", "Helpful Holstein"))

md, err := analyzer.Analyze()
h.AssertNil(t, err)
Expand All @@ -685,9 +685,9 @@ func testAnalyzer(platformAPI string) func(t *testing.T, when spec.G, it spec.S)
h.AssertEq(t, md.RunImage.TargetMetadata.ArchVariant, "MMX")
h.AssertEq(t, md.RunImage.TargetMetadata.OS, "windows")
h.AssertEq(t, md.RunImage.TargetMetadata.ID, "id software")
h.AssertNotNil(t, md.RunImage.TargetMetadata.Distribution)
h.AssertEq(t, md.RunImage.TargetMetadata.Distribution.Name, "moobuntu")
h.AssertEq(t, md.RunImage.TargetMetadata.Distribution.Version, "Helpful Holstein")
h.AssertNotNil(t, md.RunImage.TargetMetadata.Distro)
h.AssertEq(t, md.RunImage.TargetMetadata.Distro.Name, "moobuntu")
h.AssertEq(t, md.RunImage.TargetMetadata.Distro.Version, "Helpful Holstein")
}
})
})
Expand Down
26 changes: 10 additions & 16 deletions buildpack/bp_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
package buildpack

import (
"fmt"
"os"
"path/filepath"

"github.com/BurntSushi/toml"

"github.com/buildpacks/lifecycle/internal/encoding"
)

type BpDescriptor struct {
Expand All @@ -25,29 +26,22 @@ type StackMetadata struct {
}

type TargetMetadata struct {
OS string `json:"os" toml:"os"`
Arch string `json:"arch" toml:"arch"`
ArchVariant string `json:"arch-variant,omitempty" toml:"arch-variant"`
Distributions []OSDistribution `json:"distributions,omitempty" toml:"distributions"`
OS string `json:"os" toml:"os"`
Arch string `json:"arch" toml:"arch"`
ArchVariant string `json:"arch-variant,omitempty" toml:"arch-variant"`
Distros []OSDistro `json:"distros,omitempty" toml:"distros"`
}

func (t *TargetMetadata) String() string {
s := fmt.Sprintf("OS: %s, Arch: %s, ArchVariant: %s", t.OS, t.Arch, t.ArchVariant)
if len(t.Distributions) > 0 {
s += fmt.Sprintf(", Distributions: %s", t.Distributions)
}
return s
return encoding.ToJSONMaybe(*t)
}

type OSDistribution struct {
// OSDistro is an OS distribution that a buildpack or extension can support.
type OSDistro struct {
Name string `json:"name" toml:"name"`
Version string `json:"version" toml:"version"`
}

func (d OSDistribution) String() string {
return fmt.Sprintf("Distribution: (Name: %s, Version: %s)", d.Name, d.Version)
}

type BpInfo struct {
BaseInfo
SBOM []string `toml:"sbom-formats,omitempty" json:"sbom-formats,omitempty"`
Expand Down Expand Up @@ -75,7 +69,7 @@ func ReadBpDescriptor(path string) (*BpDescriptor, error) {
if len(descriptor.Targets) == 0 {
for _, stack := range descriptor.Stacks {
if stack.ID == "io.buildpacks.stacks.bionic" {
descriptor.Targets = append(descriptor.Targets, TargetMetadata{OS: "linux", Arch: "amd64", Distributions: []OSDistribution{{Name: "ubuntu", Version: "18.04"}}})
descriptor.Targets = append(descriptor.Targets, TargetMetadata{OS: "linux", Arch: "amd64", Distros: []OSDistro{{Name: "ubuntu", Version: "18.04"}}})
} else if stack.ID == "*" {
descriptor.Targets = append(descriptor.Targets, TargetMetadata{}) // matches any
}
Expand Down
20 changes: 10 additions & 10 deletions buildpack/bp_descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func testBpDescriptor(t *testing.T, when spec.G, it spec.S) {
OS: "some-os",
Arch: "some-arch",
ArchVariant: "some-arch-variant",
Distributions: []buildpack.OSDistribution{
Distros: []buildpack.OSDistro{
{
Name: "some-os-dist",
Version: "some-os-dist-version",
},
},
}
h.AssertEq(t, tm.String(), "OS: some-os, Arch: some-arch, ArchVariant: some-arch-variant, Distributions: [Distribution: (Name: some-os-dist, Version: some-os-dist-version)]")
h.AssertEq(t, tm.String(), `{"os":"some-os","arch":"some-arch","arch-variant":"some-arch-variant","distros":[{"name":"some-os-dist","version":"some-os-dist-version"}]}`)
})
})

Expand All @@ -42,7 +42,7 @@ func testBpDescriptor(t *testing.T, when spec.G, it spec.S) {
Arch: "some-arch",
ArchVariant: "some-arch-variant",
}
h.AssertEq(t, tm.String(), "OS: some-os, Arch: some-arch, ArchVariant: some-arch-variant")
h.AssertEq(t, tm.String(), `{"os":"some-os","arch":"some-arch","arch-variant":"some-arch-variant"}`)
})
})
})
Expand Down Expand Up @@ -77,8 +77,8 @@ func testBpDescriptor(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, len(descriptor.Targets), 1)
h.AssertEq(t, descriptor.Targets[0].Arch, "IA64")
h.AssertEq(t, descriptor.Targets[0].OS, "OpenVMS")
h.AssertEq(t, descriptor.Targets[0].Distributions[0].Name, "VSI OpenVMS")
h.AssertEq(t, descriptor.Targets[0].Distributions[0].Version, "V8.4-2L3")
h.AssertEq(t, descriptor.Targets[0].Distros[0].Name, "VSI OpenVMS")
h.AssertEq(t, descriptor.Targets[0].Distros[0].Version, "V8.4-2L3")
})

it("does translate one special stack value into target values for older apis", func() {
Expand All @@ -97,8 +97,8 @@ func testBpDescriptor(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, len(descriptor.Targets), 1)
h.AssertEq(t, descriptor.Targets[0].Arch, "amd64")
h.AssertEq(t, descriptor.Targets[0].OS, "linux")
h.AssertEq(t, descriptor.Targets[0].Distributions[0].Name, "ubuntu")
h.AssertEq(t, descriptor.Targets[0].Distributions[0].Version, "18.04")
h.AssertEq(t, descriptor.Targets[0].Distros[0].Name, "ubuntu")
h.AssertEq(t, descriptor.Targets[0].Distros[0].Version, "18.04")
})

it("translates one special stack value into target values", func() {
Expand All @@ -117,8 +117,8 @@ func testBpDescriptor(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, len(descriptor.Targets), 1)
h.AssertEq(t, descriptor.Targets[0].Arch, "amd64")
h.AssertEq(t, descriptor.Targets[0].OS, "linux")
h.AssertEq(t, descriptor.Targets[0].Distributions[0].Name, "ubuntu")
h.AssertEq(t, descriptor.Targets[0].Distributions[0].Version, "18.04")
h.AssertEq(t, descriptor.Targets[0].Distros[0].Name, "ubuntu")
h.AssertEq(t, descriptor.Targets[0].Distros[0].Version, "18.04")
})

it("does not translate non-special stack values", func() {
Expand Down Expand Up @@ -154,7 +154,7 @@ func testBpDescriptor(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, len(descriptor.Targets), 1)
h.AssertEq(t, descriptor.Targets[0].Arch, "")
h.AssertEq(t, descriptor.Targets[0].OS, "linux")
h.AssertEq(t, len(descriptor.Targets[0].Distributions), 0)
h.AssertEq(t, len(descriptor.Targets[0].Distros), 0)
})

it("detects windows/* if batch files are present and ignores linux", func() {
Expand Down
2 changes: 1 addition & 1 deletion buildpack/testdata/buildpack/by-id/D/v1/buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ sbom-formats = ["application/vnd.cyclonedx+json"]
[[targets]]
os = "OpenVMS"
arch = "IA64"
[[targets.distributions]]
[[targets.distros]]
name = "VSI OpenVMS"
version = "V8.4-2L3"
38 changes: 19 additions & 19 deletions detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
it("totally works if the constraints are met", func() {
detector.AnalyzeMD.RunImage = &files.RunImage{
TargetMetadata: &files.TargetMetadata{
OS: "MacOS",
Arch: "ARM64",
Distribution: &files.OSDistribution{Name: "MacOS", Version: "snow cheetah"},
OS: "MacOS",
Arch: "ARM64",
Distro: &files.OSDistro{Name: "MacOS", Version: "snow cheetah"},
},
}

Expand All @@ -837,9 +837,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
Buildpack: buildpack.BpInfo{BaseInfo: buildpack.BaseInfo{ID: "A", Version: "v1"}},
Targets: []buildpack.TargetMetadata{
{Arch: "P6", ArchVariant: "Pentium Pro", OS: "Win95",
Distributions: []buildpack.OSDistribution{
Distros: []buildpack.OSDistro{
{Name: "Windows 95", Version: "OSR1"}, {Name: "Windows 95", Version: "OSR2.5"}}},
{Arch: "ARM64", OS: "MacOS", Distributions: []buildpack.OSDistribution{{Name: "MacOS", Version: "snow cheetah"}}}},
{Arch: "ARM64", OS: "MacOS", Distros: []buildpack.OSDistro{{Name: "MacOS", Version: "snow cheetah"}}}},
}
dirStore.EXPECT().LookupBp("A", "v1").Return(bpA1, nil).AnyTimes()
executor.EXPECT().Detect(bpA1, gomock.Any(), gomock.Any())
Expand All @@ -862,9 +862,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
it("was born to be wildcard compliant", func() {
detector.AnalyzeMD.RunImage = &files.RunImage{
TargetMetadata: &files.TargetMetadata{
OS: "MacOS",
Arch: "ARM64",
Distribution: &files.OSDistribution{Name: "MacOS", Version: "snow cheetah"},
OS: "MacOS",
Arch: "ARM64",
Distro: &files.OSDistro{Name: "MacOS", Version: "snow cheetah"},
},
}

Expand Down Expand Up @@ -896,9 +896,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
it("totally works if the constraints are met", func() {
detector.AnalyzeMD.RunImage = &files.RunImage{
TargetMetadata: &files.TargetMetadata{
OS: "MacOS",
Arch: "ARM64",
Distribution: &files.OSDistribution{Name: "MacOS", Version: "snow cheetah"},
OS: "MacOS",
Arch: "ARM64",
Distro: &files.OSDistro{Name: "MacOS", Version: "snow cheetah"},
},
}

Expand All @@ -917,9 +917,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
Buildpack: buildpack.BpInfo{BaseInfo: buildpack.BaseInfo{ID: "A", Version: "v1"}},
Targets: []buildpack.TargetMetadata{
{Arch: "P6", ArchVariant: "Pentium Pro", OS: "Win95",
Distributions: []buildpack.OSDistribution{
Distros: []buildpack.OSDistro{
{Name: "Windows 95", Version: "OSR1"}, {Name: "Windows 95", Version: "OSR2.5"}}},
{Arch: "ARM64", OS: "MacOS", Distributions: []buildpack.OSDistribution{{Name: "MacOS", Version: "snow cheetah"}}}},
{Arch: "ARM64", OS: "MacOS", Distros: []buildpack.OSDistro{{Name: "MacOS", Version: "snow cheetah"}}}},
}
dirStore.EXPECT().LookupBp("A", "v1").Return(bpA1, nil).AnyTimes()

Expand All @@ -946,9 +946,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
it("errors if the buildpacks don't share that target arch/os", func() {
detector.AnalyzeMD.RunImage = &files.RunImage{
TargetMetadata: &files.TargetMetadata{
OS: "MacOS",
Arch: "ARM64",
Distribution: &files.OSDistribution{Name: "MacOS", Version: "some kind of big cat"},
OS: "MacOS",
Arch: "ARM64",
Distro: &files.OSDistro{Name: "MacOS", Version: "some kind of big cat"},
},
}

Expand All @@ -957,10 +957,10 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
Buildpack: buildpack.BpInfo{BaseInfo: buildpack.BaseInfo{ID: "A", Version: "v1"}},
Targets: []buildpack.TargetMetadata{
{Arch: "P6", ArchVariant: "Pentium Pro", OS: "Win95",
Distributions: []buildpack.OSDistribution{
Distros: []buildpack.OSDistro{
{Name: "Windows 95", Version: "OSR1"}, {Name: "Windows 95", Version: "OSR2.5"}}},
{Arch: "Pentium M", OS: "Win98",
Distributions: []buildpack.OSDistribution{{Name: "Windows 2000", Version: "Server"}}},
Distros: []buildpack.OSDistro{{Name: "Windows 2000", Version: "Server"}}},
},
}
dirStore.EXPECT().LookupBp("A", "v1").Return(bpA1, nil).AnyTimes()
Expand All @@ -972,7 +972,7 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, ok, true)
outs := val.(buildpack.DetectOutputs)
h.AssertEq(t, outs.Code, -1)
h.AssertStringContains(t, outs.Err.Error(), `unable to satisfy target os/arch constraints; run image: {"os":"MacOS","arch":"ARM64","distribution":{"name":"MacOS","version":"some kind of big cat"}}, buildpack: [{"os":"Win95","arch":"P6","arch-variant":"Pentium Pro","distributions":[{"name":"Windows 95","version":"OSR1"},{"name":"Windows 95","version":"OSR2.5"}]},{"os":"Win98","arch":"Pentium M","distributions":[{"name":"Windows 2000","version":"Server"}]}]`)
h.AssertStringContains(t, outs.Err.Error(), `unable to satisfy target os/arch constraints; run image: {"os":"MacOS","arch":"ARM64","distro":{"name":"MacOS","version":"some kind of big cat"}}, buildpack: [{"os":"Win95","arch":"P6","arch-variant":"Pentium Pro","distros":[{"name":"Windows 95","version":"OSR1"},{"name":"Windows 95","version":"OSR2.5"}]},{"os":"Win98","arch":"Pentium M","distros":[{"name":"Windows 2000","version":"Server"}]}]`)
return []buildpack.GroupElement{}, []files.BuildPlanEntry{}, nil
})

Expand Down
12 changes: 5 additions & 7 deletions platform/files/analyzed.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package files

import (
"fmt"
"os"

"github.com/BurntSushi/toml"

"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/log"
)

Expand Down Expand Up @@ -157,17 +157,15 @@ type TargetMetadata struct {
Arch string `json:"arch" toml:"arch"`
ArchVariant string `json:"arch-variant,omitempty" toml:"arch-variant,omitempty"`

Distribution *OSDistribution `json:"distribution,omitempty" toml:"distribution,omitempty"`
Distro *OSDistro `json:"distro,omitempty" toml:"distro,omitempty"`
}

func (t *TargetMetadata) String() string {
if t.Distribution != nil {
return fmt.Sprintf("OS: %s, Arch: %s, ArchVariant: %s, Distribution: (Name: %s, Version: %s)", t.OS, t.Arch, t.ArchVariant, t.Distribution.Name, t.Distribution.Version)
}
return fmt.Sprintf("OS: %s, Arch: %s, ArchVariant: %s", t.OS, t.Arch, t.ArchVariant)
return encoding.ToJSONMaybe(*t)
}

type OSDistribution struct {
// OSDistro is the OS distribution that a base image provides.
type OSDistro struct {
Name string `json:"name" toml:"name"`
Version string `json:"version" toml:"version"`
}
9 changes: 6 additions & 3 deletions platform/run_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import (
)

const (
TargetLabel = "io.buildpacks.id"
OSDistributionNameLabel = "io.buildpacks.distribution.name"
OSDistributionVersionLabel = "io.buildpacks.distribution.version"
// TargetLabel is the label containing the target ID.
TargetLabel = "io.buildpacks.base.id"
// OSDistroNameLabel is the label containing the OS distribution name.
OSDistroNameLabel = "io.buildpacks.distro.name"
// OSDistroVersionLabel is the label containing the OS distribution version.
OSDistroVersionLabel = "io.buildpacks.distro.version"
)

func BestRunImageMirrorFor(targetRegistry string, runImageMD files.RunImageForExport, checkReadAccess CheckReadAccess) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions platform/run_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func testRunImage(t *testing.T, when spec.G, it spec.S) {

when(".EnvVarsFor", func() {
it("returns the right thing", func() {
tm := files.TargetMetadata{Arch: "pentium", ArchVariant: "mmx", ID: "my-id", OS: "linux", Distribution: &files.OSDistribution{Name: "nix", Version: "22.11"}}
tm := files.TargetMetadata{Arch: "pentium", ArchVariant: "mmx", ID: "my-id", OS: "linux", Distro: &files.OSDistro{Name: "nix", Version: "22.11"}}
observed := platform.EnvVarsFor(tm)
h.AssertContains(t, observed, "CNB_TARGET_ARCH="+tm.Arch)
h.AssertContains(t, observed, "CNB_TARGET_ARCH_VARIANT="+tm.ArchVariant)
h.AssertContains(t, observed, "CNB_TARGET_DISTRO_NAME="+tm.Distribution.Name)
h.AssertContains(t, observed, "CNB_TARGET_DISTRO_VERSION="+tm.Distribution.Version)
h.AssertContains(t, observed, "CNB_TARGET_DISTRO_NAME="+tm.Distro.Name)
h.AssertContains(t, observed, "CNB_TARGET_DISTRO_VERSION="+tm.Distro.Version)
h.AssertContains(t, observed, "CNB_TARGET_OS="+tm.OS)
h.AssertEq(t, len(observed), 5)
})
Expand Down
Loading