forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
128 lines (110 loc) · 3.61 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
package etcd
import (
"fmt"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
etcderr "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors/etcd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
kubeetcd "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/etcd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/openshift/origin/pkg/route/api"
)
const (
// RoutePath is the path to route image in etcd
RoutePath string = "/routes"
)
// Etcd implements route.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 makeRouteListKey(ctx kapi.Context) string {
return kubeetcd.MakeEtcdListKey(ctx, RoutePath)
}
func makeRouteKey(ctx kapi.Context, id string) (string, error) {
return kubeetcd.MakeEtcdItemKey(ctx, RoutePath, id)
}
// ListRoutes obtains a list of Routes.
func (registry *Etcd) ListRoutes(ctx kapi.Context, selector labels.Selector) (*api.RouteList, error) {
allRoutes := api.RouteList{}
err := registry.ExtractToList(makeRouteListKey(ctx), &allRoutes)
if err != nil {
return nil, err
}
filtered := []api.Route{}
for _, route := range allRoutes.Items {
if selector.Matches(labels.Set(route.Labels)) {
filtered = append(filtered, route)
}
}
allRoutes.Items = filtered
return &allRoutes, nil
}
// GetRoute gets a specific Route specified by its ID.
func (registry *Etcd) GetRoute(ctx kapi.Context, routeID string) (*api.Route, error) {
route := api.Route{}
key, err := makeRouteKey(ctx, routeID)
if err != nil {
return nil, err
}
err = registry.ExtractObj(key, &route, false)
if err != nil {
return nil, etcderr.InterpretGetError(err, "route", routeID)
}
return &route, nil
}
// CreateRoute creates a new Route.
func (registry *Etcd) CreateRoute(ctx kapi.Context, route *api.Route) error {
key, err := makeRouteKey(ctx, route.Name)
if err != nil {
return err
}
err = registry.CreateObj(key, route, nil, 0)
return etcderr.InterpretCreateError(err, "route", route.Name)
}
// UpdateRoute replaces an existing Route.
func (registry *Etcd) UpdateRoute(ctx kapi.Context, route *api.Route) error {
key, err := makeRouteKey(ctx, route.Name)
if err != nil {
return err
}
err = registry.SetObj(key, route, nil, 0)
return etcderr.InterpretUpdateError(err, "route", route.Name)
}
// DeleteRoute deletes a Route specified by its ID.
func (registry *Etcd) DeleteRoute(ctx kapi.Context, routeID string) error {
key, err := makeRouteKey(ctx, routeID)
if err != nil {
return err
}
err = registry.Delete(key, false)
return etcderr.InterpretDeleteError(err, "route", routeID)
}
// WatchRoutes begins watching for new, changed, or deleted route configurations.
func (registry *Etcd) WatchRoutes(ctx kapi.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
if !label.Empty() {
return nil, fmt.Errorf("label selectors are not supported on routes yet")
}
version, err := tools.ParseWatchResourceVersion(resourceVersion, "pod")
if err != nil {
return nil, err
}
if value, found := field.RequiresExactMatch("ID"); found {
key, err := makeRouteKey(ctx, value)
if err != nil {
return nil, err
}
return registry.Watch(key, version, tools.Everything)
}
if field.Empty() {
key := kubeetcd.MakeEtcdListKey(ctx, RoutePath)
return registry.WatchList(key, version, tools.Everything)
}
return nil, fmt.Errorf("only the 'ID' and default (everything) field selectors are supported")
}