Skip to content

Commit

Permalink
code review: refactor out GetInboundRouteName() function
Browse files Browse the repository at this point in the history
  • Loading branch information
yskopets committed Feb 18, 2020
1 parent 655b05d commit 47f5f15
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
5 changes: 2 additions & 3 deletions pkg/xds/envoy/listeners/http_inbound_route_configurer.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package listeners

import (
"fmt"

"github.com/golang/protobuf/proto"

envoy_listener "github.com/envoyproxy/go-control-plane/envoy/api/v2/listener"
envoy_hcm "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2"
envoy_wellknown "github.com/envoyproxy/go-control-plane/pkg/wellknown"

envoy_common "github.com/Kong/kuma/pkg/xds/envoy"
envoy_names "github.com/Kong/kuma/pkg/xds/envoy/names"
envoy_routes "github.com/Kong/kuma/pkg/xds/envoy/routes"
)

Expand All @@ -29,7 +28,7 @@ type HttpInboundRouteConfigurer struct {
}

func (c *HttpInboundRouteConfigurer) Configure(filterChain *envoy_listener.FilterChain) error {
routeName := fmt.Sprintf("inbound:%s", c.service)
routeName := envoy_names.GetInboundRouteName(c.service)
routeConfig, err := envoy_routes.NewRouteConfigurationBuilder().
Configure(envoy_routes.CommonRouteConfiguration(routeName)).
Configure(envoy_routes.VirtualHost(envoy_routes.NewVirtualHostBuilder().
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generator
package names

import (
"fmt"
Expand All @@ -8,31 +8,35 @@ import (
mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1"
)

func localClusterName(port uint32) string {
func GetLocalClusterName(port uint32) string {
return fmt.Sprintf("localhost:%d", port)
}

func inboundListenerName(address string, port uint32) string {
func GetInboundListenerName(address string, port uint32) string {
return fmt.Sprintf("inbound:%s:%d", address, port)
}

func outboundListenerName(address string, port uint32) string {
func GetOutboundListenerName(address string, port uint32) string {
return fmt.Sprintf("outbound:%s:%d", address, port)
}

func outboundRouteName(service string) string {
func GetInboundRouteName(service string) string {
return fmt.Sprintf("inbound:%s", service)
}

func GetOutboundRouteName(service string) string {
return fmt.Sprintf("outbound:%s", service)
}

func envoyAdminClusterName() string {
func GetEnvoyAdminClusterName() string {
return "kuma:envoy:admin"
}

func prometheusListenerName() string {
func GetPrometheusListenerName() string {
return "kuma:metrics:prometheus"
}

func destinationClusterName(service string, selector map[string]string) string {
func GetDestinationClusterName(service string, selector map[string]string) string {
var pairs []string
for key, value := range selector {
if key == mesh_proto.ServiceTag {
Expand Down
5 changes: 3 additions & 2 deletions pkg/xds/generator/prometheus_endpoint_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

envoy_clusters "github.com/Kong/kuma/pkg/xds/envoy/clusters"
envoy_listeners "github.com/Kong/kuma/pkg/xds/envoy/listeners"
envoy_names "github.com/Kong/kuma/pkg/xds/envoy/names"
)

// PrometheusEndpointGenerator generates an inbound Envoy listener
Expand Down Expand Up @@ -57,8 +58,8 @@ func (g PrometheusEndpointGenerator) Generate(ctx xds_context.Context, proxy *co
// since it would allow a malicious user to manipulate that value and use Prometheus endpoint
// as a gateway to another host.
adminAddress := "127.0.0.1"
envoyAdminClusterName := envoyAdminClusterName()
prometheusListenerName := prometheusListenerName()
envoyAdminClusterName := envoy_names.GetEnvoyAdminClusterName()
prometheusListenerName := envoy_names.GetPrometheusListenerName()

listener, err := envoy_listeners.NewListenerBuilder().
Configure(envoy_listeners.InboundListener(prometheusListenerName, prometheusEndpointAddress, prometheusEndpoint.Port)).
Expand Down
11 changes: 6 additions & 5 deletions pkg/xds/generator/proxy_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
envoy_clusters "github.com/Kong/kuma/pkg/xds/envoy/clusters"
envoy_endpoints "github.com/Kong/kuma/pkg/xds/envoy/endpoints"
envoy_listeners "github.com/Kong/kuma/pkg/xds/envoy/listeners"
envoy_names "github.com/Kong/kuma/pkg/xds/envoy/names"
envoy_routes "github.com/Kong/kuma/pkg/xds/envoy/routes"
)

Expand Down Expand Up @@ -99,7 +100,7 @@ func (g InboundProxyGenerator) Generate(ctx xds_context.Context, proxy *model.Pr
resources := &model.ResourceSet{}
for i, endpoint := range endpoints {
// generate CDS resource
localClusterName := localClusterName(endpoint.WorkloadPort)
localClusterName := envoy_names.GetLocalClusterName(endpoint.WorkloadPort)
resources.Add(&model.Resource{
Name: localClusterName,
Version: "",
Expand All @@ -110,7 +111,7 @@ func (g InboundProxyGenerator) Generate(ctx xds_context.Context, proxy *model.Pr
iface := proxy.Dataplane.Spec.Networking.Inbound[i]
service := iface.GetService()
protocol := mesh_core.ParseProtocol(iface.GetProtocol())
inboundListenerName := inboundListenerName(endpoint.DataplaneIP, endpoint.DataplanePort)
inboundListenerName := envoy_names.GetInboundListenerName(endpoint.DataplaneIP, endpoint.DataplanePort)
filterChainBuilder := func() *envoy_listeners.FilterChainBuilder {
filterChainBuilder := envoy_listeners.NewFilterChainBuilder()
switch protocol {
Expand Down Expand Up @@ -183,8 +184,8 @@ func (g OutboundProxyGenerator) Generate(ctx xds_context.Context, proxy *model.P
protocol := InferServiceProtocol(endpoints)

// generate LDS resource
outboundListenerName := outboundListenerName(ofaces[i].DataplaneIP, ofaces[i].DataplanePort)
outboundRouteName := outboundRouteName(outbound.Service)
outboundListenerName := envoy_names.GetOutboundListenerName(ofaces[i].DataplaneIP, ofaces[i].DataplanePort)
outboundRouteName := envoy_names.GetOutboundRouteName(outbound.Service)
destinationService := outbound.Service

filterChainBuilder := func() *envoy_listeners.FilterChainBuilder {
Expand Down Expand Up @@ -238,7 +239,7 @@ func (_ OutboundProxyGenerator) determineClusters(ctx xds_context.Context, proxy
continue
}
clusters = append(clusters, envoy_common.ClusterInfo{
Name: destinationClusterName(service, destination.Destination),
Name: envoy_names.GetDestinationClusterName(service, destination.Destination),
Weight: destination.Weight,
Tags: destination.Destination,
})
Expand Down

0 comments on commit 47f5f15

Please sign in to comment.