Skip to content

Commit 88c89f4

Browse files
authored
fix: always show accumulation errors (#5693)
* fix: always show accumulation errors if the resource was successfully loaded as a base * chore: regression test * chore: fix lint violations
1 parent ce80dc9 commit 88c89f4

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

api/internal/target/kusttarget.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,6 @@ func (kt *KustTarget) accumulateResources(
449449
ra, err = kt.accumulateDirectory(ra, ldr, false)
450450
}
451451
if err != nil {
452-
if kusterr.IsMalformedYAMLError(errF) { // Some error occurred while tyring to decode YAML file
453-
return nil, errF
454-
}
455452
return nil, errors.WrapPrefixf(
456453
err, "accumulation err='%s'", errF.Error())
457454
}

api/internal/target/kusttarget_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package target_test
66
import (
77
"encoding/base64"
88
"fmt"
9+
"net/http"
10+
"net/http/httptest"
911
"reflect"
1012
"testing"
1113
"time"
@@ -560,3 +562,71 @@ func (l loaderNewThrowsError) Load(location string) ([]byte, error) {
560562
func (l loaderNewThrowsError) Cleanup() error {
561563
return l.baseLoader.Cleanup() //nolint:wrapcheck // baseLoader's error is sufficient
562564
}
565+
566+
func TestErrorMessageForMalformedYAMLAndInvalidBase(t *testing.T) {
567+
// These testcases verify behavior for the scenario described in
568+
// https://github.com/kubernetes-sigs/kustomize/issues/5692 .
569+
570+
// Use a test server to fake the remote file response
571+
handler := http.NewServeMux()
572+
handler.HandleFunc("/", func(out http.ResponseWriter, req *http.Request) {
573+
// Per issue #5692, the server should return a 200 status code with a response body that fails to parse as YAML
574+
out.WriteHeader(http.StatusOK)
575+
_, _ = out.Write([]byte(`<!DOCTYPE html>
576+
<html class="html-devise-layout ui-light-gray" lang="en">
577+
<head prefix="og: http://ogp.me/ns#">`))
578+
})
579+
svr := httptest.NewServer(handler)
580+
defer svr.Close()
581+
582+
th := kusttest_test.MakeHarness(t)
583+
th.WriteF("/should-fail/kustomization.yml", "resources:\n- "+svr.URL)
584+
th.WriteF("/should-fail/remote-repo/kustomization.yml", "this: is not a kustomization file!")
585+
586+
ldrWrapper := func(baseLoader ifc.Loader) ifc.Loader {
587+
return &loaderWithRenamedRoots{
588+
baseLoader: baseLoader,
589+
fakeRootMap: map[string]string{
590+
// Use the "remote-repo" subdir instead of the remote git repo
591+
svr.URL: "remote-repo",
592+
},
593+
}
594+
}
595+
596+
_, err := makeAndLoadKustTargetWithLoaderOverride(t, th.GetFSys(), "/should-fail", ldrWrapper).AccumulateTarget()
597+
require.Error(t, err)
598+
errString := err.Error()
599+
assert.Contains(t, errString, "accumulating resources from '"+svr.URL+"'")
600+
assert.Contains(t, errString, "MalformedYAMLError: yaml: line 3: mapping values are not allowed in this context")
601+
assert.Contains(t, errString, `invalid Kustomization: json: unknown field "this"`)
602+
}
603+
604+
// loaderWithRenamedRoots is a loader that can map New() roots to some other name
605+
type loaderWithRenamedRoots struct {
606+
baseLoader ifc.Loader
607+
fakeRootMap map[string]string
608+
}
609+
610+
func (l loaderWithRenamedRoots) Repo() string {
611+
return l.baseLoader.Repo()
612+
}
613+
614+
func (l loaderWithRenamedRoots) Root() string {
615+
return l.baseLoader.Root()
616+
}
617+
618+
func (l loaderWithRenamedRoots) New(newRoot string) (ifc.Loader, error) {
619+
if otherRoot, ok := l.fakeRootMap[newRoot]; ok {
620+
return l.baseLoader.New(otherRoot) //nolint:wrapcheck // baseLoader's error is sufficient
621+
}
622+
623+
return l.baseLoader.New(newRoot) //nolint:wrapcheck // baseLoader's error is sufficient
624+
}
625+
626+
func (l loaderWithRenamedRoots) Load(path string) ([]byte, error) {
627+
return l.baseLoader.Load(path) //nolint:wrapcheck // baseLoader's error is sufficient
628+
}
629+
630+
func (l loaderWithRenamedRoots) Cleanup() error {
631+
return l.baseLoader.Cleanup() //nolint:wrapcheck // baseLoader's error is sufficient
632+
}

0 commit comments

Comments
 (0)