mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-29 21:12:45 -08:00
cmd/k8s-operator: use h2c for Ingress backends that declare it
An Ingress backend whose Service port has the appProtocol "kubernetes.io/h2c" now gets an h2c serve proxy handler. This applies to a single-proxy Ingress and to a ProxyGroup Ingress. The operator ignores the value and records a warning event when it proxies the port over TLS. A proxy Pod on an older image ignores the new field. Fixes #18483 Signed-off-by: Brendan Creane <[email protected]>
This commit is contained in:
3 files changed
+229
-9
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
@@ -288,6 +289,121 @@ func TestL7Ingress(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestL7IngressBackendH2C checks that the Ingress proxy speaks HTTP/2 over
|
||||
// cleartext to a backend whose Service port has the appProtocol
|
||||
// "kubernetes.io/h2c", and HTTP/1.1 to a backend port without it.
|
||||
func TestL7IngressBackendH2C(t *testing.T) {
|
||||
if tnClient == nil {
|
||||
t.Skip("TestL7IngressBackendH2C requires a working tailnet client")
|
||||
}
|
||||
t.Parallel()
|
||||
|
||||
// This nginx accepts HTTP/1.1 and h2c on one port. It answers every
|
||||
// request with the protocol version of that request.
|
||||
conf := &corev1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: generateName("nginx-proto"),
|
||||
Namespace: ns,
|
||||
},
|
||||
Data: map[string]string{
|
||||
"default.conf": `server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
http2 on;
|
||||
location / {
|
||||
default_type text/plain;
|
||||
return 200 $server_protocol;
|
||||
}
|
||||
}
|
||||
`,
|
||||
},
|
||||
}
|
||||
createAndCleanup(t, kubeClient, conf)
|
||||
nginx := nginxDeployment(ns)
|
||||
nginx.Spec.Template.Spec.Volumes = []corev1.Volume{{
|
||||
Name: "conf",
|
||||
VolumeSource: corev1.VolumeSource{
|
||||
ConfigMap: &corev1.ConfigMapVolumeSource{
|
||||
LocalObjectReference: corev1.LocalObjectReference{Name: conf.Name},
|
||||
},
|
||||
},
|
||||
}}
|
||||
nginx.Spec.Template.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{{
|
||||
Name: "conf",
|
||||
MountPath: "/etc/nginx/conf.d",
|
||||
}}
|
||||
createAndCleanup(t, kubeClient, nginx)
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
appProtocol *string
|
||||
want string
|
||||
}{
|
||||
{"unset", nil, "HTTP/1.1"},
|
||||
{"h2c", new("kubernetes.io/h2c"), "HTTP/2.0"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
backend := &corev1.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: generateName("test-nginx"),
|
||||
Namespace: ns,
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Selector: map[string]string{
|
||||
"app.kubernetes.io/name": nginx.Name,
|
||||
},
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Name: "http",
|
||||
Port: 80,
|
||||
AppProtocol: tc.appProtocol,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
createAndCleanup(t, kubeClient, backend)
|
||||
|
||||
ingress := l7Ingress(ns, backend.Name, nil)
|
||||
createAndCleanup(t, kubeClient, ingress)
|
||||
hostname, err := waitForIngressHostname(t, ns, ingress.Name)
|
||||
if err != nil {
|
||||
t.Fatalf("error waiting for Ingress hostname: %v", err)
|
||||
}
|
||||
|
||||
httpClient := newHTTPClient(tnClient)
|
||||
url := fmt.Sprintf("https://%s:443", hostname)
|
||||
var got string
|
||||
if err := tstest.WaitFor(time.Minute, func() error {
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, httpm.GET, url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("unexpected status from %s: %d", url, resp.StatusCode)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
got = string(body)
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("error trying to reach %s: %v", url, err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("backend saw %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestL7IngressDNSConfig(t *testing.T) {
|
||||
if tnClient == nil {
|
||||
t.Skip("TestL7IngressDNSConfig requires a working tailnet client")
|
||||
|
||||
@@ -322,6 +322,11 @@ func validateIngressClass(ctx context.Context, cl client.Client, ingressClassNam
|
||||
return nil
|
||||
}
|
||||
|
||||
// appProtocolH2C is the Service port appProtocol value that Kubernetes defines
|
||||
// for HTTP/2 over cleartext. The proxy uses h2c for every request to a backend
|
||||
// port that has it.
|
||||
const appProtocolH2C = "kubernetes.io/h2c"
|
||||
|
||||
func handlersForIngress(ctx context.Context, ing *networkingv1.Ingress, cl client.Client, rec record.EventRecorder, tlsHost string, logger *zap.SugaredLogger) (handlers map[string]*ipn.HTTPHandler, err error) {
|
||||
addIngressBackend := func(b *networkingv1.IngressBackend, path string) {
|
||||
if path == "" {
|
||||
@@ -347,14 +352,22 @@ func handlersForIngress(ctx context.Context, ing *networkingv1.Ingress, cl clien
|
||||
return
|
||||
}
|
||||
var port int32
|
||||
if b.Service.Port.Name != "" {
|
||||
for _, p := range svc.Spec.Ports {
|
||||
if p.Name == b.Service.Port.Name {
|
||||
port = p.Port
|
||||
break
|
||||
}
|
||||
var appProtocol string
|
||||
for _, p := range svc.Spec.Ports {
|
||||
if b.Service.Port.Name != "" && p.Name != b.Service.Port.Name {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if b.Service.Port.Name == "" && p.Port != b.Service.Port.Number {
|
||||
continue
|
||||
}
|
||||
port = p.Port
|
||||
if p.AppProtocol != nil {
|
||||
appProtocol = *p.AppProtocol
|
||||
}
|
||||
break
|
||||
}
|
||||
if port == 0 && b.Service.Port.Name == "" {
|
||||
// The Service does not list the port. Use the number as given.
|
||||
port = b.Service.Port.Number
|
||||
}
|
||||
if port == 0 {
|
||||
@@ -365,9 +378,17 @@ func handlersForIngress(ctx context.Context, ing *networkingv1.Ingress, cl clien
|
||||
if port == 443 || b.Service.Port.Name == "https" {
|
||||
proto = "https+insecure://"
|
||||
}
|
||||
mak.Set(&handlers, path, &ipn.HTTPHandler{
|
||||
h := &ipn.HTTPHandler{
|
||||
Proxy: proto + net.JoinHostPort(svc.Spec.ClusterIP, fmt.Sprint(port)) + path,
|
||||
})
|
||||
}
|
||||
if appProtocol == appProtocolH2C {
|
||||
if proto == "http://" {
|
||||
h.BackendHTTP = ipn.BackendHTTPH2C
|
||||
} else {
|
||||
rec.Eventf(ing, corev1.EventTypeWarning, "InvalidIngressBackend", "backend for path %q has appProtocol %q on a port that is proxied over TLS; ignoring appProtocol", path, appProtocolH2C)
|
||||
}
|
||||
}
|
||||
mak.Set(&handlers, path, h)
|
||||
}
|
||||
addIngressBackend(ing.Spec.DefaultBackend, "/")
|
||||
for _, rule := range ing.Spec.Rules {
|
||||
|
||||
@@ -8,8 +8,10 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"go.uber.org/zap"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
@@ -1031,3 +1033,84 @@ func TestTailscaleIngressIPv6(t *testing.T) {
|
||||
secret.Labels[LabelParentName] = "test-ipv6"
|
||||
expectEqual(t, fc, secret)
|
||||
}
|
||||
|
||||
func TestHandlersForIngressAppProtocol(t *testing.T) {
|
||||
svc := &corev1.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "backend", Namespace: "default"},
|
||||
Spec: corev1.ServiceSpec{
|
||||
ClusterIP: "1.2.3.4",
|
||||
Ports: []corev1.ServicePort{
|
||||
{Name: "plain", Port: 8080},
|
||||
{Name: "connect", Port: 8081, AppProtocol: new("kubernetes.io/h2c")},
|
||||
{Name: "other", Port: 8082, AppProtocol: new("kubernetes.io/ws")},
|
||||
{Name: "https", Port: 8443, AppProtocol: new("kubernetes.io/h2c")},
|
||||
},
|
||||
},
|
||||
}
|
||||
fc := fake.NewFakeClient(svc)
|
||||
zl, err := zap.NewDevelopment()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
path := func(path string, port networkingv1.ServiceBackendPort) networkingv1.HTTPIngressPath {
|
||||
return networkingv1.HTTPIngressPath{
|
||||
Path: path,
|
||||
PathType: new(networkingv1.PathTypePrefix),
|
||||
Backend: networkingv1.IngressBackend{
|
||||
Service: &networkingv1.IngressServiceBackend{Name: "backend", Port: port},
|
||||
},
|
||||
}
|
||||
}
|
||||
ing := &networkingv1.Ingress{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"},
|
||||
Spec: networkingv1.IngressSpec{
|
||||
Rules: []networkingv1.IngressRule{{
|
||||
IngressRuleValue: networkingv1.IngressRuleValue{
|
||||
HTTP: &networkingv1.HTTPIngressRuleValue{
|
||||
Paths: []networkingv1.HTTPIngressPath{
|
||||
path("/plain", networkingv1.ServiceBackendPort{Name: "plain"}),
|
||||
path("/by-name", networkingv1.ServiceBackendPort{Name: "connect"}),
|
||||
path("/by-number", networkingv1.ServiceBackendPort{Number: 8081}),
|
||||
path("/other", networkingv1.ServiceBackendPort{Name: "other"}),
|
||||
path("/tls", networkingv1.ServiceBackendPort{Name: "https"}),
|
||||
path("/unlisted", networkingv1.ServiceBackendPort{Number: 9000}),
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
||||
rec := record.NewFakeRecorder(10)
|
||||
got, err := handlersForIngress(t.Context(), ing, fc, rec, "test-host", zl.Sugar())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]*ipn.HTTPHandler{
|
||||
"/plain": {Proxy: "http://1.2.3.4:8080/plain"},
|
||||
"/by-name": {Proxy: "http://1.2.3.4:8081/by-name", BackendHTTP: ipn.BackendHTTPH2C},
|
||||
"/by-number": {Proxy: "http://1.2.3.4:8081/by-number", BackendHTTP: ipn.BackendHTTPH2C},
|
||||
"/other": {Proxy: "http://1.2.3.4:8082/other"},
|
||||
"/tls": {Proxy: "https+insecure://1.2.3.4:8443/tls"},
|
||||
"/unlisted": {Proxy: "http://1.2.3.4:9000/unlisted"},
|
||||
}
|
||||
if diff := cmp.Diff(want, got); diff != "" {
|
||||
t.Errorf("handlers mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
|
||||
// The TLS port is the only backend that produces an event.
|
||||
select {
|
||||
case ev := <-rec.Events:
|
||||
if !strings.Contains(ev, "InvalidIngressBackend") || !strings.Contains(ev, `"/tls"`) {
|
||||
t.Errorf("unexpected event %q", ev)
|
||||
}
|
||||
default:
|
||||
t.Error("no event for h2c appProtocol on a TLS port")
|
||||
}
|
||||
select {
|
||||
case ev := <-rec.Events:
|
||||
t.Errorf("unexpected extra event %q", ev)
|
||||
default:
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user