-
Notifications
You must be signed in to change notification settings - Fork 11
/
filter.go
192 lines (157 loc) · 5.45 KB
/
filter.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package state
import (
"context"
"github.com/cosi-project/runtime/pkg/resource"
)
// Filter state access by some rules.
//
// Filter allows building RBAC access pattern or any other kinds of restictions.
func Filter(coreState CoreState, rule FilteringRule) CoreState { //nolint:ireturn
return &stateFilter{
state: coreState,
rule: rule,
}
}
// FilteringRule defines a function which gets invoked on each state access.
//
// Function might allow access by returning nil or deny access by returning an error
// which will be returned to the caller.
type FilteringRule func(ctx context.Context, access Access) error
// Access describes state API access in a generic way.
type Access struct {
ResourceNamespace resource.Namespace
ResourceType resource.Type
ResourceID resource.ID
Verb Verb
}
// Verb is API verb.
type Verb int
// Verb definitions.
const (
Get Verb = iota
List
Watch
Create
Update
Destroy
)
// Readonly returns true for verbs which don't modify data.
func (verb Verb) Readonly() bool {
return verb == Get || verb == List || verb == Watch
}
type stateFilter struct {
state CoreState
rule FilteringRule
}
// Get a resource by type and ID.
//
// If a resource is not found, error is returned.
func (filter *stateFilter) Get(ctx context.Context, resourcePointer resource.Pointer, opts ...GetOption) (resource.Resource, error) { //nolint:ireturn
if err := filter.rule(ctx, Access{
ResourceNamespace: resourcePointer.Namespace(),
ResourceType: resourcePointer.Type(),
ResourceID: resourcePointer.ID(),
Verb: Get,
}); err != nil {
return nil, err
}
return filter.state.Get(ctx, resourcePointer, opts...)
}
// List resources by type.
func (filter *stateFilter) List(ctx context.Context, resourceKind resource.Kind, opts ...ListOption) (resource.List, error) {
if err := filter.rule(ctx, Access{
ResourceNamespace: resourceKind.Namespace(),
ResourceType: resourceKind.Type(),
Verb: List,
}); err != nil {
return resource.List{}, err
}
return filter.state.List(ctx, resourceKind, opts...)
}
// Create a resource.
//
// If a resource already exists, Create returns an error.
func (filter *stateFilter) Create(ctx context.Context, res resource.Resource, opts ...CreateOption) error {
if err := filter.rule(ctx, Access{
ResourceNamespace: res.Metadata().Namespace(),
ResourceType: res.Metadata().Type(),
ResourceID: res.Metadata().ID(),
Verb: Create,
}); err != nil {
return err
}
return filter.state.Create(ctx, res, opts...)
}
// Update a resource.
//
// If a resource doesn't exist, error is returned.
// On update current version of resource `new` in the state should match
// the version on the backend, otherwise conflict error is returned.
func (filter *stateFilter) Update(ctx context.Context, newResource resource.Resource, opts ...UpdateOption) error {
if err := filter.rule(ctx, Access{
ResourceNamespace: newResource.Metadata().Namespace(),
ResourceType: newResource.Metadata().Type(),
ResourceID: newResource.Metadata().ID(),
Verb: Update,
}); err != nil {
return err
}
return filter.state.Update(ctx, newResource, opts...)
}
// Destroy a resource.
//
// If a resource doesn't exist, error is returned.
// If a resource has pending finalizers, error is returned.
func (filter *stateFilter) Destroy(ctx context.Context, resourcePointer resource.Pointer, opts ...DestroyOption) error {
if err := filter.rule(ctx, Access{
ResourceNamespace: resourcePointer.Namespace(),
ResourceType: resourcePointer.Type(),
ResourceID: resourcePointer.ID(),
Verb: Destroy,
}); err != nil {
return err
}
return filter.state.Destroy(ctx, resourcePointer, opts...)
}
// Watch state of a resource by type.
//
// It's fine to watch for a resource which doesn't exist yet.
// Watch is canceled when context gets canceled.
// Watch sends initial resource state as the very first event on the channel,
// and then sends any updates to the resource as events.
func (filter *stateFilter) Watch(ctx context.Context, resourcePointer resource.Pointer, ch chan<- Event, opts ...WatchOption) error {
if err := filter.rule(ctx, Access{
ResourceNamespace: resourcePointer.Namespace(),
ResourceType: resourcePointer.Type(),
ResourceID: resourcePointer.ID(),
Verb: Watch,
}); err != nil {
return err
}
return filter.state.Watch(ctx, resourcePointer, ch, opts...)
}
// WatchKind watches resources of specific kind (namespace and type).
func (filter *stateFilter) WatchKind(ctx context.Context, resourceKind resource.Kind, ch chan<- Event, opts ...WatchKindOption) error {
if err := filter.rule(ctx, Access{
ResourceNamespace: resourceKind.Namespace(),
ResourceType: resourceKind.Type(),
Verb: Watch,
}); err != nil {
return err
}
return filter.state.WatchKind(ctx, resourceKind, ch, opts...)
}
// WatchKindAggregate watches resources of specific kind (namespace and type).
func (filter *stateFilter) WatchKindAggregated(ctx context.Context, resourceKind resource.Kind, ch chan<- []Event, opts ...WatchKindOption) error {
if err := filter.rule(ctx, Access{
ResourceNamespace: resourceKind.Namespace(),
ResourceType: resourceKind.Type(),
Verb: Watch,
}); err != nil {
return err
}
return filter.state.WatchKindAggregated(ctx, resourceKind, ch, opts...)
}