-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jakub Dyszkiewicz <[email protected]>
- Loading branch information
1 parent
0fce606
commit 72ba565
Showing
10 changed files
with
170 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package server_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kumahq/kuma/pkg/test" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
test.RunSpecs(t, "Server Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
|
||
envoy_sd "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" | ||
envoy_xds "github.com/envoyproxy/go-control-plane/pkg/server/v3" | ||
|
||
"github.com/kumahq/kuma/pkg/kds/v2/util" | ||
"github.com/kumahq/kuma/pkg/multitenant" | ||
util_xds_v3 "github.com/kumahq/kuma/pkg/util/xds/v3" | ||
) | ||
|
||
type tenancyCallbacks struct { | ||
tenants multitenant.Tenants | ||
|
||
sync.RWMutex | ||
streamToCtx map[int64]context.Context | ||
util_xds_v3.NoopCallbacks | ||
} | ||
|
||
func NewTenancyCallbacks(tenants multitenant.Tenants) envoy_xds.Callbacks { | ||
return &tenancyCallbacks{ | ||
tenants: tenants, | ||
streamToCtx: map[int64]context.Context{}, | ||
} | ||
} | ||
|
||
func (c *tenancyCallbacks) OnDeltaStreamOpen(ctx context.Context, streamID int64, _ string) error { | ||
c.Lock() | ||
c.streamToCtx[streamID] = ctx | ||
c.Unlock() | ||
return nil | ||
} | ||
|
||
func (c *tenancyCallbacks) OnStreamDeltaRequest(streamID int64, request *envoy_sd.DeltaDiscoveryRequest) error { | ||
c.RLock() | ||
defer c.RUnlock() | ||
ctx, ok := c.streamToCtx[streamID] | ||
if !ok { | ||
// it should not happen, but just in case it's better to fail | ||
return errors.New("context is missing") | ||
} | ||
tenantID, err := c.tenants.GetID(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
util.FillTenantMetadata(tenantID, request.Node) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package server_test | ||
|
||
import ( | ||
"context" | ||
|
||
envoy_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" | ||
envoy_sd "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/kumahq/kuma/pkg/kds/v2/server" | ||
"github.com/kumahq/kuma/pkg/kds/v2/util" | ||
"github.com/kumahq/kuma/pkg/multitenant" | ||
) | ||
|
||
var _ = Describe("Tenant callbacks", func() { | ||
It("should enrich metadata with tenant info", func() { | ||
// given | ||
streamID := int64(1) | ||
callbacks := server.NewTenancyCallbacks(&sampleTenants{}) | ||
|
||
// when | ||
err := callbacks.OnDeltaStreamOpen(multitenant.WithTenant(context.Background(), "sample"), streamID, "") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
req := &envoy_sd.DeltaDiscoveryRequest{ | ||
Node: &envoy_core.Node{}, | ||
} | ||
err = callbacks.OnStreamDeltaRequest(streamID, req) | ||
|
||
// then | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
tenant, ok := util.TenantFromMetadata(req.GetNode()) | ||
Expect(ok).To(BeTrue()) | ||
Expect(tenant).To(Equal("sample")) | ||
}) | ||
}) | ||
|
||
type sampleTenants struct{} | ||
|
||
func (s sampleTenants) GetID(ctx context.Context) (string, error) { | ||
tenant, _ := multitenant.TenantFromCtx(ctx) | ||
return tenant, nil | ||
} | ||
|
||
func (s sampleTenants) GetIDs(ctx context.Context) ([]string, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package util | ||
|
||
import ( | ||
envoy_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
const tenantMetadataKey = "tenant" | ||
|
||
func FillTenantMetadata(tenantID string, node *envoy_core.Node) { | ||
if node.Metadata == nil { | ||
node.Metadata = &structpb.Struct{} | ||
} | ||
if node.Metadata.Fields == nil { | ||
node.Metadata.Fields = map[string]*structpb.Value{} | ||
} | ||
node.Metadata.Fields[tenantMetadataKey] = structpb.NewStringValue(tenantID) | ||
} | ||
|
||
func TenantFromMetadata(node *envoy_core.Node) (string, bool) { | ||
val, ok := node.GetMetadata().GetFields()[tenantMetadataKey] | ||
return val.GetStringValue(), ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters