-
Notifications
You must be signed in to change notification settings - Fork 159
/
context.go
141 lines (125 loc) · 2.8 KB
/
context.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
package service
import (
"github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"reflect"
"sort"
"strings"
"sync"
)
type Context struct{ ctx sync.Map }
func (c *Context) Get(name string) *v1.Service {
v, ok := c.ctx.Load(name)
if !ok {
return nil
}
val, ok := v.(*v1.Service)
if !ok {
glog.Errorf("not type of v1.svc: [%s]", reflect.TypeOf(v))
return nil
}
return val
}
func (c *Context) Set(name string, val *v1.Service) { c.ctx.Store(name, val) }
func (c *Context) Range(f func(key string, value *v1.Service) bool) {
c.ctx.Range(
func(key, value interface{}) bool {
return f(key.(string), value.(*v1.Service))
},
)
}
func (c *Context) Remove(name string) { c.ctx.Delete(name) }
func ServiceModeLocal(svc *v1.Service) bool {
return svc.Spec.ExternalTrafficPolicy == v1.ServiceExternalTrafficPolicyTypeLocal
}
// NeedUpdate compare old and new service for possible changes
func NeedUpdate(old, newm *v1.Service, record record.EventRecorder) bool {
if !NeedLoadBalancer(old) &&
!NeedLoadBalancer(newm) {
// no loadbalancer is needed
return false
}
if NeedLoadBalancer(old) != NeedLoadBalancer(newm) {
record.Eventf(
newm,
v1.EventTypeNormal,
"TypeChanged",
"%v -> %v",
old.Spec.Type,
newm.Spec.Type,
)
return true
}
if !reflect.DeepEqual(old.Annotations, newm.Annotations) {
record.Eventf(
newm,
v1.EventTypeNormal,
"AnnotationChanged",
"with count %v -> %v",
len(old.Annotations),
len(newm.Annotations),
)
return true
}
if old.UID != newm.UID {
record.Eventf(
newm,
v1.EventTypeNormal,
"UIDChanged",
"%v -> %v",
old.UID, newm.UID,
)
return true
}
if !reflect.DeepEqual(old.Spec, newm.Spec) {
record.Eventf(
newm,
v1.EventTypeNormal,
"ServiceSpecChanged",
"%v -> %v",
old.Spec,
newm.Spec,
)
return true
}
return false
}
func NeedLoadBalancer(service *v1.Service) bool {
return service.Spec.Type == v1.ServiceTypeLoadBalancer
}
func NodeSpecChanged(a, b *v1.Node) bool {
return NodeLabelsChanged(a.Labels, b.Labels) ||
a.Spec.Unschedulable != b.Spec.Unschedulable ||
NodeConditionChanged(a.Status.Conditions, b.Status.Conditions)
}
func NodeConditionChanged(a, b []v1.NodeCondition) bool {
if len(a) != len(b) {
return true
}
sort.SliceStable(a, func(i, j int) bool {
return strings.Compare(string(a[i].Type), string(a[j].Type)) <= 0
})
sort.SliceStable(b, func(i, j int) bool {
return strings.Compare(string(b[i].Type), string(b[j].Type)) <= 0
})
for i := range a {
if a[i].Type != b[i].Type ||
a[i].Status != b[i].Status {
return true
}
}
return false
}
func NodeLabelsChanged(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if b[k] != v {
return true
}
}
// no need for reverse compare
return false
}