forked from awslabs/mountpoint-s3-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpectations.go
More file actions
71 lines (63 loc) · 2.38 KB
/
expectations.go
File metadata and controls
71 lines (63 loc) · 2.38 KB
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
package csicontroller
import (
"sort"
"strings"
"sync"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Expectations is a structure that manages pending expectations for Kubernetes resources.
// It uses field filters as keys to track resources that are expected to be created
// helping to reduce unnecessary processing and API server load.
type Expectations struct {
pending sync.Map
}
// NewExpectations creates and returns a new Expectations instance.
func NewExpectations() *Expectations {
return &Expectations{}
}
// SetPending marks a resource as pending based on the given field filters.
// This is typically used when a create operation is initiated.
func (e *Expectations) SetPending(fieldFilters client.MatchingFields) {
key := deriveExpectationKeyFromFilters(fieldFilters)
e.pending.Store(key, struct{}{})
}
// IsPending checks if a resource is marked as pending based on the given field filters.
// Returns true if the resource is pending, false otherwise.
func (e *Expectations) IsPending(fieldFilters client.MatchingFields) bool {
key := deriveExpectationKeyFromFilters(fieldFilters)
_, ok := e.pending.Load(key)
return ok
}
// Clear removes the pending mark for a resource based on the given field filters.
// This is typically called when an expected operation has been confirmed as completed.
func (e *Expectations) Clear(fieldFilters client.MatchingFields) {
key := deriveExpectationKeyFromFilters(fieldFilters)
e.pending.Delete(key)
}
// deriveExpectationKeyFromFilters generates a deterministic string key from a map of field filters.
// It creates a consistent string representation of the filters by:
// 1. Sorting the filter keys alphabetically
// 2. Concatenating each key-value pair in the format "key=value;"
//
// For example, given filters {"foo": "bar", "baz": "qux"}, it will produce "baz=qux;foo=bar;"
//
// Parameters:
// - fieldFilters: A map of field names to their values used for filtering Kubernetes resources
//
// Returns:
// - A string that uniquely represents the combination of field filters
func deriveExpectationKeyFromFilters(fieldFilters client.MatchingFields) string {
keys := make([]string, 0, len(fieldFilters))
for k := range fieldFilters {
keys = append(keys, k)
}
sort.Strings(keys)
var sb strings.Builder
for _, k := range keys {
sb.WriteString(k)
sb.WriteString("=")
sb.WriteString(fieldFilters[k])
sb.WriteString(";")
}
return sb.String()
}