Skip to content

Kubernetes Operator: imprecise implementation of Exact path types #10730

Closed

Description

@DazWilkin

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] /metrics

As 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] /metrics

As 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:
    - something

And 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

  1. added
    L1 Very few
    P1 Nuisance
    needs-triage-eng
    and removed
    bug
    on Jan 3, 2024
  2. self-assigned this
    on Jan 12, 2024
  3. irbekrm commented on Jan 12, 2024

    @irbekrm
    Contributor

    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 /metrics and 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 /metrics gets overwritten to /metrics/. I think for that request we should send a request for /metrics to the backend either if the path is Exact or if it's Prefix.

    At the moment we actually do not implement custom logic for Ingress path types (our ingress controller uses shared reverse proxy logic that is not Kubernetes Ingress specific). Looking at Ingress spec I see that the it seems that the expected behaviour for Exact path types is that for an Ingress backend configured with a path like /foo a request for a path like /foo/bar gets 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.

  4. irbekrm commented on Jan 24, 2024

    @irbekrm
    Contributor

    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 the 1.59.12 helm chart version from tailscale-dev repo (or the ustable-v1.59.12 operator 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 Exact path types according to kube spec, see reasoning in #10865 . The fix for the reported issue applies to Prefix path types too though- you should be able to change your Ingress rule path type to Prefix. If you have strong reasons to need the Exact path type, we are interested to hear those.

  5. DazWilkin commented on Jan 24, 2024

    @DazWilkin
    Author

    Outstanding! Thank you. I'll try it before the end of the week and report back.

  6. DazWilkin commented on Jan 25, 2024

    @DazWilkin
    Author

    Thank you for the fix. This works as expected.

  7. irbekrm commented on Jan 25, 2024

    @irbekrm
    Contributor

    Thanks for confirming!

  8. irbekrm commented on Feb 9, 2024

    @irbekrm
    Contributor

    Closing as the original issue was solved.
    If someone needs strict implementation of Exact path types, they're welcome to open a new issue.

  9. added a commit that references this issue on Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

L1 Very fewP1 NuisanceT3 Performance/Debuggingkubernetesneeds-triage-eng

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions