-
Notifications
You must be signed in to change notification settings - Fork 55
/
bindingpolicy-resolver.go
327 lines (274 loc) · 12.9 KB
/
bindingpolicy-resolver.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
Copyright 2023 The KubeStellar Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package binding
import (
"fmt"
"strings"
"sync"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/kubestellar/kubestellar/api/control/v1alpha1"
"github.com/kubestellar/kubestellar/pkg/util"
)
const bindingPolicyResolutionNotFoundErrorPrefix = "bindingpolicy resolution is not found"
// A BindingPolicyResolver holds a collection of bindingpolicy resolutions.
// The collection is indexed by bindingPolicyKey strings, which are the names of
// the bindingpolicy objects. The resolution for a given key can be updated,
// exported and compared to the binding representation.
// All functions in this interface are thread-safe, and nothing mutates any
// method-parameter during a call to one of them.
type BindingPolicyResolver interface {
// GenerateBinding returns the binding for the given
// bindingpolicy key.
//
// If no resolution is associated with the given key, nil is returned.
GenerateBinding(bindingPolicyKey string) *v1alpha1.BindingSpec
// GetOwnerReference returns the owner reference for the given
// bindingpolicy key. If no resolution is associated with the given key, an
// error is returned.
GetOwnerReference(bindingPolicyKey string) (metav1.OwnerReference, error)
// CompareBinding compares the given binding spec
// with the maintained binding for the given bindingpolicy key.
// The returned value is true only if:
//
// - The destinations in the BindingSpec are an exact match
//of those in the resolution.
//
// - The same is true for every selected object.
//
// It is possible to output a false negative due to a temporary state of
// internal caches being out of sync.
CompareBinding(bindingPolicyKey string,
bindingSpec *v1alpha1.BindingSpec) bool
// NoteBindingPolicy associates a new resolution with the given
// bindingpolicy, if none is associated. This method maintains the
// singleton status reporting requirement in the resolution.
NoteBindingPolicy(bindingpolicy *v1alpha1.BindingPolicy)
// EnsureObjectIdentifierWithVersion ensures that an object's identifier is
// in the resolution for the given bindingpolicy key, and is associated
// with the given resourceVersion.
//
// The returned bool indicates whether the bindingpolicy resolution was
// changed. If no resolution is associated with the given key, an error is
// returned.
EnsureObjectIdentifierWithVersion(bindingPolicyKey string, objIdentifier util.ObjectIdentifier,
resourceVersion string) (bool, error)
// RemoveObjectIdentifier ensures the absence of the given object
// identifier from the resolution for the given bindingpolicy key.
//
// The returned bool indicates whether the bindingpolicy resolution was
// changed. If no resolution is associated with the given key, false is
// returned.
RemoveObjectIdentifier(bindingPolicyKey string, objIdentifier util.ObjectIdentifier) bool
// GetObjectIdentifiers returns the object identifiers associated with the
// given bindingpolicy key.
// If no resolution is associated with the given key, an error is returned.
GetObjectIdentifiers(bindingPolicyKey string) (sets.Set[util.ObjectIdentifier], error)
// SetDestinations updates the maintained bindingpolicy's
// destinations resolution for the given bindingpolicy key.
// The given destinations set is expected not to be mutated during and
// after this call by the caller.
// If no resolution is associated with the given key, an error is returned.
SetDestinations(bindingPolicyKey string, destinations sets.Set[string]) error
// ResolutionExists returns true if a resolution is associated with the
// given bindingpolicy key.
ResolutionExists(bindingPolicyKey string) bool
// ResolutionRequiresSingletonReportedState returns true if the
// bindingpolicy associated with the given key requires a singleton
// reported state, and it satisfies the conditions on this requirement.
//
// This means that if true is returned, then the singleton status reporting
// requirement is effective.
ResolutionRequiresSingletonReportedState(bindingPolicyKey string) bool
// DeleteResolution deletes the resolution associated with the given key,
// if it exists.
DeleteResolution(bindingPolicyKey string)
}
func NewBindingPolicyResolver() BindingPolicyResolver {
return &bindingPolicyResolver{
bindingPolicyToResolution: make(map[string]*bindingPolicyResolution),
}
}
type bindingPolicyResolver struct {
sync.RWMutex
bindingPolicyToResolution map[string]*bindingPolicyResolution
}
// GenerateBinding returns the binding for the given
// bindingpolicy key.
//
// If no resolution is associated with the given key, nil is returned.
func (resolver *bindingPolicyResolver) GenerateBinding(bindingPolicyKey string) *v1alpha1.BindingSpec {
bindingPolicyResolution := resolver.getResolution(bindingPolicyKey) // thread-safe
if bindingPolicyResolution == nil {
return nil
}
// thread-safe
return bindingPolicyResolution.toBindingSpec()
}
// GetOwnerReference returns the owner reference for the given
// bindingpolicy key. If no resolution is associated with the given key, an
// error is returned.
func (resolver *bindingPolicyResolver) GetOwnerReference(bindingPolicyKey string) (metav1.OwnerReference, error) {
bindingPolicyResolution := resolver.getResolution(bindingPolicyKey) // thread-safe
if bindingPolicyResolution == nil {
return metav1.OwnerReference{}, fmt.Errorf("%s - bindingpolicy-key: %s",
bindingPolicyResolutionNotFoundErrorPrefix, bindingPolicyKey)
}
return *bindingPolicyResolution.ownerReference, nil
}
// CompareBinding compares the given binding spec
// with the maintained binding for the given bindingpolicy key.
// The returned value is true only if:
//
// - The destinations in the BindingSpec are an exact match
// of those in the resolution.
//
// - The same is true for every selected object.
//
// It is possible to output a false negative due to a temporary state of
// internal caches being out of sync.
func (resolver *bindingPolicyResolver) CompareBinding(bindingPolicyKey string,
bindingSpec *v1alpha1.BindingSpec) bool {
bindingPolicyResolution := resolver.getResolution(bindingPolicyKey) // thread-safe
if bindingPolicyResolution == nil {
return false
}
return bindingPolicyResolution.matchesBindingSpec(bindingSpec)
}
// NoteBindingPolicy associates a new resolution with the given
// bindingpolicy, if none is associated. This method maintains the
// singleton status reporting requirement in the resolution.
func (resolver *bindingPolicyResolver) NoteBindingPolicy(bindingpolicy *v1alpha1.BindingPolicy) {
if resolution := resolver.getResolution(bindingpolicy.GetName()); resolution != nil {
resolution.requiresSingletonReportedState = bindingpolicy.Spec.WantSingletonReportedState
return
}
resolver.createResolution(bindingpolicy)
}
// EnsureObjectIdentifierWithVersion ensures that an object's identifier is
// in the resolution for the given bindingpolicy key, and is associated
// with the given resourceVersion.
//
// The returned bool indicates whether the bindingpolicy resolution was
// changed. If no resolution is associated with the given key, an error is
// returned.
func (resolver *bindingPolicyResolver) EnsureObjectIdentifierWithVersion(bindingPolicyKey string,
objIdentifier util.ObjectIdentifier, resourceVersion string) (bool, error) {
bindingPolicyResolution := resolver.getResolution(bindingPolicyKey) // thread-safe
if bindingPolicyResolution == nil {
// bindingPolicyKey is not associated with any resolution
return false, fmt.Errorf("%s - bindingpolicy-key: %s", bindingPolicyResolutionNotFoundErrorPrefix,
bindingPolicyKey)
}
// ensureObjectIdentifier is thread-safe
return bindingPolicyResolution.ensureObjectIdentifierWithVersion(objIdentifier, resourceVersion), nil
}
// RemoveObjectIdentifier ensures the absence of the given object
// identifier from the resolution for the given bindingpolicy key.
//
// The returned bool indicates whether the bindingpolicy resolution was
// changed. If no resolution is associated with the given key, false is
// returned.
func (resolver *bindingPolicyResolver) RemoveObjectIdentifier(bindingPolicyKey string,
objIdentifier util.ObjectIdentifier) bool {
bindingPolicyResolution := resolver.getResolution(bindingPolicyKey) // thread-safe
if bindingPolicyResolution == nil {
return false
}
// removeObjectIdentifier is thread-safe
return bindingPolicyResolution.removeObjectIdentifier(objIdentifier)
}
// GetObjectIdentifiers returns a copy of the object identifiers associated
// with the given bindingpolicy key.
// If no resolution is associated with the given key, an error is returned.
func (resolver *bindingPolicyResolver) GetObjectIdentifiers(bindingPolicyKey string) (sets.Set[util.ObjectIdentifier],
error) {
bindingPolicyResolution := resolver.getResolution(bindingPolicyKey) // thread-safe
if bindingPolicyResolution == nil {
return nil, fmt.Errorf("%s - bindingpolicy-key: %s", bindingPolicyResolutionNotFoundErrorPrefix,
bindingPolicyKey)
}
// getObjectIdentifiers is thread-safe
return bindingPolicyResolution.getObjectIdentifiers(), nil
}
// SetDestinations updates the maintained bindingpolicy's
// destinations resolution for the given bindingpolicy key.
// The given destinations set is expected not to be mutated during and
// after this call by the caller.
// If no resolution is associated with the given key, an error is returned.
func (resolver *bindingPolicyResolver) SetDestinations(bindingPolicyKey string,
destinations sets.Set[string]) error {
bindingPolicyResolution := resolver.getResolution(bindingPolicyKey) // thread-safe
if bindingPolicyResolution == nil {
return fmt.Errorf("%s - bindingpolicy-key: %s", bindingPolicyResolutionNotFoundErrorPrefix,
bindingPolicyKey)
}
bindingPolicyResolution.setDestinations(destinations)
return nil
}
// ResolutionExists returns true if a resolution is associated with the
// given bindingpolicy key.
func (resolver *bindingPolicyResolver) ResolutionExists(bindingPolicyKey string) bool {
if resolver.getResolution(bindingPolicyKey) == nil {
return false
}
return true
}
// ResolutionRequiresSingletonReportedState returns true if the
// bindingpolicy associated with the given key requires a singleton
// reported state, and it satisfies the conditions on this requirement.
//
// This means that if true is returned, then the singleton status reporting
// requirement is effective.
func (resolver *bindingPolicyResolver) ResolutionRequiresSingletonReportedState(bindingPolicyKey string) bool {
bindingPolicyResolution := resolver.getResolution(bindingPolicyKey) // thread-safe
if bindingPolicyResolution == nil {
return false
}
return bindingPolicyResolution.requiresSingletonReportedState
}
// DeleteResolution deletes the resolution associated with the given key,
// if it exists.
func (resolver *bindingPolicyResolver) DeleteResolution(bindingPolicyKey string) {
resolver.Lock() // lock for modifying map
defer resolver.Unlock()
delete(resolver.bindingPolicyToResolution, bindingPolicyKey)
}
// getResolution retrieves the resolution associated with the given key.
// If the resolution does not exist, nil is returned.
func (resolver *bindingPolicyResolver) getResolution(bindingPolicyKey string) *bindingPolicyResolution {
resolver.RLock() // lock for reading map
defer resolver.RUnlock() // unlock after accessing map
return resolver.bindingPolicyToResolution[bindingPolicyKey]
}
func (resolver *bindingPolicyResolver) createResolution(bindingpolicy *v1alpha1.BindingPolicy) *bindingPolicyResolution {
resolver.Lock() // lock for modifying map
defer resolver.Unlock()
// double-check existence to handle race conditions (common pattern)
if bindingPolicyResolution, exists := resolver.bindingPolicyToResolution[bindingpolicy.GetName()]; exists {
return bindingPolicyResolution
}
ownerReference := metav1.NewControllerRef(bindingpolicy, bindingpolicy.GroupVersionKind())
ownerReference.BlockOwnerDeletion = &[]bool{false}[0]
bindingPolicyResolution := &bindingPolicyResolution{
objectIdentifierToResourceVersion: make(map[util.ObjectIdentifier]string),
destinations: sets.New[string](),
ownerReference: ownerReference,
requiresSingletonReportedState: bindingpolicy.Spec.WantSingletonReportedState,
}
resolver.bindingPolicyToResolution[bindingpolicy.GetName()] = bindingPolicyResolution
return bindingPolicyResolution
}
func errorIsBindingPolicyResolutionNotFound(err error) bool {
return strings.HasPrefix(err.Error(), bindingPolicyResolutionNotFoundErrorPrefix)
}