Skip to content

Commit

Permalink
Add redirection support for localize command. Address PR comments. St…
Browse files Browse the repository at this point in the history
…art working on unit tests.
  • Loading branch information
Andrey Bogdanov authored and abogdanov37 committed Jun 26, 2024
1 parent de1f38e commit 1967df5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*.DS_store

.bin
# VSCode
.vscode/

# Hugo site
publishedSite/
Expand Down
5 changes: 4 additions & 1 deletion api/internal/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ type FileLoader struct {
cleaner func() error
}

// This redirect code does not process automaticali by http client and we can process it manualy
const MULTIPLE_CHOICES_REDIRECT_CODE = 300

// Repo returns the absolute path to the repo that contains Root if this fileLoader was created from a url
// or the empty string otherwise.
func (fl *FileLoader) Repo() string {
Expand Down Expand Up @@ -318,7 +321,7 @@ func (fl *FileLoader) httpClientGetContent(path string) ([]byte, error) {
defer resp.Body.Close()
// response unsuccessful
if resp.StatusCode < 200 || resp.StatusCode > 299 {
if resp.StatusCode == 300 {
if resp.StatusCode == MULTIPLE_CHOICES_REDIRECT_CODE {
var newPath string = resp.Header.Get("Location")
return nil, &RedirectionError{
Msg: "Response is redirect",
Expand Down
31 changes: 31 additions & 0 deletions api/internal/loader/fileloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/internal/git"
"sigs.k8s.io/kustomize/api/internal/loader"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

Expand Down Expand Up @@ -659,6 +661,35 @@ func TestLoaderHTTP(t *testing.T) {
_, err := l2.Load(x.path)
require.Error(err)
}

var testCaseRedirect = []testData{
{
path: "https://example.com/resource.yaml",
expectedContent: "https content",
},
}
for _, x := range testCaseRedirect {
expectedLocation := "https://redirect.com/resource.yaml"
hc := makeFakeHTTPClient(func(req *http.Request) *http.Response {
response := &http.Response{
StatusCode: 300,
Body: io.NopCloser(bytes.NewBufferString("")),
Header: make(http.Header),
}
response.Header.Add("Location", expectedLocation)
return response
})
l2 := l1
l2.http = hc
_, err := l2.Load(x.path)
require.Error(err)
var redErr *loader.RedirectionError
var path string = ""
if errors.As(err, &redErr) {
path = redErr.NewPath
}
require.Equal(expectedLocation, path)
}
}

// setupOnDisk sets up a file system on disk and directory that is cleaned after
Expand Down
5 changes: 5 additions & 0 deletions api/internal/localizer/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ func (lc *localizer) localizeResource(path string) (string, error) {
}
}
if fileErr != nil {
var redErr *loader.RedirectionError
if errors.As(fileErr, &redErr) {
path = redErr.NewPath
}

var rootErr error
locPath, rootErr = lc.localizeRoot(path)
if rootErr != nil {
Expand Down
8 changes: 3 additions & 5 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,11 @@ func (kt *KustTarget) accumulateResources(
return nil, errF
}
var redErr *load.RedirectionError
var ldr ifc.Loader
var err error
if errors.As(errF, &redErr) {
ldr, err = kt.ldr.New(redErr.NewPath)
} else {
ldr, err = kt.ldr.New(path)
path = redErr.NewPath
}
ldr, err := kt.ldr.New(path)

if err != nil {
// If accumulateFile found malformed YAML and there was a failure
// loading the resource as a base, then the resource is likely a
Expand Down

0 comments on commit 1967df5

Please sign in to comment.