Repository: kumahq/kuma
Pull request #1396 (2021-01-07 17:09:21)

fix(*) traffic logging to tcp backends (bp #1394)
This is an automated backport of pull request #1394 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/app/kuma-dp/cmd/run.go b/app/kuma-dp/cmd/run.go
index 7e7800e3b..be4223cda 100644
--- a/app/kuma-dp/cmd/run.go
+++ b/app/kuma-dp/cmd/run.go
@@ -122,7 +122,7 @@ func newRunCmd() *cobra.Command {
 			if err != nil {
 				return err
 			}
-			server := accesslogs.NewAccessLogServer()
+			server := accesslogs.NewAccessLogServer(cfg.Dataplane)
 
 			componentMgr := component.NewManager(leader_memory.NewNeverLeaderElector())
 			if err := componentMgr.Add(server, dataplane); err != nil {
diff --git a/app/kuma-dp/pkg/dataplane/accesslogs/server.go b/app/kuma-dp/pkg/dataplane/accesslogs/server.go
index f899c0f66..a248473b1 100644
--- a/app/kuma-dp/pkg/dataplane/accesslogs/server.go
+++ b/app/kuma-dp/pkg/dataplane/accesslogs/server.go
@@ -6,10 +6,13 @@ import (
 	"net"
 	"sync/atomic"
 
+	"github.com/kumahq/kuma/pkg/xds/envoy"
+
 	envoy_accesslog "github.com/envoyproxy/go-control-plane/envoy/service/accesslog/v2"
 	"github.com/pkg/errors"
 	"google.golang.org/grpc"
 
+	kumadp "github.com/kumahq/kuma/pkg/config/app/kuma-dp"
 	"github.com/kumahq/kuma/pkg/core"
 	"github.com/kumahq/kuma/pkg/core/runtime/component"
 )
@@ -31,9 +34,8 @@ func (s *accessLogServer) NeedLeaderElection() bool {
 	return false
 }
 
-func NewAccessLogServer() *accessLogServer {
-	id := core.NewUUID()
-	var address = fmt.Sprintf("/tmp/%s.sock", id)
+func NewAccessLogServer(dataplane kumadp.Dataplane) *accessLogServer {
+	address := envoy.AccessLogSocketName(dataplane.Name, dataplane.Mesh)
 	return &accessLogServer{
 		server:     grpc.NewServer(),
 		newHandler: defaultHandler,
diff --git a/pkg/xds/bootstrap/generator.go b/pkg/xds/bootstrap/generator.go
index 05ee08f5f..c2aa74546 100644
--- a/pkg/xds/bootstrap/generator.go
+++ b/pkg/xds/bootstrap/generator.go
@@ -12,6 +12,8 @@ import (
 	"strings"
 	"text/template"
 
+	"github.com/kumahq/kuma/pkg/xds/envoy"
+
 	"github.com/kumahq/kuma/pkg/core/resources/model"
 	"github.com/kumahq/kuma/pkg/core/resources/model/rest"
 	"github.com/kumahq/kuma/pkg/core/validators"
@@ -175,7 +177,7 @@ func (b *bootstrapGenerator) generateFor(proxyId core_xds.ProxyId, dataplane *co
 		return nil, err
 	}
 
-	var certBytes string = ""
+	var certBytes = ""
 	if b.xdsCertFile != "" {
 		cert, err := ioutil.ReadFile(b.xdsCertFile)
 		if err != nil {
@@ -183,7 +185,7 @@ func (b *bootstrapGenerator) generateFor(proxyId core_xds.ProxyId, dataplane *co
 		}
 		certBytes = base64.StdEncoding.EncodeToString(cert)
 	}
-	accessLogPipe := fmt.Sprintf("/tmp/kuma-access-logs-%s-%s.sock", request.Name, request.Mesh)
+	accessLogSocket := envoy.AccessLogSocketName(request.Name, request.Mesh)
 	params := configParameters{
 		Id:                 proxyId.String(),
 		Service:            service,
@@ -193,7 +195,7 @@ func (b *bootstrapGenerator) generateFor(proxyId core_xds.ProxyId, dataplane *co
 		XdsHost:            b.xdsHost(request),
 		XdsPort:            b.config.XdsPort,
 		XdsConnectTimeout:  b.config.XdsConnectTimeout,
-		AccessLogPipe:      accessLogPipe,
+		AccessLogPipe:      accessLogSocket,
 		DataplaneTokenPath: request.DataplaneTokenPath,
 		DataplaneResource:  request.DataplaneResource,
 		CertBytes:          certBytes,
diff --git a/pkg/xds/envoy/access_log.go b/pkg/xds/envoy/access_log.go
new file mode 100644
index 000000000..df9e3f24e
--- /dev/null
+++ b/pkg/xds/envoy/access_log.go
@@ -0,0 +1,15 @@
+package envoy
+
+import (
+	"fmt"
+)
+
+// AccessLogSocketName generates a socket path that will fit the Unix socket path limitation of 108 chars
+func AccessLogSocketName(name, mesh string) string {
+	socketName := fmt.Sprintf("/tmp/kuma-al-%s-%s", name, mesh)
+	trimLen := len(socketName)
+	if trimLen > 100 {
+		trimLen = 100
+	}
+	return socketName[:trimLen] + ".sock"
+}


Test patch:
diff --git a/pkg/xds/bootstrap/testdata/bootstrap.k8s.golden.yaml b/pkg/xds/bootstrap/testdata/bootstrap.k8s.golden.yaml
index da6dfcc34..59a1d6590 100644
--- a/pkg/xds/bootstrap/testdata/bootstrap.k8s.golden.yaml
+++ b/pkg/xds/bootstrap/testdata/bootstrap.k8s.golden.yaml
@@ -46,7 +46,7 @@ staticResources:
               - endpoint:
                   address:
                     pipe:
-                      path: /tmp/kuma-access-logs-dp-1.default-default.sock
+                      path: /tmp/kuma-al-dp-1.default-default.sock
       name: access_log_sink
       type: STATIC
       upstreamConnectionOptions:
diff --git a/pkg/xds/bootstrap/testdata/bootstrap.overridden.golden.yaml b/pkg/xds/bootstrap/testdata/bootstrap.overridden.golden.yaml
index 81c676046..72f4fe8c9 100644
--- a/pkg/xds/bootstrap/testdata/bootstrap.overridden.golden.yaml
+++ b/pkg/xds/bootstrap/testdata/bootstrap.overridden.golden.yaml
@@ -53,7 +53,7 @@ staticResources:
               - endpoint:
                   address:
                     pipe:
-                      path: /tmp/kuma-access-logs-dp-1.default-default.sock
+                      path: /tmp/kuma-al-dp-1.default-default.sock
       name: access_log_sink
       type: STATIC
       upstreamConnectionOptions:
diff --git a/pkg/xds/bootstrap/testdata/bootstrap.universal.golden.yaml b/pkg/xds/bootstrap/testdata/bootstrap.universal.golden.yaml
index 83c8fe19d..c22a92877 100644
--- a/pkg/xds/bootstrap/testdata/bootstrap.universal.golden.yaml
+++ b/pkg/xds/bootstrap/testdata/bootstrap.universal.golden.yaml
@@ -46,7 +46,7 @@ staticResources:
               - endpoint:
                   address:
                     pipe:
-                      path: /tmp/kuma-access-logs-dp-1-default.sock
+                      path: /tmp/kuma-al-dp-1-default.sock
       name: access_log_sink
       type: STATIC
       upstreamConnectionOptions:
diff --git a/pkg/xds/bootstrap/testdata/generator.custom-config-minimal-request.golden.yaml b/pkg/xds/bootstrap/testdata/generator.custom-config-minimal-request.golden.yaml
index 7dc9299c6..77e5846fb 100644
--- a/pkg/xds/bootstrap/testdata/generator.custom-config-minimal-request.golden.yaml
+++ b/pkg/xds/bootstrap/testdata/generator.custom-config-minimal-request.golden.yaml
@@ -44,7 +44,7 @@ staticResources:
               - endpoint:
                   address:
                     pipe:
-                      path: /tmp/kuma-access-logs-name.namespace-mesh.sock
+                      path: /tmp/kuma-al-name.namespace-mesh.sock
       name: access_log_sink
       type: STATIC
       upstreamConnectionOptions:
diff --git a/pkg/xds/bootstrap/testdata/generator.custom-config.golden.yaml b/pkg/xds/bootstrap/testdata/generator.custom-config.golden.yaml
index 42c37570b..7acfbaa98 100644
--- a/pkg/xds/bootstrap/testdata/generator.custom-config.golden.yaml
+++ b/pkg/xds/bootstrap/testdata/generator.custom-config.golden.yaml
@@ -54,7 +54,7 @@ staticResources:
               - endpoint:
                   address:
                     pipe:
-                      path: /tmp/kuma-access-logs-name.namespace-mesh.sock
+                      path: /tmp/kuma-al-name.namespace-mesh.sock
       name: access_log_sink
       type: STATIC
       upstreamConnectionOptions:
diff --git a/pkg/xds/bootstrap/testdata/generator.default-config-minimal-request.golden.yaml b/pkg/xds/bootstrap/testdata/generator.default-config-minimal-request.golden.yaml
index 5395f3bfe..ec03e2539 100644
--- a/pkg/xds/bootstrap/testdata/generator.default-config-minimal-request.golden.yaml
+++ b/pkg/xds/bootstrap/testdata/generator.default-config-minimal-request.golden.yaml
@@ -37,7 +37,7 @@ staticResources:
               - endpoint:
                   address:
                     pipe:
-                      path: /tmp/kuma-access-logs-name.namespace-mesh.sock
+                      path: /tmp/kuma-al-name.namespace-mesh.sock
       name: access_log_sink
       type: STATIC
       upstreamConnectionOptions:
diff --git a/pkg/xds/bootstrap/testdata/generator.default-config.golden.yaml b/pkg/xds/bootstrap/testdata/generator.default-config.golden.yaml
index 23ab19ef6..8b32be3d3 100644
--- a/pkg/xds/bootstrap/testdata/generator.default-config.golden.yaml
+++ b/pkg/xds/bootstrap/testdata/generator.default-config.golden.yaml
@@ -53,7 +53,7 @@ staticResources:
               - endpoint:
                   address:
                     pipe:
-                      path: /tmp/kuma-access-logs-name.namespace-mesh.sock
+                      path: /tmp/kuma-al-name.namespace-mesh.sock
       name: access_log_sink
       type: STATIC
       upstreamConnectionOptions: