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

Implement ADS client #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
version: v1.64
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ profile_cache:
profile_handlers:
$(MAKE) -B $(PROFILES)/BenchmarkHandlers.bench BENCH_PKG=./internal/server

profile_typeurl:
$(MAKE) -B $(PROFILES)/BenchmarkGetTrimmedTypeURL.bench BENCH_PKG=ads

profile_parse_glob_urn:
$(MAKE) -B $(PROFILES)/BenchmarkParseGlobCollectionURN.bench BENCH_PKG=ads

BENCHCOUNT = 1
BENCHTIME = 1s

Expand All @@ -78,8 +84,8 @@ else
$(error BENCH_PKG undefined)
endif
ifdef OPEN_PROFILES
go tool pprof $(BENCHBIN) $(PROFILES)/$*.cpu <<< web
go tool pprof $(PROFILES)/$*.mem <<< web
go tool pprof -http : $(BENCHBIN) $(PROFILES)/$*.cpu & \
go tool pprof -http : $(PROFILES)/$*.mem ; kill %1
else
$(info Not opening profiles since OPEN_PROFILES is not set)
endif
Expand Down
15 changes: 15 additions & 0 deletions ads/ads.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ func (r *Resource[T]) TypeURL() string {
return types.APITypePrefix + string(r.Resource.ProtoReflect().Descriptor().FullName())
}

func (r *Resource[T]) Equals(other *Resource[T]) bool {
if r == other {
return true
}
if r == nil || other == nil {
return false
}
return r.Name == other.Name &&
r.Version == other.Version &&
proto.Equal(r.Ttl, other.Ttl) &&
proto.Equal(r.CacheControl, other.CacheControl) &&
proto.Equal(r.Metadata, other.Metadata) &&
proto.Equal(r.Resource, other.Resource)
}

// UnmarshalRawResource unmarshals the given RawResource and returns a Resource of the corresponding
// type. Resource.Marshal on the returned Resource will return the given RawResource instead of
// re-serializing the resource.
Expand Down
64 changes: 40 additions & 24 deletions ads/glob_collection_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/url"
"strings"

types "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"google.golang.org/protobuf/proto"
)

// GlobCollectionURL represents the individual elements of a glob collection URL. Please refer to the
Expand All @@ -27,14 +27,22 @@ type GlobCollectionURL struct {
}

func (u GlobCollectionURL) String() string {
return u.uri(WildcardSubscription)
}

func (u GlobCollectionURL) MemberURN(name string) string {
return u.uri(name)
}

