forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune.go
69 lines (60 loc) · 2.27 KB
/
prune.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
package prune
import (
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
deployapi "github.com/openshift/origin/pkg/deploy/api"
)
// PruneFunc is a function that is invoked for each item during Prune
type PruneFunc func(item *kapi.ReplicationController) error
// PruneTask is an object that knows how to execute a single iteration of a Prune
type PruneTasker interface {
PruneTask() error
}
// pruneTask is an object that knows how to prune a data set
type pruneTask struct {
resolver Resolver
handler PruneFunc
}
// NewPruneTasker returns a PruneTasker over specified data using specified flags
// keepYoungerThan will filter out all objects from prune data set that are younger than the specified time duration
// orphans if true will include inactive orphan deployments in candidate prune set
// keepComplete is per DeploymentConfig how many of the most recent deployments should be preserved
// keepFailed is per DeploymentConfig how many of the most recent failed deployments should be preserved
func NewPruneTasker(deploymentConfigs []*deployapi.DeploymentConfig, deployments []*kapi.ReplicationController, keepYoungerThan time.Duration, orphans bool, keepComplete int, keepFailed int, handler PruneFunc) PruneTasker {
filter := &andFilter{
filterPredicates: []FilterPredicate{
FilterDeploymentsPredicate,
FilterZeroReplicaSize,
NewFilterBeforePredicate(keepYoungerThan),
},
}
deployments = filter.Filter(deployments)
dataSet := NewDataSet(deploymentConfigs, deployments)
resolvers := []Resolver{}
if orphans {
inactiveDeploymentStatus := []deployapi.DeploymentStatus{
deployapi.DeploymentStatusComplete,
deployapi.DeploymentStatusFailed,
}
resolvers = append(resolvers, NewOrphanDeploymentResolver(dataSet, inactiveDeploymentStatus))
}
resolvers = append(resolvers, NewPerDeploymentConfigResolver(dataSet, keepComplete, keepFailed))
return &pruneTask{
resolver: &mergeResolver{resolvers: resolvers},
handler: handler,
}
}
// PruneTask will visit each item in the prunable set and invoke the associated handler
func (t *pruneTask) PruneTask() error {
deployments, err := t.resolver.Resolve()
if err != nil {
return err
}
for _, deployment := range deployments {
err = t.handler(deployment)
if err != nil {
return err
}
}
return nil
}