/
component-factory.go
190 lines (174 loc) · 7.43 KB
/
component-factory.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
package circuitfactory
import (
"fmt"
"go.uber.org/fx"
policylangv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/policy/language/v1"
"github.com/fluxninja/aperture/pkg/mapstruct"
"github.com/fluxninja/aperture/pkg/policies/controlplane/components"
"github.com/fluxninja/aperture/pkg/policies/controlplane/components/controller"
"github.com/fluxninja/aperture/pkg/policies/controlplane/components/flowcontrol/rate"
"github.com/fluxninja/aperture/pkg/policies/controlplane/components/flowcontrol/regulator"
"github.com/fluxninja/aperture/pkg/policies/controlplane/components/query/promql"
"github.com/fluxninja/aperture/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/pkg/policies/controlplane/runtime"
)
// FactoryModule for component factory run via the main app.
func FactoryModule() fx.Option {
return fx.Options(
promql.Module(),
)
}
// FactoryModuleForPolicyApp for component factory run via the policy app. For singletons in the Policy scope.
func FactoryModuleForPolicyApp(circuitAPI runtime.CircuitAPI) fx.Option {
return fx.Options(
autoScaleModuleForPolicyApp(circuitAPI),
promql.ModuleForPolicyApp(circuitAPI),
)
}
// NewComponentAndOptions creates parent and leaf components and their fx options for a component spec.
func NewComponentAndOptions(
componentProto *policylangv1.Component,
componentID runtime.ComponentID,
policyReadAPI iface.Policy,
) (Tree, []*runtime.ConfiguredComponent, fx.Option, error) {
var ctor componentConstructor
switch config := componentProto.Component.(type) {
case *policylangv1.Component_GradientController:
ctor = mkCtor(config.GradientController, controller.NewGradientControllerAndOptions)
case *policylangv1.Component_Ema:
ctor = mkCtor(config.Ema, components.NewEMAAndOptions)
case *policylangv1.Component_Sma:
ctor = mkCtor(config.Sma, components.NewSMAAndOptions)
case *policylangv1.Component_ArithmeticCombinator:
ctor = mkCtor(config.ArithmeticCombinator, components.NewArithmeticCombinatorAndOptions)
case *policylangv1.Component_Variable:
ctor = mkCtor(config.Variable, components.NewVariableAndOptions)
case *policylangv1.Component_Decider:
ctor = mkCtor(config.Decider, components.NewDeciderAndOptions)
case *policylangv1.Component_Switcher:
ctor = mkCtor(config.Switcher, components.NewSwitcherAndOptions)
case *policylangv1.Component_UnaryOperator:
ctor = mkCtor(config.UnaryOperator, components.NewUnaryOperatorAndOptions)
case *policylangv1.Component_Max:
ctor = mkCtor(config.Max, components.NewMaxAndOptions)
case *policylangv1.Component_Min:
ctor = mkCtor(config.Min, components.NewMinAndOptions)
case *policylangv1.Component_Extrapolator:
ctor = mkCtor(config.Extrapolator, components.NewExtrapolatorAndOptions)
case *policylangv1.Component_FirstValid:
ctor = mkCtor(config.FirstValid, components.NewFirstValidAndOptions)
case *policylangv1.Component_Alerter:
ctor = mkCtor(config.Alerter, components.NewAlerterAndOptions)
case *policylangv1.Component_Integrator:
ctor = mkCtor(config.Integrator, components.NewIntegratorAndOptions)
case *policylangv1.Component_Differentiator:
ctor = mkCtor(config.Differentiator, components.NewDifferentiatorAndOptions)
case *policylangv1.Component_And:
ctor = mkCtor(config.And, components.NewAndAndOptions)
case *policylangv1.Component_Or:
ctor = mkCtor(config.Or, components.NewOrAndOptions)
case *policylangv1.Component_Inverter:
ctor = mkCtor(config.Inverter, components.NewInverterAndOptions)
case *policylangv1.Component_PulseGenerator:
ctor = mkCtor(config.PulseGenerator, components.NewPulseGeneratorAndOptions)
case *policylangv1.Component_Holder:
ctor = mkCtor(config.Holder, components.NewHolderAndOptions)
case *policylangv1.Component_SignalGenerator:
ctor = mkCtor(config.SignalGenerator, components.NewSignalGeneratorAndOptions)
case *policylangv1.Component_NestedSignalIngress:
ctor = mkCtor(config.NestedSignalIngress, components.NewNestedSignalIngressAndOptions)
case *policylangv1.Component_NestedSignalEgress:
ctor = mkCtor(config.NestedSignalEgress, components.NewNestedSignalEgressAndOptions)
case *policylangv1.Component_NestedCircuit:
return ParseNestedCircuit(componentID, config.NestedCircuit, policyReadAPI)
case *policylangv1.Component_Query:
query := componentProto.GetQuery()
switch queryConfig := query.Component.(type) {
case *policylangv1.Query_Promql:
ctor = mkCtor(queryConfig.Promql, promql.NewPromQLAndOptions)
}
case *policylangv1.Component_FlowControl:
flowControl := componentProto.GetFlowControl()
switch flowControlConfig := flowControl.Component.(type) {
case *policylangv1.FlowControl_RateLimiter:
ctor = mkCtor(flowControlConfig.RateLimiter, rate.NewRateLimiterAndOptions)
case *policylangv1.FlowControl_FlowRegulator:
// Convert from *policylangv1.FlowControl_FlowRegulator to *policylangv1.FlowControl_Regulator
flowRegulatorProto := flowControl.GetFlowRegulator()
regulatorProto := &policylangv1.Regulator{}
err := convertOldComponentToNew(flowRegulatorProto, regulatorProto, nil)
if err != nil {
return Tree{}, nil, nil, err
}
ctor = mkCtor(regulatorProto, regulator.NewRegulatorAndOptions)
case *policylangv1.FlowControl_Regulator:
ctor = mkCtor(flowControlConfig.Regulator, regulator.NewRegulatorAndOptions)
default:
return newFlowControlCompositeAndOptions(flowControl, componentID, policyReadAPI)
}
case *policylangv1.Component_AutoScale:
autoScale := componentProto.GetAutoScale()
return newAutoScaleCompositeAndOptions(autoScale, componentID, policyReadAPI)
default:
return Tree{}, nil, nil, fmt.Errorf("unknown component type: %T", config)
}
component, config, option, err := ctor(componentID.String(), policyReadAPI)
if err != nil {
return Tree{}, nil, nil, err
}
configuredComponent, err := prepareComponent(component, config, componentID, true)
if err != nil {
return Tree{}, nil, nil, err
}
return Tree{Node: configuredComponent}, []*runtime.ConfiguredComponent{configuredComponent}, option, nil
}
type componentConstructor func(
componentID string,
policyReadAPI iface.Policy,
) (runtime.Component, any, fx.Option, error)
func mkCtor[Config any, Comp runtime.Component](
config *Config,
origCtor func(*Config, string, iface.Policy) (Comp, fx.Option, error),
) componentConstructor {
return func(componentID string, policy iface.Policy) (runtime.Component, any, fx.Option, error) {
comp, opt, err := origCtor(config, componentID, policy)
return comp, config, opt, err
}
}
func prepareComponent(
component runtime.Component,
config any,
componentID runtime.ComponentID,
doParsePortMapping bool,
) (*runtime.ConfiguredComponent, error) {
subCircuitID, ok := componentID.ParentID()
if !ok {
return nil, fmt.Errorf("component %s is not in a circuit", componentID.String())
}
return prepareComponentInCircuit(component, config, componentID, subCircuitID, doParsePortMapping)
}
func prepareComponentInCircuit(
component runtime.Component,
config any,
componentID runtime.ComponentID,
subCircuitID runtime.ComponentID,
doParsePortMapping bool,
) (*runtime.ConfiguredComponent, error) {
mapStruct, err := mapstruct.EncodeObject(config)
if err != nil {
return nil, err
}
ports := runtime.NewPortMapping()
if doParsePortMapping {
ports, err = runtime.PortsFromComponentConfig(mapStruct, subCircuitID.String())
if err != nil {
return nil, err
}
}
return &runtime.ConfiguredComponent{
Component: component,
PortMapping: ports,
Config: mapStruct,
ComponentID: componentID,
}, nil
}