Skip to content

Commit

Permalink
Adapt extension related files
Browse files Browse the repository at this point in the history
Co-authored-by: Ralf Pannemans <[email protected]>
Signed-off-by: Nicolas Bender <[email protected]>
  • Loading branch information
nicolasbender and c0d1ngm0nk3y committed May 15, 2024
1 parent 1de7510 commit 0cc9251
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 9 deletions.
19 changes: 19 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ type BuildContext struct {

// Deprecated: StackID is the ID of the stack.
StackID string

// TargetInfo contains info of the target (os, arch, ...).
TargetInfo TargetInfo

// TargetDistro is the target distribution (name, version).
TargetDistro TargetDistro
}

// BuildResult contains the results of detection.
Expand Down Expand Up @@ -231,6 +237,19 @@ func Build(build BuildFunc, config Config) {
config.logger.Debugf("Stack: %s", ctx.StackID)
}

if API.GreaterThan(semver.MustParse("0.9")) {
ctx.TargetInfo = TargetInfo{}
ctx.TargetInfo.OS, _ = os.LookupEnv(EnvTargetOS)
ctx.TargetInfo.Arch, _ = os.LookupEnv(EnvTargetArch)
ctx.TargetInfo.Variant, _ = os.LookupEnv(EnvTargetArchVariant)
config.logger.Debugf("System: %+v", ctx.TargetInfo)

ctx.TargetDistro = TargetDistro{}
ctx.TargetDistro.Name, _ = os.LookupEnv(EnvTargetDistroName)
ctx.TargetDistro.Version, _ = os.LookupEnv(EnvTargetDistroVersion)
config.logger.Debugf("Distro: %+v", ctx.TargetDistro)
}

result, err := build(ctx)
if err != nil {
config.exitHandler.Error(err)
Expand Down
22 changes: 21 additions & 1 deletion build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ test-key = "test-value"
Expect(os.Setenv("CNB_PLATFORM_DIR", platformPath)).To(Succeed())
Expect(os.Setenv("CNB_BP_PLAN_PATH", buildpackPlanPath)).To(Succeed())

Expect(os.Setenv("CNB_TARGET_OS", "linux")).To(Succeed())
Expect(os.Setenv("CNB_TARGET_ARCH", "arm")).To(Succeed())
Expect(os.Setenv("CNB_TARGET_ARCH_VARIANT", "v6")).To(Succeed())
Expect(os.Setenv("CNB_TARGET_DISTRO_NAME", "ubuntu")).To(Succeed())
Expect(os.Setenv("CNB_TARGET_DISTRO_VERSION", "24.04")).To(Succeed())

workingDir, err = os.Getwd()
Expect(err).NotTo(HaveOccurred())
Expect(os.Chdir(applicationPath)).To(Succeed())
Expand All @@ -178,6 +184,12 @@ test-key = "test-value"
Expect(os.Unsetenv("CNB_BP_PLAN_PATH")).To(Succeed())
Expect(os.Unsetenv("CNB_LAYERS_DIR")).To(Succeed())

Expect(os.Unsetenv("CNB_TARGET_OS"))
Expect(os.Unsetenv("CNB_TARGET_ARCH"))
Expect(os.Unsetenv("CNB_TARGET_ARCH_VARIANT"))
Expect(os.Unsetenv("CNB_TARGET_DISTRO_NAME"))
Expect(os.Unsetenv("CNB_TARGET_DISTRO_VERSION"))

Expect(os.RemoveAll(applicationPath)).To(Succeed())
Expect(os.RemoveAll(buildpackPath)).To(Succeed())
Expect(os.RemoveAll(buildpackPlanPath)).To(Succeed())
Expand Down Expand Up @@ -360,7 +372,9 @@ version = "1.1.1"
it("provides target information", func() {
libcnb.Build(buildFunc,
libcnb.NewConfig(
libcnb.WithArguments([]string{commandPath})),
libcnb.WithArguments([]string{commandPath}),
libcnb.WithLogger(log.New(os.Stdout)),
),
)

Expect(ctx.Buildpack.Targets).To(HaveLen(2))
Expand All @@ -372,6 +386,12 @@ version = "1.1.1"
Expect(ctx.Buildpack.Targets[0].Distros[1].Name).To(Equal("debian"))

Expect(ctx.Buildpack.Targets[1].Variant).To(Equal("v6"))

Expect(ctx.TargetInfo.OS).To(Equal("linux"))
Expect(ctx.TargetInfo.Arch).To(Equal("arm"))
Expect(ctx.TargetInfo.Variant).To(Equal("v6"))
Expect(ctx.TargetDistro.Name).To(Equal("ubuntu"))
Expect(ctx.TargetDistro.Version).To(Equal("24.04"))
})
})

