This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
246 lines (214 loc) · 5.85 KB
/
utils.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package reconciler
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
klabels "k8s.io/apimachinery/pkg/labels"
gw "sigs.k8s.io/gateway-api/apis/v1alpha2"
)
const (
// NamespaceNameLabel represents that label added automatically to namespaces is newer Kubernetes clusters
NamespaceNameLabel = "kubernetes.io/metadata.name"
)
func routeMatchesListener(listenerName gw.SectionName, sectionName *gw.SectionName) (bool, bool) {
if sectionName == nil {
return true, false
}
return listenerName == *sectionName, true
}
func routeMatchesListenerHostname(listenerHostname *gw.Hostname, hostnames []gw.Hostname) bool {
if listenerHostname == nil || len(hostnames) == 0 {
return true
}
for _, name := range hostnames {
if hostnamesMatch(name, *listenerHostname) {
return true
}
}
return false
}
func hostnamesMatch(a, b gw.Hostname) bool {
if a == "" || a == "*" || b == "" || b == "*" {
// any wildcard always matches
return true
}
if strings.HasPrefix(string(a), "*.") || strings.HasPrefix(string(b), "*.") {
aLabels, bLabels := strings.Split(string(a), "."), strings.Split(string(b), ".")
if len(aLabels) != len(bLabels) {
return false
}
for i := 1; i < len(aLabels); i++ {
if !strings.EqualFold(aLabels[i], bLabels[i]) {
return false
}
}
return true
}
return a == b
}
func routeKindIsAllowedForListener(kinds []gw.RouteGroupKind, route *K8sRoute) bool {
if kinds == nil {
return true
}
gvk := route.GroupVersionKind()
for _, kind := range kinds {
group := gw.GroupName
if kind.Group != nil && *kind.Group != "" {
group = string(*kind.Group)
}
if string(kind.Kind) == gvk.Kind && group == gvk.Group {
return true
}
}
return false
}
func routeAllowedForListenerNamespaces(gatewayNS string, allowedRoutes *gw.AllowedRoutes, route *K8sRoute) (bool, error) {
var namespaceSelector *gw.RouteNamespaces
if allowedRoutes != nil {
// check gateway namespace
namespaceSelector = allowedRoutes.Namespaces
}
// set default is namespace selector is nil
from := gw.NamespacesFromSame
if namespaceSelector != nil && namespaceSelector.From != nil && *namespaceSelector.From != "" {
from = *namespaceSelector.From
}
switch from {
case gw.NamespacesFromAll:
return true, nil
case gw.NamespacesFromSame:
return gatewayNS == route.GetNamespace(), nil
case gw.NamespacesFromSelector:
ns, err := metav1.LabelSelectorAsSelector(namespaceSelector.Selector)
if err != nil {
return false, fmt.Errorf("error parsing label selector: %v", err)
}
return ns.Matches(toNamespaceSet(route.GetNamespace(), route.GetLabels())), nil
}
return false, nil
}
func toNamespaceSet(name string, labels map[string]string) klabels.Labels {
// If namespace label is not set, implicitly insert it to support older Kubernetes versions
if labels[NamespaceNameLabel] == name {
// Already set, avoid copies
return klabels.Set(labels)
}
// First we need a copy to not modify the underlying object
ret := make(map[string]string, len(labels)+1)
for k, v := range labels {
ret[k] = v
}
ret[NamespaceNameLabel] = name
return klabels.Set(ret)
}
func sortParents(parents []gw.RouteParentStatus) []gw.RouteParentStatus {
for _, parent := range parents {
sort.SliceStable(parent.Conditions, func(i, j int) bool {
return asJSON(parent.Conditions[i]) < asJSON(parent.Conditions[j])
})
}
sort.SliceStable(parents, func(i, j int) bool {
return asJSON(parents[i]) < asJSON(parents[j])
})
return parents
}
func asJSON(item interface{}) string {
data, err := json.Marshal(item)
if err != nil {
// everything passed to this internally should be
// serializable, if something is passed to it that
// isn't, just panic since it's a usage error at
// that point
panic(err)
}
return string(data)
}
func parseParent(stringified string) gw.ParentRef {
var ref gw.ParentRef
if err := json.Unmarshal([]byte(stringified), &ref); err != nil {
// everything passed to this internally should be
// deserializable, if something is passed to it that
// isn't, just panic since it's a usage error at
// that point
panic(err)
}
return ref
}
func conditionEqual(a, b metav1.Condition) bool {
if a.Type != b.Type ||
a.ObservedGeneration != b.ObservedGeneration ||
a.Status != b.Status ||
a.Reason != b.Reason ||
a.Message != b.Message {
return false
}
return true
}
func conditionsEqual(a, b []metav1.Condition) bool {
if len(a) != len(b) {
// we have a different number of conditions, so they aren't the same
return false
}
for i, newCondition := range a {
if !conditionEqual(newCondition, b[i]) {
return false
}
}
return true
}
func listenerStatusEqual(a, b gw.ListenerStatus) bool {
if a.Name != b.Name {
return false
}
if !reflect.DeepEqual(a.SupportedKinds, b.SupportedKinds) {
return false
}
if a.AttachedRoutes != b.AttachedRoutes {
return false
}
return conditionsEqual(a.Conditions, b.Conditions)
}
func listenerStatusesEqual(a, b []gw.ListenerStatus) bool {
if len(a) != len(b) {
// we have a different number of conditions, so they aren't the same
return false
}
for i, newStatus := range a {
if !listenerStatusEqual(newStatus, b[i]) {
return false
}
}
return true
}
func parentStatusEqual(a, b gw.RouteParentStatus) bool {
if a.ControllerName != b.ControllerName {
return false
}
if asJSON(a.ParentRef) != asJSON(b.ParentRef) {
return false
}
return conditionsEqual(a.Conditions, b.Conditions)
}
func routeStatusEqual(a, b gw.RouteStatus) bool {
if len(a.Parents) != len(b.Parents) {
return false
}
for i, oldParent := range a.Parents {
if !parentStatusEqual(oldParent, b.Parents[i]) {
return false
}
}
return true
}
func gatewayStatusEqual(a, b gw.GatewayStatus) bool {
if !conditionsEqual(a.Conditions, b.Conditions) {
return false
}
if !listenerStatusesEqual(a.Listeners, b.Listeners) {
return false
}
return true
}