forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prune.go
107 lines (90 loc) · 3.22 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
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
package prune
import (
"time"
"github.com/golang/glog"
buildapi "github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/client"
)
type Pruner interface {
// Prune is responsible for actual removal of builds identified as candidates
// for pruning based on pruning algorithm.
Prune(deleter BuildDeleter) error
}
// BuildDeleter knows how to delete builds from OpenShift.
type BuildDeleter interface {
// DeleteBuild removes the build from OpenShift's storage.
DeleteBuild(build *buildapi.Build) error
}
// pruner is an object that knows how to prune a data set
type pruner struct {
resolver Resolver
}
var _ Pruner = &pruner{}
// PrunerOptions contains the fields used to initialize a new Pruner.
type PrunerOptions struct {
// KeepYoungerThan indicates the minimum age a BuildConfig must be to be a
// candidate for pruning.
KeepYoungerThan time.Duration
// Orphans if true will include inactive orphan builds in candidate prune set
Orphans bool
// KeepComplete is per BuildConfig how many of the most recent builds should be preserved
KeepComplete int
// KeepFailed is per BuildConfig how many of the most recent failed builds should be preserved
KeepFailed int
// BuildConfigs is the entire list of buildconfigs across all namespaces in the cluster.
BuildConfigs []*buildapi.BuildConfig
// Builds is the entire list of builds across all namespaces in the cluster.
Builds []*buildapi.Build
}
// NewPruner returns a Pruner over specified data using specified options.
func NewPruner(options PrunerOptions) Pruner {
glog.V(1).Infof("Creating build pruner with keepYoungerThan=%v, orphans=%v, keepComplete=%v, keepFailed=%v",
options.KeepYoungerThan, options.Orphans, options.KeepComplete, options.KeepFailed)
filter := &andFilter{
filterPredicates: []FilterPredicate{NewFilterBeforePredicate(options.KeepYoungerThan)},
}
builds := filter.Filter(options.Builds)
dataSet := NewDataSet(options.BuildConfigs, builds)
resolvers := []Resolver{}
if options.Orphans {
inactiveBuildStatus := []buildapi.BuildPhase{
buildapi.BuildPhaseCancelled,
buildapi.BuildPhaseComplete,
buildapi.BuildPhaseError,
buildapi.BuildPhaseFailed,
}
resolvers = append(resolvers, NewOrphanBuildResolver(dataSet, inactiveBuildStatus))
}
resolvers = append(resolvers, NewPerBuildConfigResolver(dataSet, options.KeepComplete, options.KeepFailed))
return &pruner{
resolver: &mergeResolver{resolvers: resolvers},
}
}
// Prune will visit each item in the prunable set and invoke the associated BuildDeleter.
func (p *pruner) Prune(deleter BuildDeleter) error {
builds, err := p.resolver.Resolve()
if err != nil {
return err
}
for _, build := range builds {
if err := deleter.DeleteBuild(build); err != nil {
return err
}
}
return nil
}
// buildDeleter removes a build from OpenShift.
type buildDeleter struct {
builds client.BuildsNamespacer
}
var _ BuildDeleter = &buildDeleter{}
// NewBuildDeleter creates a new buildDeleter.
func NewBuildDeleter(builds client.BuildsNamespacer) BuildDeleter {
return &buildDeleter{
builds: builds,
}
}
func (p *buildDeleter) DeleteBuild(build *buildapi.Build) error {
glog.V(4).Infof("Deleting build %q", build.Name)
return p.builds.Builds(build.Namespace).Delete(build.Name)
}