forked from openshift/origin
/
etcd.go
215 lines (191 loc) · 6.34 KB
/
etcd.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
package etcd
import (
"github.com/golang/glog"
etcderr "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors/etcd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
ktools "github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kubeetcd "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/etcd"
"github.com/openshift/origin/pkg/build/api"
)
const (
// BuildPath is the path to build resources in etcd
BuildPath string = "/builds"
// BuildConfigPath is the path to buildConfig resources in etcd
BuildConfigPath string = "/buildConfigs"
)
// Etcd implements build.Registry and buildconfig.Registry backed by etcd.
type Etcd struct {
tools.EtcdHelper
}
// New creates an etcd registry.
func New(helper tools.EtcdHelper) *Etcd {
return &Etcd{
EtcdHelper: helper,
}
}
func makeBuildListKey(ctx kapi.Context) string {
return kubeetcd.MakeEtcdListKey(ctx, BuildPath)
}
func makeBuildKey(ctx kapi.Context, id string) (string, error) {
return kubeetcd.MakeEtcdItemKey(ctx, BuildPath, id)
}
// ListBuilds obtains a list of Builds.
func (r *Etcd) ListBuilds(ctx kapi.Context, selector labels.Selector) (*api.BuildList, error) {
allBuilds := api.BuildList{}
err := r.ExtractToList(makeBuildListKey(ctx), &allBuilds)
if err != nil {
return nil, err
}
filtered := []api.Build{}
for _, build := range allBuilds.Items {
if selector.Matches(labels.Set(build.Labels)) {
filtered = append(filtered, build)
}
}
allBuilds.Items = filtered
return &allBuilds, nil
}
// WatchBuilds begins watching for new, changed, or deleted Builds.
func (r *Etcd) WatchBuilds(ctx kapi.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
version, err := ktools.ParseWatchResourceVersion(resourceVersion, "build")
if err != nil {
return nil, err
}
return r.WatchList(makeBuildListKey(ctx), version, func(obj runtime.Object) bool {
build, ok := obj.(*api.Build)
if !ok {
glog.Errorf("Unexpected object during build watch: %#v", obj)
return false
}
fields := labels.Set{
"name": build.Name,
"status": string(build.Status),
"podName": build.PodName,
}
return label.Matches(labels.Set(build.Labels)) && field.Matches(fields)
})
}
// GetBuild gets a specific Build specified by its ID.
func (r *Etcd) GetBuild(ctx kapi.Context, id string) (*api.Build, error) {
var build api.Build
key, err := makeBuildKey(ctx, id)
if err != nil {
return nil, err
}
err = r.ExtractObj(key, &build, false)
if err != nil {
return nil, etcderr.InterpretGetError(err, "build", id)
}
return &build, nil
}
// CreateBuild creates a new Build.
func (r *Etcd) CreateBuild(ctx kapi.Context, build *api.Build) error {
key, err := makeBuildKey(ctx, build.Name)
if err != nil {
return err
}
err = r.CreateObj(key, build, 0)
return etcderr.InterpretCreateError(err, "build", build.Name)
}
// UpdateBuild replaces an existing Build.
func (r *Etcd) UpdateBuild(ctx kapi.Context, build *api.Build) error {
key, err := makeBuildKey(ctx, build.Name)
if err != nil {
return err
}
err = r.SetObj(key, build)
return etcderr.InterpretUpdateError(err, "build", build.Name)
}
// DeleteBuild deletes a Build specified by its ID.
func (r *Etcd) DeleteBuild(ctx kapi.Context, id string) error {
key, err := makeBuildKey(ctx, id)
if err != nil {
return err
}
err = r.Delete(key, true)
return etcderr.InterpretDeleteError(err, "build", id)
}
func makeBuildConfigListKey(ctx kapi.Context) string {
return kubeetcd.MakeEtcdListKey(ctx, BuildConfigPath)
}
func makeBuildConfigKey(ctx kapi.Context, id string) (string, error) {
return kubeetcd.MakeEtcdItemKey(ctx, BuildConfigPath, id)
}
// ListBuildConfigs obtains a list of BuildConfigs.
func (r *Etcd) ListBuildConfigs(ctx kapi.Context, selector labels.Selector) (*api.BuildConfigList, error) {
allConfigs := api.BuildConfigList{}
err := r.ExtractToList(makeBuildConfigListKey(ctx), &allConfigs)
if err != nil {
return nil, err
}
filtered := []api.BuildConfig{}
for _, config := range allConfigs.Items {
if selector.Matches(labels.Set(config.Labels)) {
filtered = append(filtered, config)
}
}
allConfigs.Items = filtered
return &allConfigs, nil
}
// GetBuildConfig gets a specific BuildConfig specified by its ID.
func (r *Etcd) GetBuildConfig(ctx kapi.Context, id string) (*api.BuildConfig, error) {
var config api.BuildConfig
key, err := makeBuildConfigKey(ctx, id)
if err != nil {
return nil, err
}
err = r.ExtractObj(key, &config, false)
if err != nil {
return nil, etcderr.InterpretGetError(err, "buildConfig", id)
}
return &config, nil
}
// CreateBuildConfig creates a new BuildConfig.
func (r *Etcd) CreateBuildConfig(ctx kapi.Context, config *api.BuildConfig) error {
key, err := makeBuildConfigKey(ctx, config.Name)
if err != nil {
return err
}
err = r.CreateObj(key, config, 0)
return etcderr.InterpretCreateError(err, "buildConfig", config.Name)
}
// UpdateBuildConfig replaces an existing BuildConfig.
func (r *Etcd) UpdateBuildConfig(ctx kapi.Context, config *api.BuildConfig) error {
key, err := makeBuildConfigKey(ctx, config.Name)
if err != nil {
return err
}
err = r.SetObj(key, config)
return etcderr.InterpretUpdateError(err, "buildConfig", config.Name)
}
// DeleteBuildConfig deletes a BuildConfig specified by its ID.
func (r *Etcd) DeleteBuildConfig(ctx kapi.Context, id string) error {
key, err := makeBuildConfigKey(ctx, id)
if err != nil {
return err
}
err = r.Delete(key, true)
return etcderr.InterpretDeleteError(err, "buildConfig", id)
}
// WatchBuildConfigs begins watching for new, changed, or deleted BuildConfigs.
func (r *Etcd) WatchBuildConfigs(ctx kapi.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
version, err := ktools.ParseWatchResourceVersion(resourceVersion, "buildConfig")
if err != nil {
return nil, err
}
return r.WatchList(makeBuildConfigListKey(ctx), version, func(obj runtime.Object) bool {
buildConfig, ok := obj.(*api.BuildConfig)
if !ok {
glog.Errorf("Unexpected object during buildConfig watch: %#v", obj)
return false
}
fields := labels.Set{
"name": buildConfig.Name,
}
return label.Matches(labels.Set(buildConfig.Labels)) && field.Matches(fields)
})
}