From 206cd7787ff0cc0c8962ef9e4f21c45df85969e9 Mon Sep 17 00:00:00 2001 From: Kajol Asabe <114986456+kasabe28@users.noreply.github.com> Date: Mon, 4 Mar 2024 14:10:33 +0530 Subject: [PATCH] Add `ResourceVersion` to internal `Machine` type (#191) Co-authored-by: Lukas Koszegy --- pkg/api/api.go | 17 ++++++++++++++--- pkg/host/store.go | 9 ++++++++- pkg/store/store.go | 5 +++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index e1aac7ce..5c64cfb0 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -10,9 +10,10 @@ type Metadata struct { Annotations map[string]string `json:"annotations"` Labels map[string]string `json:"labels"` - CreatedAt time.Time `json:"createdAt"` - DeletedAt *time.Time `json:"deletedAt,omitempty"` - Generation int64 `json:"generation"` + CreatedAt time.Time `json:"createdAt"` + DeletedAt *time.Time `json:"deletedAt,omitempty"` + Generation int64 `json:"generation"` + ResourceVersion uint64 `json:"resourceVersion"` Finalizers []string `json:"finalizers,omitempty"` } @@ -45,6 +46,10 @@ func (m *Metadata) GetFinalizers() []string { return m.Finalizers } +func (m *Metadata) GetResourceVersion() uint64 { + return m.ResourceVersion +} + func (m *Metadata) SetID(id string) { m.ID = id } @@ -73,6 +78,10 @@ func (m *Metadata) SetFinalizers(finalizers []string) { m.Finalizers = finalizers } +func (m *Metadata) IncrementResourceVersion() { + m.ResourceVersion++ +} + type Object interface { GetID() string GetAnnotations() map[string]string @@ -81,6 +90,7 @@ type Object interface { GetDeletedAt() *time.Time GetGeneration() int64 GetFinalizers() []string + GetResourceVersion() uint64 SetID(id string) SetAnnotations(annotations map[string]string) @@ -89,4 +99,5 @@ type Object interface { SetDeletedAt(deleted *time.Time) SetGeneration(generation int64) SetFinalizers(finalizers []string) + IncrementResourceVersion() } diff --git a/pkg/host/store.go b/pkg/host/store.go index 4ccc6baa..1450e7a2 100644 --- a/pkg/host/store.go +++ b/pkg/host/store.go @@ -86,6 +86,7 @@ func (s *Store[E]) Create(_ context.Context, obj E) (E, error) { } obj.SetCreatedAt(time.Now()) + obj.IncrementResourceVersion() obj, err = s.set(obj) if err != nil { @@ -128,11 +129,16 @@ func (s *Store[E]) Update(_ context.Context, obj E) (E, error) { return obj, nil } + if oldObj.GetResourceVersion() != obj.GetResourceVersion() { + return utils.Zero[E](), fmt.Errorf("failed to update object: %w", store.ErrResourceVersionNotLatest) + } + if reflect.DeepEqual(oldObj, obj) { return obj, nil } - //Todo: update version + obj.IncrementResourceVersion() + obj, err = s.set(obj) if err != nil { return utils.Zero[E](), err @@ -161,6 +167,7 @@ func (s *Store[E]) Delete(_ context.Context, id string) error { now := time.Now() obj.SetDeletedAt(&now) + obj.IncrementResourceVersion() if _, err := s.set(obj); err != nil { return fmt.Errorf("failed to set object metadata: %w", err) diff --git a/pkg/store/store.go b/pkg/store/store.go index 4a5e0b49..5c52b761 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -11,8 +11,9 @@ import ( ) var ( - ErrNotFound = errors.New("not found") - ErrAlreadyExists = errors.New("already exists") + ErrNotFound = errors.New("not found") + ErrAlreadyExists = errors.New("already exists") + ErrResourceVersionNotLatest = errors.New("resourceVersion is not latest") ) func IgnoreErrNotFound(err error) error {