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

feat(kuma-cp): add locality aware implementation for egress #8233

Merged
merged 30 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
600ea98
feat(kuma-cp): add locality aware implementation for egress
Automaat Nov 2, 2023
4700848
feat(kuma-cp): add locality aware implementation for egress
Automaat Nov 2, 2023
a91779b
Merge branch 'master' into feat/lb-egress
Automaat Nov 2, 2023
42bc05e
Merge branch 'master' into feat/lb-egress
lukidzi Nov 2, 2023
4d829ae
feat(kuma-cp): add locality aware implementation for egress
Automaat Nov 2, 2023
1565794
feat(kuma-cp): add locality aware implementation for egress
Automaat Nov 3, 2023
e515708
feat(kuma-cp): fix check
Automaat Nov 3, 2023
c3a114c
feat(kuma-cp): review
Automaat Nov 3, 2023
927d51f
Merge branch 'master' into feat/lb-egress
slonka Nov 3, 2023
bcbaeb6
feat(kuma-cp): review
Automaat Nov 3, 2023
a267787
feat(kuma-cp): review
Automaat Nov 3, 2023
b51539b
feat(kuma-cp): review
Automaat Nov 3, 2023
cb34eeb
feat(kuma-cp): review
Automaat Nov 3, 2023
d8813b9
feat(kuma-cp): review
Automaat Nov 3, 2023
dbdfc8f
feat(kuma-cp): review
Automaat Nov 4, 2023
b50c090
feat(kuma-cp): review
Automaat Nov 4, 2023
77a722c
feat(kuma-cp): review
Automaat Nov 6, 2023
ba4392c
feat(kuma-cp): review
Automaat Nov 6, 2023
72f3915
feat(kuma-cp): review
Automaat Nov 6, 2023
a572691
feat(kuma-cp): review
Automaat Nov 6, 2023
69ba403
feat(kuma-cp): review
Automaat Nov 6, 2023
a493fa3
feat(kuma-cp): review
Automaat Nov 6, 2023
39bcc8e
feat(kuma-cp): review
Automaat Nov 6, 2023
5fcd5ce
feat(kuma-cp): review
Automaat Nov 6, 2023
73d2511
feat(kuma-cp): review
Automaat Nov 6, 2023
fbd048d
feat(kuma-cp): review
Automaat Nov 6, 2023
ccb2596
Merge branch 'release-2.5' into feat/lb-egress
Automaat Nov 6, 2023
c72c08f
feat(kuma-cp): review
Automaat Nov 6, 2023
c73a352
feat(kuma-cp): review
Automaat Nov 6, 2023
fa354d1
feat(kuma-cp): review
Automaat Nov 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,6 @@ spec:
type: array
required:
- type
- zones
Automaat marked this conversation as resolved.
Show resolved Hide resolved
type: object
required:
- to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,6 @@ spec:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,6 @@ spec:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,6 @@ spec:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3391,7 +3391,6 @@ spec:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3595,7 +3595,6 @@ spec:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ spec:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
1 change: 0 additions & 1 deletion docs/generated/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4052,7 +4052,6 @@ components:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ spec:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/plugins/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type EgressPolicyPlugin interface {
PolicyPlugin
// EgressMatchedPolicies returns all the policies of the plugins' type matching the external service that
// should be applied on the zone egress.
EgressMatchedPolicies(*core_mesh.ExternalServiceResource, xds_context.Resources) (core_xds.TypedMatchingPolicies, error)
EgressMatchedPolicies(map[string]string, xds_context.Resources) (core_xds.TypedMatchingPolicies, error)
}

// ProxyPlugin a plugin to modify the proxy. This happens before any `PolicyPlugin` or any envoy generation. and it is applied both for Dataplanes and ZoneProxies
Expand Down
33 changes: 14 additions & 19 deletions pkg/plugins/policies/core/matchers/egress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (

common_api "github.com/kumahq/kuma/api/common/v1alpha1"
mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
core_xds "github.com/kumahq/kuma/pkg/core/xds"
core_rules "github.com/kumahq/kuma/pkg/plugins/policies/core/rules"
xds_context "github.com/kumahq/kuma/pkg/xds/context"
)

