forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_sorter.go
144 lines (126 loc) · 3.8 KB
/
policy_sorter.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
// Copyright (c) 2016-2017 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package calc
import (
"fmt"
"sort"
log "github.com/Sirupsen/logrus"
"github.com/projectcalico/libcalico-go/lib/backend/api"
"github.com/projectcalico/libcalico-go/lib/backend/model"
)
type PolicySorter struct {
tier *tierInfo
}
func NewPolicySorter() *PolicySorter {
return &PolicySorter{
tier: &tierInfo{
Name: "default",
Policies: make(map[model.PolicyKey]*model.Policy),
},
}
}
func (poc *PolicySorter) OnUpdate(update api.Update) (dirty bool) {
switch key := update.Key.(type) {
case model.PolicyKey:
oldPolicy := poc.tier.Policies[key]
if update.Value != nil {
newPolicy := update.Value.(*model.Policy)
if oldPolicy == nil ||
oldPolicy.Order != newPolicy.Order ||
oldPolicy.DoNotTrack != newPolicy.DoNotTrack {
dirty = true
}
poc.tier.Policies[key] = newPolicy
} else {
if oldPolicy != nil {
delete(poc.tier.Policies, key)
dirty = true
}
}
}
return
}
func (poc *PolicySorter) Sorted() *tierInfo {
tierInfo := poc.tier
tierInfo.OrderedPolicies = make([]PolKV, 0, len(tierInfo.Policies))
for k, v := range tierInfo.Policies {
tierInfo.OrderedPolicies = append(tierInfo.OrderedPolicies, PolKV{Key: k, Value: v})
}
// Note: using explicit Debugf() here rather than WithFields(); we want the []PolKV slice
// to be stringified with %v rather than %#v (as used by WithField()).
log.Debugf("Order before sorting: %v", tierInfo.OrderedPolicies)
sort.Sort(PolicyByOrder(tierInfo.OrderedPolicies))
log.Debugf("Order after sorting: %v", tierInfo.OrderedPolicies)
return tierInfo
}
type PolKV struct {
Key model.PolicyKey
Value *model.Policy
}
func (p PolKV) String() string {
orderStr := "nil policy"
if p.Value != nil {
if p.Value.Order != nil {
orderStr = fmt.Sprintf("%v", *p.Value.Order)
} else {
orderStr = "default"
}
}
return fmt.Sprintf("%s(%s)", p.Key.Name, orderStr)
}
type PolicyByOrder []PolKV
func (a PolicyByOrder) Len() int { return len(a) }
func (a PolicyByOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a PolicyByOrder) Less(i, j int) bool {
bothNil := a[i].Value.Order == nil && a[j].Value.Order == nil
bothSet := a[i].Value.Order != nil && a[j].Value.Order != nil
ordersEqual := bothNil || bothSet && (*a[i].Value.Order == *a[j].Value.Order)
if ordersEqual {
// Use name as tie-break.
result := a[i].Key.Name < a[j].Key.Name
return result
}
// nil order maps to "infinity"
if a[i].Value.Order == nil {
return false
} else if a[j].Value.Order == nil {
return true
}
// Otherwise, use numeric comparison.
return *a[i].Value.Order < *a[j].Value.Order
}
type tierInfo struct {
Name string
Valid bool
Order *float64
Policies map[model.PolicyKey]*model.Policy
OrderedPolicies []PolKV
}
func NewTierInfo(name string) *tierInfo {
return &tierInfo{
Name: name,
Policies: make(map[model.PolicyKey]*model.Policy),
}
}
func (t tierInfo) String() string {
policies := make([]string, len(t.OrderedPolicies))
for ii, pol := range t.OrderedPolicies {
untracked := "t"
if pol.Value != nil && pol.Value.DoNotTrack {
untracked = "u"
}
policies[ii] = fmt.Sprintf("%v(%v)", pol.Key.Name, untracked)
}
return fmt.Sprintf("%v -> %v", t.Name, policies)
}