Skip to content

Commit

Permalink
plugins/discovery: Set the last request and last successful request i…
Browse files Browse the repository at this point in the history
…n discovery status

This commmit sets the values for the last request and last successful request
in the discovery bundle status.

Fixes: #2630

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
(cherry picked from commit 30271bc)
  • Loading branch information
ashutosh-narkar authored and tsandall committed Aug 20, 2020
1 parent a790b65 commit 49914dd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/content/management.md
Expand Up @@ -834,6 +834,8 @@ Status updates contain the following fields:
| `bundles[_].metrics` | `object` | Metrics from the last update of the bundle. |
| `discovery.name` | `string` | Name of discovery bundle that the OPA instance is configured to download. |
| `discovery.active_revision` | `string` | Opaque revision identifier of the last successful discovery activation. |
| `discovery.last_request` | `string` | RFC3339 timestamp of last discovery bundle request. This timestamp should be >= to the successful request timestamp in normal operation. |
| `discovery.last_successful_request` | `string` | RFC3339 timestamp of last successful discovery bundle request. This timestamp should be >= to the successful download timestamp in normal operation. |
| `discovery.last_successful_download` | `string` | RFC3339 timestamp of last successful discovery bundle download. |
| `discovery.last_successful_activation` | `string` | RFC3339 timestamp of last successful discovery bundle activation. |
| `plugins` | `object` | A set of objects describing the state of configured plugins in OPA's runtime. |
Expand Down
5 changes: 4 additions & 1 deletion plugins/discovery/discovery.go
Expand Up @@ -133,6 +133,7 @@ func (c *Discovery) oneShot(ctx context.Context, u download.Update) {
}

func (c *Discovery) processUpdate(ctx context.Context, u download.Update) {
c.status.SetRequest()

if u.Error != nil {
c.logError("Discovery download failed: %v", u.Error)
Expand All @@ -141,8 +142,10 @@ func (c *Discovery) processUpdate(ctx context.Context, u download.Update) {
return
}

c.status.LastSuccessfulRequest = c.status.LastRequest

if u.Bundle != nil {
c.status.SetDownloadSuccess()
c.status.LastSuccessfulDownload = c.status.LastSuccessfulRequest

if err := c.reconfigure(ctx, u); err != nil {
c.logError("Discovery reconfiguration error occurred: %v", err)
Expand Down
72 changes: 72 additions & 0 deletions plugins/discovery/discovery_test.go
Expand Up @@ -751,6 +751,78 @@ func TestStatusUpdates(t *testing.T) {
}
}

func TestStatusUpdatesTimestamp(t *testing.T) {

ts := testServer{t: t}
ts.Start()
defer ts.Stop()

manager, err := plugins.New([]byte(fmt.Sprintf(`{
"labels": {"x": "y"},
"services": {
"localhost": {
"url": %q
}
},
"discovery": {"name": "config"},
}`, ts.server.URL)), "test-id", inmem.New())
if err != nil {
t.Fatal(err)
}

disco, err := New(manager)
if err != nil {
t.Fatal(err)
}

ctx := context.Background()

// simulate HTTP 200 response from downloader
disco.oneShot(ctx, download.Update{ETag: "etag-1", Bundle: makeDataBundle(1, `{
"config": {
"status": {}
}
}`)})

if disco.status.LastSuccessfulDownload != disco.status.LastSuccessfulRequest || disco.status.LastSuccessfulDownload != disco.status.LastRequest {
t.Fatal("expected last successful request to be same as download and request")
}

if disco.status.LastSuccessfulActivation.IsZero() {
t.Fatal("expected last successful activation to be non-zero")
}

time.Sleep(time.Millisecond)

// simulate HTTP 304 response from downloader
disco.oneShot(ctx, download.Update{ETag: "etag-1", Bundle: nil})
if disco.status.LastSuccessfulDownload == disco.status.LastSuccessfulRequest || disco.status.LastSuccessfulDownload == disco.status.LastRequest {
t.Fatal("expected last successful download to differ from request and last request")
}

// simulate HTTP 200 response from downloader
disco.oneShot(ctx, download.Update{ETag: "etag-2", Bundle: makeDataBundle(2, `{
"config": {
"status": {}
}
}`)})

if disco.status.LastSuccessfulDownload != disco.status.LastSuccessfulRequest || disco.status.LastSuccessfulDownload != disco.status.LastRequest {
t.Fatal("expected last successful request to be same as download and request")
}

if disco.status.LastSuccessfulActivation.IsZero() {
t.Fatal("expected last successful activation to be non-zero")
}

// simulate error response from downloader
disco.oneShot(ctx, download.Update{Error: fmt.Errorf("unknown error")})

if disco.status.LastSuccessfulDownload != disco.status.LastSuccessfulRequest || disco.status.LastSuccessfulDownload == disco.status.LastRequest {
t.Fatal("expected last successful request to be same as download but different from request")
}
}

func makeDataBundle(n int, s string) *bundleApi.Bundle {
return &bundleApi.Bundle{
Manifest: bundleApi.Manifest{Revision: fmt.Sprintf("test-revision-%v", n)},
Expand Down

0 comments on commit 49914dd

Please sign in to comment.