Skip to content

Commit

Permalink
Add Cache From feature (#467)
Browse files Browse the repository at this point in the history
* re-schematize cacheFrom to properly reflect its use in the context of Docker

* Remove bool logic and update test to reflect new field name

* Enable cache-from

Adds support for specifying images to use as a pull cache from a remote
registry.
Additionally adds log output to support surfacing of errors when cache pull is not
possible.

* Generate SDKS
  • Loading branch information
guineveresaenger authored Jan 27, 2023
1 parent b929d2b commit 1bd4f68
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 136 deletions.
17 changes: 5 additions & 12 deletions provider/cmd/pulumi-resource-docker/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3033,14 +3033,14 @@
]
},
"docker:index/cacheFrom:CacheFrom": {
"description": "Specifies information about where to obtain a cache",
"description": "Contains a list of images to reference when building using a cache",
"properties": {
"stages": {
"images": {
"type": "array",
"items": {
"type": "string"
},
"description": "A list of cached build stages"
"description": "Specifies cached images"
}
},
"type": "object"
Expand All @@ -3061,15 +3061,8 @@
"default": "BuilderBuildKit"
},
"cacheFrom": {
"oneOf": [
{
"type": "boolean"
},
{
"$ref": "#/types/docker:index/cacheFrom:CacheFrom"
}
],
"description": "A cached image or list of build stages to use as build cache"
"$ref": "#/types/docker:index/cacheFrom:CacheFrom",
"description": "A list of images to use as build cache"
},
"context": {
"type": "string",
Expand Down
31 changes: 14 additions & 17 deletions provider/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func (p *dockerNativeProvider) dockerBuild(ctx context.Context,
Dockerfile: img.Build.Dockerfile,
Tags: []string{img.Name}, //this should build the image locally, sans registry info
Remove: true,
//CacheFrom: img.Build.CachedImages, // TODO: this needs a login, so needs to be handled differently.
BuildArgs: build.Args,
Version: build.BuilderVersion,
CacheFrom: img.Build.CachedImages,
BuildArgs: build.Args,
Version: build.BuilderVersion,

AuthConfigs: authConfigs,
}
Expand Down Expand Up @@ -332,18 +332,10 @@ func marshalCachedImages(img Image, b resource.PropertyValue) []string {
if c.IsNull() {
return cacheImages
}
latest := img.Registry.Username + "/" + img.Name
// if we specify cacheFrom as True, we pull the latest build+push of our image implicitly, i.e. registry/image
if c.IsBool() {
useCache := c.BoolValue()
if useCache {
cacheImages = append(cacheImages, latest)
}
return cacheImages
}

// if we specify a list of stages, then we only pull those
cacheFrom := c.ObjectValue()
stages := cacheFrom["stages"].ArrayValue()
stages := cacheFrom["images"].ArrayValue()
for _, img := range stages {
stage := img.StringValue()
cacheImages = append(cacheImages, stage)
Expand Down Expand Up @@ -527,16 +519,21 @@ func processLogLine(msg string) (string, error) {
info += "failed to parse aux message: " + err.Error()
}
for _, vertex := range resp.Vertexes {
info += fmt.Sprintf("layer: %+v\n", vertex.Digest)
info += fmt.Sprintf("digest: %+v\n", vertex.Digest)
info += fmt.Sprintf("%s\n", vertex.Name)
if vertex.Error != "" {
info += fmt.Sprintf("error: %s\n", vertex.Error)
}
}
for _, status := range resp.Statuses {
info += fmt.Sprintf("status: %s\n", status.GetID())
info += fmt.Sprintf("%s\n", status.GetID())
}
for _, log := range resp.Logs {
info += fmt.Sprintf("log: %+v\n", log.GetMsg())
info += fmt.Sprintf("%s\n", string(log.Msg))

}
for _, warn := range resp.Warnings {
info += fmt.Sprintf("warning: %+v\n", warn.GetShort())
info += fmt.Sprintf("%s\n", string(warn.Short))
}

} else {
Expand Down
2 changes: 1 addition & 1 deletion provider/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func TestMarshalCachedImages(t *testing.T) {
"context": resource.NewStringProperty("/twilight/sparkle/bin"),

"cacheFrom": resource.NewObjectProperty(resource.PropertyMap{
"stages": resource.NewArrayProperty([]resource.PropertyValue{
"images": resource.NewArrayProperty([]resource.PropertyValue{
resource.NewStringProperty("apple"),
resource.NewStringProperty("banana"),
resource.NewStringProperty("cherry"),
Expand Down
17 changes: 5 additions & 12 deletions provider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,9 @@ func Provider() tfbridge.ProviderInfo {
Default: "Dockerfile",
},
"cacheFrom": {
Description: "A cached image or list of build stages to use as build cache",
Description: "A list of images to use as build cache",
TypeSpec: schema.TypeSpec{
OneOf: []schema.TypeSpec{
{
Type: "boolean",
},
{
Ref: "#/types/docker:index/cacheFrom:CacheFrom",
},
},
Ref: "#/types/docker:index/cacheFrom:CacheFrom",
},
},
"env": {
Expand Down Expand Up @@ -211,10 +204,10 @@ func Provider() tfbridge.ProviderInfo {
dockerResource(dockerMod, "CacheFrom").String(): {
ObjectTypeSpec: schema.ObjectTypeSpec{
Type: "object",
Description: "Specifies information about where to obtain a cache",
Description: "Contains a list of images to reference when building using a cache",
Properties: map[string]schema.PropertySpec{
"stages": {
Description: "A list of cached build stages",
"images": {
Description: "Specifies cached images",
TypeSpec: schema.TypeSpec{
Type: "array",
Items: &schema.TypeSpec{
Expand Down
14 changes: 7 additions & 7 deletions sdk/dotnet/Inputs/CacheFromArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ namespace Pulumi.Docker.Inputs
{

/// <summary>
/// Specifies information about where to obtain a cache
/// Contains a list of images to reference when building using a cache
/// </summary>
public sealed class CacheFromArgs : global::Pulumi.ResourceArgs
{
[Input("stages")]
private InputList<string>? _stages;
[Input("images")]
private InputList<string>? _images;

/// <summary>
/// A list of cached build stages
/// Specifies cached images
/// </summary>
public InputList<string> Stages
public InputList<string> Images
{
get => _stages ?? (_stages = new InputList<string>());
set => _stages = value;
get => _images ?? (_images = new InputList<string>());
set => _images = value;
}

public CacheFromArgs()
Expand Down
4 changes: 2 additions & 2 deletions sdk/dotnet/Inputs/DockerBuildArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public InputMap<string> Args
public Input<Pulumi.Docker.BuilderVersion>? BuilderVersion { get; set; }

/// <summary>
/// A cached image or list of build stages to use as build cache
/// A list of images to use as build cache
/// </summary>
[Input("cacheFrom")]
public InputUnion<bool, Inputs.CacheFromArgs>? CacheFrom { get; set; }
public Input<Inputs.CacheFromArgs>? CacheFrom { get; set; }

/// <summary>
/// The path to the build context to use.
Expand Down
121 changes: 104 additions & 17 deletions sdk/go/docker/pulumiTypes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1bd4f68

Please sign in to comment.