-
Notifications
You must be signed in to change notification settings - Fork 25
/
variable.go
78 lines (66 loc) · 2.78 KB
/
variable.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
package components
import (
"go.uber.org/fx"
policylangv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
)
// Variable is a dynamically configurable variable signal.
type Variable struct {
constantOutput *runtime.ConstantSignal
policyReadAPI iface.Policy
variableProto *policylangv1.Variable
}
// Name implements runtime.Component.
func (*Variable) Name() string { return "Variable" }
// Type implements runtime.Component.
func (*Variable) Type() runtime.ComponentType { return runtime.ComponentTypeSource }
// ShortDescription implements runtime.Component.
func (v *Variable) ShortDescription() string {
return v.constantOutput.Description()
}
// IsActuator implements runtime.Component.
func (*Variable) IsActuator() bool { return false }
// NewConstantSignal creates a variable component with a value that is always valid.
func NewConstantSignal(value float64) runtime.Component {
return &Variable{
constantOutput: &runtime.ConstantSignal{
Value: value,
},
}
}
// NewVariableAndOptions creates a variable components and its fx options.
func NewVariableAndOptions(variableProto *policylangv1.Variable, _ runtime.ComponentID, policyReadAPI iface.Policy) (runtime.Component, fx.Option, error) {
variable := &Variable{
policyReadAPI: policyReadAPI,
variableProto: variableProto,
}
variable.constantOutput = runtime.ConstantSignalFromProto(variableProto.GetConstantOutput())
return variable, fx.Options(), nil
}
// Execute implements runtime.Component.Execute.
func (v *Variable) Execute(inPortReadings runtime.PortToReading, circuitAPI runtime.CircuitAPI) (runtime.PortToReading, error) {
// Always emit the value.
return runtime.PortToReading{
"output": []runtime.Reading{runtime.NewReading(v.constantOutput.Float())},
}, nil
}
// DynamicConfigUpdate finds the dynamic config and syncs the constant value.
func (v *Variable) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
logger := v.policyReadAPI.GetStatusRegistry().GetLogger()
key := v.variableProto.GetConfigKey()
// read dynamic config
if !unmarshaller.IsSet(key) {
v.constantOutput = runtime.ConstantSignalFromProto(v.variableProto.GetConstantOutput())
return
}
constantSignalProto := &policylangv1.ConstantSignal{}
if err := unmarshaller.UnmarshalKey(key, constantSignalProto); err != nil {
logger.Error().Err(err).Msg("Failed to unmarshal dynamic config")
v.constantOutput = runtime.ConstantSignalFromProto(v.variableProto.GetConstantOutput())
return
}
v.constantOutput = runtime.ConstantSignalFromProto(constantSignalProto)
}