func EgressMatchedPolicies(rType core_model.ResourceType, es *core_mesh.ExternalServiceResource, resources xds_context.Resources) (core_xds.TypedMatchingPolicies, error) {
func EgressMatchedPolicies(rType core_model.ResourceType, tags map[string]string, resources xds_context.Resources) (core_xds.TypedMatchingPolicies, error) {
policies := resources.ListOrEmpty(rType)

if len(policies.GetItems()) == 0 {
Expand All @@ -41,9 +40,9 @@ func EgressMatchedPolicies(rType core_model.ResourceType, es *core_mesh.External
var fr core_rules.FromRules
var err error
if isFrom {
fr, err = processFromRules(es, policies)
fr, err = processFromRules(tags, policies)
} else {
fr, err = processToRules(es, policies)
fr, err = processToRules(tags, policies)
}
if err != nil {
return core_xds.TypedMatchingPolicies{}, err
Expand All @@ -56,14 +55,14 @@ func EgressMatchedPolicies(rType core_model.ResourceType, es *core_mesh.External
}

func processFromRules(
es *core_mesh.ExternalServiceResource,
tags map[string]string,
Automaat marked this conversation as resolved.
Show resolved Hide resolved
rl core_model.ResourceList,
) (core_rules.FromRules, error) {
matchedPolicies := []core_model.Resource{}

for _, policy := range rl.GetItems() {
spec := policy.GetSpec().(core_model.Policy)
if !externalServiceSelectedByTargetRef(spec.GetTargetRef(), es) {
if !serviceSelectedByTargetRef(spec.GetTargetRef(), tags) {
continue
}
matchedPolicies = append(matchedPolicies, policy)
Expand Down Expand Up @@ -113,11 +112,8 @@ func processFromRules(
// disabled: true
//
// that's why processToRules() method produces FromRules for the Egress.
func processToRules(
es *core_mesh.ExternalServiceResource,
rl core_model.ResourceList,
) (core_rules.FromRules, error) {
matchedPolicies := []core_model.Resource{}
func processToRules(tags map[string]string, rl core_model.ResourceList) (core_rules.FromRules, error) {
var matchedPolicies []core_model.Resource

for _, policy := range rl.GetItems() {
spec := policy.GetSpec().(core_model.Policy)
Expand All @@ -128,18 +124,18 @@ func processToRules(
}

for _, item := range to.GetToList() {
if externalServiceSelectedByTargetRef(item.GetTargetRef(), es) {
if serviceSelectedByTargetRef(item.GetTargetRef(), tags) {
matchedPolicies = append(matchedPolicies, policy)
}
}
}

sort.Sort(ByTargetRef(matchedPolicies))

toList := []core_rules.PolicyItemWithMeta{}
var toList []core_rules.PolicyItemWithMeta
for _, policy := range matchedPolicies {
for _, item := range policy.GetSpec().(core_model.PolicyWithToList).GetToList() {
if !externalServiceSelectedByTargetRef(item.GetTargetRef(), es) {
if !serviceSelectedByTargetRef(item.GetTargetRef(), tags) {
continue
}
// convert 'to' policyItem to 'from' policyItem
Expand Down Expand Up @@ -175,17 +171,16 @@ func (a *artificialPolicyItem) GetDefault() interface{} {
return a.conf
}

func externalServiceSelectedByTargetRef(tr common_api.TargetRef, es *core_mesh.ExternalServiceResource) bool {
func serviceSelectedByTargetRef(tr common_api.TargetRef, tags map[string]string) bool {
switch tr.Kind {
case common_api.Mesh:
return true
case common_api.MeshSubset:
return mesh_proto.TagSelector(tr.Tags).Matches(es.Spec.GetTags())
return mesh_proto.TagSelector(tr.Tags).Matches(tags)
case common_api.MeshService:
return tr.Name == es.Spec.GetTags()[mesh_proto.ServiceTag]
return tr.Name == tags[mesh_proto.ServiceTag]
case common_api.MeshServiceSubset:
return tr.Name == es.Spec.GetTags()[mesh_proto.ServiceTag] &&
mesh_proto.TagSelector(tr.Tags).Matches(es.Spec.GetTags())
return tr.Name == tags[mesh_proto.ServiceTag] && mesh_proto.TagSelector(tr.Tags).Matches(tags)
}
return false
}
4 changes: 2 additions & 2 deletions pkg/plugins/policies/core/matchers/egress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var _ = Describe("EgressMatchedPolicies", func() {
resources, _ := readPolicies(given.policiesFile)

// when
policies, err := matchers.EgressMatchedPolicies(policies_api.MeshTrafficPermissionType, es, resources)
policies, err := matchers.EgressMatchedPolicies(policies_api.MeshTrafficPermissionType, es.Spec.Tags, resources)
Expect(err).ToNot(HaveOccurred())

// then
Expand All @@ -76,7 +76,7 @@ var _ = Describe("EgressMatchedPolicies", func() {
resources, _ := readPolicies(given.policiesFile)

// when
policies, err := matchers.EgressMatchedPolicies(v1alpha1.MeshLoadBalancingStrategyType, es, resources)
policies, err := matchers.EgressMatchedPolicies(v1alpha1.MeshLoadBalancingStrategyType, es.Spec.Tags, resources)
Expect(err).ToNot(HaveOccurred())

// then
Expand Down
5 changes: 5 additions & 0 deletions pkg/plugins/policies/core/xds/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/metadata"
"github.com/kumahq/kuma/pkg/xds/envoy/tags"
"github.com/kumahq/kuma/pkg/xds/generator"
"github.com/kumahq/kuma/pkg/xds/generator/egress"
)

type Clusters struct {
Inbound map[string]*envoy_cluster.Cluster
Outbound map[string]*envoy_cluster.Cluster
OutboundSplit map[string][]*envoy_cluster.Cluster
Gateway map[string]*envoy_cluster.Cluster
Egress map[string]*envoy_cluster.Cluster
}

func GatherClusters(rs *core_xds.ResourceSet) Clusters {
Expand All @@ -25,6 +27,7 @@ func GatherClusters(rs *core_xds.ResourceSet) Clusters {
Outbound: map[string]*envoy_cluster.Cluster{},
OutboundSplit: map[string][]*envoy_cluster.Cluster{},
Gateway: map[string]*envoy_cluster.Cluster{},
Egress: map[string]*envoy_cluster.Cluster{},
}
for _, res := range rs.Resources(envoy_resource.ClusterType) {
cluster := res.Resource.(*envoy_cluster.Cluster)
Expand All @@ -42,6 +45,8 @@ func GatherClusters(rs *core_xds.ResourceSet) Clusters {
clusters.Inbound[cluster.Name] = cluster
case metadata.OriginGateway:
clusters.Gateway[cluster.Name] = cluster
case egress.OriginEgress:
clusters.Egress[cluster.Name] = cluster
default:
continue
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/plugins/policies/core/xds/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,26 @@ func GatherEndpoints(rs *xds.ResourceSet) EndpointMap {
return em
}

func GatherEgressEndpoints(rs *xds.ResourceSet) map[string]*endpointv3.ClusterLoadAssignment {
em := map[string]*endpointv3.ClusterLoadAssignment{}
func GatherEgressEndpoints(rs *xds.ResourceSet) EndpointMap {
em := EndpointMap{}
for _, res := range rs.Resources(envoy_resource.EndpointType) {
if res.Origin != egress.OriginEgress {
continue
}

cla := res.Resource.(*endpointv3.ClusterLoadAssignment)
serviceName := tags.ServiceFromClusterName(cla.ClusterName)
em[serviceName] = append(em[serviceName], cla)
}

for _, res := range rs.Resources(envoy_resource.ClusterType) {
if res.Origin != egress.OriginEgress {
continue
}

cluster := res.Resource.(*clusterv3.Cluster)
if cluster.LoadAssignment != nil {
em[cluster.Name] = cluster.LoadAssignment
em[cluster.Name] = append(em[cluster.Name], cluster.LoadAssignment)
}
}
return em
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type FromZone struct {
type ToZone struct {
// Type defines how target zones will be picked from available zones
Type ToZoneType `json:"type"`
Zones []string `json:"zones"`
Zones *[]string `json:"zones,omitempty"`
Automaat marked this conversation as resolved.
Show resolved Hide resolved
}

type FailoverThreshold struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ properties:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func validateCrossZone(crossZone *CrossZone) validators.ValidationError {
toZonesPath := path.Field("to").Field("zones")
switch failover.To.Type {
case Any, None:
if len(failover.To.Zones) > 0 {
if failover.To.Zones != nil && len(*failover.To.Zones) > 0 {
verr.AddViolationAt(toZonesPath, fmt.Sprintf("must be empty when type is %s", failover.To.Type))
}
case AnyExcept, Only:
if len(failover.To.Zones) == 0 {
if failover.To.Zones == nil || len(*failover.To.Zones) == 0 {
verr.AddViolationAt(toZonesPath, fmt.Sprintf("must not be empty when type is %s", failover.To.Type))
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ to:
},
{
Field: "spec.to[0].default.localityAwareness.crossZone.failover[4].to.zones",
Message: "must not be empty when type is Only",
},
{
Field: "spec.to[0].default.localityAwareness.crossZone.failover[5].to.zones",
Message: "must not be empty when type is AnyExcept",
},
{
Field: "spec.to[0].default.localityAwareness.crossZone.failover[6].to.zones",
Message: "must not be empty when type is AnyExcept",
},
}, `
Expand Down Expand Up @@ -355,9 +363,14 @@ to:
- to:
type: Only
zones: []
- to:
type: Only
- to:
type: AnyExcept
zones: []
- to:
type: AnyExcept

`),
)

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ spec:
type: array
required:
- type
- zones
type: object
required:
- to
Expand Down
Loading
Loading