Skip to content

Commit

Permalink
chore(pkg/catalog/*) : Add a function to create ingress policy name
Browse files Browse the repository at this point in the history
Creating a function to build the ingress policy name rather than building it in-line

Signed-off-by: Sneha Chhabria <snchh@microsoft.com>
  • Loading branch information
snehachhabria committed Feb 9, 2021
1 parent ad6879c commit 5ae3dcb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/catalog/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (mc *MeshCatalog) GetIngressPoliciesForService(svc service.MeshService, sa

for _, ingress := range ingresses {
if ingress.Spec.Backend != nil && ingress.Spec.Backend.ServiceName == svc.Name {
wildcardIngressPolicy := trafficpolicy.NewInboundTrafficPolicy(fmt.Sprintf("%s.%s|%s", ingress.ObjectMeta.Name, ingress.ObjectMeta.Namespace, constants.WildcardHTTPMethod), []string{constants.WildcardHTTPMethod})
wildcardIngressPolicy := trafficpolicy.NewInboundTrafficPolicy(buildIngressPolicyName(ingress.ObjectMeta.Name, ingress.ObjectMeta.Namespace, constants.WildcardHTTPMethod), []string{constants.WildcardHTTPMethod})
wildcardIngressPolicy.AddRule(*trafficpolicy.NewRouteWeightedCluster(wildCardRouteMatch, ingressWeightedCluster), sa)
inboundIngressPolicies = trafficpolicy.MergeInboundPolicies(false, inboundIngressPolicies, wildcardIngressPolicy)
}
Expand All @@ -81,7 +81,7 @@ func (mc *MeshCatalog) GetIngressPoliciesForService(svc service.MeshService, sa
if domain == "" {
domain = constants.WildcardHTTPMethod
}
ingressPolicy := trafficpolicy.NewInboundTrafficPolicy(fmt.Sprintf("%s.%s|%s", ingress.ObjectMeta.Name, ingress.ObjectMeta.Namespace, domain), []string{domain})
ingressPolicy := trafficpolicy.NewInboundTrafficPolicy(buildIngressPolicyName(ingress.ObjectMeta.Name, ingress.ObjectMeta.Namespace, domain), []string{domain})

for _, ingressPath := range rule.HTTP.Paths {
if ingressPath.Backend.ServiceName != svc.Name {
Expand All @@ -99,3 +99,8 @@ func (mc *MeshCatalog) GetIngressPoliciesForService(svc service.MeshService, sa
}
return inboundIngressPolicies, nil
}

func buildIngressPolicyName(name, namespace, host string) string {
policyName := fmt.Sprintf("%s.%s|%s", name, namespace, host)
return policyName
}
30 changes: 30 additions & 0 deletions pkg/catalog/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,33 @@ func TestGetIngressPoliciesForService(t *testing.T) {
}
}
}

func TestBuildIngressPolicyName(t *testing.T) {
assert := tassert.New(t)
testCases := []struct {
name string
namespace string
host string
expectedName string
}{
{
name: "bookbuyer",
namespace: "default",
host: "*",
expectedName: "bookbuyer.default|*",
},
{
name: "bookbuyer",
namespace: "bookbuyer-ns",
host: "foobar.com",
expectedName: "bookbuyer.bookbuyer-ns|foobar.com",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := buildIngressPolicyName(tc.name, tc.namespace, tc.host)
assert.Equal(tc.expectedName, actual)
})
}
}

0 comments on commit 5ae3dcb

Please sign in to comment.