-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathorder.go
More file actions
141 lines (126 loc) · 4.19 KB
/
order.go
File metadata and controls
141 lines (126 loc) · 4.19 KB
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
package diff
import (
"github.com/kong/go-database-reconciler/pkg/crud"
"github.com/kong/go-database-reconciler/pkg/types"
)
/*
Root
|
+----------+----------+---------+------------+---------------+--------------+----------+
| | | | | | | |
v v v v v v v v
L1 Service RbacRole Upstream Certificate CACertificate Consumer ---+--- Partial KeySet
Package | | | | | | | |
| v v v | v v | v
L2 | RBACRole Target SNI +-> Service Credentials | Key
| Endpoint | | (7) |
| | | |
| | | |
L3 +---------------------------> Service <---+ +-> Route |
| Version | | |
| | | | |
| | | v |
L4 +----------> Document <---------+ +-> Plugins / <---------+
FilterChains
CustomEntities - DegraphqlRoute
- GraphqlRateLimitingCostDecoration
*/
// dependencyOrder defines the order in which entities will be synced by decK.
// Entities at the same level are processed concurrently.
// Entities at level n will only be processed after all entities at level n-1
// have been processed.
// The processing order for create and update stage is top-down while that
// for delete stage is bottom-up.
var dependencyOrder = [][]types.EntityType{
{
types.ServicePackage,
types.RBACRole,
types.Certificate,
types.CACertificate,
types.Consumer,
types.Vault,
types.License,
types.Partial,
types.KeySet,
},
{
types.ConsumerGroup,
types.RBACEndpointPermission,
types.SNI,
types.Service,
types.Upstream,
types.Key,
types.KeyAuth, types.HMACAuth, types.JWTAuth,
types.BasicAuth, types.OAuth2Cred, types.ACLGroup,
types.MTLSAuth,
},
{
types.ServiceVersion,
types.Route,
types.Target,
types.ConsumerGroupConsumer,
types.ConsumerGroupPlugin,
},
{
types.Plugin,
types.FilterChain,
types.Document,
types.DegraphqlRoute,
types.GraphqlRateLimitingCostDecoration,
},
}
func order() [][]types.EntityType {
return deepCopy(dependencyOrder)
}
func reverseOrder() [][]types.EntityType {
order := deepCopy(dependencyOrder)
return reverse(order)
}
func reverse(src [][]types.EntityType) [][]types.EntityType {
src = deepCopy(src)
i := 0
j := len(src) - 1
for i < j {
temp := src[i]
src[i] = src[j]
src[j] = temp
i++
j--
}
return src
}
func deepCopy(src [][]types.EntityType) [][]types.EntityType {
res := make([][]types.EntityType, len(src))
for i := range src {
res[i] = make([]types.EntityType, len(src[i]))
copy(res[i], src[i])
}
return res
}
func eventsInOrder(events []crud.Event, order [][]types.EntityType) [][]crud.Event {
// kindToLevel maps a Kind to its level in the order to avoid repeated lookups.
kindToLevel := make(map[crud.Kind]int)
// eventsByLevel is a slice of slices of events, where each slice of events is at the same level and can be
// processed concurrently.
eventsByLevel := make([][]crud.Event, len(order))
for _, event := range events {
level, ok := kindToLevel[event.Kind]
if !ok {
level = levelForEvent(event, order)
kindToLevel[event.Kind] = level
}
eventsByLevel[level] = append(eventsByLevel[level], event)
}
return eventsByLevel
}
func levelForEvent(event crud.Event, order [][]types.EntityType) int {
for i, level := range order {
for _, entityType := range level {
if event.Kind == crud.Kind(entityType) {
return i
}
}
}
// This should never happen.
return -1
}