-
Notifications
You must be signed in to change notification settings - Fork 41
/
memory.go
148 lines (131 loc) · 3.76 KB
/
memory.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
package ipam
import (
"context"
"fmt"
"sync"
)
type memory struct {
prefixes map[string]map[string]Prefix
lock sync.RWMutex
}
// NewMemory create a memory storage for ipam
func NewMemory(ctx context.Context) Storage {
m := &memory{
prefixes: make(map[string]map[string]Prefix),
lock: sync.RWMutex{},
}
_ = m.CreateNamespace(ctx, defaultNamespace)
return m
}
func (m *memory) Name() string {
return "memory"
}
func (m *memory) CreatePrefix(_ context.Context, prefix Prefix, namespace string) (Prefix, error) {
m.lock.Lock()
defer m.lock.Unlock()
if _, ok := m.prefixes[namespace]; !ok {
return Prefix{}, ErrNamespaceDoesNotExist
}
_, ok := m.prefixes[namespace][prefix.Cidr]
if ok {
return Prefix{}, fmt.Errorf("prefix already created:%v", prefix)
}
m.prefixes[namespace][prefix.Cidr] = *prefix.deepCopy()
return prefix, nil
}
func (m *memory) ReadPrefix(_ context.Context, prefix, namespace string) (Prefix, error) {
m.lock.RLock()
defer m.lock.RUnlock()
if _, ok := m.prefixes[namespace]; !ok {
return Prefix{}, ErrNamespaceDoesNotExist
}
result, ok := m.prefixes[namespace][prefix]
if !ok {
return Prefix{}, fmt.Errorf("%w prefix %s not found", ErrNotFound, prefix)
}
return *result.deepCopy(), nil
}
func (m *memory) DeleteAllPrefixes(_ context.Context, namespace string) error {
m.lock.RLock()
defer m.lock.RUnlock()
m.prefixes[namespace] = make(map[string]Prefix)
return nil
}
func (m *memory) ReadAllPrefixes(_ context.Context, namespace string) (Prefixes, error) {
m.lock.RLock()
defer m.lock.RUnlock()
if _, ok := m.prefixes[namespace]; !ok {
return nil, ErrNamespaceDoesNotExist
}
ps := make([]Prefix, 0, len(m.prefixes[namespace]))
for _, v := range m.prefixes[namespace] {
ps = append(ps, *v.deepCopy())
}
return ps, nil
}
func (m *memory) ReadAllPrefixCidrs(_ context.Context, namespace string) ([]string, error) {
m.lock.RLock()
defer m.lock.RUnlock()
if _, ok := m.prefixes[namespace]; !ok {
return nil, ErrNamespaceDoesNotExist
}
ps := make([]string, 0, len(m.prefixes[namespace]))
for _, v := range m.prefixes[namespace] {
ps = append(ps, v.Cidr)
}
return ps, nil
}
func (m *memory) UpdatePrefix(_ context.Context, prefix Prefix, namespace string) (Prefix, error) {
m.lock.Lock()
defer m.lock.Unlock()
oldVersion := prefix.version
prefix.version = oldVersion + 1
if prefix.Cidr == "" {
return Prefix{}, fmt.Errorf("prefix not present:%v", prefix)
}
if _, ok := m.prefixes[namespace]; !ok {
return Prefix{}, ErrNamespaceDoesNotExist
}
oldPrefix, ok := m.prefixes[namespace][prefix.Cidr]
if !ok {
return Prefix{}, fmt.Errorf("prefix not found:%s", prefix.Cidr)
}
if oldPrefix.version != oldVersion {
return Prefix{}, fmt.Errorf("%w: unable to update prefix:%s", ErrOptimisticLockError, prefix.Cidr)
}
m.prefixes[namespace][prefix.Cidr] = *prefix.deepCopy()
return prefix, nil
}
func (m *memory) DeletePrefix(_ context.Context, prefix Prefix, namespace string) (Prefix, error) {
m.lock.Lock()
defer m.lock.Unlock()
if _, ok := m.prefixes[namespace]; !ok {
return Prefix{}, ErrNamespaceDoesNotExist
}
delete(m.prefixes[namespace], prefix.Cidr)
return *prefix.deepCopy(), nil
}
func (m *memory) CreateNamespace(_ context.Context, namespace string) error {
m.lock.Lock()
defer m.lock.Unlock()
if _, ok := m.prefixes[namespace]; !ok {
m.prefixes[namespace] = make(map[string]Prefix)
}
return nil
}
func (m *memory) ListNamespaces(_ context.Context) ([]string, error) {
m.lock.Lock()
defer m.lock.Unlock()
var result []string
for n := range m.prefixes {
result = append(result, n)
}
return result, nil
}
func (m *memory) DeleteNamespace(_ context.Context, namespace string) error {
if _, ok := m.prefixes[namespace]; !ok {
return ErrNamespaceDoesNotExist
}
delete(m.prefixes, namespace)
return nil
}