-
Notifications
You must be signed in to change notification settings - Fork 19
/
builder_view_policy.go
58 lines (51 loc) · 1.79 KB
/
builder_view_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
package visualization
import (
"github.com/Aptomi/aptomi/pkg/lang"
"github.com/Aptomi/aptomi/pkg/runtime"
)
// PolicyCfg defines graph generation parameters for Policy
type PolicyCfg struct {
showServiceComponentsOnlyForTopLevel bool
showServiceComponents bool
}
// PolicyCfgDefault is default graph generation parameters for Policy
var PolicyCfgDefault = &PolicyCfg{
showServiceComponentsOnlyForTopLevel: true,
showServiceComponents: true,
}
// Policy produces just a policy graph without showing any resolution data
func (b *GraphBuilder) Policy(cfg *PolicyCfg) *Graph {
// we need to find all top-level contracts
edgesIn := make(map[string]int)
for _, contractObj := range b.policy.GetObjectsByKind(lang.ContractObject.Kind) {
contract := contractObj.(*lang.Contract)
b.findEdgesIn(contract, edgesIn)
}
// trace all top-level contracts
for _, contractObj := range b.policy.GetObjectsByKind(lang.ContractObject.Kind) {
contract := contractObj.(*lang.Contract)
if edgesIn[runtime.KeyForStorable(contract)] <= 0 {
b.traceContract(contract, nil, "", 0, cfg)
}
}
return b.graph
}
func (b *GraphBuilder) findEdgesIn(contract *lang.Contract, edgesIn map[string]int) {
for _, context := range contract.Contexts {
serviceObj, errService := b.policy.GetObject(lang.ServiceObject.Kind, context.Allocation.Service, contract.Namespace)
if errService != nil {
continue
}
service := serviceObj.(*lang.Service)
for _, component := range service.Components {
if len(component.Contract) > 0 {
contractObjNew, errContract := b.policy.GetObject(lang.ContractObject.Kind, component.Contract, service.Namespace)
if errContract != nil {
continue
}
contract := contractObjNew.(*lang.Contract)
edgesIn[runtime.KeyForStorable(contract)]++
}
}
}
}