Repository: kumahq/kuma
Pull request #3406 (2021-12-01 11:33:41)

fix(kuma-cp) enable metrics hijacker for current version of Kuma (backport #3405)
This is an automatic backport of pull request #3405 done by [Mergify](https://mergify.com).


---


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

<br />

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

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.com/) you can:

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

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


codecov-commenter: # [Codecov](https://codecov.io/gh/kumahq/kuma/pull/3406?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq) Report
> Merging [#3406](https://codecov.io/gh/kumahq/kuma/pull/3406?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq) (aefecfa) into [release-1.4](https://codecov.io/gh/kumahq/kuma/commit/ab406bcb58e9ba32d2ebdd840e38f8a2a987a84f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq) (ab406bc) will **increase** coverage by `0.00%`.
> The diff coverage is `87.50%`.

[![Impacted file tree graph](https://codecov.io/gh/kumahq/kuma/pull/3406/graphs/tree.svg?width=650&height=150&src=pr&token=D7Yvqs6rwV&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq)](https://codecov.io/gh/kumahq/kuma/pull/3406?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq)

```diff
@@             Coverage Diff              @@
##           release-1.4    #3406   +/-   ##
============================================
  Coverage        52.29%   52.29%           
============================================
  Files              927      927           
  Lines            53986    53977    -9     
============================================
- Hits             28232    28229    -3     
+ Misses           23532    23527    -5     
+ Partials          2222     2221    -1     
```


| [Impacted Files](https://codecov.io/gh/kumahq/kuma/pull/3406?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq) | Coverage Δ | |
|---|---|---|
| [pkg/xds/generator/prometheus\_endpoint\_generator.go](https://codecov.io/gh/kumahq/kuma/pull/3406/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL3hkcy9nZW5lcmF0b3IvcHJvbWV0aGV1c19lbmRwb2ludF9nZW5lcmF0b3IuZ28=) | `90.24% <87.50%> (-0.97%)` | :arrow_down: |
| [pkg/core/resources/manager/cache.go](https://codecov.io/gh/kumahq/kuma/pull/3406/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL2NvcmUvcmVzb3VyY2VzL21hbmFnZXIvY2FjaGUuZ28=) | `85.71% <0.00%> (ø)` | |
| [pkg/plugins/runtime/gateway/route/sorter.go](https://codecov.io/gh/kumahq/kuma/pull/3406/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL3BsdWdpbnMvcnVudGltZS9nYXRld2F5L3JvdXRlL3NvcnRlci5nbw==) | `66.66% <0.00%> (ø)` | |
| [pkg/dns/vips\_allocator.go](https://codecov.io/gh/kumahq/kuma/pull/3406/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL2Rucy92aXBzX2FsbG9jYXRvci5nbw==) | `74.50% <0.00%> (+1.30%)` | :arrow_up: |
| [pkg/core/runtime/component/component.go](https://codecov.io/gh/kumahq/kuma/pull/3406/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq#diff-cGtnL2NvcmUvcnVudGltZS9jb21wb25lbnQvY29tcG9uZW50Lmdv) | `88.67% <0.00%> (+7.54%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kumahq/kuma/pull/3406?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kumahq/kuma/pull/3406?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq). Last update [ab406bc...aefecfa](https://codecov.io/gh/kumahq/kuma/pull/3406?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kumahq).


Patch:
diff --git a/pkg/xds/generator/prometheus_endpoint_generator.go b/pkg/xds/generator/prometheus_endpoint_generator.go
index 6d959be23..f6451b0e9 100644
--- a/pkg/xds/generator/prometheus_endpoint_generator.go
+++ b/pkg/xds/generator/prometheus_endpoint_generator.go
@@ -2,7 +2,6 @@ package generator
 
 import (
 	"net"
-	"strings"
 
 	"github.com/pkg/errors"
 
@@ -31,10 +30,6 @@ const OriginPrometheus = "prometheus"
 type PrometheusEndpointGenerator struct {
 }
 
-var HasMetricsHijacker = func(version *mesh_proto.Version) bool {
-	return strings.HasPrefix(version.GetKumaDp().GetVersion(), "1.2.")
-}
-
 func (g PrometheusEndpointGenerator) Generate(ctx xds_context.Context, proxy *core_xds.Proxy) (*core_xds.ResourceSet, error) {
 	prometheusEndpoint, err := proxy.Dataplane.GetPrometheusEndpoint(ctx.Mesh.Resource)
 	if err != nil {
@@ -64,34 +59,22 @@ func (g PrometheusEndpointGenerator) Generate(ctx xds_context.Context, proxy *co
 
 	resources := core_xds.NewResourceSet()
 
-	// backwards compatibility
-	// Kuma DP <1.2.x does not have metrics hijacker builtin therefore we configure those Envoys with just Envoy prometheus endpoint.
-	var clusterName string
-	var statsPath string
-
-	if HasMetricsHijacker(proxy.Metadata.Version) {
-		metricsHijackerClusterName := envoy_names.GetMetricsHijackerClusterName()
-		cluster, err := envoy_clusters.NewClusterBuilder(proxy.APIVersion).
-			Configure(envoy_clusters.StaticClusterUnixSocket(metricsHijackerClusterName,
-				envoy_common.MetricsHijackerSocketName(proxy.Dataplane.Meta.GetName(), proxy.Dataplane.Meta.GetMesh()))).
-			Build()
-		if err != nil {
-			return nil, err
-		}
-
-		resources.Add(&core_xds.Resource{
-			Name:     cluster.GetName(),
-			Origin:   OriginPrometheus,
-			Resource: cluster,
-		})
-
-		clusterName = metricsHijackerClusterName
-		statsPath = "/"
-	} else {
-		clusterName = envoy_names.GetEnvoyAdminClusterName()
-		statsPath = "/stats/prometheus"
+	const statsPath = "/"
+	metricsHijackerClusterName := envoy_names.GetMetricsHijackerClusterName()
+	cluster, err := envoy_clusters.NewClusterBuilder(proxy.APIVersion).
+		Configure(envoy_clusters.StaticClusterUnixSocket(metricsHijackerClusterName,
+			envoy_common.MetricsHijackerSocketName(proxy.Dataplane.Meta.GetName(), proxy.Dataplane.Meta.GetMesh()))).
+		Build()
+	if err != nil {
+		return nil, err
 	}
 
+	resources.Add(&core_xds.Resource{
+		Name:     cluster.GetName(),
+		Origin:   OriginPrometheus,
+		Resource: cluster,
+	})
+
 	// Cluster is generated by AdminProxyGenerator
 	prometheusListenerName := envoy_names.GetPrometheusListenerName()
 
@@ -112,7 +95,7 @@ func (g PrometheusEndpointGenerator) Generate(ctx xds_context.Context, proxy *co
 					envoy_listeners.StaticEndpoints(prometheusListenerName,
 						[]*envoy_common.StaticEndpointPath{
 							{
-								ClusterName: clusterName,
+								ClusterName: metricsHijackerClusterName,
 								Path:        prometheusEndpoint.Path,
 								RewritePath: statsPath,
 							},
@@ -125,7 +108,7 @@ func (g PrometheusEndpointGenerator) Generate(ctx xds_context.Context, proxy *co
 					envoy_listeners.StaticEndpoints(prometheusListenerName,
 						[]*envoy_common.StaticEndpointPath{
 							{
-								ClusterName: clusterName,
+								ClusterName: metricsHijackerClusterName,
 								Path:        prometheusEndpoint.Path,
 								RewritePath: statsPath,
 							},
@@ -139,7 +122,7 @@ func (g PrometheusEndpointGenerator) Generate(ctx xds_context.Context, proxy *co
 			Configure(envoy_listeners.FilterChain(envoy_listeners.NewFilterChainBuilder(proxy.APIVersion).
 				Configure(envoy_listeners.StaticEndpoints(prometheusListenerName, []*envoy_common.StaticEndpointPath{
 					{
-						ClusterName: clusterName,
+						ClusterName: metricsHijackerClusterName,
 						Path:        prometheusEndpoint.Path,
 						RewritePath: statsPath,
 					},


Test patch:
diff --git a/pkg/xds/generator/testdata/prometheus-endpoint/default-without-hijacker.envoy-config.golden.yaml b/pkg/xds/generator/testdata/prometheus-endpoint/default-without-hijacker.envoy-config.golden.yaml
index e9832c44b..2d215fab6 100644
--- a/pkg/xds/generator/testdata/prometheus-endpoint/default-without-hijacker.envoy-config.golden.yaml
+++ b/pkg/xds/generator/testdata/prometheus-endpoint/default-without-hijacker.envoy-config.golden.yaml
@@ -1,4 +1,19 @@
 resources:
+- name: kuma:metrics:hijacker
+  resource:
+    '@type': type.googleapis.com/envoy.config.cluster.v3.Cluster
+    altStatName: kuma_metrics_hijacker
+    connectTimeout: 10s
+    loadAssignment:
+      clusterName: kuma:metrics:hijacker
+      endpoints:
+      - lbEndpoints:
+        - endpoint:
+            address:
+              pipe:
+                path: /tmp/kuma-mh-backend-01-demo.sock
+    name: kuma:metrics:hijacker
+    type: STATIC
 - name: kuma:metrics:prometheus
   resource:
     '@type': type.googleapis.com/envoy.config.listener.v3.Listener
@@ -23,8 +38,8 @@ resources:
               - match:
                   prefix: /non-standard-path
                 route:
-                  cluster: kuma:envoy:admin
-                  prefixRewrite: /stats/prometheus
+                  cluster: kuma:metrics:hijacker
+                  prefixRewrite: /
           statPrefix: kuma_metrics_prometheus
     name: kuma:metrics:prometheus
     trafficDirection: INBOUND