Skip to content

Commit

Permalink
ref(pkg): update NewRouteWeightedClusters method
Browse files Browse the repository at this point in the history
+ now accepts a slice of service.WeightedCluster
+ easier to create RouteWeightedCluster now where there
is more than one backend/weighted cluster
+ prep for openservicemesh#2368

Signed-off-by: Michelle Noorali <minooral@microsoft.com>
  • Loading branch information
Michelle Noorali committed Mar 16, 2021
1 parent 70d6bf2 commit 265b5ab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pkg/catalog/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (mc *MeshCatalog) GetIngressPoliciesForService(svc service.MeshService) ([]
for _, ingress := range ingresses {
if ingress.Spec.Backend != nil && ingress.Spec.Backend.ServiceName == svc.Name {
wildcardIngressPolicy := trafficpolicy.NewInboundTrafficPolicy(buildIngressPolicyName(ingress.ObjectMeta.Name, ingress.ObjectMeta.Namespace, constants.WildcardHTTPMethod), []string{constants.WildcardHTTPMethod})
wildcardIngressPolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(wildCardRouteMatch, ingressWeightedCluster), wildcardServiceAccount)
wildcardIngressPolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(wildCardRouteMatch, []service.WeightedCluster{ingressWeightedCluster}), wildcardServiceAccount)
inboundIngressPolicies = trafficpolicy.MergeInboundPolicies(false, inboundIngressPolicies, wildcardIngressPolicy)
}

Expand All @@ -50,7 +50,7 @@ func (mc *MeshCatalog) GetIngressPoliciesForService(svc service.MeshService) ([]
if ingressPath.Path != "" {
routePolicy.PathRegex = ingressPath.Path
}
ingressPolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(routePolicy, ingressWeightedCluster), wildcardServiceAccount)
ingressPolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(routePolicy, []service.WeightedCluster{ingressWeightedCluster}), wildcardServiceAccount)
}

inboundIngressPolicies = trafficpolicy.MergeInboundPolicies(false, inboundIngressPolicies, ingressPolicy)
Expand Down
17 changes: 8 additions & 9 deletions pkg/catalog/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,18 @@ func (mc *MeshCatalog) listOutboundTrafficPoliciesForTrafficSplits(sourceNamespa
}
policy := trafficpolicy.NewOutboundTrafficPolicy(buildPolicyName(svc, sourceNamespace == svc.Namespace), hostnames)

rwc := trafficpolicy.RouteWeightedClusters{
HTTPRouteMatch: wildCardRouteMatch,
WeightedClusters: mapset.NewSet(),
}
weightedClusters := []service.WeightedCluster{}
for _, backend := range split.Spec.Backends {
ms := service.MeshService{Name: backend.Service, Namespace: split.ObjectMeta.Namespace}
wc := service.WeightedCluster{
ClusterName: service.ClusterName(ms.String()),
Weight: backend.Weight,
}
rwc.WeightedClusters.Add(wc)
weightedClusters = append(weightedClusters, wc)
}
policy.Routes = []*trafficpolicy.RouteWeightedClusters{&rwc}

rwc := trafficpolicy.NewRouteWeightedCluster(wildCardRouteMatch, weightedClusters)
policy.Routes = []*trafficpolicy.RouteWeightedClusters{rwc}

if apexServices.Contains(svc) {
log.Error().Msgf("Skipping Traffic Split policy %s in namespaces %s as there is already a traffic split policy for apex service %v", split.Name, split.Namespace, svc)
Expand Down Expand Up @@ -137,7 +136,7 @@ func (mc *MeshCatalog) listInboundPoliciesForTrafficSplits(upstreamIdentity serv

for _, sourceServiceAccount := range trafficTargetIdentitiesToSvcAccounts(t.Spec.Sources) {
for _, routeMatch := range routeMatches {
servicePolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(routeMatch, weightedCluster), sourceServiceAccount)
servicePolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(routeMatch, []service.WeightedCluster{weightedCluster}), sourceServiceAccount)
}
}
inboundPolicies = trafficpolicy.MergeInboundPolicies(false, inboundPolicies, servicePolicy)
Expand Down Expand Up @@ -310,7 +309,7 @@ func (mc *MeshCatalog) buildInboundPolicies(t *access.TrafficTarget, svc service

for _, sourceServiceAccount := range trafficTargetIdentitiesToSvcAccounts(t.Spec.Sources) {
for _, routeMatch := range routeMatches {
servicePolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(routeMatch, weightedCluster), sourceServiceAccount)
servicePolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(routeMatch, []service.WeightedCluster{weightedCluster}), sourceServiceAccount)
}
}

Expand All @@ -337,7 +336,7 @@ func (mc *MeshCatalog) buildInboundPermissiveModePolicies(svc service.MeshServic
// Build a rule for every service account in the mesh
for _, svcAccount := range svcAccounts {
sa := utils.SvcAccountToK8sSvcAccount(svcAccount)
servicePolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(wildCardRouteMatch, weightedCluster), sa)
servicePolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(wildCardRouteMatch, []service.WeightedCluster{weightedCluster}), sa)
}

if len(servicePolicy.Rules) > 0 {
Expand Down
10 changes: 8 additions & 2 deletions pkg/trafficpolicy/trafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import (
)

// NewRouteWeightedCluster takes a route and weighted cluster and returns a *RouteWeightedCluster
func NewRouteWeightedCluster(route HTTPRouteMatch, weightedCluster service.WeightedCluster) *RouteWeightedClusters {
func NewRouteWeightedCluster(route HTTPRouteMatch, weightedClusters []service.WeightedCluster) *RouteWeightedClusters {

weightedClusterSet := set.NewSet()
for _, wc := range weightedClusters {
weightedClusterSet.Add(wc)
}

return &RouteWeightedClusters{
HTTPRouteMatch: route,
WeightedClusters: set.NewSet(weightedCluster),
WeightedClusters: weightedClusterSet,
}
}

Expand Down
23 changes: 20 additions & 3 deletions pkg/trafficpolicy/trafficpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,27 @@ func TestNewInboundTrafficPolicy(t *testing.T) {

func TestNewRouteWeightedCluster(t *testing.T) {
assert := tassert.New(t)
expected := &RouteWeightedClusters{HTTPRouteMatch: testHTTPRouteMatch, WeightedClusters: set.NewSet(testWeightedCluster)}

actual := NewRouteWeightedCluster(testHTTPRouteMatch, testWeightedCluster)
assert.Equal(expected, actual)
testCases := []struct {
name string
route HTTPRouteMatch
weightedClusters []service.WeightedCluster
expected *RouteWeightedClusters
}{
{
name: "single weighted cluster in set",
route: testHTTPRouteMatch,
weightedClusters: []service.WeightedCluster{testWeightedCluster},
expected: &RouteWeightedClusters{HTTPRouteMatch: testHTTPRouteMatch, WeightedClusters: set.NewSet(testWeightedCluster)},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := NewRouteWeightedCluster(tc.route, tc.weightedClusters)
assert.Equal(tc.expected, actual)
})
}
}

func TestNewOutboundPolicy(t *testing.T) {
Expand Down

0 comments on commit 265b5ab

Please sign in to comment.