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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package scale
import (
"context"
"fmt"
"strings"
"time"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const DefaultHold = 2 * time.Minute
const NoHold time.Duration = -1
const DefaultMaxNodes = 10
const DefaultPodsPerNode = 8
const AddonOwnerAnnotation = "objectset.rio.cattle.io/owner-gvk"
const K3sAddonOwnerGVK = "k3s.cattle.io/v1, Kind=Addon"
const AddonOwnerNameAnnotation = "objectset.rio.cattle.io/owner-name"
func AppliedByTheControlPlane(meta metav1.ObjectMeta) bool {
return meta.Annotations[AddonOwnerAnnotation] == K3sAddonOwnerGVK
}
func StampAppliedByTheControlPlane(meta *metav1.ObjectMeta) {
if meta.Annotations == nil {
meta.Annotations = map[string]string{}
}
meta.Annotations[AddonOwnerAnnotation] = K3sAddonOwnerGVK
meta.Annotations[AddonOwnerNameAnnotation] = "inherited from the owner this object was created by"
}
type Observation struct {
Pods []corev1.Pod
Jobs []batchv1.Job
Deployments []appsv1.Deployment
StatefulSets []appsv1.StatefulSet
DaemonSets []appsv1.DaemonSet
Placement Placement
InFlight int
UnreconciledCustomResources []CustomResource
}
func Intent(o Observation) bool {
if len(o.UnreconciledCustomResources) > 0 {
return true
}
for i := range o.Pods {
p := &o.Pods[i]
if AppliedByTheControlPlane(p.ObjectMeta) || FollowsTheNodes(p) {
continue
}
switch p.Status.Phase {
case corev1.PodPending, corev1.PodRunning, corev1.PodUnknown:
return true
}
}
for i := range o.Jobs {
j := &o.Jobs[i]
if AppliedByTheControlPlane(j.ObjectMeta) {
continue
}
if j.Status.CompletionTime != nil {
continue
}
if j.Spec.Suspend != nil && *j.Spec.Suspend {
continue
}
return true
}
for i := range o.Deployments {
d := &o.Deployments[i]
if AppliedByTheControlPlane(d.ObjectMeta) {
continue
}
if replicas(d.Spec.Replicas) > 0 {
return true
}
}
for i := range o.StatefulSets {
s := &o.StatefulSets[i]
if AppliedByTheControlPlane(s.ObjectMeta) {
continue
}
if replicas(s.Spec.Replicas) > 0 {
return true
}
}
return false
}
func replicas(r *int32) int32 {
if r == nil {
return 1
}
return *r
}
func Demand(o Observation) int {
total := len(o.UnreconciledCustomResources)
for i := range o.Pods {
p := &o.Pods[i]
if AppliedByTheControlPlane(p.ObjectMeta) || FollowsTheNodes(p) {
continue
}
switch p.Status.Phase {
case corev1.PodPending, corev1.PodRunning, corev1.PodUnknown:
total++
}
}
for i := range o.Jobs {
j := &o.Jobs[i]
if AppliedByTheControlPlane(j.ObjectMeta) {
continue
}
if j.Status.CompletionTime != nil {
continue
}
if j.Spec.Suspend != nil && *j.Spec.Suspend {
continue
}
total += jobStillNeeds(j)
}
for i := range o.Deployments {
d := &o.Deployments[i]
if AppliedByTheControlPlane(d.ObjectMeta) {
continue
}
if r := int(replicas(d.Spec.Replicas)); r > 0 {
total += r
}
}
for i := range o.StatefulSets {
s := &o.StatefulSets[i]
if AppliedByTheControlPlane(s.ObjectMeta) {
continue
}
if r := int(replicas(s.Spec.Replicas)); r > 0 {
total += r
}
}
return total
}
func jobStillNeeds(j *batchv1.Job) int {
want := 1
if j.Spec.Completions != nil {
want = int(*j.Spec.Completions)
}
needs := max(want-int(j.Status.Succeeded), 1)
return needs
}
type Action int
const (
None Action = iota
Provision
Teardown
Release
)
func (a Action) String() string {
switch a {
case Provision:
return "Provision"
case Teardown:
return "Teardown"
case Release:
return "Release"
case None:
return "None"
}
return fmt.Sprintf("Action(%d)", int(a))
}
type State struct {
Nodes int
IdleSince time.Time
Backoff Backoff
NodeIdleSince map[string]time.Time
}
type Decision struct {
Action Action
Nodes int
Intent bool
Idle time.Duration
BackoffUntil time.Time
Release string
NodeIdleSince map[string]time.Time
Reason string
Capped string
}
type Options struct {
Hold time.Duration
MaxNodes int
PodsPerNode int
BackoffBase time.Duration
BackoffCap time.Duration
}
func (o Options) hold() time.Duration {
switch {
case o.Hold == 0:
return DefaultHold
case o.Hold < 0:
return 0
}
return o.Hold
}
func (o Options) maxNodes() int {
if o.MaxNodes <= 0 {
return DefaultMaxNodes
}
return o.MaxNodes
}
func (o Options) podsPerNode() int {
if o.PodsPerNode <= 0 {
return DefaultPodsPerNode
}
return o.PodsPerNode
}
func Target(o Observation, opts Options) int {
return Wanted(o, opts, time.Time{}).Target
}
func Decide(o Observation, st State, now time.Time, opts Options) Decision {
d := decide(o, st, now, opts)
if d.NodeIdleSince == nil {
d.NodeIdleSince = nodeClocks(o.Placement, st.NodeIdleSince, now)
}
return d
}
func decide(o Observation, st State, now time.Time, opts Options) Decision {
intent := Intent(o)
if intent {
w := Wanted(o, opts, now)
d := decideIntent(o, st, now, opts, w)
if w.Wanted > w.Target {
d.Capped = fmt.Sprintf("the cluster wants %d nodes (%s) and the max-nodes cap is %d", w.Wanted, w.Why, w.Target)
if len(w.Waiting) > 0 {
d.Capped += "; pod(s) " + strings.Join(w.Waiting, ", ") + " stay Pending"
}
d.Reason += "; " + d.Capped
} else if w.Why != "" {
d.Reason += "; target from " + w.Why
}
if w.Hold != "" {
d.Reason += "; " + w.Hold
}
return d
}
return decideIdle(st, now, opts)
}
func decideIntent(o Observation, st State, now time.Time, opts Options, w Want) Decision {
target := w.Target
switch {
case st.Nodes < target && st.Backoff.Holds(now):
return backingOff(st, target, now)
case st.Nodes < target:
if st.Nodes == 0 {
return Decision{
Action: Provision, Nodes: target, Intent: true,
Reason: "intent and no node",
}
}
return Decision{
Action: Provision, Nodes: target, Intent: true,
Reason: fmt.Sprintf("intent, and %d nodes are counted for a target of %d", st.Nodes, target),
}
case st.Nodes > target:
return scaleIn(o, st, now, opts, target, nodeClocks(o.Placement, st.NodeIdleSince, now))
default:
return Decision{
Action: None, Nodes: st.Nodes, Intent: true,
Reason: fmt.Sprintf("intent, and %d nodes are counted for a target of %d", st.Nodes, target),
}
}
}
func decideIdle(st State, now time.Time, opts Options) Decision {
if st.Nodes == 0 {
return Decision{
Action: None, Nodes: 0,
Reason: "nothing running and no node",
}
}
idleSince := st.IdleSince
if idleSince.IsZero() {
idleSince = now
}
idle := now.Sub(idleSince)
if idle < opts.hold() {
return Decision{
Action: None, Nodes: st.Nodes, Idle: idle,
Reason: fmt.Sprintf("nothing running, holding the node for %s more", opts.hold()-idle),
}
}
return Decision{
Action: Teardown, Nodes: 0, Idle: idle,
Reason: fmt.Sprintf("nothing running for %s, past the %s hold", idle, opts.hold()),
}
}
func (st State) Next(d Decision, now time.Time) State {
switch d.Action {
case Provision:
return State{Nodes: d.Nodes, Backoff: st.Backoff, NodeIdleSince: d.NodeIdleSince}
case Teardown:
return State{Nodes: 0}
case Release:
return State{Nodes: d.Nodes, Backoff: st.Backoff,
NodeIdleSince: restartedClocks(d.NodeIdleSince, d.Release, now)}
}
if d.Intent {
return State{Nodes: st.Nodes, Backoff: st.Backoff, NodeIdleSince: d.NodeIdleSince}
}
if st.Nodes == 0 {
return State{}
}
if !st.IdleSince.IsZero() {
return State{Nodes: st.Nodes, IdleSince: st.IdleSince, NodeIdleSince: d.NodeIdleSince}
}
return State{Nodes: st.Nodes, IdleSince: now, NodeIdleSince: d.NodeIdleSince}
}
type Nodes interface {
Provision(ctx context.Context) error
Teardown(ctx context.Context) error
}
type Releaser interface {
Release(ctx context.Context, node string) error
}