Skip to content

Commit a74fbaf

Browse files
committed
feat: add includeTag parameter to doBuild for tagged references
This fixes an issue where "resourceKoBuildCreate" uses the result of "doPublish" as the "image_ref", but in "resourceKoBuildRead" the result of "doBuild" as the "image_ref". This causes an issue when a single "tags" is configured and the "image_ref" from "doPublish" will include the tag, but the the "image_ref" from "doBuild" does not, and will always cause the resource to be recreated. - Add includeTag boolean parameter to doBuild function - When includeTag is true, return tagged reference with digest appended - Update doBuild calls: false for Create, true for Read operations - Maintains backward compatibility with existing behavior
1 parent f05a408 commit a74fbaf

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

internal/provider/resource_ko_build.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ var baseImages sync.Map // Cache of base image lookups.
231231
// doBuild builds the image and returns the built image, and the full name.Reference by digest that the image would be pushed to.
232232
//
233233
// doBuild doesn't publish images, use doPublish to publish the build.Result that doBuild returns.
234-
func doBuild(ctx context.Context, opts buildOptions) (build.Result, string, error) {
234+
func doBuild(ctx context.Context, opts buildOptions, includeTag bool) (build.Result, string, error) {
235235
if opts.imageRepo == "" {
236236
return nil, "", errors.New("one of KO_DOCKER_REPO env var, or provider `repo`, or image resource `repo` must be set")
237237
}
@@ -253,6 +253,12 @@ func doBuild(ctx context.Context, opts buildOptions) (build.Result, string, erro
253253
return nil, "", fmt.Errorf("ParseReference: %w", err)
254254
}
255255

256+
if includeTag && len(opts.tags) == 1 {
257+
// Return the tagged reference with digest appended (same format as doPublish)
258+
taggedRef := ref.Context().Tag(opts.tags[0])
259+
return res, taggedRef.String() + "@" + dig.String(), nil
260+
}
261+
256262
return res, ref.Context().Digest(dig.String()).String(), nil
257263
}
258264

@@ -350,7 +356,7 @@ func resourceKoBuildCreate(ctx context.Context, d *schema.ResourceData, meta int
350356
return diag.Errorf("configuring provider: %v", err)
351357
}
352358

353-
res, _, err := doBuild(ctx, fromData(d, po))
359+
res, _, err := doBuild(ctx, fromData(d, po), false)
354360
if err != nil {
355361
return diag.Errorf("[id=%s] create doBuild: %v", d.Id(), err)
356362
}
@@ -373,7 +379,7 @@ func resourceKoBuildRead(ctx context.Context, d *schema.ResourceData, meta inter
373379
}
374380

375381
var diags diag.Diagnostics
376-
_, ref, err := doBuild(ctx, fromData(d, po))
382+
_, ref, err := doBuild(ctx, fromData(d, po), true)
377383
if err != nil {
378384
ref = zeroRef
379385
diags = append(diags, diag.Diagnostic{

0 commit comments

Comments
 (0)