/
suspender.go
280 lines (263 loc) · 8.89 KB
/
suspender.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
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
package engine
import (
"context"
"errors"
"fmt"
"sync"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
)
// Suspender receives namespaces from Watcher and handles them. It means that
// it will read and write namespaces' annotations, and scale resources.
func (eng *Engine) Suspender(ctx context.Context, cs *kubernetes.Clientset) {
eng.Mutex.Lock()
eng.RunningNamespacesList = make(map[string]time.Time)
eng.Logger.Info().Str("routine", "suspender").Msg("suspender started")
eng.Mutex.Unlock()
for {
// wait for the next namespace to check
n := <-eng.Wl
start := time.Now()
// we create a sublogger to avoid "namespace" field duplication at each
// loop
eng.Mutex.Lock()
sLogger := eng.Logger.With().Str("routine", "suspender").Str("namespace", n.Name).Logger()
eng.Mutex.Unlock()
sLogger.Trace().Msgf("namespace %s received from watcher", n.Name)
eng.Mutex.Lock()
desiredState, ok := n.Annotations[eng.Options.Prefix+"desiredState"]
if !ok {
// the annotation does not exist, which means that it is the first
// time we see this namespace. So by default, it should be "running"
now, suspendAt, err := getTimes(n.Annotations[eng.Options.Prefix+"dailySuspendTime"])
if err != nil {
sLogger.Error().
Err(err).
Msgf("cannot parse dailySuspendTime time on namespace %s", n.Name)
}
sLogger.Trace().Msgf("now: %d, dailySuspendTime: %d, remaining: %s", now, suspendAt,
time.Duration(suspendAt)*time.Minute-time.Duration(now)*time.Minute)
if suspendAt <= now {
// patch the namespace
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
res, err := cs.CoreV1().
Namespaces().Get(ctx, n.Name, metav1.GetOptions{})
if err != nil {
return err
}
res.Annotations[eng.Options.Prefix+"desiredState"] = Suspended
var updateOpts metav1.UpdateOptions
// if the flag -dryrun is used, do not update resources
if eng.Options.DryRun {
updateOpts = metav1.UpdateOptions{
DryRun: append(updateOpts.DryRun, "All"),
}
}
_, err = cs.CoreV1().
Namespaces().Update(ctx, res, metav1.UpdateOptions{})
return err
}); err != nil {
sLogger.Error().
Err(err).
Msgf("cannot update namespace %s object", n.Name)
} else {
sLogger.Info().
Msgf("suspended namespace %s based on daily suspend time", n.Name)
}
desiredState = Suspended
} else {
eng.Mutex.Unlock()
sLogger.Debug().Msgf("suspender loop for namespace %s duration: %s", n.Name, time.Since(start))
continue
}
}
switch desiredState {
case Running:
if date, ok := eng.RunningNamespacesList[n.Name]; !ok {
// first time we see this namespace as running, so we simply add
// it to the map with the current time
eng.RunningNamespacesList[n.Name] = time.Now().Local()
// then we add the annotation that indicates when the namespace
// will be automtically suspended again
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
res, err := cs.CoreV1().
Namespaces().Get(ctx, n.Name, metav1.GetOptions{})
if err != nil {
return err
}
res.Annotations[eng.Options.Prefix+"auto_nextSuspendTime"] = time.Now().
Add(eng.RunningDuration).Format(time.Kitchen)
var updateOpts metav1.UpdateOptions
// if the flag -dryrun is used, do not update resources
if eng.Options.DryRun {
updateOpts = metav1.UpdateOptions{
DryRun: append(updateOpts.DryRun, "All"),
}
}
_, err = cs.CoreV1().
Namespaces().Update(ctx, res, metav1.UpdateOptions{})
return err
}); err != nil {
sLogger.Error().
Err(err).
Msgf("cannot add nextSuspendTime annotation to namespace %s", n.Name)
} else {
sLogger.Debug().
Msgf("added nextSuspendTime annotation to %s on %s ", time.Now().
Add(eng.RunningDuration).Format(time.Kitchen), n.Name)
}
} else {
if time.Now().Local().Sub(date) < eng.RunningDuration {
// we do not have to suspend the namespace yet, so we
// continue
eng.Mutex.Unlock()
sLogger.Debug().Msgf("suspender loop for namespace %s duration: %s", n.Name, time.Since(start))
continue
}
// if we end up here, it means that we have to suspend the
// namespace as it as been running for too long
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
res, err := cs.CoreV1().
Namespaces().Get(ctx, n.Name, metav1.GetOptions{})
if err != nil {
return err
}
res.Annotations[eng.Options.Prefix+"desiredState"] = Suspended
delete(res.Annotations, eng.Options.Prefix+"auto_nextSuspendTime")
var updateOpts metav1.UpdateOptions
// if the flag -dryrun is used, do not update resources
if eng.Options.DryRun {
updateOpts = metav1.UpdateOptions{
DryRun: append(updateOpts.DryRun, "All"),
}
}
_, err = cs.CoreV1().
Namespaces().Update(ctx, res, metav1.UpdateOptions{})
return err
}); err != nil {
sLogger.Error().
Err(err).
Msgf("cannot update namespace %s object", n.Name)
} else {
sLogger.Info().
Msgf("suspended namespace %s based on uptime", n.Name)
desiredState = Suspended
delete(eng.RunningNamespacesList, n.Name)
}
}
case Suspended:
// TODO: think about removing ²the annotation auto_nextSuspendTime
// here. Elsewhere, it will stay until the next running call.
default:
sLogger.Error().
Err(errors.New("state not recognised: "+desiredState)).
Msgf("state %s is not recognised", desiredState)
eng.Mutex.Unlock()
sLogger.Debug().Msgf("suspender loop for namespace %s duration: %s", n.Name, time.Since(start))
continue
}
// we do a switch again, but with the updated value
// get deployments of the namespace
deployments, err := cs.AppsV1().Deployments(n.Name).List(ctx, metav1.ListOptions{})
if err != nil {
sLogger.Fatal().
Err(err).
Str("object", "deployment").
Msg("cannot list deployments")
}
// get cronjobs of the namespace
cronjobs, err := cs.BatchV1beta1().CronJobs(n.Name).List(ctx, metav1.ListOptions{})
if err != nil {
sLogger.Fatal().
Err(err).
Str("object", "cronjob").
Msg("cannot list cronjobs")
}
// get statefulsets of the namespace
statefulsets, err := cs.AppsV1().StatefulSets(n.Name).List(ctx, metav1.ListOptions{})
if err != nil {
sLogger.Fatal().
Err(err).
Str("object", "statefulset").
Msg("cannot list statefulsets")
}
var wg sync.WaitGroup
switch desiredState {
case Running:
wg.Add(3)
// check and patch deployments
go func() {
if err := checkRunningDeploymentsConformity(ctx, sLogger, deployments.Items, cs, n.Name, eng.Options.DryRun); err != nil {
sLogger.Error().
Err(err).
Str("object", "deployment").
Msg("running deployments conformity checks failed")
}
wg.Done()
}()
// check and patch cronjobs
go func() {
if err := checkRunningCronjobsConformity(ctx, sLogger, cronjobs.Items, cs, n.Name, eng.Options.DryRun); err != nil {
sLogger.Error().
Err(err).
Str("object", "cronjob").
Msg("running cronjobs conformity checks failed")
}
wg.Done()
}()
// check and patch statefulsets
go func() {
if err := checkRunningStatefulsetsConformity(ctx, sLogger, statefulsets.Items, cs, n.Name, eng.Options.DryRun); err != nil {
sLogger.Error().
Err(err).
Str("object", "statefulset").
Msg("running steatfulsets conformity checks failed")
}
wg.Done()
}()
case Suspended:
wg.Add(3)
// check and patch deployments
go func() {
if err := checkSuspendedDeploymentsConformity(ctx, sLogger, deployments.Items, cs, n.Name, eng.Options.DryRun); err != nil {
sLogger.Error().
Err(err).
Str("object", "deployment").
Msg("suspended conformity checks failed")
}
wg.Done()
}()
// check and patch cronjobs
go func() {
if err := checkSuspendedCronjobsConformity(ctx, sLogger, cronjobs.Items, cs, n.Name, eng.Options.DryRun); err != nil {
sLogger.Error().
Err(err).
Str("object", "cronjob").
Msg("suspended cronjobs conformity checks failed")
}
wg.Done()
}()
// check and patch statefulsets
go func() {
if err := checkSuspendedStatefulsetsConformity(ctx, sLogger, statefulsets.Items, cs, n.Name, eng.Options.DryRun); err != nil {
sLogger.Error().
Err(err).
Str("object", "statefulset").
Msg("suspended steatfulsets conformity checks failed")
}
wg.Done()
}()
default:
errMsg := fmt.Sprintf("state %s is not a supported state", desiredState)
sLogger.Error().
Err(errors.New(errMsg)).
Msg("desired state cannot be recognised")
}
wg.Wait()
// modify resources now based on n state
eng.Mutex.Unlock()
sLogger.Debug().Msgf("suspender loop for namespace %s duration: %s", n.Name, time.Since(start))
}
}