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

Close HTTP response body on failed GET attempts #105591

Merged
merged 2 commits into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions staging/src/k8s.io/cli-runtime/pkg/resource/builder.go
Expand Up @@ -1155,10 +1155,9 @@ func (b *Builder) Do() *Result {
helpers = append(helpers, RetrieveLazy)
}
if b.continueOnError {
r.visitor = NewDecoratedVisitor(ContinueOnErrorVisitor{r.visitor}, helpers...)
} else {
r.visitor = NewDecoratedVisitor(r.visitor, helpers...)
r.visitor = ContinueOnErrorVisitor{Visitor: r.visitor}
}
r.visitor = NewDecoratedVisitor(r.visitor, helpers...)
return r
}

Expand Down
30 changes: 17 additions & 13 deletions staging/src/k8s.io/cli-runtime/pkg/resource/visitor.go
Expand Up @@ -204,9 +204,9 @@ type EagerVisitorList []Visitor
// Visit implements Visitor, and gathers errors that occur during processing until
// all sub visitors have been visited.
func (l EagerVisitorList) Visit(fn VisitorFunc) error {
errs := []error(nil)
var errs []error
for i := range l {
if err := l[i].Visit(func(info *Info, err error) error {
err := l[i].Visit(func(info *Info, err error) error {
if err != nil {
errs = append(errs, err)
return nil
Expand All @@ -215,7 +215,8 @@ func (l EagerVisitorList) Visit(fn VisitorFunc) error {
errs = append(errs, err)
}
return nil
}); err != nil {
})
if err != nil {
errs = append(errs, err)
}
}
Expand Down Expand Up @@ -253,13 +254,15 @@ func (v *URLVisitor) Visit(fn VisitorFunc) error {
// readHttpWithRetries tries to http.Get the v.URL retries times before giving up.
func readHttpWithRetries(get httpget, duration time.Duration, u string, attempts int) (io.ReadCloser, error) {
var err error
var body io.ReadCloser
if attempts <= 0 {
return nil, fmt.Errorf("http attempts must be greater than 0, was %d", attempts)
}
for i := 0; i < attempts; i++ {
var statusCode int
var status string
var (
statusCode int
status string
body io.ReadCloser
)
if i > 0 {
time.Sleep(duration)
}
Expand All @@ -272,10 +275,12 @@ func readHttpWithRetries(get httpget, duration time.Duration, u string, attempts
continue
}

// Error - Set the error condition from the StatusCode
if statusCode != http.StatusOK {
err = fmt.Errorf("unable to read URL %q, server reported %s, status code=%d", u, status, statusCode)
if statusCode == http.StatusOK {
return body, nil
}
body.Close()
// Error - Set the error condition from the StatusCode
err = fmt.Errorf("unable to read URL %q, server reported %s, status code=%d", u, status, statusCode)

if statusCode >= 500 && statusCode < 600 {
// Retry 500's
Expand All @@ -285,7 +290,7 @@ func readHttpWithRetries(get httpget, duration time.Duration, u string, attempts
break
}
}
return body, err
return nil, err
}

// httpget Defines function to retrieve a url and return the results. Exists for unit test stubbing.
Expand Down Expand Up @@ -346,7 +351,7 @@ type ContinueOnErrorVisitor struct {
// returned by the visitor directly may still result in some items
// not being visited.
func (v ContinueOnErrorVisitor) Visit(fn VisitorFunc) error {
errs := []error{}
var errs []error
err := v.Visitor.Visit(func(info *Info, err error) error {
if err != nil {
errs = append(errs, err)
Expand Down Expand Up @@ -420,7 +425,7 @@ func (v FlattenListVisitor) Visit(fn VisitorFunc) error {
if info.Mapping != nil && !info.Mapping.GroupVersionKind.Empty() {
preferredGVKs = append(preferredGVKs, info.Mapping.GroupVersionKind)
}
errs := []error{}
var errs []error
for i := range items {
item, err := v.mapper.infoForObject(items[i], v.typer, preferredGVKs)
if err != nil {
Expand All @@ -439,7 +444,6 @@ func (v FlattenListVisitor) Visit(fn VisitorFunc) error {
}
}
return utilerrors.NewAggregate(errs)

})
}

Expand Down
8 changes: 4 additions & 4 deletions staging/src/k8s.io/cli-runtime/pkg/resource/visitor_test.go
Expand Up @@ -75,7 +75,7 @@ func TestVisitorHttpGet(t *testing.T) {
httpRetries: func(url string) (int, string, io.ReadCloser, error) {
assert.Equal(t, "hello", url)
i++
return 501, "Status", nil, nil
return 501, "Status", ioutil.NopCloser(new(bytes.Buffer)), nil
Copy link
Member Author

Choose a reason for hiding this comment

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

Body is guaranteed to be present unless there is an error. Here there is no error, but body was not returned - invalid test.

},
args: httpArgs{
duration: 0,
Expand All @@ -89,7 +89,7 @@ func TestVisitorHttpGet(t *testing.T) {
httpRetries: func(url string) (int, string, io.ReadCloser, error) {
assert.Equal(t, "hello", url)
i++
return 300, "Status", nil, nil
return 300, "Status", ioutil.NopCloser(new(bytes.Buffer)), nil

},
args: httpArgs{
Expand All @@ -104,7 +104,7 @@ func TestVisitorHttpGet(t *testing.T) {
httpRetries: func(url string) (int, string, io.ReadCloser, error) {
assert.Equal(t, "hello", url)
i++
return 501, "Status", nil, nil
return 501, "Status", ioutil.NopCloser(new(bytes.Buffer)), nil

},
args: httpArgs{
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestVisitorHttpGet(t *testing.T) {
if i > 1 {
return 200, "Status", ioutil.NopCloser(new(bytes.Buffer)), nil
}
return 501, "Status", nil, nil
return 501, "Status", ioutil.NopCloser(new(bytes.Buffer)), nil

},
args: httpArgs{
Expand Down