-
Notifications
You must be signed in to change notification settings - Fork 17
/
hotbackup_validation.go
72 lines (58 loc) · 2.08 KB
/
hotbackup_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
package v1alpha1
import (
"context"
"encoding/json"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/hazelcast/hazelcast-platform-operator/internal/kubeclient"
n "github.com/hazelcast/hazelcast-platform-operator/internal/naming"
)
type hotbackupValidator struct {
fieldValidator
}
func NewHotBackupValidator(o client.Object) hotbackupValidator {
return hotbackupValidator{NewFieldValidator(o)}
}
func ValidateHotBackupPersistence(hb *HotBackup, h *Hazelcast) error {
v := NewHotBackupValidator(hb)
v.validateHotBackupPersistence(h)
return v.Err()
}
func (v *hotbackupValidator) validateHotBackupPersistence(h *Hazelcast) {
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
}
lastSpec := &HazelcastSpec{}
err := json.Unmarshal([]byte(s), lastSpec)
if err != nil {
v.InternalError(Path("spec"), fmt.Errorf("error parsing last Hazelcast spec for update errors: %w", err))
return
}
if !lastSpec.Persistence.IsEnabled() {
v.Invalid(Path("spec", "persistenceEnabled"), lastSpec.Persistence.IsEnabled(), fmt.Sprintf("Hazelcast '%s' must enable persistence", h.Name))
return
}
}
func ValidateHotBackupIsNotReferencedByHazelcast(hb *HotBackup) error {
v := NewHotBackupValidator(hb)
hzList := HazelcastList{}
err := kubeclient.List(context.Background(), &hzList, &client.ListOptions{Namespace: hb.Namespace})
if err != nil {
hotbackuplog.Error(err, "error on listing Hazelcast resources")
return nil
}
for _, hz := range hzList.Items {
hzCopy := hz
v.validateHotBackupRestoreReference(&hzCopy, hb)
}
return v.Err()
}
func (v *hotbackupValidator) validateHotBackupRestoreReference(h *Hazelcast, hb *HotBackup) {
if h.Spec.Persistence.IsRestoreEnabled() {
if h.Spec.Persistence.Restore.HotBackupResourceName == hb.Name && h.DeletionTimestamp == nil {
v.Forbidden(Path("spec", "persistence", "restore", "hotBackupResourceName"), fmt.Sprintf("Hazelcast '%s' has a restore reference to the Hotbackup", h.Name))
}
}
}