Skip to content

Commit

Permalink
Add ResourceVersion to internal Machine type (#191)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Koszegy <[email protected]>
  • Loading branch information
kasabe28 and lukas016 authored Mar 4, 2024
1 parent dcbc48c commit 206cd77
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
17 changes: 14 additions & 3 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -81,6 +90,7 @@ type Object interface {
GetDeletedAt() *time.Time
GetGeneration() int64
GetFinalizers() []string
GetResourceVersion() uint64

SetID(id string)
SetAnnotations(annotations map[string]string)
Expand All @@ -89,4 +99,5 @@ type Object interface {
SetDeletedAt(deleted *time.Time)
SetGeneration(generation int64)
SetFinalizers(finalizers []string)
IncrementResourceVersion()
}
9 changes: 8 additions & 1 deletion pkg/host/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 206cd77

Please sign in to comment.