-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sink: wait for infra-agent to be ready before starting (#346)
Co-authored-by: Guillermo Sanchez Gavier <gsanchez@newrelic.com> Co-authored-by: Paolo Gallina <paologallina1992@gmail.com>
- Loading branch information
1 parent
a0ed2ac
commit 1d4c557
Showing
7 changed files
with
255 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package prober | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/newrelic/nri-kubernetes/v3/internal/logutil" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Prober is an object that polls and http URL and returns an error if it does not return 200 Ok within the specified | ||
// timeout. | ||
type Prober struct { | ||
timeout time.Duration | ||
backoff time.Duration | ||
Logger *log.Logger | ||
} | ||
|
||
var ErrProbeTimeout = errors.New("probe timed out") | ||
var errProbeNotOk = errors.New("probe did not return 200 Ok") | ||
|
||
// New creates a Prober that will check an endpoint every backoff seconds. | ||
func New(timeout, backoff time.Duration) *Prober { | ||
return &Prober{ | ||
timeout: timeout, | ||
backoff: backoff, | ||
Logger: logutil.Discard, | ||
} | ||
} | ||
|
||
// Probe repeatedly hits the specified url with a GET request every Prober.backoff, and blocks until a request returns | ||
// 200, or Prober.timeout passes. | ||
func (p *Prober) Probe(url string) error { | ||
start := time.Now() | ||
for { | ||
if time.Since(start) > p.timeout { | ||
return fmt.Errorf("%w after %s", ErrProbeTimeout, p.timeout) | ||
} | ||
|
||
err := p.attempt(url) | ||
if err != nil { | ||
p.Logger.Debug(err) | ||
p.Logger.Debugf("Retrying in %s", p.backoff) | ||
time.Sleep(p.backoff) | ||
continue | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// attempt makes a request to the specified URL and returns an error if it does not return 200. | ||
func (p *Prober) attempt(url string) error { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return fmt.Errorf("probe attempt to %s failed: %w", url, err) | ||
} | ||
|
||
_ = resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("%w: %d", errProbeNotOk, resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package prober_test | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/newrelic/nri-kubernetes/v3/src/integration/prober" | ||
) | ||
|
||
func succeedAfter(duration time.Duration) http.HandlerFunc { | ||
creation := time.Now() | ||
return func(rw http.ResponseWriter, request *http.Request) { | ||
if time.Since(creation) > duration { | ||
rw.WriteHeader(http.StatusOK) | ||
return | ||
} | ||
|
||
rw.WriteHeader(http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func TestProber_fails_as_expected(t *testing.T) { | ||
t.Parallel() | ||
|
||
p := prober.New(4*time.Second, 300*time.Millisecond) | ||
server := httptest.NewServer(succeedAfter(5 * time.Second)) | ||
|
||
err := p.Probe(server.URL) | ||
if !errors.Is(err, prober.ErrProbeTimeout) { | ||
t.Fatalf("Expected timeout error, got %v", err) | ||
} | ||
} | ||
|
||
func TestProber_succeeds(t *testing.T) { | ||
t.Parallel() | ||
|
||
p := prober.New(15*time.Second, 300*time.Millisecond) | ||
server := httptest.NewServer(succeedAfter(5 * time.Second)) | ||
|
||
err := p.Probe(server.URL) | ||
if errors.Is(err, prober.ErrProbeTimeout) { | ||
t.Fatalf("Expected timeout error, got %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.