forked from jakubkulhan/ingress-merge
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathingress_bucket.go
133 lines (105 loc) · 3.48 KB
/
ingress_bucket.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
package ingress_merge
import (
"sort"
networkingv1 "k8s.io/api/networking/v1"
types "k8s.io/apimachinery/pkg/types"
)
type IngressBucket struct {
FreeSlots int
Ingresses []networkingv1.Ingress
DestinationIngress *networkingv1.Ingress
}
func GenerateIngressBuckets(origins, destinations []networkingv1.Ingress, maxServices int) []*IngressBucket {
type dependencyKey struct {
name string
uid types.UID
}
bucketsWithDestinationMap := map[dependencyKey]*IngressBucket{}
bucketsWithDestination := []*IngressBucket{}
bucketsWithoutDestination := []*IngressBucket{}
ingressesWithoutDestination := []networkingv1.Ingress{}
originToDestinationMap := map[dependencyKey]*networkingv1.Ingress{}
for i := range destinations {
k := dependencyKey{destinations[i].Name, destinations[i].UID}
bucketsWithDestinationMap[k] = &IngressBucket{
FreeSlots: maxServices,
DestinationIngress: &destinations[i],
}
for _, ownerReference := range destinations[i].OwnerReferences {
if ownerReference.Kind != "Ingress" {
continue
}
k = dependencyKey{ownerReference.Name, ownerReference.UID}
originToDestinationMap[k] = &destinations[i]
}
}
for _, bucket := range bucketsWithDestinationMap {
bucketsWithDestination = append(bucketsWithDestination, bucket)
}
for _, origin := range origins {
k := dependencyKey{origin.Name, origin.UID}
if originToDestinationMap[k] != nil {
destination := originToDestinationMap[k]
destinationKey := dependencyKey{destination.Name, destination.UID}
bucketsWithDestinationMap[destinationKey].Ingresses = append(bucketsWithDestinationMap[destinationKey].Ingresses, origin)
bucketsWithDestinationMap[destinationKey].FreeSlots -= ingressSlots(&origin)
continue
}
ingressesWithoutDestination = append(ingressesWithoutDestination, origin)
}
sort.Slice(ingressesWithoutDestination, func(i, j int) bool {
slotsI := ingressSlots(&ingressesWithoutDestination[i])
slotsJ := ingressSlots(&ingressesWithoutDestination[j])
if slotsI != slotsJ {
return slotsI < slotsJ
}
return ingressesWithoutDestination[i].ObjectMeta.CreationTimestamp.After(ingressesWithoutDestination[j].ObjectMeta.CreationTimestamp.Time)
})
sort.Slice(bucketsWithDestination, func(i, j int) bool {
return bucketsWithDestination[i].FreeSlots > bucketsWithDestination[j].FreeSlots
})
var currentBucket *IngressBucket
reuseBucketsWithAddrPos := -1
nextBucket := func() {
for reuseBucketsWithAddrPos < len(bucketsWithDestination)-1 {
reuseBucketsWithAddrPos++
currentBucket = bucketsWithDestination[reuseBucketsWithAddrPos]
if currentBucket.FreeSlots > 0 {
return
}
}
currentBucket = &IngressBucket{
FreeSlots: maxServices,
}
bucketsWithoutDestination = append(bucketsWithoutDestination, currentBucket)
}
if len(ingressesWithoutDestination) > 0 {
nextBucket()
}
for _, ingress := range ingressesWithoutDestination {
slots := ingressSlots(&ingress)
if currentBucket.FreeSlots-slots < 0 {
nextBucket()
}
currentBucket.Ingresses = append(currentBucket.Ingresses, ingress)
currentBucket.FreeSlots -= slots
}
result := []*IngressBucket{}
result = append(result, bucketsWithDestination...)
result = append(result, bucketsWithoutDestination...)
return result
}
func ingressSlots(ingress *networkingv1.Ingress) int {
slots := 0
for _, rule := range ingress.Spec.Rules {
paths := len(rule.HTTP.Paths)
if paths == 0 {
paths = 1
}
slots += paths
}
if slots == 0 {
return 1
}
return slots
}