-
Notifications
You must be signed in to change notification settings - Fork 94
/
diff.go
246 lines (218 loc) · 5.83 KB
/
diff.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 api
import (
"errors"
"net"
"reflect"
"strings"
stringz "github.com/appscode/go/strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
ingressClassAnnotationKey = "kubernetes.io/ingress.class"
ingressClassAnnotationValue = "voyager"
)
// if ingressClass == "voyager", then only handle ingress that has voyager annotation
// if ingressClass == "", then handle no annotaion or voyager annotation
func (r Ingress) ShouldHandleIngress(ingressClass string) bool {
// https://github.com/appscode/voyager/blob/master/api/conversion_v1beta1.go#L44
if r.Annotations[APISchema] == APISchemaEngress {
// Resource Type is Extended Ingress So we should always Handle this
return true
}
kubeAnnotation, _ := r.Annotations[ingressClassAnnotationKey]
return kubeAnnotation == ingressClass || kubeAnnotation == ingressClassAnnotationValue
}
func (r Ingress) HasChanged(o Ingress) (bool, error) {
if r.Name != o.Name ||
r.Namespace != o.Namespace ||
r.APISchema() != o.APISchema() {
return false, errors.New("Not the same Ingress.")
}
if !reflect.DeepEqual(r.Spec, o.Spec) {
return true, nil
}
ra := map[string]string{}
for k, v := range r.Annotations {
if strings.HasPrefix(k, EngressKey+"/") {
ra[k] = v
}
}
oa := map[string]string{}
for k, v := range o.Annotations {
if strings.HasPrefix(k, EngressKey+"/") {
oa[k] = v
}
}
return !reflect.DeepEqual(ra, oa), nil
}
func (r Ingress) IsPortChanged(o Ingress) bool {
if (r.Spec.Backend == nil && o.Spec.Backend != r.Spec.Backend) ||
(r.Spec.Backend != nil && o.Spec.Backend == nil) {
return true
}
var oldPort80Open, oldPort443Open bool
oldPortLists := make([]string, 0)
for _, rs := range r.Spec.Rules {
if rs.HTTP != nil {
for _, tls := range r.Spec.TLS {
if stringz.Contains(tls.Hosts, rs.Host) {
oldPort443Open = true
} else {
oldPort80Open = true
}
}
}
for _, port := range rs.TCP {
oldPortLists = append(oldPortLists, port.Port.String())
}
}
var newPort80Open, newPort443Open bool
for _, rs := range o.Spec.Rules {
if rs.HTTP != nil {
for _, tls := range o.Spec.TLS {
if stringz.Contains(tls.Hosts, rs.Host) {
newPort443Open = true
} else {
newPort80Open = true
}
}
}
for _, port := range rs.TCP {
if !stringz.Contains(oldPortLists, port.Port.String()) {
return true
}
}
}
return (oldPort80Open != newPort80Open) || (oldPort443Open != newPort443Open)
}
func (r Ingress) IsSecretChanged(o Ingress) bool {
oldSecretLists := map[string]bool{}
for _, rs := range r.Spec.TLS {
oldSecretLists[rs.SecretName] = true
}
for _, rs := range r.Spec.Rules {
for _, tcp := range rs.TCP {
oldSecretLists[tcp.SecretName] = true
}
}
for _, rs := range o.Spec.Rules {
for _, port := range rs.TCP {
if _, ok := oldSecretLists[port.SecretName]; !ok {
return true
}
}
}
for _, rs := range o.Spec.TLS {
if _, ok := oldSecretLists[rs.SecretName]; !ok {
return true
}
}
return false
}
func (r Ingress) IsLoadBalancerSourceRangeChanged(o Ingress) bool {
oldipset := make(map[string]bool)
for _, oldrange := range r.Spec.LoadBalancerSourceRanges {
k, ok := ipnet(oldrange)
if ok {
oldipset[k] = true
}
}
newipset := make(map[string]bool)
for _, newrange := range o.Spec.LoadBalancerSourceRanges {
k, ok := ipnet(newrange)
if ok {
newipset[k] = true
if _, found := oldipset[k]; !found {
return true
}
}
}
return len(newipset) != len(oldipset)
}
func ipnet(spec string) (string, bool) {
spec = strings.TrimSpace(spec)
_, ipnet, err := net.ParseCIDR(spec)
if err != nil {
return "", false
}
return ipnet.String(), true
}
func (r Ingress) IsStatsChanged(o Ingress) bool {
return isMapKeyChanged(r.Annotations, o.Annotations, StatsOn) ||
isMapKeyChanged(r.Annotations, o.Annotations, StatsPort) ||
isMapKeyChanged(r.Annotations, o.Annotations, StatsServiceName) ||
isMapKeyChanged(r.Annotations, o.Annotations, StatsSecret)
}
func (r Ingress) IsStatsSecretChanged(o Ingress) bool {
return isMapKeyChanged(r.Annotations, o.Annotations, StatsSecret)
}
func (r Ingress) IsKeepSourceChanged(o Ingress, cloudProvider string) bool {
return cloudProvider == "aws" &&
o.LBType() == LBTypeLoadBalancer &&
isMapKeyChanged(r.Annotations, o.Annotations, KeepSourceIP)
}
func isMapKeyChanged(oldMap map[string]string, newMap map[string]string, key string) bool {
oldValue, oldOk := oldMap[key]
newValue, newOk := newMap[key]
return oldOk != newOk || oldValue != newValue
}
func (r Ingress) BackendServices() map[string]metav1.ObjectMeta {
services := map[string]metav1.ObjectMeta{}
record := func(svcName string) {
parts := strings.SplitN(svcName, ".", 2)
if len(parts) == 2 {
services[svcName] = metav1.ObjectMeta{
Name: parts[0],
Namespace: parts[1],
}
} else {
services[svcName+"."+r.Namespace] = metav1.ObjectMeta{
Name: parts[0],
Namespace: r.Namespace,
}
}
}
if r.Spec.Backend != nil {
record(r.Spec.Backend.ServiceName)
}
for _, rules := range r.Spec.Rules {
if rules.HTTP != nil {
for _, svc := range rules.HTTP.Paths {
record(svc.Backend.ServiceName)
}
}
for _, svc := range rules.TCP {
record(svc.Backend.ServiceName)
}
}
return services
}
func (r Ingress) HasBackendService(name, namespace string) bool {
svcFQN := name + "." + namespace
fqn := func(svcName string) string {
if strings.ContainsRune(svcName, '.') {
return svcName
}
return svcName + "." + r.Namespace
}
if r.Spec.Backend != nil {
if fqn(r.Spec.Backend.ServiceName) == svcFQN {
return true
}
}
for _, rules := range r.Spec.Rules {
if rules.HTTP != nil {
for _, svc := range rules.HTTP.Paths {
if fqn(svc.Backend.ServiceName) == svcFQN {
return true
}
}
}
for _, svc := range rules.TCP {
if fqn(svc.Backend.ServiceName) == svcFQN {
return true
}
}
}
return false
}