-
Notifications
You must be signed in to change notification settings - Fork 24
/
circuit.go
68 lines (60 loc) · 1.94 KB
/
circuit.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
package circuitfactory
import (
"go.uber.org/fx"
policylangv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
policymonitoringv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/monitoring/v1"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
)
// Circuit is a compiled Circuit
//
// Circuit can also be converted to its graph view.
type Circuit struct {
Tree Tree
LeafComponents []*runtime.ConfiguredComponent
}
// Components returns a list of CompiledComponents, ready to create runtime.Circuit.
func (circuit *Circuit) Components() []*runtime.ConfiguredComponent {
return circuit.LeafComponents
}
// CompileFromProto compiles a protobuf circuit definition into a Circuit.
//
// This is helper for CreateComponents + runtime.Compile.
func CompileFromProto(
policyProto *policylangv1.Policy,
policyReadAPI iface.Policy,
) (*Circuit, fx.Option, error) {
componentsProto := policyProto.GetCircuit().Components
childrenTrees, leafComponents, option, err := CreateComponents(componentsProto, runtime.NewComponentID(runtime.RootComponentID), policyReadAPI)
if err != nil {
return nil, nil, err
}
tree, err := RootTree(policyProto.GetCircuit())
if err != nil {
return nil, nil, err
}
tree.Children = childrenTrees
err = runtime.Compile(
leafComponents,
policyReadAPI.GetStatusRegistry().GetLogger(),
)
if err != nil {
return nil, option, err
}
return &Circuit{
Tree: tree,
LeafComponents: leafComponents,
}, option, nil
}
// CircuitView returns a CircuitView of the circuit.
func (circuit *Circuit) CircuitView() (*policymonitoringv1.CircuitView, error) {
tree, err := circuit.Tree.TreeGraph()
if err != nil {
log.Errorf("Error getting circuit tree: %v", err)
return nil, err
}
return &policymonitoringv1.CircuitView{
Tree: tree,
}, nil
}