-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kuma-cp) initial connection policy support for Gateway (#2933)
Gateway resources create connections from the gateway (source) to services running in the mesh (destination). Add support for matching connection policies based on these sources and destinations. The sources for connection policies are gateway listeners, but the destinations aren't known until we process the relevant gateway route types. So, we cache the policies that matched on the source (along with their match rank) on each virtual host, and complete the match once the final route table has been built. Note that some Kuma connection policies are implemented by configuring both the Envoy HTTP connection manager and the relevant upstream cluster. These connection policies cannot be implemented in Gateway, since they cannot configure the HTTP connection manager with different setting for each destination. Signed-off-by: James Peach <james.peach@konghq.com> (cherry picked from commit c10ab8a)
- Loading branch information
Showing
26 changed files
with
1,600 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package policy | ||
|
||
import ( | ||
mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1" | ||
) | ||
|
||
// MatchSelector succeeds if any of the given selectors matches the tags. It | ||
// additionally returns the rank of the selector that matched the tag. | ||
func MatchSelector(tags map[string]string, selectors []*mesh_proto.Selector) (mesh_proto.TagSelectorRank, bool) { | ||
for _, selector := range selectors { | ||
sourceSelector := mesh_proto.TagSelector(selector.GetMatch()) | ||
if sourceSelector.Matches(tags) { | ||
return sourceSelector.Rank(), true | ||
} | ||
} | ||
|
||
return mesh_proto.TagSelectorRank{}, false | ||
} |
83 changes: 83 additions & 0 deletions
83
pkg/plugins/runtime/gateway/connection_policy_generator.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,83 @@ | ||
package gateway | ||
|
||
import ( | ||
"sort" | ||
|
||
mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1" | ||
"github.com/kumahq/kuma/pkg/core/policy" | ||
"github.com/kumahq/kuma/pkg/core/resources/model" | ||
core_xds "github.com/kumahq/kuma/pkg/core/xds" | ||
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/match" | ||
xds_context "github.com/kumahq/kuma/pkg/xds/context" | ||
"github.com/kumahq/kuma/pkg/xds/envoy" | ||
) | ||
|
||
// ConnectionPolicyGenerator matches connection policies for each route table | ||
// entry that forwards traffic. | ||
type ConnectionPolicyGenerator struct { | ||
} | ||
|
||
func (*ConnectionPolicyGenerator) SupportsProtocol(p mesh_proto.Gateway_Listener_Protocol) bool { | ||
return true | ||
} | ||
|
||
func (g *ConnectionPolicyGenerator) GenerateHost(ctx xds_context.Context, info *GatewayResourceInfo) (*core_xds.ResourceSet, error) { | ||
for _, e := range info.RouteTable.Entries { | ||
for i, destination := range e.Action.Forward { | ||
e.Action.Forward[i].Policies = mapPoliciesForDestination(destination.Destination, info) | ||
} | ||
if e.Mirror != nil { | ||
e.Mirror.Forward.Policies = mapPoliciesForDestination(e.Mirror.Forward.Destination, info) | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func mapPoliciesForDestination(destination envoy.Tags, info *GatewayResourceInfo) map[model.ResourceType]model.Resource { | ||
policies := map[model.ResourceType]model.Resource{} | ||
|
||
for _, policyType := range ConnectionPolicyTypes { | ||
if policy := matchConnectionPolicy(info.Host.Policies[policyType], destination); policy != nil { | ||
policies[policyType] = policy | ||
} | ||
} | ||
|
||
return policies | ||
} | ||
|
||
func matchConnectionPolicy(candidates []match.RankedPolicy, destination envoy.Tags) model.Resource { | ||
var matches []match.RankedPolicy | ||
|
||
for _, c := range candidates { | ||
if rank, ok := policy.MatchSelector(destination, c.Policy.Destinations()); ok { | ||
// Track this match with the combined source+destination rank. | ||
matches = append(matches, match.RankedPolicy{ | ||
Rank: rank.CombinedWith(c.Rank), | ||
Policy: c.Policy, | ||
}) | ||
} | ||
} | ||
|
||
if len(matches) == 0 { | ||
return nil | ||
} | ||
|
||
// Sort more specific (higher ranked) policies first. | ||
sort.Slice(matches, func(i, j int) bool { | ||
n := matches[i].Rank.CompareTo(matches[j].Rank) | ||
switch { | ||
case n < 0: | ||
return false | ||
case n > 0: | ||
return true | ||
default /* i == 0 */ : | ||
// If the rank is the same, the most recent | ||
// policy sorts to the front (i.e. takes priority). | ||
return matches[i].Policy.GetMeta().GetCreationTime().After( | ||
matches[j].Policy.GetMeta().GetCreationTime()) | ||
} | ||
}) | ||
|
||
return matches[0].Policy | ||
} |
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.