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

Simplify health check to ignore response #258

Merged
merged 1 commit into from
May 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package healthcheck

import (
"context"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -50,7 +49,6 @@ const (
errDeleteLCValidationBucket = "failed to delete lifecycle configuration validation bucket"
errUpdateHealthStatus = "failed to update health status of provider config"
errFailedHealthCheckReq = "failed to forward health check request"
errUnexpectedResp = "unexpected response from health check request: %s"

healthCheckSuffix = "-health-check"

Expand Down Expand Up @@ -199,23 +197,15 @@ func (c *Controller) doHealthCheck(ctx context.Context, providerConfig *apisv1al

return doErr
}
// We don't actually check the response code or body for the health check.
// This is because a HTTP Get request to RGW equates to a ListBuckets S3 request and
// it is possible that an authorisation error will occur, resulting in a 4XX error.
// Instead, we assume healthiness by simply making the connection successfully.

defer func() {
if err := resp.Body.Close(); err != nil {
c.log.Info("failed to close response reader after health check", "error", err.Error())
}
}()

if resp.StatusCode != http.StatusOK {
statusErr := errors.New(fmt.Sprintf(errUnexpectedResp, resp.Status))
traces.SetAndRecordError(span, statusErr)

return statusErr
}
// Health check completed successfully, update status.
providerConfig.Status.SetConditions(v1alpha1.HealthCheckSuccess())

return nil
return resp.Body.Close()
}

// unpauseBuckets lists all buckets that exist on the given backend by using the custom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package healthcheck

import (
"context"
"fmt"
"net/http"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -59,6 +59,8 @@ func NewTestClient(fn RoundTripFunc) *http.Client {
func TestReconcile(t *testing.T) {
t.Parallel()
backendName := "test-backend"
someErr := errors.New("some error")
urlErr := url.Error{Op: "Get", URL: "http:", Err: someErr}

type fields struct {
fakeS3Client func(*backendstorefakes.FakeS3Client)
Expand Down Expand Up @@ -88,10 +90,7 @@ func TestReconcile(t *testing.T) {
"ProviderConfig has been deleted": {
fields: fields{
testHttpClient: NewTestClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
}, nil
return &http.Response{}, nil
}),
fakeS3Client: func(fake *backendstorefakes.FakeS3Client) {
// cleanup calls HeadBucket
Expand Down Expand Up @@ -163,10 +162,7 @@ func TestReconcile(t *testing.T) {
"ProviderConfig goes from healthy to unhealthy with bad request": {
fields: fields{
testHttpClient: NewTestClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadRequest,
Status: http.StatusText(http.StatusBadRequest),
}, nil
return &http.Response{}, someErr
}),
providerConfig: &apisv1alpha1.ProviderConfig{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -195,7 +191,7 @@ func TestReconcile(t *testing.T) {
},
want: want{
res: ctrl.Result{},
err: errors.New(fmt.Sprintf(errUnexpectedResp, http.StatusText(http.StatusBadRequest))),
err: errors.Wrap(&urlErr, errFailedHealthCheckReq),
pc: &apisv1alpha1.ProviderConfig{
ObjectMeta: metav1.ObjectMeta{
Name: backendName,
Expand All @@ -208,7 +204,7 @@ func TestReconcile(t *testing.T) {
ConditionedStatus: xpv1.ConditionedStatus{
Conditions: []xpv1.Condition{
v1alpha1.HealthCheckFail().
WithMessage(fmt.Sprintf(errUnexpectedResp, http.StatusText(http.StatusBadRequest))),
WithMessage(errors.Wrap(&urlErr, errFailedHealthCheckReq).Error()),
},
},
},
Expand All @@ -219,10 +215,7 @@ func TestReconcile(t *testing.T) {
"ProviderConfig goes from unhealthy to healthy so its buckets should be unpaused": {
fields: fields{
testHttpClient: NewTestClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
}, nil
return &http.Response{}, nil
}),
providerConfig: &apisv1alpha1.ProviderConfig{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -353,10 +346,7 @@ func TestReconcile(t *testing.T) {
"ProviderConfig goes from health check disabled to healthy so its buckets should be unpaused": {
fields: fields{
testHttpClient: NewTestClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
}, nil
return &http.Response{}, nil
}),
providerConfig: &apisv1alpha1.ProviderConfig{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Loading