Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CN-960] Refactor hotbackup validation #875

Merged
merged 1 commit into from Sep 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 33 additions & 7 deletions api/v1alpha1/hotbackup_validation.go
Expand Up @@ -5,25 +5,51 @@ import (
"fmt"

n "github.com/hazelcast/hazelcast-platform-operator/internal/naming"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func ValidateHotBackupPersistence(h *Hazelcast) *field.Error {
type hotbackupValidator struct {
fieldValidator
name string
}

func (v *hotbackupValidator) Err() error {
if len(v.fieldValidator) != 0 {
return kerrors.NewInvalid(
schema.GroupKind{Group: "hazelcast.com", Kind: "Hazelcast"},
v.name,
field.ErrorList(v.fieldValidator),
)
}
return nil
}

func ValidateHotBackupPersistence(h *Hazelcast) error {
v := hotbackupValidator{
name: h.Name,
}
v.validateHotBackupPersistence(h)
return v.Err()
}

func (v *hotbackupValidator) validateHotBackupPersistence(h *Hazelcast) {
s, ok := h.ObjectMeta.Annotations[n.LastSuccessfulSpecAnnotation]
if !ok {
return field.InternalError(field.NewPath("spec"), fmt.Errorf("hazelcast resource %s is not successfully started yet", h.Name))
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 {
return field.InternalError(field.NewPath("spec"), fmt.Errorf("error parsing last Hazelcast spec for update errors: %w", err))
v.InternalError(Path("spec"), fmt.Errorf("error parsing last Hazelcast spec for update errors: %w", err))
return
}

if !lastSpec.Persistence.IsEnabled() {
return field.Invalid(field.NewPath("spec").Child("persistenceEnabled"), lastSpec.Persistence.IsEnabled(),
"Persistence must be enabled at Hazelcast")
v.Invalid(Path("spec", "persistenceEnabled"), lastSpec.Persistence.IsEnabled(), "Persistence must be enabled at Hazelcast")
return
}

return nil
}