Skip to content

Commit aa2a16f

Browse files
authored
docs: Add manual trigger to integration docs (#7473)
Following: https://github.com/orgs/open-policy-agent/discussions/685 This comes up often enough that it should be documented. Based on the handy example in: #3828 (comment) Signed-off-by: Charlie Egan <[email protected]>
1 parent c34a941 commit aa2a16f

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

Diff for: docs/content/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ included in the actual bundle gzipped tarball.
757757
| `bundles[_].service` | `string` | Yes | Name of service to use to contact remote server. |
758758
| `bundles[_].polling.min_delay_seconds` | `int64` | No (default: `60`) | Minimum amount of time to wait between bundle downloads. |
759759
| `bundles[_].polling.max_delay_seconds` | `int64` | No (default: `120`) | Maximum amount of time to wait between bundle downloads. |
760-
| `bundles[_].trigger` | `string` (default: `periodic`) | No | Controls how bundle is downloaded from the remote server. Allowed values are `periodic` and `manual` (`manual` triggers are only possible when using OPA as a Go package). |
760+
| `bundles[_].trigger` | `string` (default: `periodic`) | No | Controls how bundle is downloaded from the remote server. Allowed values are `periodic` and `manual` ([`manual` triggers](./integration/#manually-triggering-bundle-reloads) are only possible when running OPA as a SDK instance from the Go package). |
761761
| `bundles[_].polling.long_polling_timeout_seconds` | `int64` | No | Maximum amount of time the server should wait before issuing a timeout if there's no update available. |
762762
| `bundles[_].persist` | `bool` | No | Persist activated bundles to disk. |
763763
| `bundles[_].signing.keyid` | `string` | No | Name of the key to use for bundle signature verification. |

Diff for: docs/content/integration.md

+43-6
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,17 @@ The [reference documentation](../rest-api) is also a good place to start.
183183

184184
### Integrating with the Go SDK
185185

186+
{{< info >}}
187+
This section documents the v1 SDK package.
188+
Please see [v0 Backwards Compatibility](../v0-compatibility) for notes on using
189+
the v0 SDK package.
190+
{{< /info >}}
191+
186192
The [SDK](https://pkg.go.dev/github.com/open-policy-agent/opa/sdk) package contains high-level APIs for embedding OPA
187193
inside of Go programs and obtaining the output of query evaluation. To get started, import the `sdk` package:
188194

189195
```go
190-
import "github.com/open-policy-agent/opa/sdk"
196+
import "github.com/open-policy-agent/opa/v1/sdk"
191197
```
192198

193199
A typical workflow when using the `sdk` package would involve first creating a new `sdk.OPA` object by calling
@@ -273,11 +279,42 @@ Setting an `ID` in `sdk.Options` is optional, but recommended. If you do not set
273279
for the system. While this is fine for testing, it makes it difficult to monitor the system over time, as a new ID will
274280
be created each time the SDK is initialized, such as when the process is restarted.
275281

276-
{{< info >}}
277-
This section documents the v1 SDK package.
278-
Please see [v0 Backwards Compatibility](../v0-compatibility) for notes on using
279-
the v0 SDK package.
280-
{{< /info >}}
282+
#### Manually Triggering Bundle Reloads
283+
284+
Users of the SDK can
285+
[manually trigger](./configuration/#bundles)
286+
the SDK's Bundle plugin to load new bundles immediately based on external
287+
events. When doing so, it's recommended to set `bundles[_].trigger` to `manual`
288+
if you want to disable periodic bundle polling.
289+
290+
In this short example, the `bundle` plugin is loaded from the SDK instance and
291+
triggered to check for new bundles. Do this sparingly, it is not intended to be
292+
used as a replacement for periodic bundle polling. For best performance, only
293+
trigger the bundle plugin when you know that new bundles are available.
294+
295+
```go
296+
options := sdk.Options{
297+
Config: bytes.NewReader(config),
298+
Logger: logger,
299+
Ready: make(chan struct{}), // <-- needed or else sdk.New will block
300+
}
301+
302+
opa, err := sdk.New(ctx, options)
303+
if err != nil {
304+
log.Fatal(err)
305+
}
306+
defer opa.Stop(ctx)
307+
308+
bundle, ok := opa.Plugin("bundle").(*bundle.Plugin)
309+
if !ok {
310+
log.Fatal("bundle plugin not found")
311+
}
312+
313+
err = bundle.Trigger(ctx)
314+
if err != nil {
315+
log.Fatal(err)
316+
}
317+
```
281318

282319
### Integrating with the Go API
283320

0 commit comments

Comments
 (0)