Repository: kumahq/kuma
Pull request #1188 (2020-11-20 13:35:11)

fix(kuma-cp) handle missing TrafficRoute
### Summary

Since 1.0 traffic is only routed when there is a TrafficRoute in the cluster. It is legal to remove it and see no traffic.
Before this PR, the Envoy config generation would fail, but what we want to do is to generate a config that does not route to the cluster.

My prediction is that 99% of cases when user will see this log is when they will delete TrafficRoute by accident therefore I'm putting a link in logs to the website where there will be a section with default TrafficRoute and an explanation that this will be required.

### Documentation

- [X] https://github.com/kumahq/kuma-website/pull/322



Patch:
diff --git a/pkg/xds/envoy/listeners/tcp_proxy_configurer.go b/pkg/xds/envoy/listeners/tcp_proxy_configurer.go
index eacde1842..97658bf3a 100644
--- a/pkg/xds/envoy/listeners/tcp_proxy_configurer.go
+++ b/pkg/xds/envoy/listeners/tcp_proxy_configurer.go
@@ -25,6 +25,9 @@ type TcpProxyConfigurer struct {
 }
 
 func (c *TcpProxyConfigurer) Configure(filterChain *envoy_listener.FilterChain) error {
+	if len(c.clusters) == 0 {
+		return nil
+	}
 	tcpProxy := c.tcpProxy()
 
 	pbst, err := proto.MarshalAnyDeterministic(tcpProxy)
diff --git a/pkg/xds/generator/outbound_proxy_generator.go b/pkg/xds/generator/outbound_proxy_generator.go
index e3e900d1f..8e046ece7 100644
--- a/pkg/xds/generator/outbound_proxy_generator.go
+++ b/pkg/xds/generator/outbound_proxy_generator.go
@@ -8,6 +8,7 @@ import (
 
 	"github.com/pkg/errors"
 
+	"github.com/kumahq/kuma/pkg/core"
 	"github.com/kumahq/kuma/pkg/core/validators"
 	envoy_names "github.com/kumahq/kuma/pkg/xds/envoy/names"
 
@@ -21,6 +22,8 @@ import (
 	envoy_listeners "github.com/kumahq/kuma/pkg/xds/envoy/listeners"
 )
 
+var outboundLog = core.Log.WithName("outbound-proxy-generator")
+
 // OriginOutbound is a marker to indicate by which ProxyGenerator resources were generated.
 const OriginOutbound = "outbound"
 
@@ -212,8 +215,9 @@ func (_ OutboundProxyGenerator) determineSubsets(proxy *model.Proxy, outbound *k
 	oface := proxy.Dataplane.Spec.Networking.ToOutboundInterface(outbound)
 
 	route := proxy.TrafficRoutes[oface]
-	if route == nil { // should not happen since we always generate default route if TrafficRoute is not found
-		return nil, errors.Errorf("no TrafficRoute for outbound %s", oface)
+	if route == nil {
+		outboundLog.Info("there is no selected TrafficRoute for the outbound interface, which means that the traffic won't be routed. Visit https://kuma.io/docs/latest/policies/traffic-route/ to check how to introduce the routing.", "dataplane", proxy.Dataplane.Meta.GetName(), "mesh", proxy.Dataplane.Meta.GetMesh(), "outbound", oface)
+		return nil, nil
 	}
 
 	for j, destination := range route.Spec.GetConf().GetSplit() {


Test patch:
diff --git a/pkg/xds/generator/outbound_proxy_generator_test.go b/pkg/xds/generator/outbound_proxy_generator_test.go
index 8cb1a0fdd..08d37d304 100644
--- a/pkg/xds/generator/outbound_proxy_generator_test.go
+++ b/pkg/xds/generator/outbound_proxy_generator_test.go
@@ -277,6 +277,10 @@ var _ = Describe("OutboundProxyGenerator", func() {
 							},
 						},
 					},
+					mesh_proto.OutboundInterface{
+						DataplaneIP:   "127.0.0.1",
+						DataplanePort: 4040,
+					}: nil,
 				},
 				OutboundSelectors: model.DestinationMap{
 					"api-http": model.TagSelectorSet{
@@ -457,6 +461,22 @@ var _ = Describe("OutboundProxyGenerator", func() {
 `,
 			expected: "06.envoy.golden.yaml",
 		}),
+		Entry("07. no TrafficRoute", testCase{
+			ctx: mtlsCtx,
+			dataplane: `
+            networking:
+              address: 10.0.0.1
+              inbound:
+              - port: 8080
+                tags:
+                  kuma.io/service: web
+              outbound:
+              - port: 4040
+                tags:
+                  kuma.io/service: service-without-traffic-route
+`,
+			expected: "07.envoy.golden.yaml",
+		}),
 	)
 
 	It("Add sanitized alternative cluster name for stats", func() {
diff --git a/pkg/xds/generator/testdata/outbound-proxy/07.envoy.golden.yaml b/pkg/xds/generator/testdata/outbound-proxy/07.envoy.golden.yaml
new file mode 100644
index 000000000..affdac5cb
--- /dev/null
+++ b/pkg/xds/generator/testdata/outbound-proxy/07.envoy.golden.yaml
@@ -0,0 +1,12 @@
+resources:
+  - name: outbound:127.0.0.1:4040
+    resource:
+      '@type': type.googleapis.com/envoy.api.v2.Listener
+      address:
+        socketAddress:
+          address: 127.0.0.1
+          portValue: 4040
+      filterChains:
+        - {}
+      name: outbound:127.0.0.1:4040
+      trafficDirection: OUTBOUND