build(deps): bump github.com/open-policy-agent/opa from 0.59.0 to 0.60.0
Bumps [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) from 0.59.0 to 0.60.0. - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v0.59.0...v0.60.0) --- updated-dependencies: - dependency-name: github.com/open-policy-agent/opa dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
committed by
Ralf Haferkamp
parent
b62ba69bba
commit
f989854f0a
+103
@@ -8,9 +8,11 @@ package plugins
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
mr "math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/open-policy-agent/opa/internal/report"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
|
||||
@@ -148,6 +150,9 @@ const (
|
||||
DefaultTriggerMode TriggerMode = "periodic"
|
||||
)
|
||||
|
||||
// default interval between OPA report uploads
|
||||
var defaultUploadIntervalSec = int64(3600)
|
||||
|
||||
// Status has a Plugin's current status plus an optional Message.
|
||||
type Status struct {
|
||||
State State `json:"state"`
|
||||
@@ -198,8 +203,13 @@ type Manager struct {
|
||||
tracerProvider *trace.TracerProvider
|
||||
distributedTacingOpts tracing.Options
|
||||
registeredNDCacheTriggers []func(bool)
|
||||
registeredTelemetryGatherers map[string]report.Gatherer
|
||||
bootstrapConfigLabels map[string]string
|
||||
hooks hooks.Hooks
|
||||
enableTelemetry bool
|
||||
reporter *report.Reporter
|
||||
opaReportNotifyCh chan struct{}
|
||||
stop chan chan struct{}
|
||||
}
|
||||
|
||||
type managerContextKey string
|
||||
@@ -385,6 +395,21 @@ func WithHooks(hs hooks.Hooks) func(*Manager) {
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnableTelemetry controls whether OPA will send telemetry reports to an external service.
|
||||
func WithEnableTelemetry(enableTelemetry bool) func(*Manager) {
|
||||
return func(m *Manager) {
|
||||
m.enableTelemetry = enableTelemetry
|
||||
}
|
||||
}
|
||||
|
||||
// WithTelemetryGatherers allows registration of telemetry gatherers which enable injection of additional data in the
|
||||
// telemetry report
|
||||
func WithTelemetryGatherers(gs map[string]report.Gatherer) func(*Manager) {
|
||||
return func(m *Manager) {
|
||||
m.registeredTelemetryGatherers = gs
|
||||
}
|
||||
}
|
||||
|
||||
// New creates a new Manager using config.
|
||||
func New(raw []byte, id string, store storage.Store, opts ...func(*Manager)) (*Manager, error) {
|
||||
|
||||
@@ -453,6 +478,27 @@ func New(raw []byte, id string, store storage.Store, opts ...func(*Manager)) (*M
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if m.enableTelemetry {
|
||||
reporter, err := report.New(id, report.Options{Logger: m.logger})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.reporter = reporter
|
||||
|
||||
m.reporter.RegisterGatherer("min_compatible_version", func(_ context.Context) (any, error) {
|
||||
var minimumCompatibleVersion string
|
||||
if m.compiler != nil && m.compiler.Required != nil {
|
||||
minimumCompatibleVersion, _ = m.compiler.Required.MinimumCompatibleVersion()
|
||||
}
|
||||
return minimumCompatibleVersion, nil
|
||||
})
|
||||
|
||||
// register any additional gatherers
|
||||
for k, g := range m.registeredTelemetryGatherers {
|
||||
m.reporter.RegisterGatherer(k, g)
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -469,6 +515,12 @@ func (m *Manager) Init(ctx context.Context) error {
|
||||
Context: storage.NewContext(),
|
||||
}
|
||||
|
||||
if m.enableTelemetry {
|
||||
m.opaReportNotifyCh = make(chan struct{})
|
||||
m.stop = make(chan chan struct{})
|
||||
go m.sendOPAUpdateLoop(ctx)
|
||||
}
|
||||
|
||||
err := storage.Txn(ctx, m.Store, params, func(txn storage.Transaction) error {
|
||||
|
||||
result, err := initload.InsertAndCompile(ctx, initload.InsertAndCompileOptions{
|
||||
@@ -497,6 +549,12 @@ func (m *Manager) Init(ctx context.Context) error {
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if m.stop != nil {
|
||||
done := make(chan struct{})
|
||||
m.stop <- done
|
||||
<-done
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -673,6 +731,12 @@ func (m *Manager) Stop(ctx context.Context) {
|
||||
m.logger.Error("Error closing store: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if m.stop != nil {
|
||||
done := make(chan struct{})
|
||||
m.stop <- done
|
||||
<-done
|
||||
}
|
||||
}
|
||||
|
||||
// Reconfigure updates the configuration on the manager.
|
||||
@@ -817,6 +881,11 @@ func (m *Manager) onCommit(ctx context.Context, txn storage.Transaction, event s
|
||||
|
||||
if compiler != nil {
|
||||
m.setCompiler(compiler)
|
||||
|
||||
if m.enableTelemetry && event.PolicyChanged() {
|
||||
m.opaReportNotifyCh <- struct{}{}
|
||||
}
|
||||
|
||||
for _, f := range m.registeredTriggers {
|
||||
f(txn)
|
||||
}
|
||||
@@ -982,3 +1051,37 @@ func (m *Manager) RegisterNDCacheTrigger(trigger func(bool)) {
|
||||
defer m.mtx.Unlock()
|
||||
m.registeredNDCacheTriggers = append(m.registeredNDCacheTriggers, trigger)
|
||||
}
|
||||
|
||||
func (m *Manager) sendOPAUpdateLoop(ctx context.Context) {
|
||||
ticker := time.NewTicker(time.Duration(int64(time.Second) * defaultUploadIntervalSec))
|
||||
mr.New(mr.NewSource(time.Now().UnixNano()))
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
var opaReportNotify bool
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-m.opaReportNotifyCh:
|
||||
opaReportNotify = true
|
||||
case <-ticker.C:
|
||||
ticker.Stop()
|
||||
|
||||
if opaReportNotify {
|
||||
opaReportNotify = false
|
||||
_, err := m.reporter.SendReport(ctx)
|
||||
if err != nil {
|
||||
m.logger.WithFields(map[string]interface{}{"err": err}).Debug("Unable to send OPA telemetry report.")
|
||||
}
|
||||
}
|
||||
|
||||
newInterval := mr.Int63n(defaultUploadIntervalSec) + defaultUploadIntervalSec
|
||||
ticker = time.NewTicker(time.Duration(int64(time.Second) * newInterval))
|
||||
case done := <-m.stop:
|
||||
cancel()
|
||||
ticker.Stop()
|
||||
done <- struct{}{}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user