Skip to content

Commit

Permalink
Add Buildkit Option (#434)
Browse files Browse the repository at this point in the history
* add first buildkit types field

* Add logic for Builder Version on client. Add input marshaling tests

* Update Image description

* Regenerate schema and SDKs

* make BuilderVersion an enum. Add err check on tests. Fix up tests. Generate schema

* Add SDK
  • Loading branch information
guineveresaenger authored Dec 5, 2022
1 parent 8ebdf9c commit 7cfa185
Show file tree
Hide file tree
Showing 36 changed files with 626 additions and 45 deletions.
23 changes: 22 additions & 1 deletion provider/cmd/pulumi-resource-docker/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3016,6 +3016,22 @@
"value"
]
},
"docker:index/builderVersion:BuilderVersion": {
"description": "The version of the Docker builder",
"type": "string",
"enum": [
{
"name": "BuilderV1",
"description": "The first generation builder for Docker Daemon",
"value": "BuilderV1"
},
{
"name": "BuilderBuildKit",
"description": "The builder based on moby/buildkit project",
"value": "BuilderBuildKit"
}
]
},
"docker:index/cacheFrom:CacheFrom": {
"description": "Specifies information about where to obtain a cache",
"properties": {
Expand All @@ -3039,6 +3055,11 @@
},
"description": "An optional map of named build-time argument variables to set during the Docker build. This flag allows you to pass built-time variablesthat can be accessed like environment variables inside the RUN instruction."
},
"builderVersion": {
"$ref": "#/types/docker:index/builderVersion:BuilderVersion",
"description": "The version of the Docker builder. ",
"default": "BuilderBuildKit"
},
"cacheFrom": {
"oneOf": [
{
Expand Down Expand Up @@ -4419,7 +4440,7 @@
}
},
"docker:index/image:Image": {
"description": "A real CRUD docker image we hope",
"description": "Builds a Docker Image and pushes to a Docker registry.",
"properties": {
"baseImageName": {
"type": "string",
Expand Down
61 changes: 47 additions & 14 deletions provider/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

const defaultDockerfile = "Dockerfile"
const defaultBuilder = "2"

type Image struct {
Name string
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
}
}
Loading

0 comments on commit 7cfa185

Please sign in to comment.