From 63f1b077dcb31b8904f585f983b4fe6fee4fdaa6 Mon Sep 17 00:00:00 2001 From: Lou Yihua Date: Sat, 11 Mar 2017 02:34:31 +0800 Subject: [PATCH] Add Host field to TCPSocketAction Currently, TCPSocketAction always uses Pod's IP in connection. But when a pod uses the host network, sometimes firewall rules may prevent kubelet from connecting through the Pod's IP. This PR introduces the 'Host' field for TCPSocketAction, and if it is set to non-empty string, the probe will be performed on the configured host rather than the Pod's IP. This gives users an opportunity to explicitly specify 'localhost' as the target for the above situations. --- pkg/api/types.go | 3 +++ pkg/api/v1/types.go | 3 +++ pkg/kubelet/prober/prober.go | 8 ++++++-- pkg/printers/internalversion/describe.go | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/api/types.go b/pkg/api/types.go index 40a2101987a..237562ef46c 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -1391,6 +1391,9 @@ type TCPSocketAction struct { // Required: Port to connect to. // +optional Port intstr.IntOrString + // Optional: Host name to connect to, defaults to the pod IP. + // +optional + Host string } // ExecAction describes a "run in container" action. diff --git a/pkg/api/v1/types.go b/pkg/api/v1/types.go index 86e508f57a0..04da19b7c2f 100644 --- a/pkg/api/v1/types.go +++ b/pkg/api/v1/types.go @@ -1492,6 +1492,9 @@ type TCPSocketAction struct { // Number must be in the range 1 to 65535. // Name must be an IANA_SVC_NAME. Port intstr.IntOrString `json:"port" protobuf:"bytes,1,opt,name=port"` + // Optional: Host name to connect to, defaults to the pod IP. + // +optional + Host string `json:"host,omitempty" protobuf:"bytes,2,opt,name=host"` } // ExecAction describes a "run in container" action. diff --git a/pkg/kubelet/prober/prober.go b/pkg/kubelet/prober/prober.go index 47f85b9824c..acd70bb1ae5 100644 --- a/pkg/kubelet/prober/prober.go +++ b/pkg/kubelet/prober/prober.go @@ -168,8 +168,12 @@ func (pb *prober) runProbe(p *v1.Probe, pod *v1.Pod, status v1.PodStatus, contai if err != nil { return probe.Unknown, "", err } - glog.V(4).Infof("TCP-Probe PodIP: %v, Port: %v, Timeout: %v", status.PodIP, port, timeout) - return pb.tcp.Probe(status.PodIP, port, timeout) + host := p.TCPSocket.Host + if host == "" { + host = status.PodIP + } + glog.V(4).Infof("TCP-Probe Host: %v, Port: %v, Timeout: %v", host, port, timeout) + return pb.tcp.Probe(host, port, timeout) } glog.Warningf("Failed to find probe builder for container: %v", container) return probe.Unknown, "", fmt.Errorf("Missing probe handler for %s:%s", format.Pod(pod), container.Name) diff --git a/pkg/printers/internalversion/describe.go b/pkg/printers/internalversion/describe.go index 19f50a65c3d..4555b6f3184 100644 --- a/pkg/printers/internalversion/describe.go +++ b/pkg/printers/internalversion/describe.go @@ -1144,7 +1144,7 @@ func DescribeProbe(probe *api.Probe) string { url.Path = probe.HTTPGet.Path return fmt.Sprintf("http-get %s %s", url.String(), attrs) case probe.TCPSocket != nil: - return fmt.Sprintf("tcp-socket :%s %s", probe.TCPSocket.Port.String(), attrs) + return fmt.Sprintf("tcp-socket %s:%s %s", probe.TCPSocket.Host, probe.TCPSocket.Port.String(), attrs) } return fmt.Sprintf("unknown %s", attrs) }