Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to custom handle redirects #5678

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*.DS_store

.bin
# VSCode
.vscode/
koba1t marked this conversation as resolved.
Show resolved Hide resolved

# Hugo site
publishedSite/
Expand Down
9 changes: 9 additions & 0 deletions api/internal/loader/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ var (
ErrHTTP = errors.Errorf("HTTP Error")
ErrRtNotDir = errors.Errorf("must build at directory")
)

type RedirectionError struct {
NewPath string
Msg string
}

func (e *RedirectionError) Error() string {
return e.Msg
}
16 changes: 12 additions & 4 deletions api/internal/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,19 @@
defer resp.Body.Close()
// response unsuccessful
if resp.StatusCode < 200 || resp.StatusCode > 299 {
_, err = git.NewRepoSpecFromURL(path)
if err == nil {
return nil, errors.Errorf("URL is a git repository")
if resp.StatusCode == 300 {

Check failure on line 321 in api/internal/loader/fileloader.go

View workflow job for this annotation

GitHub Actions / Lint

mnd: Magic number: 300, in <condition> detected (gomnd)
var newPath string = resp.Header.Get("Location")
return nil, &RedirectionError{
Msg: "Response is redirect",
NewPath: newPath,
}
Comment on lines +325 to +329
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe That is better to solve redirects here than return RedirectionError and a new path.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! It does not work in all cases. For example if I return redirect to git. In that case httpClient has noe credentials for download content

} else {
_, err = git.NewRepoSpecFromURL(path)
if err == nil {
return nil, errors.Errorf("URL is a git repository")
}
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
}
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
}
content, err := io.ReadAll(resp.Body)
return content, errors.Wrap(err)
Expand Down
9 changes: 8 additions & 1 deletion api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ func (kt *KustTarget) accumulateResources(
if errors.Is(errF, load.ErrHTTP) {
return nil, errF
}
ldr, err := kt.ldr.New(path)
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)
}
koba1t marked this conversation as resolved.
Show resolved Hide resolved
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
Loading