Skip to content

Commit b35004a

Browse files
authored
feat: add warning diags to aid troubleshooting (#30)
Adds warning diagnostics to aid in troubleshooting issues with cached builds.
1 parent 9a4bf09 commit b35004a

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

internal/provider/cached_image_resource.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ func (r *CachedImageResource) Read(ctx context.Context, req resource.ReadRequest
292292
// If the previous state is that Image == BuilderImage, then we previously did
293293
// not find the image. We will need to run another cache probe.
294294
if data.Image.Equal(data.BuilderImage) {
295-
tflog.Debug(ctx, "Image previously not found. Recreating.", map[string]any{"ref": data.Image.ValueString()})
295+
resp.Diagnostics.AddWarning(
296+
"Re-running cache probe due to previous miss.",
297+
fmt.Sprintf(`The previous state specifies image == builder_image %q, which indicates a previous cache miss.`,
298+
data.Image.ValueString(),
299+
))
296300
resp.State.RemoveResource(ctx)
297301
return
298302
}
@@ -301,12 +305,22 @@ func (r *CachedImageResource) Read(ctx context.Context, req resource.ReadRequest
301305
img, err := getRemoteImage(data.Image.ValueString())
302306
if err != nil {
303307
if !strings.Contains(err.Error(), "MANIFEST_UNKNOWN") {
304-
resp.Diagnostics.AddError("Error checking remote image", err.Error())
308+
// Explicitly not making this an error diag.
309+
resp.Diagnostics.AddWarning("Unable to check remote image.",
310+
fmt.Sprintf("The repository %q returned the following error while checking for a cached image %q: %q",
311+
data.CacheRepo.ValueString(),
312+
data.Image.ValueString(),
313+
err.Error(),
314+
))
305315
return
306316
}
307317
// Image does not exist any longer! Remove the resource so we can re-create
308318
// it next time.
309-
tflog.Debug(ctx, "Remote image does not exist any longer. Recreating.", map[string]any{"ref": data.Image.ValueString()})
319+
resp.Diagnostics.AddWarning("Previously built image not found, recreating.",
320+
fmt.Sprintf("The repository %q does not contain the cached image %q. It will be rebuilt in the next apply.",
321+
data.CacheRepo.ValueString(),
322+
data.Image.ValueString(),
323+
))
310324
resp.State.RemoveResource(ctx)
311325
return
312326
}
@@ -365,7 +379,11 @@ func (r *CachedImageResource) Create(ctx context.Context, req resource.CreateReq
365379
// FIXME: there are legit errors that can crop up here.
366380
// We should add a sentinel error in Kaniko for uncached layers, and check
367381
// it here.
368-
tflog.Info(ctx, "cached image not found", map[string]any{"err": err.Error()})
382+
resp.Diagnostics.AddWarning("Cached image not found.", fmt.Sprintf(
383+
"Failed to find cached image in repository %q. It will be rebuilt in the next apply. Error: %s",
384+
data.CacheRepo.ValueString(),
385+
err.Error(),
386+
))
369387
data.Image = data.BuilderImage
370388
} else if digest, err := cachedImg.Digest(); err != nil {
371389
// There's something seriously up with this image!

internal/provider/cached_image_resource_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1111
)
1212

13-
func TestAccCachedImageDataSource(t *testing.T) {
13+
func TestAccCachedImageResource(t *testing.T) {
1414
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
1515
defer cancel()
1616
files := map[string]string{

0 commit comments

Comments
 (0)