-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sidecar-proxy controller: Add support for transparent proxy
This currently does not support inferring destinations from intentions.
- Loading branch information
Showing
32 changed files
with
1,854 additions
and
478 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
41 changes: 41 additions & 0 deletions
41
internal/mesh/internal/cache/sidecarproxycache/proxy_configuration_cache.go
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,41 @@ | ||
package sidecarproxycache | ||
|
||
import ( | ||
"github.com/hashicorp/consul/internal/mesh/internal/types" | ||
"github.com/hashicorp/consul/internal/resource" | ||
"github.com/hashicorp/consul/internal/resource/mappers/bimapper" | ||
"github.com/hashicorp/consul/proto-public/pbresource" | ||
) | ||
|
||
// ProxyConfigurationCache tracks mappings between proxy configurations and proxy IDs | ||
// that a configuration applies to. It is the responsibility of the controller to | ||
// keep this cache up-to-date. | ||
type ProxyConfigurationCache struct { | ||
mapper *bimapper.Mapper | ||
} | ||
|
||
func NewProxyConfigurationCache() *ProxyConfigurationCache { | ||
return &ProxyConfigurationCache{ | ||
mapper: bimapper.New(types.ProxyConfigurationType, types.ProxyStateTemplateType), | ||
} | ||
} | ||
|
||
// ProxyConfigurationsByProxyID returns proxy configuration IDs given the id of the proxy state template. | ||
func (c *ProxyConfigurationCache) ProxyConfigurationsByProxyID(id *pbresource.ID) []*pbresource.ID { | ||
return c.mapper.ItemIDsForLink(id) | ||
} | ||
|
||
// TrackProxyConfiguration tracks given proxy configuration ID and the linked proxy state template IDs. | ||
func (c *ProxyConfigurationCache) TrackProxyConfiguration(proxyCfgID *pbresource.ID, proxyIDs []resource.ReferenceOrID) { | ||
c.mapper.TrackItem(proxyCfgID, proxyIDs) | ||
} | ||
|
||
// UntrackProxyConfiguration removes tracking for the given proxy configuration ID. | ||
func (c *ProxyConfigurationCache) UntrackProxyConfiguration(proxyCfgID *pbresource.ID) { | ||
c.mapper.UntrackItem(proxyCfgID) | ||
} | ||
|
||
// UntrackProxyID removes tracking for the given proxy state template ID. | ||
func (c *ProxyConfigurationCache) UntrackProxyID(proxyID *pbresource.ID) { | ||
c.mapper.UntrackLink(proxyID) | ||
} |
68 changes: 68 additions & 0 deletions
68
internal/mesh/internal/cache/sidecarproxycache/proxy_configuration_cache_test.go
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,68 @@ | ||
package sidecarproxycache | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/internal/mesh/internal/types" | ||
"github.com/hashicorp/consul/internal/resource" | ||
"github.com/hashicorp/consul/internal/resource/resourcetest" | ||
"github.com/hashicorp/consul/proto-public/pbresource" | ||
"github.com/hashicorp/consul/proto/private/prototest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProxyConfigurationCache(t *testing.T) { | ||
cache := NewProxyConfigurationCache() | ||
|
||
// Create some proxy configurations. | ||
proxyCfg1 := resourcetest.Resource(types.ProxyConfigurationType, "test-cfg-1").ID() | ||
proxyCfg2 := resourcetest.Resource(types.ProxyConfigurationType, "test-cfg-2").ID() | ||
proxyCfg3 := resourcetest.Resource(types.ProxyConfigurationType, "test-cfg-3").ID() | ||
|
||
// Create some proxy state templates. | ||
p1 := resourcetest.Resource(types.ProxyStateTemplateType, "w-111").ID() | ||
p2 := resourcetest.Resource(types.ProxyStateTemplateType, "w-222").ID() | ||
p3 := resourcetest.Resource(types.ProxyStateTemplateType, "w-333").ID() | ||
p4 := resourcetest.Resource(types.ProxyStateTemplateType, "w-444").ID() | ||
p5 := resourcetest.Resource(types.ProxyStateTemplateType, "w-555").ID() | ||
|
||
// Track these and make sure there's some overlap. | ||
cache.TrackProxyConfiguration(proxyCfg1, []resource.ReferenceOrID{p1, p2, p4}) | ||
cache.TrackProxyConfiguration(proxyCfg2, []resource.ReferenceOrID{p3, p4, p5}) | ||
cache.TrackProxyConfiguration(proxyCfg3, []resource.ReferenceOrID{p1, p3}) | ||
|
||
// Read proxy configurations by proxy. | ||
requireProxyConfigurations(t, cache, p1, proxyCfg1, proxyCfg3) | ||
requireProxyConfigurations(t, cache, p2, proxyCfg1) | ||
requireProxyConfigurations(t, cache, p3, proxyCfg2, proxyCfg3) | ||
requireProxyConfigurations(t, cache, p4, proxyCfg1, proxyCfg2) | ||
requireProxyConfigurations(t, cache, p5, proxyCfg2) | ||
|
||
// Untrack some proxy IDs. | ||
cache.UntrackProxyID(p1) | ||
|
||
requireProxyConfigurations(t, cache, p1) | ||
|
||
// Untrack some proxy IDs. | ||
cache.UntrackProxyID(p3) | ||
|
||
requireProxyConfigurations(t, cache, p3) | ||
|
||
// Untrack proxy cfg. | ||
cache.UntrackProxyConfiguration(proxyCfg1) | ||
|
||
requireProxyConfigurations(t, cache, p1) // no-op because we untracked it earlier | ||
requireProxyConfigurations(t, cache, p2) | ||
requireProxyConfigurations(t, cache, p3) // no-op because we untracked it earlier | ||
requireProxyConfigurations(t, cache, p4, proxyCfg2) | ||
requireProxyConfigurations(t, cache, p5, proxyCfg2) | ||
} | ||
|
||
func requireProxyConfigurations(t *testing.T, cache *ProxyConfigurationCache, proxyID *pbresource.ID, proxyCfgs ...*pbresource.ID) { | ||
t.Helper() | ||
|
||
actualProxyCfgs := cache.ProxyConfigurationsByProxyID(proxyID) | ||
|
||
require.Len(t, actualProxyCfgs, len(proxyCfgs)) | ||
prototest.AssertElementsMatch(t, proxyCfgs, actualProxyCfgs) | ||
} |
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
Oops, something went wrong.