Skip to content

Commit

Permalink
refactoring: make ResourceSet type reusable by both primary ADS ser…
Browse files Browse the repository at this point in the history
…ver and future MADS server
  • Loading branch information
yskopets committed Jan 8, 2020
1 parent dd21cbb commit 794158f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 61 deletions.
48 changes: 48 additions & 0 deletions pkg/core/xds/resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xds

import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"

envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
Expand Down Expand Up @@ -35,3 +36,50 @@ func (rs ResourceList) ToDeltaDiscoveryResponse() *envoy.DeltaDiscoveryResponse
}
return resp
}

// ResourceSet represents a set of generic xDS resources.
type ResourceSet struct {
// we want to keep resources in the order they were added
resources []*Resource
// we want to prevent duplicates
typeToNamesIndex map[string]map[string]bool
}

func (s *ResourceSet) Contains(name string, resource ResourcePayload) bool {
names, ok := s.typeToNamesIndex[s.typeName(resource)]
if !ok {
return false
}
_, ok = names[name]
return ok
}

func (s *ResourceSet) Add(resources ...*Resource) *ResourceSet {
for _, resource := range resources {
if s.Contains(resource.Name, resource.Resource) {
continue
}
s.resources = append(s.resources, resource)
s.index(resource)
}
return s
}

func (s *ResourceSet) typeName(resource ResourcePayload) string {
return proto.MessageName(resource)
}

func (s *ResourceSet) index(resource *Resource) {
if s.typeToNamesIndex == nil {
s.typeToNamesIndex = map[string]map[string]bool{}
}
typeName := s.typeName(resource.Resource)
if s.typeToNamesIndex[typeName] == nil {
s.typeToNamesIndex[typeName] = map[string]bool{}
}
s.typeToNamesIndex[typeName][resource.Name] = true
}

func (s *ResourceSet) List() []*Resource {
return s.resources
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package generator_test
package xds_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "github.com/Kong/kuma/pkg/xds/generator"

core_xds "github.com/Kong/kuma/pkg/core/xds"
. "github.com/Kong/kuma/pkg/core/xds"

envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
)
Expand All @@ -22,7 +20,7 @@ var _ = Describe("ResourceSet", func() {

It("set of 1 element should return a list of 1 element", func() {
// given
resource := &core_xds.Resource{
resource := &Resource{
Name: "backend",
Version: "v1",
Resource: &envoy.Cluster{
Expand All @@ -40,14 +38,14 @@ var _ = Describe("ResourceSet", func() {

It("set of 2 elements should return a list of 2 elements", func() {
// given
resource1 := &core_xds.Resource{
resource1 := &Resource{
Name: "backend",
Version: "v1",
Resource: &envoy.Cluster{
Name: "backend",
},
}
resource2 := &core_xds.Resource{
resource2 := &Resource{
Name: "outbound:127.0.0.1:8080",
Version: "v2",
Resource: &envoy.Listener{
Expand All @@ -68,14 +66,14 @@ var _ = Describe("ResourceSet", func() {

It("should not be possible to add 2 resources with same name and type", func() {
// given
resource1 := &core_xds.Resource{
resource1 := &Resource{
Name: "backend",
Version: "v1",
Resource: &envoy.Cluster{
Name: "backend",
},
}
resource2 := &core_xds.Resource{
resource2 := &Resource{
Name: "backend",
Version: "v2",
Resource: &envoy.Cluster{
Expand All @@ -96,14 +94,14 @@ var _ = Describe("ResourceSet", func() {

It("should be possible to add 2 resources with same name but different types", func() {
// given
resource1 := &core_xds.Resource{
resource1 := &Resource{
Name: "backend",
Version: "v1",
Resource: &envoy.Cluster{
Name: "backend",
},
}
resource2 := &core_xds.Resource{
resource2 := &Resource{
Name: "backend",
Version: "v2",
Resource: &envoy.Listener{
Expand Down
4 changes: 2 additions & 2 deletions pkg/xds/generator/proxy_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (_ InboundProxyGenerator) Generate(ctx xds_context.Context, proxy *model.Pr
return nil, nil
}
virtual := proxy.Dataplane.Spec.Networking.GetTransparentProxying().GetRedirectPort() != 0
resources := &ResourceSet{}
resources := &model.ResourceSet{}
for _, endpoint := range endpoints {
// generate CDS resource
localClusterName := localClusterName(endpoint.WorkloadPort)
Expand Down Expand Up @@ -124,7 +124,7 @@ func (g OutboundProxyGenerator) Generate(ctx xds_context.Context, proxy *model.P
return nil, nil
}
virtual := proxy.Dataplane.Spec.Networking.GetTransparentProxying().GetRedirectPort() != 0
resources := &ResourceSet{}
resources := &model.ResourceSet{}
sourceService := proxy.Dataplane.Spec.GetIdentifyingService()
for i, oface := range ofaces {
endpoint, err := kuma_mesh.ParseOutboundInterface(oface.Interface)
Expand Down
48 changes: 0 additions & 48 deletions pkg/xds/generator/resource_generator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package generator

import (
"github.com/golang/protobuf/proto"

model "github.com/Kong/kuma/pkg/core/xds"
xds_context "github.com/Kong/kuma/pkg/xds/context"
)
Expand All @@ -24,49 +22,3 @@ func (c CompositeResourceGenerator) Generate(ctx xds_context.Context, proxy *mod
}
return resources, nil
}

type ResourceSet struct {
// we want to keep resources in the order they were added
resources []*model.Resource
// we want to prevent duplicates
typeToNamesIndex map[string]map[string]bool
}

func (s *ResourceSet) Contains(name string, resource model.ResourcePayload) bool {
names, ok := s.typeToNamesIndex[s.typeName(resource)]
if !ok {
return false
}
_, ok = names[name]
return ok
}

func (s *ResourceSet) Add(resources ...*model.Resource) *ResourceSet {
for _, resource := range resources {
if s.Contains(resource.Name, resource.Resource) {
continue
}
s.resources = append(s.resources, resource)
s.index(resource)
}
return s
}

func (s *ResourceSet) typeName(resource model.ResourcePayload) string {
return proto.MessageName(resource)
}

func (s *ResourceSet) index(resource *model.Resource) {
if s.typeToNamesIndex == nil {
s.typeToNamesIndex = map[string]map[string]bool{}
}
typeName := s.typeName(resource.Resource)
if s.typeToNamesIndex[typeName] == nil {
s.typeToNamesIndex[typeName] = map[string]bool{}
}
s.typeToNamesIndex[typeName][resource.Name] = true
}

func (s *ResourceSet) List() []*model.Resource {
return s.resources
}

0 comments on commit 794158f

Please sign in to comment.