Repository navigation
Description
What is the issue?
I discovered this issue trying to expose Prometheus' /metrics endpoint through a Tailscale Ingress.
Per Kubernetes' Ingress documentation Examples, a path of e.g. /metrics with pathType: Exact should match /metrics.
In my experience, this is rewritten (by Tailscale Operator|Ingress) to /metrics/ which does not work.
In this case /metrics corresponds to Prometheus metrics endpoint and the Golang client library (link) expects the unadorned path.
Steps to reproduce
E.g.:
package main
import (
"flag"
"fmt"
"log/slog"
"net/http"
)
var (
endpoint = flag.String("endpoint", ":3333", "Endpoint of HTTP server")
)
func root(w http.ResponseWriter, r *http.Request) {
handler := "root"
slog.Info("Response",
"handler", handler,
"path", r.URL.Path,
)
fmt.Fprintf(w, "[%s] %s", handler, r.URL.Path)
}
func main() {
flag.Parse()
http.HandleFunc("/", root)
slog.Info(
"Starting HTTP server",
"endpoint", *endpoint,
)
slog.Info(
"Unable to start HTTP server",
"error", http.ListenAndServe(*endpoint, nil),
)
}And:
PORT="3333"
ENDPOINT="http://localhost:${PORT}"
curl \
--silent \
${ENDPOINT}/metrics[root] /metricsAs expected, the server logs the request path as /metrics (without the additional of any trailing slash)
Deploying to Kubernetes, creating a Service (NodePort) and running the above bash script against the NodePort:
ENDPOINT="http://localhost:${NODE_PORT}"[root] /metricsAs expected, the server logs the request path as /metrics (without the additional of any trailing slash)
Then adding an Ingress with a /metrics (Exact) Ingress:
Ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: server
spec:
ingressClassName: tailscale
rules:
- http:
paths:
- backend:
service:
name: server
port:
number: 7777
path: /metrics
pathType: Exact
tls:
- hosts:
- somethingAnd running the bash script against the endpoint https://something.${TSNET}/metrics
[root] /metrics/Not what is expected.
Again, per Kubernetes' Ingress documentation Examples, a path of e.g. /metrics with pathType: Exact should match /metrics.
In this case, if the handler is being served by Prometheus, this results in an error because the handler is only accessible as /metrics and not as /metrics/:
Leaving the Kubernetes resources unchanged with the exception of updating the Deployment's image to reflect the following change to the code:
package main
import (
"flag"
"log/slog"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
endpoint = flag.String("endpoint", ":3333", "Endpoint of HTTP server")
)
func main() {
flag.Parse()
// Prometheus metrics
http.Handle("/metrics", promhttp.Handler())
slog.Info(
"Starting HTTP server",
"endpoint", *endpoint,
)
slog.Info(
"Unable to start HTTP server",
"error", http.ListenAndServe(*endpoint, nil),
)
}And running the bash script against the endpoint https://something.${TSNET}/metrics yields a 404 (not found) because (as we know from the above), the server is receiving a request on the path /metrics/ (rather than /metrics).
Are there any recent changes that introduced the issue?
No.
OS
Linux
OS version
Ubuntu 22.04.3 LTS
Tailscale version
1.56.1
Other software
Go 1.21.5
docker.io/tailscale/k8s-operator:unstable (sha256:e1874ff6f060ecde391f7660203804ee046ee0ed81ebbe4ae08281a63651aa48)
Bug report
No response
Activity
irbekrm commentedon Jan 12, 2024
Thank you for the detailed issue description @DazWilkin .
I can reproduce this and I see that it's happening in our reverse proxy where we determine the backend path that the proxy should forward the request to (we rewrite the request URL here, at this point the proxy target URL path will be
/metricsand the current proxy request URL path will be empty as the path stripping logic here removes it if it matches the configured proxy backend path- so we end up with a/metrics/here).We should certainly fix the issue where a request for a path like
/metricsgets overwritten to/metrics/. I think for that request we should send a request for/metricsto the backend either if the path isExactor if it'sPrefix.At the moment we actually do not implement custom logic for
Ingresspath types (our ingress controller uses shared reverse proxy logic that is not KubernetesIngressspecific). Looking atIngressspec I see that the it seems that the expected behaviour forExactpath types is that for anIngressbackend configured with a path like/fooa request for a path like/foo/bargets rejected at the ingress controller (as opposed to just forwarding it to the backend). We would need to add some additional logic to support this.ipn/ipnlocal: fix proxy path that matches mount point
cmd/k8s-operator: warn if unsupported Ingress Exact path type is used.
ipn/ipnlocal: fix proxy path that matches mount point
cmd/k8s-operator: warn if unsupported Ingress Exact path type is used. (
cmd/k8s-operator: warn if unsupported Ingress Exact path type is used. (
ipn/ipnlocal: fix proxy path that matches mount point (#10864)
irbekrm commentedon Jan 24, 2024
Hi @DazWilkin thank you for alerting us on this issue.
The issue with the
/suffix being added to a path got fixed in #10864 and the1.59.12helm chart version from tailscale-dev repo (or theustable-v1.59.12operator image tag) has the fix- do let us know if you see further issues.As a side note- we also added a warning that we don't currently implement
Exactpath types according to kube spec, see reasoning in #10865 . The fix for the reported issue applies toPrefixpath types too though- you should be able to change yourIngressrule path type toPrefix. If you have strong reasons to need theExactpath type, we are interested to hear those.DazWilkin commentedon Jan 24, 2024
Outstanding! Thank you. I'll try it before the end of the week and report back.
DazWilkin commentedon Jan 25, 2024
Thank you for the fix. This works as expected.
irbekrm commentedon Jan 25, 2024
Thanks for confirming!
irbekrm commentedon Feb 9, 2024
Closing as the original issue was solved.
If someone needs strict implementation of Exact path types, they're welcome to open a new issue.
N (#4)
cmd/k8s-operator: warn if unsupported Ingress Exact path type is used. (
ipn/ipnlocal: fix proxy path that matches mount point (tailscale#10864)