forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
229 lines (197 loc) · 7.24 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package etcd
import (
"github.com/golang/glog"
etcderr "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors/etcd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
kubeetcd "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/etcd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/openshift/origin/pkg/deploy/api"
)
const (
// DeploymentPath is the path to deployment resources in etcd
DeploymentPath string = "/deployments"
// DeploymentConfigPath is the path to deploymentConfig resources in etcd
DeploymentConfigPath string = "/deploymentConfigs"
)
// Etcd implements deployment.Registry and deploymentconfig.Registry interfaces.
type Etcd struct {
tools.EtcdHelper
}
// New creates an etcd registry.
func New(helper tools.EtcdHelper) *Etcd {
return &Etcd{
EtcdHelper: helper,
}
}
// ListDeployments obtains a list of Deployments.
func (r *Etcd) ListDeployments(ctx kapi.Context, label labels.Selector, field fields.Selector) (*api.DeploymentList, error) {
deployments := api.DeploymentList{}
err := r.ExtractToList(makeDeploymentListKey(ctx), &deployments)
if err != nil {
return nil, err
}
filtered := []api.Deployment{}
for _, item := range deployments.Items {
fields := labels.Set{
"name": item.Name,
"status": string(item.Status),
}
if label.Matches(labels.Set(item.Labels)) && field.Matches(fields) {
filtered = append(filtered, item)
}
}
deployments.Items = filtered
return &deployments, err
}
func makeDeploymentListKey(ctx kapi.Context) string {
return kubeetcd.MakeEtcdListKey(ctx, DeploymentPath)
}
func makeDeploymentKey(ctx kapi.Context, id string) (string, error) {
return kubeetcd.MakeEtcdItemKey(ctx, DeploymentPath, id)
}
// GetDeployment gets a specific Deployment specified by its ID.
func (r *Etcd) GetDeployment(ctx kapi.Context, id string) (*api.Deployment, error) {
var deployment api.Deployment
key, err := makeDeploymentKey(ctx, id)
if err != nil {
return nil, err
}
err = r.ExtractObj(key, &deployment, false)
if err != nil {
return nil, etcderr.InterpretGetError(err, "deployment", id)
}
return &deployment, nil
}
// CreateDeployment creates a new Deployment.
func (r *Etcd) CreateDeployment(ctx kapi.Context, deployment *api.Deployment) error {
key, err := makeDeploymentKey(ctx, deployment.Name)
if err != nil {
return err
}
err = r.CreateObj(key, deployment, nil, 0)
return etcderr.InterpretCreateError(err, "deployment", deployment.Name)
}
// UpdateDeployment replaces an existing Deployment.
func (r *Etcd) UpdateDeployment(ctx kapi.Context, deployment *api.Deployment) error {
key, err := makeDeploymentKey(ctx, deployment.Name)
if err != nil {
return err
}
err = r.SetObj(key, deployment, nil, 0)
return etcderr.InterpretUpdateError(err, "deployment", deployment.Name)
}
// DeleteDeployment deletes a Deployment specified by its ID.
func (r *Etcd) DeleteDeployment(ctx kapi.Context, id string) error {
key, err := makeDeploymentKey(ctx, id)
if err != nil {
return err
}
err = r.Delete(key, false)
return etcderr.InterpretDeleteError(err, "deployment", id)
}
// WatchDeployments begins watching for new, changed, or deleted Deployments.
func (r *Etcd) WatchDeployments(ctx kapi.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
version, err := tools.ParseWatchResourceVersion(resourceVersion, "deployment")
if err != nil {
return nil, err
}
return r.WatchList(makeDeploymentListKey(ctx), version, func(obj runtime.Object) bool {
deployment, ok := obj.(*api.Deployment)
if !ok {
glog.Errorf("Unexpected object during deployment watch: %#v", obj)
return false
}
fields := labels.Set{
"name": deployment.Name,
"status": string(deployment.Status),
}
return label.Matches(labels.Set(deployment.Labels)) && field.Matches(fields)
})
}
// ListDeploymentConfigs obtains a list of DeploymentConfigs.
func (r *Etcd) ListDeploymentConfigs(ctx kapi.Context, label labels.Selector, field fields.Selector) (*api.DeploymentConfigList, error) {
deploymentConfigs := api.DeploymentConfigList{}
err := r.ExtractToList(makeDeploymentConfigListKey(ctx), &deploymentConfigs)
if err != nil {
return nil, err
}
filtered := []api.DeploymentConfig{}
for _, item := range deploymentConfigs.Items {
fields := labels.Set{
"name": item.Name,
}
if label.Matches(labels.Set(item.Labels)) && field.Matches(fields) {
filtered = append(filtered, item)
}
}
deploymentConfigs.Items = filtered
return &deploymentConfigs, err
}
// WatchDeploymentConfigs begins watching for new, changed, or deleted DeploymentConfigs.
func (r *Etcd) WatchDeploymentConfigs(ctx kapi.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
version, err := tools.ParseWatchResourceVersion(resourceVersion, "deploymentConfig")
if err != nil {
return nil, err
}
return r.WatchList(makeDeploymentConfigListKey(ctx), version, func(obj runtime.Object) bool {
config, ok := obj.(*api.DeploymentConfig)
if !ok {
glog.Errorf("Unexpected object during deploymentConfig watch: %#v", obj)
return false
}
fields := labels.Set{
"name": config.Name,
}
return label.Matches(labels.Set(config.Labels)) && field.Matches(fields)
})
}
func makeDeploymentConfigListKey(ctx kapi.Context) string {
return kubeetcd.MakeEtcdListKey(ctx, DeploymentConfigPath)
}
func makeDeploymentConfigKey(ctx kapi.Context, id string) (string, error) {
return kubeetcd.MakeEtcdItemKey(ctx, DeploymentConfigPath, id)
}
// GetDeploymentConfig gets a specific DeploymentConfig specified by its ID.
func (r *Etcd) GetDeploymentConfig(ctx kapi.Context, id string) (*api.DeploymentConfig, error) {
var deploymentConfig api.DeploymentConfig
key, err := makeDeploymentConfigKey(ctx, id)
if err != nil {
return nil, err
}
err = r.ExtractObj(key, &deploymentConfig, false)
if err != nil {
return nil, etcderr.InterpretGetError(err, "deploymentConfig", id)
}
return &deploymentConfig, nil
}
// CreateDeploymentConfig creates a new DeploymentConfig.
func (r *Etcd) CreateDeploymentConfig(ctx kapi.Context, deploymentConfig *api.DeploymentConfig) error {
key, err := makeDeploymentConfigKey(ctx, deploymentConfig.Name)
if err != nil {
return err
}
err = r.CreateObj(key, deploymentConfig, nil, 0)
return etcderr.InterpretCreateError(err, "deploymentConfig", deploymentConfig.Name)
}
// UpdateDeploymentConfig replaces an existing DeploymentConfig.
func (r *Etcd) UpdateDeploymentConfig(ctx kapi.Context, deploymentConfig *api.DeploymentConfig) error {
key, err := makeDeploymentConfigKey(ctx, deploymentConfig.Name)
if err != nil {
return err
}
err = r.SetObj(key, deploymentConfig, nil, 0)
return etcderr.InterpretUpdateError(err, "deploymentConfig", deploymentConfig.Name)
}
// DeleteDeploymentConfig deletes a DeploymentConfig specified by its ID.
func (r *Etcd) DeleteDeploymentConfig(ctx kapi.Context, id string) error {
key, err := makeDeploymentConfigKey(ctx, id)
if err != nil {
return err
}
err = r.Delete(key, false)
return etcderr.InterpretDeleteError(err, "deploymentConfig", id)
}