-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
stack.go
114 lines (97 loc) · 3.4 KB
/
stack.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
package core
import (
"github.com/pkg/errors"
"reflect"
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core/graph"
)
// Stack presents a resource graph, where resources can depend on each other.
type Stack interface {
// stackID returns a unique ID for stack.
StackID() StackID
// Add a resource into stack.
AddResource(res Resource) error
// Add a dependency relationship between resources.
AddDependency(dependee Resource, depender Resource) error
// ListResources list all resources for specific type.
// pResourceSlice must be a pointer to a slice of resources, which will be filled.
ListResources(pResourceSlice interface{}) error
// TopologicalTraversal visits resources in stack in topological order.
TopologicalTraversal(visitor ResourceVisitor) error
}
// NewDefaultStack constructs new stack.
func NewDefaultStack(stackID StackID) *defaultStack {
return &defaultStack{
stackID: stackID,
resources: make(map[graph.ResourceUID]Resource),
resourceGraph: graph.NewDefaultResourceGraph(),
}
}
var _ Stack = &defaultStack{}
// default implementation for stack.
type defaultStack struct {
stackID StackID
resources map[graph.ResourceUID]Resource
resourceGraph graph.ResourceGraph
}
func (s *defaultStack) StackID() StackID {
return s.stackID
}
// Add a resource.
func (s *defaultStack) AddResource(res Resource) error {
resUID := s.computeResourceUID(res)
if _, ok := s.resources[resUID]; ok {
return errors.Errorf("resource already exists, type: %v, id: %v", res.Type(), res.ID())
}
s.resources[resUID] = res
s.resourceGraph.AddNode(resUID)
return nil
}
// Add a dependency relationship between resources.
func (s *defaultStack) AddDependency(dependee Resource, depender Resource) error {
dependeeResUID := s.computeResourceUID(dependee)
dependerResUID := s.computeResourceUID(depender)
if _, ok := s.resources[dependeeResUID]; !ok {
return errors.Errorf("dependee resource didn't exists, type: %v, id: %v", dependee.Type(), dependee.ID())
}
if _, ok := s.resources[dependerResUID]; !ok {
return errors.Errorf("depender resource didn't exists, type: %v, id: %v", depender.Type(), depender.ID())
}
s.resourceGraph.AddEdge(dependeeResUID, dependerResUID)
return nil
}
// ListResources list all resources for specific type.
// pResourceSlice must be a pointer to a slice of resources, which will be filled.
func (s *defaultStack) ListResources(pResourceSlice interface{}) error {
v := reflect.ValueOf(pResourceSlice)
if v.Kind() != reflect.Ptr {
return errors.New("pResourceSlice must be pointer to resource slice")
}
v = v.Elem()
if v.Kind() != reflect.Slice {
return errors.New("pResourceSlice must be pointer to resource slice")
}
resType := v.Type().Elem()
var resForType []Resource
for resID, res := range s.resources {
if resID.ResType == resType {
resForType = append(resForType, res)
}
}
v.Set(reflect.MakeSlice(v.Type(), len(resForType), len(resForType)))
for i := range resForType {
v.Index(i).Set(reflect.ValueOf(resForType[i]))
}
return nil
}
func (s *defaultStack) TopologicalTraversal(visitor ResourceVisitor) error {
return graph.TopologicalTraversal(s.resourceGraph, func(uid graph.ResourceUID) error {
return visitor.Visit(s.resources[uid])
})
}
// computeResourceUID returns the UID for resources.
func (s *defaultStack) computeResourceUID(res Resource) graph.ResourceUID {
return graph.ResourceUID{
ResType: reflect.TypeOf(res),
ResID: res.ID(),
}
}