Skip to content

Commit

Permalink
Enable package-buildpack for windows
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Emengo <[email protected]>
Signed-off-by: Andrew Meyer <[email protected]>
Signed-off-by: Victoria Henry <[email protected]>
  • Loading branch information
Anthony Emengo authored and TisVictress committed Sep 17, 2020
1 parent 6ebfc96 commit b25523f
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 126 deletions.
12 changes: 0 additions & 12 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,12 +1089,6 @@ func testAcceptance(
var packageImageName string

it.Before(func() {
h.SkipIf(t,
!pack.Supports("package-buildpack"),
"--buildpack does not accept buildpackage unless package-buildpack is supported",
)
h.SkipIf(t, dockerHostOS() == "windows", "These tests are not yet compatible with Windows-based containers")

packageImageName = packageBuildpackAsImage(t,
assert,
pack,
Expand Down Expand Up @@ -1134,12 +1128,6 @@ func testAcceptance(
)

it.Before(func() {
h.SkipIf(t,
!pack.Supports("package-buildpack --format"),
"--buildpack does not accept buildpackage file unless package-buildpack with --format is supported",
)
h.SkipIf(t, dockerHostOS() == "windows", "These tests are not yet compatible with Windows-based containers")

var err error
tmpDir, err = ioutil.TempDir("", "package-file")
assert.Nil(err)
Expand Down
6 changes: 5 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,11 @@ func (c *Client) processBuildpacks(ctx context.Context, builderImage imgutil.Ima
return fetchedBPs, order, errors.Wrapf(err, "extracting buildpacks from %s", style.Symbol(bp))
}
} else {
layerWriterFactory, err := layer.NewWriterFactory(builderImage)
imageOS, err := builderImage.OS()
if err != nil {
return fetchedBPs, order, errors.Wrap(err, "getting image OS")
}
layerWriterFactory, err := layer.NewWriterFactory(imageOS)
if err != nil {
return fetchedBPs, order, errors.Wrapf(err, "get tar writer factory for image %s", style.Symbol(builderImage.Name()))
}
Expand Down
6 changes: 5 additions & 1 deletion create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ func (c *Client) addBuildpacksToBuilder(ctx context.Context, opts CreateBuilderO
return errors.Wrapf(err, "extracting buildpacks from %s", style.Symbol(b.ID))
}
} else {
layerWriterFactory, err := layer.NewWriterFactory(bldr.Image())
imageOS, err := bldr.Image().OS()
if err != nil {
return errors.Wrap(err, "getting image OS")
}
layerWriterFactory, err := layer.NewWriterFactory(imageOS)
if err != nil {
return errors.Wrapf(err, "get tar writer factory for image %s", style.Symbol(bldr.Name()))
}
Expand Down
13 changes: 9 additions & 4 deletions create_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"runtime"
"testing"

"github.com/buildpacks/pack/config"

"github.com/buildpacks/imgutil/fakes"
"github.com/buildpacks/lifecycle/api"
"github.com/docker/docker/api/types"
"github.com/golang/mock/gomock"
"github.com/heroku/color"
"github.com/pkg/errors"
Expand All @@ -23,6 +22,7 @@ import (
"github.com/buildpacks/pack"
pubbldr "github.com/buildpacks/pack/builder"
pubbldpkg "github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/blob"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/dist"
Expand All @@ -47,6 +47,7 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
mockDownloader *testmocks.MockDownloader
mockImageFactory *testmocks.MockImageFactory
mockImageFetcher *testmocks.MockImageFetcher
mockDockerClient *testmocks.MockCommonAPIClient
fakeBuildImage *fakes.Image
fakeRunImage *fakes.Image
fakeRunImageMirror *fakes.Image
Expand All @@ -63,6 +64,7 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
mockDownloader = testmocks.NewMockDownloader(mockController)
mockImageFetcher = testmocks.NewMockImageFetcher(mockController)
mockImageFactory = testmocks.NewMockImageFactory(mockController)
mockDockerClient = testmocks.NewMockCommonAPIClient(mockController)

fakeBuildImage = fakes.NewImage("some/build-image", "", nil)
h.AssertNil(t, fakeBuildImage.SetLabel("io.buildpacks.stack.id", "some.stack.id"))
Expand All @@ -87,9 +89,12 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
pack.WithDownloader(mockDownloader),
pack.WithImageFactory(mockImageFactory),
pack.WithFetcher(mockImageFetcher),
pack.WithDockerClient(mockDockerClient),
)
h.AssertNil(t, err)

mockDockerClient.EXPECT().Info(context.TODO()).Return(types.Info{OSType: "linux"}, nil).AnyTimes()

opts = pack.CreateBuilderOptions{
BuilderName: "some/builder",
Config: pubbldr.Config{
Expand Down Expand Up @@ -562,9 +567,9 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
bldr := successfullyCreateBuilder()

h.AssertEq(t, bldr.LifecycleDescriptor().Info.Version.String(), "0.0.0")
//nolint:staticcheck
// nolint:staticcheck
h.AssertEq(t, bldr.LifecycleDescriptor().API.BuildpackVersion.String(), "0.2")
//nolint:staticcheck
// nolint:staticcheck
h.AssertEq(t, bldr.LifecycleDescriptor().API.PlatformVersion.String(), "0.2")
h.AssertEq(t, bldr.LifecycleDescriptor().APIs.Buildpack.Deprecated.AsStrings(), []string{})
h.AssertEq(t, bldr.LifecycleDescriptor().APIs.Buildpack.Supported.AsStrings(), []string{"0.2", "0.3", "0.4"})
Expand Down
6 changes: 5 additions & 1 deletion internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ func New(baseImage imgutil.Image, name string) (*Builder, error) {
}

func constructBuilder(img imgutil.Image, newName string, metadata Metadata) (*Builder, error) {
layerWriterFactory, err := layer.NewWriterFactory(img)
imageOS, err := img.OS()
if err != nil {
return nil, errors.Wrap(err, "getting image OS")
}
layerWriterFactory, err := layer.NewWriterFactory(imageOS)
if err != nil {
return nil, err
}
Expand Down
34 changes: 29 additions & 5 deletions internal/buildpackage/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package buildpackage
import (
"archive/tar"
"compress/gzip"
"context"
"io/ioutil"
"os"

Expand All @@ -14,16 +15,23 @@ import (
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/pkg/errors"

pubcfg "github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/archive"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/stack"
"github.com/buildpacks/pack/internal/style"
)

const windowsPackageBase = "mcr.microsoft.com/windows/nanoserver:1809-amd64" // TODO: Should this be hard-coded?

type ImageFactory interface {
NewImage(repoName string, local bool) (imgutil.Image, error)
}

type ImageFetcher interface {
Fetch(ctx context.Context, name string, daemon bool, pullPolicy pubcfg.PullPolicy) (imgutil.Image, error)
}

type WorkableImage interface {
SetLabel(string, string) error
AddLayerWithDiffID(path, diffID string) error
Expand Down Expand Up @@ -62,12 +70,16 @@ func (i *layoutImage) AddLayerWithDiffID(path, _ string) error {
type PackageBuilder struct {
buildpack dist.Buildpack
dependencies []dist.Buildpack
imageOS string
imageFactory ImageFactory
imageFetcher ImageFetcher
}

func NewBuilder(imageFactory ImageFactory) *PackageBuilder {
func NewBuilder(imageOS string, imageFactory ImageFactory, imageFetcher ImageFetcher) *PackageBuilder {
return &PackageBuilder{
imageOS: imageOS,
imageFactory: imageFactory,
imageFetcher: imageFetcher,
}
}

Expand Down Expand Up @@ -192,14 +204,26 @@ func (b *PackageBuilder) SaveAsFile(path string) error {
return archive.WriteDirToTar(tw, layoutDir, "/", 0, 0, 0755, true, nil)
}

func (b *PackageBuilder) SaveAsImage(repoName string, publish bool) (imgutil.Image, error) {
func (b *PackageBuilder) SaveAsImage(ctx context.Context, repoName string, publish bool, pullPolicy pubcfg.PullPolicy) (imgutil.Image, error) {
if err := b.validate(); err != nil {
return nil, err
}

image, err := b.imageFactory.NewImage(repoName, !publish)
if err != nil {
return nil, errors.Wrapf(err, "creating image")
var (
image imgutil.Image
err error
)
if b.imageOS == "windows" {
image, err = b.imageFetcher.Fetch(ctx, windowsPackageBase, !publish, pullPolicy)
if err != nil {
return nil, errors.Wrapf(err, "fetching base image")
}
image.Rename(repoName)
} else {
image, err = b.imageFactory.NewImage(repoName, !publish)
if err != nil {
return nil, errors.Wrapf(err, "creating image")
}
}

tmpDir, err := ioutil.TempDir("", "package-buildpack")
Expand Down
Loading

0 comments on commit b25523f

Please sign in to comment.