-
Notifications
You must be signed in to change notification settings - Fork 109
/
yandex_preemptibly_delete_preemtible_instances.go
245 lines (204 loc) · 6.16 KB
/
yandex_preemptibly_delete_preemtible_instances.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
/*
Copyright 2022 Flant JSC
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 hooks
import (
"fmt"
"sort"
"time"
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
"github.com/flant/shell-operator/pkg/kube_events_manager/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/deckhouse/deckhouse/go_lib/set"
)
const (
preemtibleVMDeletionDuration = 24 * time.Hour
)
type Machine struct {
Name string
CreationTimestamp metav1.Time
Terminating bool
MachineClassKind string
MachineClassName string
}
type YandexMachineClass struct {
Name string
}
func applyMachineFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
var terminating bool
if obj.GetDeletionTimestamp() != nil {
terminating = true
}
classKind, _, err := unstructured.NestedString(obj.UnstructuredContent(), "spec", "class", "kind")
if err != nil {
return nil, fmt.Errorf("can't access class name of Machine %q: %s", obj.GetName(), err)
}
if len(classKind) == 0 {
return nil, fmt.Errorf("spec.class.kind is empty in %q", obj.GetName())
}
className, _, err := unstructured.NestedString(obj.UnstructuredContent(), "spec", "class", "name")
if err != nil {
return nil, fmt.Errorf("can't access class name of Machine %q: %s", obj.GetName(), err)
}
if len(className) == 0 {
return nil, fmt.Errorf("spec.class.name is empty in %q", obj.GetName())
}
return &Machine{
Name: obj.GetName(),
CreationTimestamp: obj.GetCreationTimestamp(),
Terminating: terminating,
MachineClassKind: classKind,
MachineClassName: className,
}, nil
}
func isPreemptibleFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
preemptible, ok, err := unstructured.NestedBool(obj.UnstructuredContent(), "spec", "schedulingPolicy", "preemptible")
if err != nil {
return nil, fmt.Errorf("can't access field \"spec.schedulingPolicy.preemptible\" of YandexMachineClass %q: %s", obj.GetName(), err)
}
if ok && preemptible {
return &YandexMachineClass{
Name: obj.GetName(),
}, nil
}
return nil, nil
}
var _ = sdk.RegisterFunc(&go_hook.HookConfig{
Queue: "/modules/cloud-provider-yandex/preemtibly-delete-preemtible-instances",
Schedule: []go_hook.ScheduleConfig{
{
Name: "every-15",
Crontab: "0/15 * * * *",
},
},
Kubernetes: []go_hook.KubernetesConfig{
{
Name: "mcs",
ExecuteHookOnEvents: go_hook.Bool(false),
ApiVersion: "machine.sapcloud.io/v1alpha1",
Kind: "YandexMachineClass",
NamespaceSelector: &types.NamespaceSelector{
NameSelector: &types.NameSelector{
MatchNames: []string{"d8-cloud-instance-manager"},
},
},
FilterFunc: isPreemptibleFilter,
},
{
Name: "machines",
ExecuteHookOnEvents: go_hook.Bool(false),
ApiVersion: "machine.sapcloud.io/v1alpha1",
Kind: "Machine",
NamespaceSelector: &types.NamespaceSelector{
NameSelector: &types.NameSelector{
MatchNames: []string{"d8-cloud-instance-manager"},
},
},
FilterFunc: applyMachineFilter,
},
},
}, deleteMachines)
func deleteMachines(input *go_hook.HookInput) error {
var (
timeNow = time.Now().UTC()
preemptibleMachineClassesSet = set.Set{}
machines []*Machine
)
for _, mcRaw := range input.Snapshots["mcs"] {
if mcRaw == nil {
continue
}
ic, ok := mcRaw.(*YandexMachineClass)
if !ok {
return fmt.Errorf("failed to assert to *YandexMachineClass")
}
preemptibleMachineClassesSet.Add(ic.Name)
}
if preemptibleMachineClassesSet.Size() == 0 {
return nil
}
for _, machineRaw := range input.Snapshots["machines"] {
machine, ok := machineRaw.(*Machine)
if !ok {
return fmt.Errorf("failed to assert to *Machine")
}
if machine.Terminating {
continue
}
if machine.MachineClassKind != "YandexMachineClass" {
continue
}
if !preemptibleMachineClassesSet.Has(machine.MachineClassName) {
continue
}
machines = append(machines, machine)
}
if len(machines) == 0 {
return nil
}
for _, m := range getMachinesToDelete(timeNow, machines) {
input.PatchCollector.Delete("machine.sapcloud.io/v1alpha1", "Machine", "d8-cloud-instance-manager", m)
}
return nil
}
// delete all after 23h mark
// afterwards delete in 15 minutes increments, no more than batch size
func getMachinesToDelete(timeNow time.Time, machines []*Machine) (machinesToDelete []string) {
const (
// 12 * 0.25 = 3 hours
durationIterations = 12
slidingStep = 15 * time.Minute
)
var (
currentSlidingDuration = preemtibleVMDeletionDuration - time.Hour
)
sort.Slice(machines, func(i, j int) bool {
return machines[i].CreationTimestamp.Before(&machines[j].CreationTimestamp)
})
batch := len(machines) / durationIterations
if batch == 0 {
batch = 1
}
var (
cursor int
)
// short-circuit if there are Machines older than 23 hours
for _, m := range machines {
if expires(timeNow, m.CreationTimestamp.Time, currentSlidingDuration) {
machinesToDelete = append(machinesToDelete, m.Name)
cursor++
}
}
if len(machinesToDelete) != 0 {
return machinesToDelete
}
for t := 0; t < durationIterations; t++ {
currentSlidingDuration -= slidingStep
for cursor < len(machines) {
if len(machinesToDelete) >= batch {
break
}
if expires(timeNow, machines[cursor].CreationTimestamp.Time, currentSlidingDuration) {
machinesToDelete = append(machinesToDelete, machines[cursor].Name)
cursor++
} else {
break
}
}
}
return
}
func expires(now, timestamp time.Time, expirationDuration time.Duration) bool {
return timestamp.Add(expirationDuration).Before(now)
}