-
Notifications
You must be signed in to change notification settings - Fork 25
/
policy.go
339 lines (296 loc) · 11 KB
/
policy.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package controlplane
import (
"context"
"encoding/base64"
"fmt"
"math"
"time"
goObjectHash "github.com/benlaurie/objecthash/go/objecthash"
"go.uber.org/fx"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
policylangv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
policysyncv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/sync/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/jobs"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/circuitfactory"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/resources/classifier"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/resources/fluxmeter"
inframeters "github.com/fluxninja/aperture/v2/pkg/policies/controlplane/resources/infra-meters"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
"github.com/fluxninja/aperture/v2/pkg/status"
)
// policyModule returns Fx options of Policy for the Main App.
func policyModule() fx.Option {
return fx.Options(
circuitfactory.Module(),
runtime.BackgroundSchedulerModule(),
)
}
// Policy invokes the Circuit runtime at tick frequency.
type Policy struct {
iface.PolicyBase
// status registry
registry status.Registry
// Circuit
circuit *runtime.Circuit
// job group
circuitJobGroup *jobs.JobGroup
// Job name
jobName string
// Evaluation interval determines how often the Circuit gets executed
evaluationInterval time.Duration
}
// Make sure Policy complies with PolicyAPI interface.
var _ iface.Policy = (*Policy)(nil)
// newPolicyOptions creates a new Policy object and returns its Fx options for the per Policy App.
func newPolicyOptions(wrapperMessage *policysyncv1.PolicyWrapper, registry status.Registry) (fx.Option, error) {
// List of options for the policy.
policyOptions := []fx.Option{}
policy, compiledCircuit, partialPolicyOption, err := compilePolicyWrapper(wrapperMessage, registry)
if err != nil {
return nil, err
}
policyOptions = append(policyOptions, partialPolicyOption)
policyOptions = append(policyOptions, fx.Supply(
fx.Annotate(policy, fx.As(new(iface.Policy))),
))
// Create circuit
circuit, circuitOption := runtime.NewCircuitAndOptions(compiledCircuit.Components(), policy)
policyOptions = append(policyOptions, circuitOption)
policyOptions = append(policyOptions, circuitfactory.FactoryModuleForPolicyApp(circuit))
policyOptions = append(policyOptions, runtime.BackgroundSchedulerModuleForPolicyApp(circuit))
policyOptions = append(policyOptions, fx.Supply(fx.Annotate(circuit, fx.As(new(runtime.CircuitSuperAPI)))))
policy.circuit = circuit
return fx.Options(policyOptions...), nil
}
// CompilePolicy takes policyMessage and returns a compiled policy. This is a helper method for standalone consumption of policy compiler.
func CompilePolicy(policyMessage *policylangv1.Policy, policyName string, registry status.Registry) (*circuitfactory.Circuit, error) {
wrapperMessage, err := hashAndPolicyWrap(policyMessage, policyName)
if err != nil {
return nil, err
}
_, circuit, _, err := compilePolicyWrapper(wrapperMessage, registry)
if err != nil {
return nil, err
}
return circuit, nil
}
// compilePolicyWrapper takes policyProto and returns a compiled policy.
func compilePolicyWrapper(wrapperMessage *policysyncv1.PolicyWrapper, registry status.Registry) (*Policy, *circuitfactory.Circuit, fx.Option, error) {
if wrapperMessage == nil {
return nil, nil, nil, fmt.Errorf("nil policy wrapper message")
}
policy := &Policy{
PolicyBase: wrapperMessage.GetCommonAttributes(),
registry: registry,
}
// Get Policy Proto
policyProto := wrapperMessage.GetPolicy()
if policyProto == nil {
return nil, nil, nil, fmt.Errorf("nil policy proto")
}
var resourceOptions []fx.Option
resources := policyProto.GetResources()
if resources != nil {
flowControl := resources.GetFlowControl()
if flowControl != nil {
// Initialize flux meters
fluxMeters := flowControl.GetFluxMeters()
for name, fluxMeterProto := range fluxMeters {
fluxMeterOption, err := fluxmeter.NewFluxMeterOptions(name, fluxMeterProto, policy)
if err != nil {
return nil, nil, nil, err
}
resourceOptions = append(resourceOptions, fluxMeterOption)
}
// Initialize classifiers
classifiers := flowControl.GetClassifiers()
for index, classifierProto := range classifiers {
classifierOption, err := classifier.NewClassifierOptions(index, classifierProto, policy)
if err != nil {
return nil, nil, nil, err
}
resourceOptions = append(resourceOptions, classifierOption)
}
}
infraMeters := resources.GetInfraMeters()
// Deprecated: v3.0.0.
telemetryCollectors := resources.GetTelemetryCollectors()
for _, tc := range telemetryCollectors {
if infraMeters == nil {
infraMeters = make(map[string]*policylangv1.InfraMeter)
}
for name, infraMeter := range tc.GetInfraMeters() {
if _, exists := infraMeters[name]; !exists {
infraMeter.AgentGroup = tc.GetAgentGroup()
infraMeters[name] = infraMeter
} else {
return nil, nil, nil, fmt.Errorf("duplicate infra meter name '%s' found in telemetry_collectors and infra_meters", name)
}
}
}
if infraMeters != nil {
tcOption, err := inframeters.NewInfraMetersOptions(infraMeters, policy)
if err != nil {
return nil, nil, nil, err
}
resourceOptions = append(resourceOptions, tcOption)
}
}
rootTree, rootTreeErr := circuitfactory.RootTree(&policylangv1.Circuit{})
if rootTreeErr != nil {
return nil, nil, nil, rootTreeErr
}
compiledCircuit := &circuitfactory.Circuit{
LeafComponents: make([]*runtime.ConfiguredComponent, 0),
Tree: rootTree,
}
partialCircuitOption := fx.Options()
var err error
if policyProto.GetCircuit() != nil {
// Read evaluation interval
policy.evaluationInterval = policyProto.GetCircuit().GetEvaluationInterval().AsDuration()
compiledCircuit, partialCircuitOption, err = circuitfactory.CompileFromProto(policyProto, policy)
if err != nil {
return nil, nil, nil, err
}
}
return policy, compiledCircuit, fx.Options(
fx.Options(resourceOptions...),
partialCircuitOption,
fx.Invoke(
policy.setupCircuitJob,
policy.setupDynamicConfig,
),
), nil
}
// GetEvaluationInterval returns the ID of the policy.
func (policy *Policy) GetEvaluationInterval() time.Duration {
return policy.evaluationInterval
}
// TicksInDurationPb returns the number of ticks in duration pb. If duration pb is nil, it returns 1.
func (policy *Policy) TicksInDurationPb(duration *durationpb.Duration) int {
if duration == nil {
return 1
}
return policy.TicksInDuration(duration.AsDuration())
}
// TicksInDuration returns the number of ticks in duration.
func (policy *Policy) TicksInDuration(duration time.Duration) int {
// period of tick
return int(math.Ceil(float64(duration) / float64(policy.evaluationInterval)))
}
func (policy *Policy) setupCircuitJob(lifecycle fx.Lifecycle, circuitJobGroup *jobs.JobGroup) error {
if policy.evaluationInterval <= 0 {
return nil
}
logger := policy.GetStatusRegistry().GetLogger()
// Job name
policy.jobName = fmt.Sprintf("Policy-%s", policy.GetPolicyName())
// Job group
policy.circuitJobGroup = circuitJobGroup
lifecycle.Append(fx.Hook{
OnStart: func(_ context.Context) error {
// Create a job that runs every tick i.e. evaluation_interval. Set timeout duration to half of evaluation_interval
job := jobs.NewBasicJob(policy.jobName, policy.executeTick)
executionPeriod := config.MakeDuration(policy.evaluationInterval)
executionTimeout := config.MakeDuration(time.Millisecond * 100)
jobConfig := jobs.JobConfig{
InitiallyHealthy: true,
ExecutionPeriod: executionPeriod,
ExecutionTimeout: executionTimeout,
}
// Register job with registry
err := policy.circuitJobGroup.RegisterJob(job, jobConfig)
if err != nil {
logger.Error().Err(err).Str("job", policy.jobName).Msg("Error registering job")
return err
}
return nil
},
OnStop: func(_ context.Context) error {
// Deregister job from registry
_ = policy.circuitJobGroup.DeregisterJob(policy.jobName)
return nil
},
})
return nil
}
func (policy *Policy) setupDynamicConfig(dynamicConfigWatcher notifiers.Watcher, lifecycle fx.Lifecycle) error {
unmarshaller, _ := config.KoanfUnmarshallerConstructor{}.NewKoanfUnmarshaller([]byte{})
unmarshalNotifier, err := notifiers.NewUnmarshalKeyNotifier(
notifiers.Key(policy.GetPolicyName()),
unmarshaller,
policy.dynamicConfigUpdate,
)
if err != nil {
return err
}
lifecycle.Append(fx.Hook{
OnStart: func(_ context.Context) error {
return dynamicConfigWatcher.AddKeyNotifier(unmarshalNotifier)
},
OnStop: func(_ context.Context) error {
return dynamicConfigWatcher.RemoveKeyNotifier(unmarshalNotifier)
},
})
return nil
}
func (policy *Policy) dynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
policy.circuit.DynamicConfigUpdate(event, unmarshaller)
}
func (policy *Policy) executeTick(jobCtxt context.Context) (proto.Message, error) {
// Get JobInfo
jobInfo, err := policy.circuitJobGroup.JobInfo(policy.jobName)
if err != nil {
return nil, err
}
tickInfo := runtime.NewTickInfo(jobInfo.LastExecuteTime,
jobInfo.ExecuteCount,
policy.evaluationInterval)
// Execute Circuit
err = policy.circuit.Execute(tickInfo)
// TODO: return tick info (publish to health framework) instead of returning nil proto.Message
return nil, err
}
// GetStatusRegistry returns the status registry of the policy.
func (policy *Policy) GetStatusRegistry() status.Registry {
return policy.registry
}
// hashAndPolicyWrap wraps a proto message with a config properties wrapper and hashes it.
func hashAndPolicyWrap(policyMessage *policylangv1.Policy, policyName string) (*policysyncv1.PolicyWrapper, error) {
hash, err := HashPolicy(policyMessage)
if err != nil {
return nil, err
}
return &policysyncv1.PolicyWrapper{
Policy: policyMessage,
CommonAttributes: &policysyncv1.CommonAttributes{
PolicyName: policyName,
PolicyHash: hash,
},
}, nil
}
// HashPolicy returns hash of the policy.
func HashPolicy(policy *policylangv1.Policy) (string, error) {
// FIXME: The "Deterministic" is still not deterministic enough for our
// purposes – output may change with different version of aperture.
dat, err := proto.MarshalOptions{Deterministic: true}.Marshal(policy)
if err != nil {
log.Error().Err(err).Msgf("Failed to marshal proto message %+v", policy)
return "", err
}
log.Trace().Msgf("Policy message: %s", string(dat))
// FIXME: Use sha256.Sum256() directly instead of goObjectHash. This will
// result in different though.
hashBytes, hashErr := goObjectHash.ObjectHash(dat)
if hashErr != nil {
log.Warn().Err(hashErr).Msgf("Failed to hash json serialized proto message %s", string(dat))
return "", hashErr
}
return base64.StdEncoding.EncodeToString(hashBytes[:]), nil
}