/
cache.go
198 lines (175 loc) · 5.27 KB
/
cache.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
/*
* Copyright ©2020. The virtual-kubelet authors
*
* 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 util
import (
"sync"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/klog"
gochache "github.com/patrickmn/go-cache"
)
// UnschedulableCache contaiens cache ownerid/node/freezeTime
type UnschedulableCache struct {
cache map[string]*gochache.Cache
sync.RWMutex
}
// NewUnschedulableCache init a new Unschedulable
func NewUnschedulableCache() *UnschedulableCache {
return &UnschedulableCache{cache: map[string]*gochache.Cache{}}
}
// Add add node/ownerID to cache
func (c *UnschedulableCache) Add(node, ownerID string) {
c.Lock()
defer c.Unlock()
now := time.Now()
freezeCache := c.cache[ownerID]
if freezeCache == nil {
freezeCache = gochache.New(3*time.Minute, 6*time.Minute)
}
freezeCache.Add(node, &now, 0)
c.cache[ownerID] = freezeCache
}
// GetFreezeNodes return the freezed nodes
func (c *UnschedulableCache) GetFreezeNodes(ownerID string) []string {
c.Lock()
defer c.Unlock()
freezeCache := c.cache[ownerID]
if freezeCache == nil {
return nil
}
nodes := make([]string, 0)
for key := range freezeCache.Items() {
nodes = append(nodes, key)
}
return nodes
}
// GetFreezeTime returns node/ownerID freeze time
func (c *UnschedulableCache) GetFreezeTime(node, ownerID string) *time.Time {
c.RLock()
defer c.RUnlock()
if c.cache[ownerID] == nil {
return nil
}
timePtr, found := c.cache[ownerID].Get(node)
if !found {
return nil
}
return timePtr.(*time.Time)
}
// CheckValidFunc defines the check func
type CheckValidFunc func(string, string, time.Duration) bool
// ReplacePodNodeNameNodeAffinity replaces the RequiredDuringSchedulingIgnoredDuringExecution
// NodeAffinity of the given affinity with a new NodeAffinity that selects the given nodeName.
// Note that this function assumes that no NodeAffinity conflicts with the selected nodeName.
func ReplacePodNodeNameNodeAffinity(affinity *v1.Affinity, ownerID string, expireTime time.Duration,
checkFuc CheckValidFunc, nodeNames ...string) (*v1.Affinity,
int) {
nodeSelReq := v1.NodeSelectorRequirement{
// Key: "metadata.name",
Key: "kubernetes.io/hostname",
Operator: v1.NodeSelectorOpNotIn,
Values: nodeNames,
}
nodeSelector := &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{nodeSelReq},
},
},
}
count := 1
if affinity == nil {
return &v1.Affinity{
NodeAffinity: &v1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: nodeSelector,
},
}, count
}
if affinity.NodeAffinity == nil {
affinity.NodeAffinity = &v1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: nodeSelector,
}
return affinity, count
}
nodeAffinity := affinity.NodeAffinity
if nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil {
nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = nodeSelector
return affinity, count
}
terms := nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms
if terms == nil {
nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = []v1.NodeSelectorTerm{
{
MatchFields: []v1.NodeSelectorRequirement{nodeSelReq},
},
}
return affinity, count
}
newTerms := make([]v1.NodeSelectorTerm, 0)
for _, term := range terms {
if term.MatchExpressions == nil {
continue
}
mes, noScheduleCount := getNodeSelectorRequirement(term, ownerID, nodeSelReq, checkFuc, expireTime)
count = noScheduleCount
term.MatchExpressions = mes
newTerms = append(newTerms, term)
}
// Replace node selector with the new one.
nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = newTerms
affinity.NodeAffinity = nodeAffinity
return affinity, count
}
func getNodeSelectorRequirement(term v1.NodeSelectorTerm,
ownerID string, nodeSelReq v1.NodeSelectorRequirement, checkFuc CheckValidFunc, expireTime time.Duration) ([]v1.NodeSelectorRequirement, int) {
mes := make([]v1.NodeSelectorRequirement, 0)
count := 0
for _, me := range term.MatchExpressions {
if me.Key != nodeSelReq.Key || me.Operator != nodeSelReq.Operator {
mes = append(mes, me)
continue
}
values := make([]string, 0)
for _, v := range me.Values {
klog.V(4).Infof("current term value %v", v)
if checkContains(nodeSelReq.Values, v) {
continue
}
if checkFuc == nil {
values = append(values, v)
continue
}
if !checkFuc(v, ownerID, expireTime) && len(v) > 0 {
values = append(values, v)
}
}
me.Values = append(values, nodeSelReq.Values...)
count = len(values)
mes = append(mes, me)
}
return mes, count
}
func checkContains(exists []string, value string) bool {
if len(exists) == 0 {
return false
}
for _, v := range exists {
if v == value {
return true
}
}
return false
}