Skip to content

Commit

Permalink
Add isPreview param to and ensure minimum required inputs for a local…
Browse files Browse the repository at this point in the history
… preview build
  • Loading branch information
guineveresaenger committed Dec 4, 2023
1 parent a9902c0 commit 63358d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
5 changes: 3 additions & 2 deletions provider/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type Config struct {
func (p *dockerNativeProvider) dockerBuild(ctx context.Context,
urn resource.URN,
props *structpb.Struct,
isPreview bool,
) (string, *structpb.Struct, error) {

inputs, err := plugin.UnmarshalProperties(props, plugin.MarshalOptions{KeepUnknowns: true, SkipNulls: true})
Expand Down Expand Up @@ -313,8 +314,8 @@ func (p *dockerNativeProvider) dockerBuild(ctx context.Context,
return "", nil, err
}

// if we are not pushing to the registry, we return after building the local image.
if img.SkipPush {
// if we are not pushing to the registry, or we are in Preview mode, we return after building the local image.
if img.SkipPush || isPreview {
// Obtain image digest from docker inspect
imageInspect, _, inspErr := docker.ImageInspectWithRaw(ctx, img.Name)
if inspErr != nil {
Expand Down
44 changes: 30 additions & 14 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,21 +341,22 @@ func (p *dockerNativeProvider) Create(ctx context.Context, req *rpc.CreateReques
// verify buildOnPreview is Known; if not, send warning and continue.
if inputs["buildOnPreview"].ContainsUnknowns() {
msg = "buildOnPreview is unresolved; cannot build on preview. Continuing without preview image build. " +
"To avoid this warning, set buildOnPreview explicitly, and ensure all inputs are resolved at preview."
"To avoid this warning, set buildOnPreview explicitly."
returnWithoutBuild = true
}
// if we're in preview mode and buildOnPreview is set to false, we return the inputs
if !inputs["buildOnPreview"].BoolValue() {
returnWithoutBuild = true
}
// buildOnPreview needs all inputs to be resolved. Warn and continue without building the image
// TODO: there is room for some future granularity here - we should be able to build a local image without
// (TODO cont) knowing inputs for a registry, for example.
if inputs.ContainsUnknowns() {
msg = "cannot build on preview with unresolved inputs. Continuing without preview image build. " +
"To avoid this warning, set buildOnPreview to False, or ensure all inputs are resolved at preview."

// buildOnPreview needs image name, dockerfile, and context to be resolved.
// Warn and continue without building the image
if !ensureMinimumBuildInputs(inputs) {
returnWithoutBuild = true
msg = "Minimum inputs for build are unresolved. Continuing without preview image build. " +
"To avoid this warning, ensure image name, dockerfile, and context are resolved at preview."
}

if returnWithoutBuild {
if msg != "" {
err = p.host.Log(ctx, "warning", urn, msg)
Expand All @@ -369,7 +370,7 @@ func (p *dockerNativeProvider) Create(ctx context.Context, req *rpc.CreateReques
}
}

id, outputProperties, err := p.dockerBuild(ctx, urn, req.GetProperties())
id, outputProperties, err := p.dockerBuild(ctx, urn, req.GetProperties(), req.Preview)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -450,13 +451,14 @@ func (p *dockerNativeProvider) Update(ctx context.Context, req *rpc.UpdateReques
if !newInputs["buildOnPreview"].BoolValue() {
returnWithoutBuild = true
}
// buildOnPreview needs all inputs to be resolved. Warn and continue without building the image
// TODO: there is room for some future granularity here - see above TODO in Create method
if newInputs.ContainsUnknowns() {
msg = "cannot build on preview with unresolved inputs. Continuing without preview image build. " +
"To avoid this warning, set buildOnPreview to False, or ensure all inputs are resolved at preview."
// buildOnPreview needs image name, dockerfile, and context to be resolved.
// Warn and continue without building the image
if !ensureMinimumBuildInputs(newInputs) {
returnWithoutBuild = true
msg = "Minimum inputs for build are unresolved. Continuing without preview image build. " +
"To avoid this warning, ensure image name, dockerfile, and context are resolved at preview."
}

if returnWithoutBuild {
if msg != "" {
err = p.host.Log(ctx, "warning", urn, msg)
Expand All @@ -471,7 +473,7 @@ func (p *dockerNativeProvider) Update(ctx context.Context, req *rpc.UpdateReques
}

// When the docker image is updated, we build and push again.
_, outputProperties, err := p.dockerBuild(ctx, urn, req.GetNews())
_, outputProperties, err := p.dockerBuild(ctx, urn, req.GetNews(), req.Preview)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -732,3 +734,17 @@ func marshalBuildOnPreview(inputs resource.PropertyMap) bool {
}
return inputs["buildOnPreview"].BoolValue()
}

func ensureMinimumBuildInputs(inputs resource.PropertyMap) bool {
if !inputs["build"].IsObject() {
return false
} else {
if inputs["build"].ObjectValue()["dockerfile"].ContainsUnknowns() || inputs["build"].ObjectValue()["context"].ContainsUnknowns() {
return false
}
}
if inputs["imageName"].ContainsUnknowns() {
return false
}
return true
}

0 comments on commit 63358d6

Please sign in to comment.