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

fix(kuma-cp) add creation time #903

Merged
merged 3 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pkg/kds/store/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package store

import (
"context"
"reflect"

"github.com/go-logr/logr"
"reflect"
"time"

"github.com/kumahq/kuma/pkg/core/resources/model"
"github.com/kumahq/kuma/pkg/core/resources/registry"
Expand Down Expand Up @@ -117,16 +117,21 @@ func (s *syncResourceStore) Sync(upstream model.ResourceList, fs ...SyncOptionFu
for _, r := range onCreate {
rk := model.MetaToResourceKey(r.GetMeta())
s.log.Info("creating a new resource from upstream", "resourceKey", rk)
creationTime := r.GetMeta().GetCreationTime()
// some Stores try to cast ResourceMeta to own Store type that's why we have to set meta to nil
r.SetMeta(nil)
if err := s.resourceStore.Create(ctx, r, store.CreateBy(rk)); err != nil {
if err := s.resourceStore.Create(ctx, r, store.CreateBy(rk), store.CreatedAt(creationTime)); err != nil {
return err
}
}

for _, r := range onUpdate {
s.log.Info("updating a resource", "resourceKey", model.MetaToResourceKey(r.GetMeta()))
if err := s.resourceStore.Update(ctx, r); err != nil {
now := time.Now()
// some stores manage ModificationTime time on they own (Kubernetes), in order to be consistent
// we set ModificationTime when we add to downstream store. This time is almost the same with ModificationTime
// from upstream store, because we update downstream only when resource have changed in upstream
if err := s.resourceStore.Update(ctx, r, store.ModifiedAt(now)); err != nil {
return err
}
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/kds/util/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ type resourceMeta struct {
modificationTime *time.Time
}

func ResourceKeyToMeta(name, mesh string) model.ResourceMeta {
func NewResourceMeta(name, mesh, version string, creationTime, modificationTime time.Time) model.ResourceMeta {
return &resourceMeta{
name: name,
mesh: mesh,
name: name,
mesh: mesh,
version: version,
creationTime: &creationTime,
modificationTime: &modificationTime,
}
}

Expand Down
12 changes: 4 additions & 8 deletions pkg/kds/util/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,17 @@ func ToEnvoyResources(rlist model.ResourceList) ([]envoy_types.Resource, error)
func AddPrefixToNames(rs []model.Resource, prefix string) {
for _, r := range rs {
newName := fmt.Sprintf("%s.%s", prefix, r.GetMeta().GetName())
// method Sync takes into account only 'Name' and 'Mesh'. Another ResourceMeta's fields like
// 'Version', 'CreationTime' and 'ModificationTime' will be taken from downstream store.
// That's why we can set 'Name' like this
m := ResourceKeyToMeta(newName, r.GetMeta().GetMesh())
m := NewResourceMeta(newName, r.GetMeta().GetMesh(), r.GetMeta().GetVersion(),
r.GetMeta().GetCreationTime(), r.GetMeta().GetModificationTime())
r.SetMeta(m)
}
}

func AddSuffixToNames(rs []model.Resource, suffix string) {
for _, r := range rs {
newName := fmt.Sprintf("%s.%s", r.GetMeta().GetName(), suffix)
// method Sync takes into account only 'Name' and 'Mesh'. Another ResourceMeta's fields like
// 'Version', 'CreationTime' and 'ModificationTime' will be taken from downstream store.
// That's why we can set 'Name' like this
m := ResourceKeyToMeta(newName, r.GetMeta().GetMesh())
m := NewResourceMeta(newName, r.GetMeta().GetMesh(), r.GetMeta().GetVersion(),
r.GetMeta().GetCreationTime(), r.GetMeta().GetModificationTime())
r.SetMeta(m)
}
}
Expand Down