Expand Down
17 changes: 11 additions & 6 deletions buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ type BuildpackStack struct {
Mixins []string `toml:"mixins"`
}

// BuildpackTargetDistro is the supported target distro
type BuildpackTargetDistro struct {
// TargetDistro is the supported target distro
type TargetDistro struct {
// Name is the name of the supported distro.
Name string `toml:"name"`

// Version is the version of the supported distro.
Version string `toml:"version"`
}

// BuildpackTarget is a target supported by the buildpack.
type BuildpackTarget struct {
// TargetInfo is the supported target
type TargetInfo struct {
// OS is the supported os.
OS string `toml:"os"`

Expand All @@ -104,9 +104,14 @@ type BuildpackTarget struct {

// Variant is the supported variant of the architecture.
Variant string `toml:"variant"`
}

// Target is a target supported by the buildpack.
type Target struct {
TargetInfo

// Distros is the collection of distros associated with the target.
Distros []BuildpackTargetDistro `toml:"distros"`
Distros []TargetDistro `toml:"distros"`
}

// Buildpack is the contents of the buildpack.toml file.
Expand All @@ -124,7 +129,7 @@ type Buildpack struct {
Stacks []BuildpackStack `toml:"stacks"`

// Targets is the collection of targets supported by the buildpack.
Targets []BuildpackTarget `toml:"targets"`
Targets []Target `toml:"targets"`

// Metadata is arbitrary metadata attached to the buildpack.
Metadata map[string]interface{} `toml:"metadata"`
Expand Down
3 changes: 3 additions & 0 deletions extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Extension struct {
// Path is the path to the extension.
Path string `toml:"-"`

// Targets is the collection of targets supported by the buildpack.
Targets []Target `toml:"targets"`

// Metadata is arbitrary metadata attached to the extension.
Metadata map[string]interface{} `toml:"metadata"`
}
19 changes: 19 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ type GenerateContext struct {
// Platform is the contents of the platform.
Platform Platform

// TargetInfo contains info of the target (os, arch, ...).
TargetInfo TargetInfo

// TargetDistro is the target distribution (name, version).
TargetDistro TargetDistro

// Deprecated: StackID is the ID of the stack.
StackID string
}
Expand Down Expand Up @@ -184,6 +190,19 @@ func Generate(generate GenerateFunc, config Config) {
config.logger.Debugf("Stack: %s", ctx.StackID)
}

if API.GreaterThan(semver.MustParse("0.9")) {
ctx.TargetInfo = TargetInfo{}
ctx.TargetInfo.OS, _ = os.LookupEnv(EnvTargetOS)
ctx.TargetInfo.Arch, _ = os.LookupEnv(EnvTargetArch)
ctx.TargetInfo.Variant, _ = os.LookupEnv(EnvTargetArchVariant)
config.logger.Debugf("System: %+v", ctx.TargetInfo)

ctx.TargetDistro = TargetDistro{}
ctx.TargetDistro.Name, _ = os.LookupEnv(EnvTargetDistroName)
ctx.TargetDistro.Version, _ = os.LookupEnv(EnvTargetDistroVersion)
config.logger.Debugf("Distro: %+v", ctx.TargetDistro)
}

result, err := generate(ctx)
if err != nil {
config.exitHandler.Error(err)
Expand Down
79 changes: 77 additions & 2 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ api = "{{.APIVersion}}"
id = "test-id"
name = "test-name"
version = "1.1.1"
description = "A test buildpack"
keywords = ["test", "buildpack"]
description = "A test extension"
keywords = ["test", "extension"]
[[extension.licenses]]
type = "Apache-2.0"
Expand Down Expand Up @@ -147,6 +147,12 @@ test-key = "test-value"
Expect(os.Setenv("CNB_PLATFORM_DIR", platformPath)).To(Succeed())
Expect(os.Setenv("CNB_BP_PLAN_PATH", buildpackPlanPath)).To(Succeed())

Expect(os.Setenv("CNB_TARGET_OS", "linux")).To(Succeed())
Expect(os.Setenv("CNB_TARGET_ARCH", "arm")).To(Succeed())
Expect(os.Setenv("CNB_TARGET_ARCH_VARIANT", "v6")).To(Succeed())
Expect(os.Setenv("CNB_TARGET_DISTRO_NAME", "ubuntu")).To(Succeed())
Expect(os.Setenv("CNB_TARGET_DISTRO_VERSION", "24.04")).To(Succeed())

workingDir, err = os.Getwd()
Expect(err).NotTo(HaveOccurred())
Expect(os.Chdir(applicationPath)).To(Succeed())
Expand All @@ -160,6 +166,12 @@ test-key = "test-value"
Expect(os.Unsetenv("CNB_BP_PLAN_PATH")).To(Succeed())
Expect(os.Unsetenv("CNB_OUTPUT_DIR")).To(Succeed())

Expect(os.Unsetenv("CNB_TARGET_OS"))
Expect(os.Unsetenv("CNB_TARGET_ARCH"))
Expect(os.Unsetenv("CNB_TARGET_ARCH_VARIANT"))
Expect(os.Unsetenv("CNB_TARGET_DISTRO_NAME"))
Expect(os.Unsetenv("CNB_TARGET_DISTRO_VERSION"))

Expect(os.RemoveAll(applicationPath)).To(Succeed())
Expect(os.RemoveAll(extensionPath)).To(Succeed())
Expect(os.RemoveAll(buildpackPlanPath)).To(Succeed())
Expand Down Expand Up @@ -300,6 +312,69 @@ version = "1.1.1"
})
})

context("has a build environment specifying target metadata", func() {
var ctx libcnb.GenerateContext

it.Before(func() {
Expect(os.WriteFile(filepath.Join(extensionPath, "extension.toml"),
[]byte(`
api = "0.10"
[extension]
id = "test-id"
name = "test-name"
version = "1.1.1"
[[targets]]
os = "linux"
arch = "amd64"
[[targets.distros]]
name = "ubuntu"
version = "18.04"
[[targets.distros]]
name = "debian"
[[targets]]
os = "linux"
arch = "arm"
variant = "v6"
`), 0600),
).To(Succeed())

generateFunc = func(context libcnb.GenerateContext) (libcnb.GenerateResult, error) {
ctx = context
return libcnb.NewGenerateResult(), nil
}
})

it("provides target information", func() {
libcnb.Generate(generateFunc,
libcnb.NewConfig(
libcnb.WithArguments([]string{commandPath}),
libcnb.WithLogger(log.New(os.Stdout)),
),
)

Expect(ctx.Extension.Targets).To(HaveLen(2))
Expect(ctx.Extension.Targets[0].OS).To(Equal("linux"))
Expect(ctx.Extension.Targets[0].Arch).To(Equal("amd64"))
Expect(ctx.Extension.Targets[0].Distros).To(HaveLen(2))
Expect(ctx.Extension.Targets[0].Distros[0].Name).To(Equal("ubuntu"))
Expect(ctx.Extension.Targets[0].Distros[0].Version).To(Equal("18.04"))
Expect(ctx.Extension.Targets[0].Distros[1].Name).To(Equal("debian"))

Expect(ctx.Extension.Targets[1].Variant).To(Equal("v6"))

Expect(ctx.TargetInfo.OS).To(Equal("linux"))
Expect(ctx.TargetInfo.Arch).To(Equal("arm"))
Expect(ctx.TargetInfo.Variant).To(Equal("v6"))
Expect(ctx.TargetDistro.Name).To(Equal("ubuntu"))
Expect(ctx.TargetDistro.Version).To(Equal("24.04"))
})
})

it("fails if CNB_EXTENSION_DIR is not set", func() {
Expect(os.Unsetenv("CNB_EXTENSION_DIR")).To(Succeed())

Expand Down
15 changes: 15 additions & 0 deletions platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ const (
// Deprecated: EnvStackID is the name of the environment variable that contains the stack id
EnvStackID = "CNB_STACK_ID"

// EnvTargetOS contains the name of the os
EnvTargetOS = "CNB_TARGET_OS"

// EnvTargetArch contains the architecture
EnvTargetArch = "CNB_TARGET_ARCH"

// EnvTargetOS contains the variant of the architecture
EnvTargetArchVariant = "CNB_TARGET_ARCH_VARIANT"

// EnvTargetDistroName contains the name of the ditro
EnvTargetDistroName = "CNB_TARGET_DISTRO_NAME"

// EnvTargetDistroVersion contains the version of the distro
EnvTargetDistroVersion = "CNB_TARGET_DISTRO_VERSION"

// DefaultPlatformBindingsLocation is the typical location for bindings, which exists under the platform directory
//
// Not guaranteed to exist, but often does. This should only be used as a fallback if EnvServiceBindings and EnvPlatformDirectory are not set
Expand Down

0 comments on commit 0cc9251

Please sign in to comment.