-
Notifications
You must be signed in to change notification settings - Fork 24
/
flow-control-components.go
86 lines (73 loc) · 2.86 KB
/
flow-control-components.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
package circuitfactory
import (
"fmt"
"go.uber.org/fx"
policylangv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
loadscheduler "github.com/fluxninja/aperture/v2/pkg/policies/controlplane/components/flowcontrol/load-scheduler"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/components/flowcontrol/regulator"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
)
// newFlowControlNestedAndOptions creates parent and leaf components and their fx options for a component stack spec.
func newFlowControlNestedAndOptions(
flowControlComponentProto *policylangv1.FlowControl,
componentID runtime.ComponentID,
policyReadAPI iface.Policy,
) (Tree, []*runtime.ConfiguredComponent, fx.Option, error) {
retErr := func(err error) (Tree, []*runtime.ConfiguredComponent, fx.Option, error) {
return Tree{}, nil, nil, err
}
loadSchedulerProto := &policylangv1.LoadScheduler{}
isLoadScheduler := false
if proto := flowControlComponentProto.GetLoadScheduler(); proto != nil {
loadSchedulerProto = proto
isLoadScheduler = true
}
adaptiveLoadSchedulerProto := &policylangv1.AdaptiveLoadScheduler{}
isAdaptiveLoadScheduler := false
if proto := flowControlComponentProto.GetAdaptiveLoadScheduler(); proto != nil {
adaptiveLoadSchedulerProto = proto
isAdaptiveLoadScheduler = true
}
loadRampProto := &policylangv1.LoadRamp{}
isLoadRamp := false
if proto := flowControlComponentProto.GetLoadRamp(); proto != nil {
loadRampProto = proto
isLoadRamp = true
}
// Factory parser to determine what kind of composite component to create
if isLoadScheduler {
var options []fx.Option
// sync config
configSyncOptions, err := loadscheduler.NewConfigSyncOptions(loadSchedulerProto,
componentID,
policyReadAPI)
if err != nil {
return retErr(err)
}
options = append(options, configSyncOptions)
nestedCircuit, err := loadscheduler.ParseLoadScheduler(loadSchedulerProto, componentID, policyReadAPI)
if err != nil {
return retErr(err)
}
tree, configuredComponents, nestedOptions, err := ParseNestedCircuit(componentID, nestedCircuit, policyReadAPI)
if err != nil {
return retErr(err)
}
options = append(options, nestedOptions)
return tree, configuredComponents, fx.Options(options...), nil
} else if isAdaptiveLoadScheduler {
nestedCircuit, err := loadscheduler.ParseAdaptiveLoadScheduler(adaptiveLoadSchedulerProto)
if err != nil {
return retErr(err)
}
return ParseNestedCircuit(componentID, nestedCircuit, policyReadAPI)
} else if isLoadRamp {
nestedCircuit, err := regulator.ParseLoadRamp(loadRampProto)
if err != nil {
return retErr(err)
}
return ParseNestedCircuit(componentID, nestedCircuit, policyReadAPI)
}
return retErr(fmt.Errorf("unsupported/missing component type, proto: %+v", flowControlComponentProto))
}