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.
This commit is contained in:
Lou Yihua 2017-03-11 02:34:31 +08:00
parent 5e29e1ee05
commit 63f1b077dc
4 changed files with 13 additions and 3 deletions

View File

@ -1391,6 +1391,9 @@ type TCPSocketAction struct {
// Required: Port to connect to. // Required: Port to connect to.
// +optional // +optional
Port intstr.IntOrString Port intstr.IntOrString
// Optional: Host name to connect to, defaults to the pod IP.
// +optional
Host string
} }
// ExecAction describes a "run in container" action. // ExecAction describes a "run in container" action.

View File

@ -1492,6 +1492,9 @@ type TCPSocketAction struct {
// Number must be in the range 1 to 65535. // Number must be in the range 1 to 65535.
// Name must be an IANA_SVC_NAME. // Name must be an IANA_SVC_NAME.
Port intstr.IntOrString `json:"port" protobuf:"bytes,1,opt,name=port"` 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. // ExecAction describes a "run in container" action.

View File

@ -168,8 +168,12 @@ func (pb *prober) runProbe(p *v1.Probe, pod *v1.Pod, status v1.PodStatus, contai
if err != nil { if err != nil {
return probe.Unknown, "", err return probe.Unknown, "", err
} }
glog.V(4).Infof("TCP-Probe PodIP: %v, Port: %v, Timeout: %v", status.PodIP, port, timeout) host := p.TCPSocket.Host
return pb.tcp.Probe(status.PodIP, port, timeout) 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) 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) return probe.Unknown, "", fmt.Errorf("Missing probe handler for %s:%s", format.Pod(pod), container.Name)

View File

@ -1144,7 +1144,7 @@ func DescribeProbe(probe *api.Probe) string {
url.Path = probe.HTTPGet.Path url.Path = probe.HTTPGet.Path
return fmt.Sprintf("http-get %s %s", url.String(), attrs) return fmt.Sprintf("http-get %s %s", url.String(), attrs)
case probe.TCPSocket != nil: 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) return fmt.Sprintf("unknown %s", attrs)
} }