This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.go
140 lines (121 loc) · 4.19 KB
/
data.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
package prune
import (
"fmt"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
kapi "k8s.io/kubernetes/pkg/api"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
)
// BuildByBuildConfigIndexFunc indexes Build items by their associated BuildConfig, if none, index with key "orphan"
func BuildByBuildConfigIndexFunc(obj interface{}) ([]string, error) {
build, ok := obj.(*buildapi.Build)
if !ok {
return nil, fmt.Errorf("not a build: %v", build)
}
config := build.Status.Config
if config == nil {
return []string{"orphan"}, nil
}
return []string{config.Namespace + "/" + config.Name}, nil
}
// Filter filters the set of objects
type Filter interface {
Filter(builds []*buildapi.Build) []*buildapi.Build
}
// andFilter ands a set of predicate functions to know if it should be included in the return set
type andFilter struct {
filterPredicates []FilterPredicate
}
// Filter ands the set of predicates evaluated against each Build to make a filtered set
func (a *andFilter) Filter(builds []*buildapi.Build) []*buildapi.Build {
results := []*buildapi.Build{}
for _, build := range builds {
include := true
for _, filterPredicate := range a.filterPredicates {
include = include && filterPredicate(build)
}
if include {
results = append(results, build)
}
}
return results
}
// FilterPredicate is a function that returns true if the object should be included in the filtered set
type FilterPredicate func(build *buildapi.Build) bool
// NewFilterBeforePredicate is a function that returns true if the build was created before the current time minus specified duration
func NewFilterBeforePredicate(d time.Duration) FilterPredicate {
now := metav1.Now()
before := metav1.NewTime(now.Time.Add(-1 * d))
return func(build *buildapi.Build) bool {
return build.CreationTimestamp.Before(before)
}
}
// DataSet provides functions for working with build data
type DataSet interface {
GetBuildConfig(build *buildapi.Build) (*buildapi.BuildConfig, bool, error)
ListBuildConfigs() ([]*buildapi.BuildConfig, error)
ListBuilds() ([]*buildapi.Build, error)
ListBuildsByBuildConfig(buildConfig *buildapi.BuildConfig) ([]*buildapi.Build, error)
}
type dataSet struct {
buildConfigStore cache.Store
buildIndexer cache.Indexer
}
// NewDataSet returns a DataSet over the specified items
func NewDataSet(buildConfigs []*buildapi.BuildConfig, builds []*buildapi.Build) DataSet {
buildConfigStore := cache.NewStore(cache.MetaNamespaceKeyFunc)
for _, buildConfig := range buildConfigs {
buildConfigStore.Add(buildConfig)
}
buildIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{
"buildConfig": BuildByBuildConfigIndexFunc,
})
for _, build := range builds {
buildIndexer.Add(build)
}
return &dataSet{
buildConfigStore: buildConfigStore,
buildIndexer: buildIndexer,
}
}
func (d *dataSet) GetBuildConfig(build *buildapi.Build) (*buildapi.BuildConfig, bool, error) {
config := build.Status.Config
if config == nil {
return nil, false, nil
}
var buildConfig *buildapi.BuildConfig
key := &buildapi.BuildConfig{ObjectMeta: metav1.ObjectMeta{Name: config.Name, Namespace: config.Namespace}}
item, exists, err := d.buildConfigStore.Get(key)
if exists {
buildConfig = item.(*buildapi.BuildConfig)
}
return buildConfig, exists, err
}
func (d *dataSet) ListBuildConfigs() ([]*buildapi.BuildConfig, error) {
results := []*buildapi.BuildConfig{}
for _, item := range d.buildConfigStore.List() {
results = append(results, item.(*buildapi.BuildConfig))
}
return results, nil
}
func (d *dataSet) ListBuilds() ([]*buildapi.Build, error) {
results := []*buildapi.Build{}
for _, item := range d.buildIndexer.List() {
results = append(results, item.(*buildapi.Build))
}
return results, nil
}
func (d *dataSet) ListBuildsByBuildConfig(buildConfig *buildapi.BuildConfig) ([]*buildapi.Build, error) {
results := []*buildapi.Build{}
key := &buildapi.Build{}
key.Status.Config = &kapi.ObjectReference{Name: buildConfig.Name, Namespace: buildConfig.Namespace}
items, err := d.buildIndexer.Index("buildConfig", key)
if err != nil {
return nil, err
}
for _, item := range items {
results = append(results, item.(*buildapi.Build))
}
return results, nil
}