Repository: kumahq/kuma
Pull request #1190 (2020-11-20 16:40:09)

fix(kuma-cp) handle missing TrafficRoute (bp #1188)
This is an automated backport of pull request #1188 done by Mergify
---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the [documentation](https://docs.mergify.io/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.io/) you can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.io/
</details>



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