Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix our probes with mTLS enabled. #4017

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion config/activator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,23 @@ spec:
- "-stderrthreshold=FATAL"
readinessProbe:
httpGet:
# The path does not matter, we look for kubelet probe headers.
# The path does not matter, we look for the kubelet user-agent
# (or our header below)
path: /healthz
port: 8012
httpHeaders:
# Istio with mTLS strips the Kubelet user-agent, so pass a header too.
- name: k-kubelet-probe
value: "activator"
livenessProbe:
httpGet:
# The path does not matter, we look for kubelet probe headers.
path: /healthz
port: 8012
httpHeaders:
# Istio with mTLS strips the Kubelet user-agent, so pass a header too.
- name: k-kubelet-probe
value: "activator"
resources:
# Request 2x what we saw running e2e
requests:
Expand Down
7 changes: 6 additions & 1 deletion pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ const (
// User-Agent = "kube-probe/{major-version}.{minor-version}".
kubeProbeUAPrefix = "kube-probe/"

// Istio with mTLS rewrites probes, but their probes pass a different
// user-agent. So we augment the probes with this header.
KubeletProbeHeaderName = "K-Kubelet-Probe"

// DefaultConnTimeout specifies a short default connection timeout
// to avoid hitting the issue fixed in
// https://github.com/kubernetes/kubernetes/pull/72534 but only
Expand Down Expand Up @@ -260,7 +264,8 @@ func checkTemplate(t *template.Template) error {

// IsKubeletProbe returns true if the request is a kubernetes probe.
func IsKubeletProbe(r *http.Request) bool {
return strings.HasPrefix(r.Header.Get("User-Agent"), kubeProbeUAPrefix)
return strings.HasPrefix(r.Header.Get("User-Agent"), kubeProbeUAPrefix) ||
r.Header.Get(KubeletProbeHeaderName) != ""
}

// RewriteHostIn removes the `Host` header from the inbound (server) request
Expand Down
8 changes: 8 additions & 0 deletions pkg/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,14 @@ func TestIsKubeletProbe(t *testing.T) {
if !IsKubeletProbe(req) {
t.Error("kubelet probe but not counted as such")
}
req.Header.Del("User-Agent")
if IsKubeletProbe(req) {
t.Error("Not a kubelet probe but counted as such")
}
req.Header.Set(KubeletProbeHeaderName, "no matter")
if !IsKubeletProbe(req) {
t.Error("kubelet probe but not counted as such")
}
}

func TestRewriteHost(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/reconciler/revision/resources/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ func rewriteUserProbe(p *corev1.Probe, userPort int) {
// so that we know the queue proxy is ready/live as well.
// It doesn't matter to which queue serving port we are forwarding the probe.
p.HTTPGet.Port = intstr.FromInt(networking.BackendHTTPPort)
// With mTLS enabled, Istio rewrites probes, but doesn't spoof the kubelet
// user agent, so we need to inject an extra header to be able to distinguish
// between probes and real requests.
p.HTTPGet.HTTPHeaders = append(p.HTTPGet.HTTPHeaders, corev1.HTTPHeader{
Name: network.KubeletProbeHeaderName,
Value: "queue",
})
case p.TCPSocket != nil:
p.TCPSocket.Port = intstr.FromInt(userPort)
}
Expand Down
19 changes: 18 additions & 1 deletion pkg/reconciler/revision/resources/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,19 @@ func withHTTPReadinessProbe(port int) containerOption {
})
}

func withHTTPQPReadinessProbe(c *corev1.Container) {
withReadinessProbe(corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Port: intstr.FromInt(networking.BackendHTTPPort),
Path: "/",
HTTPHeaders: []corev1.HTTPHeader{{
Name: network.KubeletProbeHeaderName,
Value: "queue",
}},
},
})(c)
}

func withExecReadinessProbe(command []string) containerOption {
return withReadinessProbe(corev1.Handler{
Exec: &corev1.ExecAction{
Expand Down Expand Up @@ -506,7 +519,7 @@ func TestMakePodSpec(t *testing.T) {
cc: &deployment.Config{},
want: podSpec([]corev1.Container{
userContainer(
withHTTPReadinessProbe(networking.BackendHTTPPort),
withHTTPQPReadinessProbe,
),
queueContainer(
withEnvVar("CONTAINER_CONCURRENCY", "0"),
Expand Down Expand Up @@ -556,6 +569,10 @@ func TestMakePodSpec(t *testing.T) {
HTTPGet: &corev1.HTTPGetAction{
Path: "/",
Port: intstr.FromInt(networking.BackendHTTPPort),
HTTPHeaders: []corev1.HTTPHeader{{
Name: network.KubeletProbeHeaderName,
Value: "queue",
}},
},
}),
),
Expand Down