func (u GlobCollectionURL) uri(name string) string {
var path string
switch u.Path {
case "":
path = WildcardSubscription
path = name
case "/":
path = "/" + WildcardSubscription
path = "/" + name
default:
path = u.Path + "/" + WildcardSubscription
path = u.Path + "/" + name
}

return XDSTPScheme +
Expand All @@ -44,6 +52,15 @@ func (u GlobCollectionURL) String() string {
u.ContextParameters
}

func NewGlobCollectionURL[T proto.Message](authority, path string, contextParameters url.Values) GlobCollectionURL {
return GlobCollectionURL{
Authority: authority,
ResourceType: getTrimmedTypeURL[T](),
Path: path,
ContextParameters: contextParameters.Encode(),
}
}

// ErrInvalidGlobCollectionURI is always returned by the various glob collection URL parsing
// functions.
var ErrInvalidGlobCollectionURI = errors.New("diderot: invalid glob collection URI")
Expand All @@ -57,25 +74,23 @@ var ErrInvalidGlobCollectionURI = errors.New("diderot: invalid glob collection U
// exact definition of a glob collection.
//
// [TP1 proposal]: https://github.com/cncf/xds/blob/main/proposals/TP1-xds-transport-next.md#uri-based-xds-resource-names
func ParseGlobCollectionURL(name, resourceType string) (GlobCollectionURL, error) {
gcURL, err := parseXDSTPURI(name, resourceType)
func ParseGlobCollectionURL[T proto.Message](name string) (GlobCollectionURL, error) {
gcURL, resource, err := ParseGlobCollectionURN[T](name)
if err != nil {
return GlobCollectionURL{}, err
}

var ok bool
gcURL.Path, ok = strings.CutSuffix(gcURL.Path, "/"+WildcardSubscription)
if !ok {
if resource != WildcardSubscription {
// URLs must end with /*
return GlobCollectionURL{}, ErrInvalidGlobCollectionURI
}

return gcURL, nil
}

// ExtractGlobCollectionURLFromResourceURN checks if the given name is a resource URN, and returns
// the corresponding GlobCollectionURL. The format of a resource URN is defined in the
// [TP1 proposal], and looks like this:
// ParseGlobCollectionURN checks if the given name is a resource URN, and returns the corresponding
// GlobCollectionURL. The format of a resource URN is defined in the [TP1 proposal], and looks like
// this:
//
// xdstp://[{authority}]/{resource type}/{id/*}?{context parameters}
//
Expand All @@ -99,33 +114,30 @@ func ParseGlobCollectionURL(name, resourceType string) (GlobCollectionURL, error
//
// [TP1 proposal]: https://github.com/cncf/xds/blob/main/proposals/TP1-xds-transport-next.md#uri-based-xds-resource-names
// [here]: https://github.com/cncf/xds/issues/91
func ExtractGlobCollectionURLFromResourceURN(name, resourceType string) (GlobCollectionURL, error) {
gcURL, err := parseXDSTPURI(name, resourceType)
func ParseGlobCollectionURN[T proto.Message](name string) (GlobCollectionURL, string, error) {
gcURL, err := parseXDSTPURI[T](name)
if err != nil {
return GlobCollectionURL{}, err
return GlobCollectionURL{}, "", err
}

lastSlash := strings.LastIndex(gcURL.Path, "/")
if lastSlash == -1 {
// Missing path in URL
return GlobCollectionURL{}, ErrInvalidGlobCollectionURI
return GlobCollectionURL{}, "", ErrInvalidGlobCollectionURI
}

if gcURL.Path[lastSlash:] == "/"+WildcardSubscription {
// resource URN cannot end in /*
return GlobCollectionURL{}, ErrInvalidGlobCollectionURI
}
resource := gcURL.Path[lastSlash+1:]

if lastSlash == 0 {
gcURL.Path = "/"
} else {
gcURL.Path = gcURL.Path[:lastSlash]
}

return gcURL, nil
return gcURL, resource, nil
}

func parseXDSTPURI(resourceName, resourceType string) (GlobCollectionURL, error) {
func parseXDSTPURI[T proto.Message](resourceName string) (GlobCollectionURL, error) {
// Skip deserializing the resource name if it doesn't start with the correct scheme
if !strings.HasPrefix(resourceName, XDSTPScheme) {
// doesn't start with xdstp://
Expand All @@ -138,8 +150,7 @@ func parseXDSTPURI(resourceName, resourceType string) (GlobCollectionURL, error)
return GlobCollectionURL{}, ErrInvalidGlobCollectionURI
}

// Glob collection URLs do not start with the type prefix, so trim it here.
resourceType = strings.TrimPrefix(resourceType, types.APITypePrefix)
resourceType := getTrimmedTypeURL[T]()

collectionPath, ok := strings.CutPrefix(parsedURL.EscapedPath(), "/"+resourceType+"/")
if !ok {
Expand All @@ -160,3 +171,8 @@ func parseXDSTPURI(resourceName, resourceType string) (GlobCollectionURL, error)

return u, nil
}

func getTrimmedTypeURL[T proto.Message]() string {
var t T
return string(t.ProtoReflect().Descriptor().FullName())
}
87 changes: 74 additions & 13 deletions ads/glob_collection_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ package ads
import (
"testing"

cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/wrapperspb"
)

const (
resourceType = "google.protobuf.Int64Value"
)

func testBadURIs(t *testing.T, parser func(string, string) (GlobCollectionURL, error)) {
func testBadURIs(t *testing.T, parser func(string) (GlobCollectionURL, error)) {
badURIs := []struct {
name string
resourceName string
Expand Down Expand Up @@ -43,13 +50,13 @@ func testBadURIs(t *testing.T, parser func(string, string) (GlobCollectionURL, e

for _, test := range badURIs {
t.Run(test.name, func(t *testing.T) {
_, err := parser(test.resourceName, resourceType)
_, err := parser(test.resourceName)
require.Error(t, err)
})
}
}

func testGoodURIs(t *testing.T, id string, parser func(string, string) (GlobCollectionURL, error)) {
func testGoodURIs(t *testing.T, id string, parser func(string) (GlobCollectionURL, error)) {
tests := []struct {
name string
resourceName string
Expand Down Expand Up @@ -120,7 +127,7 @@ func testGoodURIs(t *testing.T, id string, parser func(string, string) (GlobColl

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := parser(test.resourceName, resourceType)
actual, err := parser(test.resourceName)
if test.expectErr {
require.Error(t, err)
} else {
Expand All @@ -133,26 +140,80 @@ func testGoodURIs(t *testing.T, id string, parser func(string, string) (GlobColl

func TestParseGlobCollectionURL(t *testing.T) {
t.Run("bad URIs", func(t *testing.T) {
testBadURIs(t, ParseGlobCollectionURL)
testBadURIs(t, ParseGlobCollectionURL[*wrapperspb.Int64Value])
})
t.Run("good URIs", func(t *testing.T) {
testGoodURIs(t, WildcardSubscription, ParseGlobCollectionURL)
testGoodURIs(t, WildcardSubscription, ParseGlobCollectionURL[*wrapperspb.Int64Value])
})
t.Run("rejects URNs", func(t *testing.T) {
_, err := ParseGlobCollectionURL("xdstp:///"+resourceType+"/foo/bar", resourceType)
_, err := ParseGlobCollectionURL[*wrapperspb.Int64Value]("xdstp:///" + resourceType + "/foo/bar")
require.Error(t, err)
})
}

func TestExtractGlobCollectionURLFromResourceURN(t *testing.T) {
func TestParseGlobCollectionURN(t *testing.T) {
parser := func(s string) (GlobCollectionURL, error) {
gcURL, _, err := ParseGlobCollectionURN[*wrapperspb.Int64Value](s)
return gcURL, err
}

t.Run("bad URIs", func(t *testing.T) {
testBadURIs(t, ExtractGlobCollectionURLFromResourceURN)
testBadURIs(t, parser)
})
t.Run("good URIs", func(t *testing.T) {
testGoodURIs(t, "foo", ExtractGlobCollectionURLFromResourceURN)
testGoodURIs(t, "foo", parser)
})
t.Run("rejects glob collection URLs", func(t *testing.T) {
_, err := ExtractGlobCollectionURLFromResourceURN("xdstp:///"+resourceType+"/foo/*", resourceType)
require.Error(t, err)
t.Run("handles glob collection URLs", func(t *testing.T) {
gcURL, r, err := ParseGlobCollectionURN[*wrapperspb.Int64Value]("xdstp:///" + resourceType + "/foo/*")
require.NoError(t, err)
require.Equal(t, NewGlobCollectionURL[*wrapperspb.Int64Value]("", "foo", nil), gcURL)
require.Equal(t, WildcardSubscription, r)
})
}

func TestGetTrimmedTypeURL(t *testing.T) {
check := func(expected, actualTrimmed string) {
require.Equal(t, expected, resource.APITypePrefix+actualTrimmed)
}
check(resource.ListenerType, getTrimmedTypeURL[*listener.Listener]())
check(resource.EndpointType, getTrimmedTypeURL[*endpoint.ClusterLoadAssignment]())
check(resource.ClusterType, getTrimmedTypeURL[*cluster.Cluster]())
check(resource.RouteType, getTrimmedTypeURL[*route.RouteConfiguration]())
}

func BenchmarkGetTrimmedTypeURL(b *testing.B) {
benchmarkGetTrimmedTypeURL[*wrapperspb.Int64Value](b)
benchmarkGetTrimmedTypeURL[*cluster.Cluster](b)
}

func benchmarkGetTrimmedTypeURL[T proto.Message](b *testing.B) {
b.Run(getTrimmedTypeURL[T](), func(b *testing.B) {
var url string
for range b.N {
url = getTrimmedTypeURL[T]()
}
require.NotEmpty(b, url)
})
}

func BenchmarkParseGlobCollectionURN(b *testing.B) {
benchmarkParseGlobCollectionURN[*wrapperspb.Int64Value](b)
benchmarkParseGlobCollectionURN[*cluster.Cluster](b)
}

func benchmarkParseGlobCollectionURN[T proto.Message](b *testing.B) {
expectedURL := NewGlobCollectionURL[T]("foo", "bar", nil)
url := expectedURL.String()

var err error
b.Run(url, func(b *testing.B) {
var actualURL GlobCollectionURL
for range b.N {
actualURL, _, err = ParseGlobCollectionURN[T](url)
if err != nil {
b.Fatal(err)
}
}
require.Equal(b, expectedURL, actualURL)
})
}
Loading