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

kuma-cp: add generic versioner for xDS resources #529

Merged
merged 2 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

Changes:

* feature: add a generic versioner for xDS Snapshots
[#529](https://github.com/Kong/kuma/pull/529)
* feature: add a custom version of SnapshotCache that supports arbitrary xDS resources
[#528](https://github.com/Kong/kuma/pull/528)
* feature: add proto definition for Monitoring Assignment Discovery Service (MADS)
Expand Down
51 changes: 51 additions & 0 deletions pkg/util/xds/versioner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package xds

import (
envoy_cache "github.com/envoyproxy/go-control-plane/pkg/cache"
"github.com/golang/protobuf/proto"
)

// SnapshotVersioner assigns versions to xDS resources in a new Snapshot.
type SnapshotVersioner interface {
Version(new, old Snapshot) Snapshot
}

// SnapshotAutoVersioner assigns versions to xDS resources in a new Snapshot
// by reusing if possible a version from the old snapshot and
// generating a new version (UUID) otherwise.
type SnapshotAutoVersioner struct {
UUID func() string
}

func (v SnapshotAutoVersioner) Version(new, old Snapshot) Snapshot {
if new == nil {
return nil
}
for _, typ := range new.GetSupportedTypes() {
version := new.GetVersion(typ)
if version != "" {
// favour a version assigned by resource generator
continue
}
if old != nil && v.equal(new.GetResources(typ), old.GetResources(typ)) {
version = old.GetVersion(typ)
}
if version == "" {
version = v.UUID()
}
new = new.WithVersion(typ, version)
}
return new
}

func (_ SnapshotAutoVersioner) equal(new, old map[string]envoy_cache.Resource) bool {
if len(new) != len(old) {
return false
}
for key, newValue := range new {
if oldValue, hasOldValue := old[key]; !hasOldValue || !proto.Equal(newValue, oldValue) {
return false
}
}
return true
}
Loading