mirror of
https://github.com/rancher/rke.git
synced 2025-09-05 17:00:20 +00:00
Set timeout on portchecker
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
package cluster
|
package cluster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
|
"github.com/docker/docker/pkg/stdcopy"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
"github.com/rancher/rke/docker"
|
"github.com/rancher/rke/docker"
|
||||||
"github.com/rancher/rke/hosts"
|
"github.com/rancher/rke/hosts"
|
||||||
@@ -402,13 +402,16 @@ func (c *Cluster) runServicePortChecks(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkPlaneTCPPortsFromHost(ctx context.Context, host *hosts.Host, portList []string, planeHosts []*hosts.Host, image string, prsMap map[string]v3.PrivateRegistry) error {
|
func checkPlaneTCPPortsFromHost(ctx context.Context, host *hosts.Host, portList []string, planeHosts []*hosts.Host, image string, prsMap map[string]v3.PrivateRegistry) error {
|
||||||
hosts := []string{}
|
var hosts []string
|
||||||
|
var portCheckLogs []string
|
||||||
|
var containerStdout bytes.Buffer
|
||||||
|
var containerStderr bytes.Buffer
|
||||||
|
|
||||||
for _, host := range planeHosts {
|
for _, host := range planeHosts {
|
||||||
hosts = append(hosts, host.InternalAddress)
|
hosts = append(hosts, host.InternalAddress)
|
||||||
}
|
}
|
||||||
imageCfg := &container.Config{
|
imageCfg := &container.Config{
|
||||||
Image: image,
|
Image: image,
|
||||||
Tty: true,
|
|
||||||
Env: []string{
|
Env: []string{
|
||||||
fmt.Sprintf("HOSTS=%s", strings.Join(hosts, " ")),
|
fmt.Sprintf("HOSTS=%s", strings.Join(hosts, " ")),
|
||||||
fmt.Sprintf("PORTS=%s", strings.Join(portList, " ")),
|
fmt.Sprintf("PORTS=%s", strings.Join(portList, " ")),
|
||||||
@@ -416,11 +419,14 @@ func checkPlaneTCPPortsFromHost(ctx context.Context, host *hosts.Host, portList
|
|||||||
Cmd: []string{
|
Cmd: []string{
|
||||||
"sh",
|
"sh",
|
||||||
"-c",
|
"-c",
|
||||||
"for host in $HOSTS; do for port in $PORTS ; do nc -z $host $port > /dev/null || echo $host $port ; done; done",
|
"for host in $HOSTS; do for port in $PORTS ; do echo \"Checking host ${host} on port ${port}\" >&1 & nc -w 5 -z $host $port > /dev/null || echo \"${host}:${port}\" >&2 & done; wait; done",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
hostCfg := &container.HostConfig{
|
hostCfg := &container.HostConfig{
|
||||||
NetworkMode: "host",
|
NetworkMode: "host",
|
||||||
|
LogConfig: container.LogConfig{
|
||||||
|
Type: "json-file",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
if err := docker.DoRemoveContainer(ctx, host.DClient, PortCheckContainer, host.Address); err != nil {
|
if err := docker.DoRemoveContainer(ctx, host.DClient, PortCheckContainer, host.Address); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -428,42 +434,28 @@ func checkPlaneTCPPortsFromHost(ctx context.Context, host *hosts.Host, portList
|
|||||||
if err := docker.DoRunContainer(ctx, host.DClient, imageCfg, hostCfg, PortCheckContainer, host.Address, "network", prsMap); err != nil {
|
if err := docker.DoRunContainer(ctx, host.DClient, imageCfg, hostCfg, PortCheckContainer, host.Address, "network", prsMap); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := docker.WaitForContainer(ctx, host.DClient, host.Address, PortCheckContainer); err != nil {
|
|
||||||
return err
|
clogs, err := docker.ReadContainerLogs(ctx, host.DClient, PortCheckContainer)
|
||||||
}
|
|
||||||
logs, err := docker.ReadContainerLogs(ctx, host.DClient, PortCheckContainer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer logs.Close()
|
defer clogs.Close()
|
||||||
|
|
||||||
|
stdcopy.StdCopy(&containerStdout, &containerStderr, clogs)
|
||||||
|
containerLog := containerStderr.String()
|
||||||
|
logrus.Debugf("[network] containerLog [%s] on host: %s", containerLog, host.Address)
|
||||||
|
|
||||||
if err := docker.RemoveContainer(ctx, host.DClient, host.Address, PortCheckContainer); err != nil {
|
if err := docker.RemoveContainer(ctx, host.DClient, host.Address, PortCheckContainer); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
portCheckLogs, err := getPortCheckLogs(logs)
|
logrus.Debugf("[network] Length of portCheckLogs is [%d] on host: %s", len(portCheckLogs), host.Address)
|
||||||
if err != nil {
|
if len(containerLog) > 0 {
|
||||||
return err
|
portCheckLogs := strings.Join(strings.Split(strings.TrimSpace(containerLog), "\n"), ", ")
|
||||||
}
|
return fmt.Errorf("[network] Port check for ports: [%s] failed on host: [%s]", portCheckLogs, host.Address)
|
||||||
if len(portCheckLogs) > 0 {
|
|
||||||
|
|
||||||
return fmt.Errorf("[network] Port check for ports: [%s] failed on host: [%s]", strings.Join(portCheckLogs, ", "), host.Address)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPortCheckLogs(reader io.ReadCloser) ([]string, error) {
|
|
||||||
logLines := bufio.NewScanner(reader)
|
|
||||||
hostPortLines := []string{}
|
|
||||||
for logLines.Scan() {
|
|
||||||
logLine := strings.Split(logLines.Text(), " ")
|
|
||||||
hostPortLines = append(hostPortLines, fmt.Sprintf("%s:%s", logLine[0], logLine[1]))
|
|
||||||
}
|
|
||||||
if err := logLines.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return hostPortLines, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPortBindings(hostAddress string, portList []string) []nat.PortBinding {
|
func getPortBindings(hostAddress string, portList []string) []nat.PortBinding {
|
||||||
portBindingList := []nat.PortBinding{}
|
portBindingList := []nat.PortBinding{}
|
||||||
for _, portNumber := range portList {
|
for _, portNumber := range portList {
|
||||||
|
@@ -333,8 +333,7 @@ func ReadFileFromContainer(ctx context.Context, dClient *client.Client, hostname
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ReadContainerLogs(ctx context.Context, dClient *client.Client, containerName string) (io.ReadCloser, error) {
|
func ReadContainerLogs(ctx context.Context, dClient *client.Client, containerName string) (io.ReadCloser, error) {
|
||||||
return dClient.ContainerLogs(ctx, containerName, types.ContainerLogsOptions{ShowStdout: true})
|
return dClient.ContainerLogs(ctx, containerName, types.ContainerLogsOptions{Follow: true, ShowStdout: true, ShowStderr: true, Timestamps: false})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func tryRegistryAuth(pr v3.PrivateRegistry) types.RequestPrivilegeFunc {
|
func tryRegistryAuth(pr v3.PrivateRegistry) types.RequestPrivilegeFunc {
|
||||||
|
Reference in New Issue
Block a user