Skip to content

Commit

Permalink
Auto detect native image
Browse files Browse the repository at this point in the history
* understand the `native-processed` PlanEntry provided by an upstream buildpack and treats it as an activation sign
  • Loading branch information
anthonydahanne committed May 26, 2023
1 parent eca0be1 commit 31eb823
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Most users should not use this component buildpack directly and should instead u
This buildpack will participate if one the following conditions are met:

* `$BP_NATIVE_IMAGE` is set.
* An upstream buildpack requests `native-image-application` in the build plan.
* An upstream buildpack requests `native-image-application` in the build plan.
* An upstream buildpack provides `native-processed` in the build plan.

The buildpack will do the following:

Expand Down
33 changes: 30 additions & 3 deletions native/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
BinaryCompressionMethod = "BP_BINARY_COMPRESSION_METHOD"

PlanEntryNativeImage = "native-image-application"
PlanEntryNativeProcessed = "native-processed"
PlanEntryNativeImageBuilder = "native-image-builder"
PlanEntryJVMApplication = "jvm-application"
PlanEntrySpringBoot = "spring-boot"
Expand Down Expand Up @@ -69,6 +70,24 @@ func (d Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{
Name: PlanEntryNativeImage,
},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: PlanEntryNativeImageBuilder,
},
{
Name: PlanEntryNativeProcessed,
},
{
Name: PlanEntryNativeImage,
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{
Expand All @@ -93,9 +112,17 @@ func (d Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error
return libcnb.DetectResult{}, err
} else if ok {
for i := range result.Plans {
result.Plans[i].Requires = append(result.Plans[i].Requires, libcnb.BuildPlanRequire{
Name: PlanEntryNativeImage,
})
found := false
for _, r := range result.Plans[i].Requires {
if r.Name == PlanEntryNativeImage {
found = true
}
}
if !found {
result.Plans[i].Requires = append(result.Plans[i].Requires, libcnb.BuildPlanRequire{
Name: PlanEntryNativeImage,
})
}
}
}

Expand Down
131 changes: 131 additions & 0 deletions native/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: "native-image-builder",
},
{
Name: "native-processed",
},
{
Name: "native-image-application",
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
Expand Down Expand Up @@ -112,6 +128,22 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: "native-image-builder",
},
{
Name: "native-processed",
},
{
Name: "native-image-application",
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
Expand Down Expand Up @@ -165,6 +197,22 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: "native-image-builder",
},
{
Name: "native-processed",
},
{
Name: "native-image-application",
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
Expand Down Expand Up @@ -246,6 +294,25 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: "native-image-builder",
},
{
Name: "native-processed",
},
{
Name: "native-image-application",
},
{
Name: "upx",
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
Expand Down Expand Up @@ -305,6 +372,22 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: "native-image-builder",
},
{
Name: "native-processed",
},
{
Name: "native-image-application",
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
Expand Down Expand Up @@ -361,6 +444,22 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: "native-image-builder",
},
{
Name: "native-processed",
},
{
Name: "native-image-application",
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
Expand Down Expand Up @@ -417,6 +516,22 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: "native-image-builder",
},
{
Name: "native-processed",
},
{
Name: "native-image-application",
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
Expand Down Expand Up @@ -474,6 +589,22 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
},
Requires: []libcnb.BuildPlanRequire{
{
Name: "native-image-builder",
},
{
Name: "native-processed",
},
{
Name: "native-image-application",
},
},
},
{
Provides: []libcnb.BuildPlanProvide{
{Name: "native-image-application"},
Expand Down

0 comments on commit 31eb823

Please sign in to comment.