forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
84 lines (67 loc) · 2.79 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package download
import (
"fmt"
"time"
"github.com/open-policy-agent/opa/plugins"
)
const (
defaultMinDelaySeconds = int64(60)
defaultMaxDelaySeconds = int64(120)
// deltaBundleMode indicates that OPA supports delta bundle processing
deltaBundleMode = "delta"
// defaultBundleMode indicates that OPA supports snapshot bundle processing
defaultBundleMode = "snapshot"
)
// PollingConfig represents polling configuration for the downloader.
type PollingConfig struct {
MinDelaySeconds *int64 `json:"min_delay_seconds,omitempty"` // min amount of time to wait between successful poll attempts
MaxDelaySeconds *int64 `json:"max_delay_seconds,omitempty"` // max amount of time to wait between poll attempts
LongPollingTimeoutSeconds *int64 `json:"long_polling_timeout_seconds,omitempty"` // max amount of time the server should wait before issuing a timeout if there's no update available
}
// Config represents the configuration for the downloader.
type Config struct {
Trigger *plugins.TriggerMode `json:"trigger,omitempty"`
Polling PollingConfig `json:"polling"`
}
// ValidateAndInjectDefaults checks for configuration errors and ensures all
// values are set on the Config object.
func (c *Config) ValidateAndInjectDefaults() error {
if c.Trigger == nil {
t := plugins.DefaultTriggerMode
c.Trigger = &t
}
switch *c.Trigger {
case plugins.TriggerPeriodic, plugins.TriggerManual:
break
default:
return fmt.Errorf("invalid trigger mode %q (want %q or %q)", *c.Trigger, plugins.TriggerPeriodic, plugins.TriggerManual)
}
min := defaultMinDelaySeconds
max := defaultMaxDelaySeconds
// reject bad min/max values
if c.Polling.MaxDelaySeconds != nil && c.Polling.MinDelaySeconds != nil {
if *c.Polling.MaxDelaySeconds < *c.Polling.MinDelaySeconds {
return fmt.Errorf("max polling delay must be >= min polling delay")
}
min = *c.Polling.MinDelaySeconds
max = *c.Polling.MaxDelaySeconds
} else if c.Polling.MaxDelaySeconds == nil && c.Polling.MinDelaySeconds != nil {
return fmt.Errorf("polling configuration missing 'max_delay_seconds'")
} else if c.Polling.MinDelaySeconds == nil && c.Polling.MaxDelaySeconds != nil {
return fmt.Errorf("polling configuration missing 'min_delay_seconds'")
}
// scale to seconds
minSeconds := int64(time.Duration(min) * time.Second)
c.Polling.MinDelaySeconds = &minSeconds
maxSeconds := int64(time.Duration(max) * time.Second)
c.Polling.MaxDelaySeconds = &maxSeconds
if c.Polling.LongPollingTimeoutSeconds != nil {
if *c.Polling.LongPollingTimeoutSeconds < 1 {
return fmt.Errorf("'long_polling_timeout_seconds' must be at least 1")
}
}
return nil
}