-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
190 lines (162 loc) · 4.49 KB
/
plan.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2018 CyberAgent, Inc. All rights reserved.
// https://github.com/openfresh/external-ips
package plan
import (
"github.com/openfresh/external-ips/firewall/inbound"
)
type Plan struct {
// List of current rules
Current []*inbound.InboundRules
// List of desired rules
Desired []*inbound.InboundRules
// List of changes necessary to move towards desired state
// Populated after calling Calculate()
Changes *Changes
}
type InstanceRule struct {
ProviderID string
RulesName string
}
type ByProviderID []*InstanceRule
func (a ByProviderID) Len() int { return len(a) }
func (a ByProviderID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByProviderID) Less(i, j int) bool { return a[i].ProviderID < a[j].ProviderID }
type Changes struct {
// Rules that need to be created
Create []*inbound.InboundRules
// Rules that need to be updated (current data)
UpdateOld []*inbound.InboundRules
// Rules that need to be updated (desired data)
UpdateNew []*inbound.InboundRules
// Rules that need to be deleted
Delete []*inbound.InboundRules
Set []*InstanceRule
Unset []*InstanceRule
}
type planTable struct {
rows map[string]*planTableRow
}
func newPlanTable() planTable { //TODO: make resolver configurable
return planTable{map[string]*planTableRow{}}
}
type planTableRow struct {
current *inbound.InboundRules
candidate *inbound.InboundRules
}
func (t planTable) addCurrent(r *inbound.InboundRules) {
if _, ok := t.rows[r.Name]; !ok {
t.rows[r.Name] = &planTableRow{}
}
t.rows[r.Name].current = r
}
func (t planTable) addCandidate(r *inbound.InboundRules) {
if _, ok := t.rows[r.Name]; !ok {
t.rows[r.Name] = &planTableRow{}
}
t.rows[r.Name].candidate = r
}
type planTable2 struct {
rows map[string]*planTable2Row
}
func newPlanTable2() planTable2 { //TODO: make resolver configurable
return planTable2{map[string]*planTable2Row{}}
}
type planTable2Row struct {
current *InstanceRule
candidate *InstanceRule
}
func (t planTable2) addCurrent(i *InstanceRule) {
if _, ok := t.rows[i.ProviderID+i.RulesName]; !ok {
t.rows[i.ProviderID+i.RulesName] = &planTable2Row{}
}
t.rows[i.ProviderID+i.RulesName].current = i
}
func (t planTable2) addCandidate(i *InstanceRule) {
if _, ok := t.rows[i.ProviderID+i.RulesName]; !ok {
t.rows[i.ProviderID+i.RulesName] = &planTable2Row{}
}
t.rows[i.ProviderID+i.RulesName].candidate = i
}
func (t planTable) getUpdates() (updateNew []*inbound.InboundRules, updateOld []*inbound.InboundRules) {
for _, row := range t.rows {
if row.current != nil && row.candidate != nil {
if !row.current.Same(row.current) {
updateNew = append(updateNew, row.candidate)
updateOld = append(updateOld, row.current)
}
continue
}
}
return
}
func (t planTable) getCreates() (createList []*inbound.InboundRules) {
for _, row := range t.rows {
if row.current == nil {
createList = append(createList, row.candidate)
}
}
return
}
func (t planTable) getDeletes() (deleteList []*inbound.InboundRules) {
for _, row := range t.rows {
if row.current != nil && row.candidate == nil {
deleteList = append(deleteList, row.current)
}
}
return
}
func (t planTable2) getSets() (setList []*InstanceRule) {
for _, row := range t.rows {
if row.current == nil {
setList = append(setList, row.candidate)
}
}
return
}
func (t planTable2) getUnsets() (unsetList []*InstanceRule) {
for _, row := range t.rows {
if row.current != nil && row.candidate == nil {
unsetList = append(unsetList, row.current)
}
}
return
}
// Calculate computes the actions needed to move current state towards desired
// state. It then passes those changes to the current policy for further
// processing. It returns a copy of Plan with the changes populated.
func (p *Plan) Calculate() *Plan {
t := newPlanTable()
t2 := newPlanTable2()
for _, current := range p.Current {
t.addCurrent(current)
for _, id := range current.ProviderIDs {
ir := InstanceRule{
ProviderID: id,
RulesName: current.Name,
}
t2.addCurrent(&ir)
}
}
for _, desired := range p.Desired {
t.addCandidate(desired)
for _, id := range desired.ProviderIDs {
ir := InstanceRule{
ProviderID: id,
RulesName: desired.Name,
}
t2.addCandidate(&ir)
}
}
changes := &Changes{}
changes.Create = t.getCreates()
changes.Delete = t.getDeletes()
changes.UpdateNew, changes.UpdateOld = t.getUpdates()
changes.Set = t2.getSets()
changes.Unset = t2.getUnsets()
plan := &Plan{
Current: p.Current,
Desired: p.Desired,
Changes: changes,
}
return plan
}