forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune_controller.go
363 lines (308 loc) · 11.7 KB
/
prune_controller.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
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
package prune
import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"k8s.io/klog"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
"github.com/openshift/library-go/pkg/operator/resource/resourceread"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/prune/bindata"
"github.com/openshift/library-go/pkg/operator/v1helpers"
)
// PruneController is a controller that watches static installer pod revision statuses and spawns
// a pruner pod to delete old revision resources from disk
type PruneController struct {
targetNamespace, podResourcePrefix string
// command is the string to use for the pruning pod command
command []string
// prunerPodImageFn returns the image name for the pruning pod
prunerPodImageFn func() string
// ownerRefsFn sets the ownerrefs on the pruner pod
ownerRefsFn func(revision int32) ([]metav1.OwnerReference, error)
operatorClient v1helpers.StaticPodOperatorClient
configMapGetter corev1client.ConfigMapsGetter
secretGetter corev1client.SecretsGetter
podGetter corev1client.PodsGetter
cachesToSync []cache.InformerSynced
queue workqueue.RateLimitingInterface
eventRecorder events.Recorder
}
const (
pruneControllerWorkQueueKey = "key"
statusConfigMapName = "revision-status-"
defaultRevisionLimit = int32(5)
)
// NewPruneController creates a new pruning controller
func NewPruneController(
targetNamespace string,
podResourcePrefix string,
command []string,
configMapGetter corev1client.ConfigMapsGetter,
secretGetter corev1client.SecretsGetter,
podGetter corev1client.PodsGetter,
operatorClient v1helpers.StaticPodOperatorClient,
eventRecorder events.Recorder,
) *PruneController {
c := &PruneController{
targetNamespace: targetNamespace,
podResourcePrefix: podResourcePrefix,
command: command,
operatorClient: operatorClient,
configMapGetter: configMapGetter,
secretGetter: secretGetter,
podGetter: podGetter,
eventRecorder: eventRecorder.WithComponentSuffix("prune-controller"),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "PruneController"),
prunerPodImageFn: getPrunerPodImageFromEnv,
}
c.ownerRefsFn = c.setOwnerRefs
operatorClient.Informer().AddEventHandler(c.eventHandler())
c.cachesToSync = append(c.cachesToSync, operatorClient.Informer().HasSynced)
return c
}
func getRevisionLimits(operatorSpec *operatorv1.StaticPodOperatorSpec) (int32, int32) {
failedRevisionLimit := defaultRevisionLimit
succeededRevisionLimit := defaultRevisionLimit
if operatorSpec.FailedRevisionLimit != 0 {
failedRevisionLimit = operatorSpec.FailedRevisionLimit
}
if operatorSpec.SucceededRevisionLimit != 0 {
succeededRevisionLimit = operatorSpec.SucceededRevisionLimit
}
return failedRevisionLimit, succeededRevisionLimit
}
func (c *PruneController) excludedRevisionHistory(operatorStatus *operatorv1.StaticPodOperatorStatus, failedRevisionLimit, succeededRevisionLimit int32) ([]int, error) {
var succeededRevisions, failedRevisions, inProgressRevisions, unknownStatusRevisions []int
configMaps, err := c.configMapGetter.ConfigMaps(c.targetNamespace).List(metav1.ListOptions{})
if err != nil {
return []int{}, err
}
for _, configMap := range configMaps.Items {
if !strings.HasPrefix(configMap.Name, statusConfigMapName) {
continue
}
if revision, ok := configMap.Data["revision"]; ok {
revisionNumber, err := strconv.Atoi(revision)
if err != nil {
return []int{}, err
}
switch configMap.Data["status"] {
case string(corev1.PodSucceeded):
succeededRevisions = append(succeededRevisions, revisionNumber)
case string(corev1.PodFailed):
failedRevisions = append(failedRevisions, revisionNumber)
case "InProgress":
// we always protect inprogress
inProgressRevisions = append(inProgressRevisions, revisionNumber)
default:
// protect things you don't understand
unknownStatusRevisions = append(unknownStatusRevisions, revisionNumber)
c.eventRecorder.Event("UnknownRevisionStatus", fmt.Sprintf("unknown status for revision %d: %v", revisionNumber, configMap.Data["status"]))
}
}
}
// Return early if nothing to prune
if len(succeededRevisions)+len(failedRevisions) == 0 {
klog.V(2).Info("no revision IDs currently eligible to prune")
return []int{}, nil
}
// Get list of protected IDs
protectedSucceededRevisions := protectedRevisions(succeededRevisions, int(succeededRevisionLimit))
protectedFailedRevisions := protectedRevisions(failedRevisions, int(failedRevisionLimit))
excludedRevisions := make([]int, 0, len(protectedSucceededRevisions)+len(protectedFailedRevisions)+len(inProgressRevisions)+len(unknownStatusRevisions))
excludedRevisions = append(excludedRevisions, protectedSucceededRevisions...)
excludedRevisions = append(excludedRevisions, protectedFailedRevisions...)
excludedRevisions = append(excludedRevisions, inProgressRevisions...)
excludedRevisions = append(excludedRevisions, unknownStatusRevisions...)
sort.Ints(excludedRevisions)
// There should always be at least 1 excluded ID, otherwise we'll delete the current revision
if len(excludedRevisions) == 0 {
return []int{}, fmt.Errorf("need at least 1 excluded ID for revision pruning")
}
return excludedRevisions, nil
}
func (c *PruneController) pruneDiskResources(operatorStatus *operatorv1.StaticPodOperatorStatus, excludedRevisions []int, maxEligibleRevision int) error {
// Run pruning pod on each node and pin it to that node
for _, nodeStatus := range operatorStatus.NodeStatuses {
// Use the highest value between CurrentRevision and LastFailedRevision
// Because CurrentRevision only updates on successful installs and we still prune on an unsuccessful install
if err := c.ensurePrunePod(nodeStatus.NodeName, maxEligibleRevision, excludedRevisions, max(nodeStatus.LastFailedRevision, nodeStatus.CurrentRevision)); err != nil {
return err
}
}
return nil
}
func (c *PruneController) pruneAPIResources(excludedRevisions []int, maxEligibleRevision int) error {
protectedRevisions := sets.NewInt(excludedRevisions...)
statusConfigMaps, err := c.configMapGetter.ConfigMaps(c.targetNamespace).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, cm := range statusConfigMaps.Items {
if !strings.HasPrefix(cm.Name, statusConfigMapName) {
continue
}
revision, err := strconv.Atoi(cm.Data["revision"])
if err != nil {
return fmt.Errorf("unexpected error converting revision to int: %+v", err)
}
if protectedRevisions.Has(revision) {
continue
}
if revision > maxEligibleRevision {
continue
}
if err := c.configMapGetter.ConfigMaps(c.targetNamespace).Delete(cm.Name, &metav1.DeleteOptions{}); err != nil {
return err
}
}
return nil
}
func protectedRevisions(revisions []int, revisionLimit int) []int {
sort.Ints(revisions)
if len(revisions) == 0 {
return revisions
}
startKey := 0
// We use -1 = unlimited revisions, so protect all. Limit shouldn't ever be literally 0 either
if revisionLimit > 0 && len(revisions) > revisionLimit {
startKey = len(revisions) - revisionLimit
}
return revisions[startKey:]
}
func (c *PruneController) ensurePrunePod(nodeName string, maxEligibleRevision int, protectedRevisions []int, revision int32) error {
if revision == 0 {
return nil
}
pod := resourceread.ReadPodV1OrDie(bindata.MustAsset(filepath.Join("pkg/operator/staticpod/controller/prune", "manifests/pruner-pod.yaml")))
pod.Name = getPrunerPodName(nodeName, revision)
pod.Namespace = c.targetNamespace
pod.Spec.NodeName = nodeName
pod.Spec.Containers[0].Image = c.prunerPodImageFn()
pod.Spec.Containers[0].Command = c.command
pod.Spec.Containers[0].Args = append(pod.Spec.Containers[0].Args,
fmt.Sprintf("-v=%d", 4),
fmt.Sprintf("--max-eligible-revision=%d", maxEligibleRevision),
fmt.Sprintf("--protected-revisions=%s", revisionsToString(protectedRevisions)),
fmt.Sprintf("--resource-dir=%s", "/etc/kubernetes/static-pod-resources"),
fmt.Sprintf("--static-pod-name=%s", c.podResourcePrefix),
)
ownerRefs, err := c.ownerRefsFn(revision)
if err != nil {
return fmt.Errorf("unable to set pruner pod ownerrefs: %+v", err)
}
pod.OwnerReferences = ownerRefs
_, _, err = resourceapply.ApplyPod(c.podGetter, c.eventRecorder, pod)
return err
}
func (c *PruneController) setOwnerRefs(revision int32) ([]metav1.OwnerReference, error) {
ownerReferences := []metav1.OwnerReference{}
statusConfigMap, err := c.configMapGetter.ConfigMaps(c.targetNamespace).Get(fmt.Sprintf("revision-status-%d", revision), metav1.GetOptions{})
if err == nil {
ownerReferences = append(ownerReferences, metav1.OwnerReference{
APIVersion: "v1",
Kind: "ConfigMap",
Name: statusConfigMap.Name,
UID: statusConfigMap.UID,
})
}
return ownerReferences, err
}
func getPrunerPodName(nodeName string, revision int32) string {
return fmt.Sprintf("revision-pruner-%d-%s", revision, nodeName)
}
func revisionsToString(revisions []int) string {
values := []string{}
for _, id := range revisions {
value := strconv.Itoa(id)
values = append(values, value)
}
return strings.Join(values, ",")
}
func getPrunerPodImageFromEnv() string {
return os.Getenv("OPERATOR_IMAGE")
}
func (c *PruneController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Infof("Starting PruneController")
defer klog.Infof("Shutting down PruneController")
if !cache.WaitForCacheSync(stopCh, c.cachesToSync...) {
return
}
// doesn't matter what workers say, only start one.
go wait.Until(c.runWorker, time.Second, stopCh)
<-stopCh
}
func (c *PruneController) runWorker() {
for c.processNextWorkItem() {
}
}
func (c *PruneController) processNextWorkItem() bool {
dsKey, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(dsKey)
err := c.sync()
if err == nil {
c.queue.Forget(dsKey)
return true
}
utilruntime.HandleError(fmt.Errorf("%v failed with : %v", dsKey, err))
c.queue.AddRateLimited(dsKey)
return true
}
func (c *PruneController) sync() error {
klog.V(5).Info("Syncing revision pruner")
operatorSpec, operatorStatus, _, err := c.operatorClient.GetStaticPodOperatorState()
if err != nil {
return err
}
failedLimit, succeededLimit := getRevisionLimits(operatorSpec)
excludedRevisions, err := c.excludedRevisionHistory(operatorStatus, failedLimit, succeededLimit)
if err != nil {
return err
}
// if no IDs are excluded, then there is nothing to prune
if len(excludedRevisions) == 0 {
klog.Info("No excluded revisions to prune, skipping")
return nil
}
errs := []error{}
if diskErr := c.pruneDiskResources(operatorStatus, excludedRevisions, excludedRevisions[len(excludedRevisions)-1]); diskErr != nil {
errs = append(errs, diskErr)
}
if apiErr := c.pruneAPIResources(excludedRevisions, excludedRevisions[len(excludedRevisions)-1]); apiErr != nil {
errs = append(errs, apiErr)
}
return v1helpers.NewMultiLineAggregate(errs)
}
// eventHandler queues the operator to check spec and status
func (c *PruneController) eventHandler() cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { c.queue.Add(pruneControllerWorkQueueKey) },
UpdateFunc: func(old, new interface{}) { c.queue.Add(pruneControllerWorkQueueKey) },
DeleteFunc: func(obj interface{}) { c.queue.Add(pruneControllerWorkQueueKey) },
}
}
func max(a, b int32) int32 {
if a > b {
return a
}
return b
}