forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
155 lines (134 loc) · 5.43 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package prune
import (
"fmt"
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
deployapi "github.com/openshift/origin/pkg/deploy/api"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)
// DeploymentByDeploymentConfigIndexFunc indexes Deployment items by their associated DeploymentConfig, if none, index with key "orphan"
func DeploymentByDeploymentConfigIndexFunc(obj interface{}) (string, error) {
controller, ok := obj.(*kapi.ReplicationController)
if !ok {
return "", fmt.Errorf("not a replication controller: %v", obj)
}
name := deployutil.DeploymentConfigNameFor(controller)
if len(name) == 0 {
return "orphan", nil
}
return controller.Namespace + "/" + name, nil
}
// Filter filters the set of objects
type Filter interface {
Filter(items []*kapi.ReplicationController) []*kapi.ReplicationController
}
// 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 item to make a filtered set
func (a *andFilter) Filter(items []*kapi.ReplicationController) []*kapi.ReplicationController {
results := []*kapi.ReplicationController{}
for _, item := range items {
include := true
for _, filterPredicate := range a.filterPredicates {
include = include && filterPredicate(item)
}
if include {
results = append(results, item)
}
}
return results
}
// FilterPredicate is a function that returns true if the object should be included in the filtered set
type FilterPredicate func(item *kapi.ReplicationController) 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 := util.Now()
before := util.NewTime(now.Time.Add(-1 * d))
return func(item *kapi.ReplicationController) bool {
return item.CreationTimestamp.Before(before)
}
}
// FilterDeploymentsPredicate is a function that returns true if the replication controller is associated with a DeploymentConfig
func FilterDeploymentsPredicate(item *kapi.ReplicationController) bool {
return len(deployutil.DeploymentConfigNameFor(item)) > 0
}
// FilterZeroReplicaSize is a function that returns true if the replication controller size is 0
func FilterZeroReplicaSize(item *kapi.ReplicationController) bool {
return item.Spec.Replicas == 0 && item.Status.Replicas == 0
}
// DataSet provides functions for working with deployment data
type DataSet interface {
GetDeploymentConfig(deployment *kapi.ReplicationController) (*deployapi.DeploymentConfig, bool, error)
ListDeploymentConfigs() ([]*deployapi.DeploymentConfig, error)
ListDeployments() ([]*kapi.ReplicationController, error)
ListDeploymentsByDeploymentConfig(config *deployapi.DeploymentConfig) ([]*kapi.ReplicationController, error)
}
type dataSet struct {
deploymentConfigStore cache.Store
deploymentIndexer cache.Indexer
}
// NewDataSet returns a DataSet over the specified items
func NewDataSet(deploymentConfigs []*deployapi.DeploymentConfig, deployments []*kapi.ReplicationController) DataSet {
deploymentConfigStore := cache.NewStore(cache.MetaNamespaceKeyFunc)
for _, deploymentConfig := range deploymentConfigs {
deploymentConfigStore.Add(deploymentConfig)
}
deploymentIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{
"deploymentConfig": DeploymentByDeploymentConfigIndexFunc,
})
for _, deployment := range deployments {
deploymentIndexer.Add(deployment)
}
return &dataSet{
deploymentConfigStore: deploymentConfigStore,
deploymentIndexer: deploymentIndexer,
}
}
func (d *dataSet) GetDeploymentConfig(controller *kapi.ReplicationController) (*deployapi.DeploymentConfig, bool, error) {
name := deployutil.DeploymentConfigNameFor(controller)
if len(name) == 0 {
return nil, false, nil
}
var deploymentConfig *deployapi.DeploymentConfig
key := &deployapi.DeploymentConfig{ObjectMeta: kapi.ObjectMeta{Name: name, Namespace: controller.Namespace}}
item, exists, err := d.deploymentConfigStore.Get(key)
if exists {
deploymentConfig = item.(*deployapi.DeploymentConfig)
}
return deploymentConfig, exists, err
}
func (d *dataSet) ListDeploymentConfigs() ([]*deployapi.DeploymentConfig, error) {
results := []*deployapi.DeploymentConfig{}
for _, item := range d.deploymentConfigStore.List() {
results = append(results, item.(*deployapi.DeploymentConfig))
}
return results, nil
}
func (d *dataSet) ListDeployments() ([]*kapi.ReplicationController, error) {
results := []*kapi.ReplicationController{}
for _, item := range d.deploymentIndexer.List() {
results = append(results, item.(*kapi.ReplicationController))
}
return results, nil
}
func (d *dataSet) ListDeploymentsByDeploymentConfig(deploymentConfig *deployapi.DeploymentConfig) ([]*kapi.ReplicationController, error) {
results := []*kapi.ReplicationController{}
key := &kapi.ReplicationController{
ObjectMeta: kapi.ObjectMeta{
Namespace: deploymentConfig.Namespace,
Annotations: map[string]string{deployapi.DeploymentConfigAnnotation: deploymentConfig.Name},
},
}
items, err := d.deploymentIndexer.Index("deploymentConfig", key)
if err != nil {
return nil, err
}
for _, item := range items {
results = append(results, item.(*kapi.ReplicationController))
}
return results, nil
}