/
map_validation.go
241 lines (206 loc) · 6.22 KB
/
map_validation.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
package v1alpha1
import (
"encoding/json"
"fmt"
"reflect"
n "github.com/hazelcast/hazelcast-platform-operator/internal/naming"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type mapValidator struct {
datastructValidator
}
func NewMapValidator(o client.Object) mapValidator {
return mapValidator{NewDatastructValidator(o)}
}
func ValidateMapSpecCreate(m *Map) error {
v := NewMapValidator(m)
v.validateDataStructureSpec(&m.Spec.DataStructureSpec)
return v.Err()
}
func ValidateMapSpecUpdate(m *Map) error {
v := NewMapValidator(m)
v.validateMapSpecUpdate(m)
v.validateDataStructureSpec(&m.Spec.DataStructureSpec)
return v.Err()
}
func ValidateMapSpec(m *Map, h *Hazelcast) error {
v := NewMapValidator(m)
v.validateMapSpecCurrent(m, h)
v.validateMapSpecUpdate(m)
v.validateDataStructureSpec(&m.Spec.DataStructureSpec)
return v.Err()
}
func (v *mapValidator) validateMapSpecCurrent(m *Map, h *Hazelcast) {
v.validateMapPersistence(m, h)
v.validateMapNativeMemory(m, h)
v.validateNearCacheMemory(m, h)
v.validateMapTieredStore(m, h)
}
func (v *mapValidator) validateMapPersistence(m *Map, h *Hazelcast) {
if !m.Spec.PersistenceEnabled {
return
}
lastSpec := v.getHzSpec(h)
if lastSpec == nil {
return
}
if !lastSpec.Persistence.IsEnabled() {
v.Invalid(Path("spec", "persistenceEnabled"), lastSpec.Persistence.IsEnabled(), "Persistence must be enabled at Hazelcast")
return
}
}
func (v *mapValidator) validateNearCacheMemory(m *Map, h *Hazelcast) {
if m.Spec.NearCache == nil {
return
}
if m.Spec.NearCache.InMemoryFormat != InMemoryFormatNative {
return
}
lastSpec := v.getHzSpec(h)
if lastSpec == nil {
return
}
if !lastSpec.NativeMemory.IsEnabled() {
v.Invalid(Path("spec", "inMemoryFormat"), lastSpec.NativeMemory.IsEnabled(), "Native Memory must be enabled at Hazelcast")
return
}
}
func (v *mapValidator) validateMapNativeMemory(m *Map, h *Hazelcast) {
if m.Spec.InMemoryFormat != InMemoryFormatNative {
return
}
lastSpec := v.getHzSpec(h)
if lastSpec == nil {
return
}
if !lastSpec.NativeMemory.IsEnabled() {
v.Invalid(Path("spec", "inMemoryFormat"), lastSpec.NativeMemory.IsEnabled(), "Native Memory must be enabled at Hazelcast")
return
}
}
func (v *mapValidator) validateMapTieredStore(m *Map, h *Hazelcast) {
if m.Spec.TieredStore == nil {
return
}
if m.Spec.InMemoryFormat != InMemoryFormatNative {
v.Invalid(Path("spec", "inMemoryFormat"), m.Spec.InMemoryFormat, "In-memory format of the map must be NATIVE to enable the Tiered Storage")
}
if len(m.Spec.Indexes) != 0 {
v.Invalid(Path("spec", "indexes"), m.Spec.Indexes, "Indexes can not be created on maps that have Tiered Storage enabled")
}
lastSpec := v.getHzSpec(h)
if lastSpec == nil {
return
}
if !deviceExist(lastSpec.LocalDevices, m.Spec.TieredStore.DiskDeviceName) {
v.Invalid(Path("spec", "tieredStore", "diskDeviceName"), m.Spec.TieredStore.DiskDeviceName, fmt.Sprintf("device with the name %s does not exist", m.Spec.TieredStore.DiskDeviceName))
}
}
func (v *mapValidator) validateMapSpecUpdate(m *Map) {
last, ok := m.ObjectMeta.Annotations[n.LastSuccessfulSpecAnnotation]
if !ok {
return
}
var parsed MapSpec
if err := json.Unmarshal([]byte(last), &parsed); err != nil {
v.InternalError(Path("spec"), fmt.Errorf("error parsing last Map spec for update errors: %w", err))
return
}
v.validateNotUpdatableMapFields(&m.Spec, &parsed)
}
func (v *mapValidator) validateNotUpdatableMapFields(current *MapSpec, last *MapSpec) {
if current.Name != last.Name {
v.Forbidden(Path("spec", "name"), "field cannot be updated")
}
if *current.BackupCount != *last.BackupCount {
v.Forbidden(Path("spec", "backupCount"), "field cannot be updated")
}
if current.AsyncBackupCount != last.AsyncBackupCount {
v.Forbidden(Path("spec", "asyncBackupCount"), "field cannot be updated")
}
if !indexConfigSliceEquals(current.Indexes, last.Indexes) {
v.Forbidden(Path("spec", "indexes"), "field cannot be updated")
}
if current.PersistenceEnabled != last.PersistenceEnabled {
v.Forbidden(Path("spec", "persistenceEnabled"), "field cannot be updated")
}
if current.HazelcastResourceName != last.HazelcastResourceName {
v.Forbidden(Path("spec", "hazelcastResourceName"), "field cannot be updated")
}
if current.InMemoryFormat != last.InMemoryFormat {
v.Forbidden(Path("spec", "inMemoryFormat"), "field cannot be updated")
}
if !reflect.DeepEqual(current.EventJournal, last.EventJournal) {
v.Forbidden(Path("spec", "eventJournal"), "field cannot be updated")
}
if !reflect.DeepEqual(current.NearCache, last.NearCache) {
v.Forbidden(Path("spec", "nearCache"), "field cannot be updated")
}
if !reflect.DeepEqual(current.TieredStore, last.TieredStore) {
v.Forbidden(Path("spec", "tieredStore"), "field cannot be updated")
}
}
func indexConfigSliceEquals(a, b []IndexConfig) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if !indexConfigEquals(v, b[i]) {
return false
}
}
return true
}
func indexConfigEquals(a, b IndexConfig) bool {
if a.Name != b.Name {
return false
}
if a.Type != b.Type {
return false
}
if !stringSliceEquals(a.Attributes, b.Attributes) {
return false
}
// if both a and b not nil
if (a.BitmapIndexOptions != nil) && (b.BitmapIndexOptions != nil) {
return *a.BitmapIndexOptions != *b.BitmapIndexOptions
}
// If one of a and b not nil
if (a.BitmapIndexOptions != nil) || (b.BitmapIndexOptions != nil) {
return false
}
return true
}
func stringSliceEquals(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func (v *mapValidator) getHzSpec(h *Hazelcast) *HazelcastSpec {
s, ok := h.ObjectMeta.Annotations[n.LastSuccessfulSpecAnnotation]
if !ok {
v.InternalError(Path("spec"), fmt.Errorf("hazelcast resource %s is not successfully started yet", h.Name))
return nil
}
lastSpec := &HazelcastSpec{}
err := json.Unmarshal([]byte(s), lastSpec)
if err != nil {
v.InternalError(Path("spec"), fmt.Errorf("error parsing last Hazelcast spec: %w", err))
return nil
}
return lastSpec
}
func deviceExist(localDevices []LocalDeviceConfig, deviceName string) bool {
for _, localDevice := range localDevices {
if localDevice.Name == deviceName {
return true
}
}
return false
}