-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add Buildkit Option #434
Add Buildkit Option #434
Changes from 4 commits
7601099
1c621c9
8390c85
0c94958
88f3cb9
b80f7b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
) | ||
|
||
const defaultDockerfile = "Dockerfile" | ||
const defaultBuilder = "2" | ||
|
||
type Image struct { | ||
Name string | ||
|
@@ -33,13 +34,14 @@ type Registry struct { | |
} | ||
|
||
type Build struct { | ||
Context string | ||
Dockerfile string | ||
CachedImages []string | ||
Env map[string]string | ||
Args map[string]*string | ||
ExtraOptions []string | ||
Target string | ||
Context string | ||
Dockerfile string | ||
CachedImages []string | ||
Env map[string]string | ||
Args map[string]*string | ||
ExtraOptions []string | ||
Target string | ||
BuilderVersion types.BuilderVersion | ||
} | ||
|
||
func (p *dockerNativeProvider) dockerBuild(ctx context.Context, | ||
|
@@ -59,7 +61,10 @@ func (p *dockerNativeProvider) dockerBuild(ctx context.Context, | |
Registry: reg, | ||
} | ||
|
||
build := marshalBuildAndApplyDefaults(inputs["build"]) | ||
build, err := marshalBuildAndApplyDefaults(inputs["build"]) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
cache := marshalCachedImages(img, inputs["build"]) | ||
|
||
build.CachedImages = cache | ||
|
@@ -91,7 +96,7 @@ func (p *dockerNativeProvider) dockerBuild(ctx context.Context, | |
Remove: true, | ||
//CacheFrom: img.Build.CachedImages, // TODO: this needs a login, so needs to be handled differently. | ||
BuildArgs: build.Args, | ||
//Version: types.BuilderBuildKit, // TODO: parse this setting from the `env` input | ||
Version: build.BuilderVersion, | ||
} | ||
|
||
imgBuildResp, err := docker.ImageBuild(ctx, tar, opts) | ||
|
@@ -199,21 +204,21 @@ func (p *dockerNativeProvider) dockerBuild(ctx context.Context, | |
return img.Name, pbstruct, err | ||
} | ||
|
||
func marshalBuildAndApplyDefaults(b resource.PropertyValue) Build { | ||
func marshalBuildAndApplyDefaults(b resource.PropertyValue) (Build, error) { | ||
|
||
// build can be nil, a string or an object; we will also use reasonable defaults here. | ||
var build Build | ||
if b.IsNull() { | ||
// use the default build context | ||
//// use the default build context | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo? |
||
build.Dockerfile = defaultDockerfile | ||
build.Context = "." | ||
return build | ||
return build, nil | ||
} | ||
if b.IsString() { | ||
// use the filepath as context | ||
build.Context = b.StringValue() | ||
build.Dockerfile = defaultDockerfile | ||
return build | ||
return build, nil | ||
} | ||
|
||
// read in the build type fields | ||
|
@@ -232,8 +237,16 @@ func marshalBuildAndApplyDefaults(b resource.PropertyValue) Build { | |
} else { | ||
build.Context = buildObject["context"].StringValue() | ||
} | ||
// Envs | ||
|
||
// BuildKit | ||
version, err := marshalBuilder(buildObject["builderVersion"]) | ||
|
||
if err != nil { | ||
return build, err | ||
} | ||
build.BuilderVersion = version | ||
|
||
// Envs | ||
build.Env = marshalEnvs(buildObject["env"]) | ||
|
||
// Args | ||
|
@@ -251,7 +264,7 @@ func marshalBuildAndApplyDefaults(b resource.PropertyValue) Build { | |
if !buildObject["target"].IsNull() { | ||
build.Target = buildObject["target"].StringValue() | ||
} | ||
return build | ||
return build, nil | ||
} | ||
|
||
func marshalCachedImages(img Image, b resource.PropertyValue) []string { | ||
|
@@ -328,3 +341,23 @@ func marshalEnvs(e resource.PropertyValue) map[string]string { | |
} | ||
return envs | ||
} | ||
|
||
func marshalBuilder(builder resource.PropertyValue) (types.BuilderVersion, error) { | ||
var version types.BuilderVersion | ||
|
||
if builder.IsNull() { | ||
//set default | ||
return defaultBuilder, nil | ||
} | ||
// verify valid input | ||
switch builder.StringValue() { | ||
case "BuilderV1": | ||
return "1", nil | ||
case "BuilderBuildKit": | ||
return "2", nil | ||
default: | ||
// because the Docker client will default to `BuilderV1` | ||
// when version isn't set, we return an error | ||
return version, errors.Errorf("Invalid Docker Builder version") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/docker/docker/api/types" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
@@ -53,7 +54,7 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
Dockerfile: "Dockerfile", | ||
} | ||
input := resource.NewObjectProperty(resource.PropertyMap{}) | ||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
|
@@ -63,7 +64,7 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
Dockerfile: "Dockerfile", | ||
} | ||
input := resource.NewStringProperty("/twilight/sparkle/bin") | ||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
|
@@ -75,7 +76,7 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"dockerfile": resource.NewStringProperty("TheLastUnicorn"), | ||
}) | ||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
|
@@ -89,7 +90,7 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
"context": resource.NewStringProperty("/twilight/sparkle/bin"), | ||
}) | ||
|
||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
|
@@ -109,7 +110,7 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
}), | ||
}) | ||
|
||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
|
@@ -129,7 +130,7 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
}), | ||
}) | ||
|
||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
|
@@ -148,7 +149,7 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
}), | ||
}) | ||
|
||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
|
@@ -162,7 +163,7 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
"extraOptions": resource.NewArrayProperty([]resource.PropertyValue{}), | ||
}) | ||
|
||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit more verbose but could use
|
||
assert.Equal(t, expected, actual) | ||
}) | ||
t.Run("Sets Target", func(t *testing.T) { | ||
|
@@ -176,7 +177,21 @@ func TestMarshalBuildAndApplyDefaults(t *testing.T) { | |
"target": resource.NewStringProperty("bullseye"), | ||
}) | ||
|
||
actual := marshalBuildAndApplyDefaults(input) | ||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
t.Run("Sets Target", func(t *testing.T) { | ||
expected := Build{ | ||
Context: ".", | ||
Dockerfile: "Dockerfile", | ||
Target: "bullseye", | ||
} | ||
|
||
input := resource.NewObjectProperty(resource.PropertyMap{ | ||
"target": resource.NewStringProperty("bullseye"), | ||
}) | ||
|
||
actual, _ := marshalBuildAndApplyDefaults(input) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
@@ -295,3 +310,37 @@ func TestMarshalCachedImages(t *testing.T) { | |
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func TestMarshalBuilder(t *testing.T) { | ||
t.Run("Test Builder Version Default", func(t *testing.T) { | ||
expected := types.BuilderBuildKit | ||
input := resource.NewPropertyValue(nil) | ||
actual, _ := marshalBuilder(input) | ||
assert.Equal(t, expected, actual) | ||
|
||
}) | ||
t.Run("Test Builder BuildKit Version", func(t *testing.T) { | ||
expected := types.BuilderBuildKit | ||
input := resource.NewStringProperty("BuilderBuildKit") | ||
|
||
actual, _ := marshalBuilder(input) | ||
assert.Equal(t, expected, actual) | ||
|
||
}) | ||
t.Run("Test Builder V1 Version", func(t *testing.T) { | ||
expected := types.BuilderV1 | ||
input := resource.NewStringProperty("BuilderV1") | ||
|
||
actual, _ := marshalBuilder(input) | ||
assert.Equal(t, expected, actual) | ||
|
||
}) | ||
t.Run("Test Invalid Builder Returns Error", func(t *testing.T) { | ||
expected := types.BuilderV1 | ||
input := resource.NewStringProperty("BuilderV1") | ||
|
||
actual, _ := marshalBuilder(input) | ||
assert.Equal(t, expected, actual) | ||
|
||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,6 +199,15 @@ func Provider() tfbridge.ProviderInfo { | |
Description: "The target of the Dockerfile to build", | ||
TypeSpec: schema.TypeSpec{Type: "string"}, | ||
}, | ||
"builderVersion": { | ||
Description: "The version of the Docker builder. " + | ||
"Valid inputs are: \n" + | ||
"`BuilderV1` - the first generation builder in docker daemon\n" + "" + | ||
"`BuilderBuildKit - the builder based on moby/buildkit project\n " + | ||
"Defaults to `BuilderBuildKit`.", | ||
TypeSpec: schema.TypeSpec{Type: "string"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above comment on perhaps representing this as an enum instead? |
||
Default: "BuilderBuildKit", | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -224,7 +233,7 @@ func Provider() tfbridge.ProviderInfo { | |
dockerResource(dockerMod, "Image").String(): { | ||
ObjectTypeSpec: schema.ObjectTypeSpec{ | ||
Type: "object", | ||
Description: "A real CRUD docker image we hope", // TODO: update description | ||
Description: "Builds a Docker Image and pushes to a Docker registry.", | ||
Properties: map[string]schema.PropertySpec{ | ||
"imageName": { | ||
Description: "The fully qualified image name", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make this an enum instead? https://www.pulumi.com/docs/guides/pulumi-packages/schema/#complextype