Skip to content

Commit

Permalink
fix(platform): validate storage addr failed (#2103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Ryu authored Oct 10, 2022
1 parent 1fede76 commit f5c03d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
13 changes: 9 additions & 4 deletions pkg/platform/provider/baremetal/validation/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"context"
"encoding/base64"
"fmt"
appsv1alpha1 "github.com/clusternet/apis/apps/v1alpha1"
"math"
"net"
"strconv"
"strings"
"time"

appsv1alpha1 "github.com/clusternet/apis/apps/v1alpha1"
"tkestack.io/tke/pkg/mesh/util/json"

k8serror "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -354,9 +355,11 @@ func ValidateOSVersion(fldPath *field.Path, sshs []*ssh.SSH) field.ErrorList {
func ValidateReservePorts(fldPath *field.Path, sshs []*ssh.SSH) field.ErrorList {
allErrs := field.ErrorList{}
for i, one := range sshs {
err := ssh.ReservePorts(one, "127.0.0.1", reservePorts)
isInused, message, err := ssh.ReservePorts(one, "127.0.0.1", reservePorts)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), one.Host, err.Error()))
} else if isInused {
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), one.Host, message))
}
}
return allErrs
Expand Down Expand Up @@ -457,9 +460,11 @@ func ValidateCephFS(cls *platform.Cluster, fld *field.Path) field.ErrorList {
allErrs = append(allErrs, field.Invalid(fld, err, "ssh machine failed"))
}

err = ssh.ReservePorts(machine, ip, []int{p})
isInused, _, err := ssh.ReservePorts(machine, ip, []int{p})
if err != nil {
allErrs = append(allErrs, field.Invalid(fld, err, "invalid port"))
allErrs = append(allErrs, field.Invalid(fld, fmt.Sprintf("ceph IP: %s, port: %v", ip, p), fmt.Sprintf("check ceph connection failed: %v", err)))
} else if !isInused {
allErrs = append(allErrs, field.Invalid(fld, fmt.Sprintf("ceph IP: %s, port: %v", ip, p), "cannot connect given ceph addr"))
}
}
return allErrs
Expand Down
14 changes: 7 additions & 7 deletions pkg/util/ssh/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,26 @@ func OSVersion(s Interface) (os string, err error) {
return id + version, nil
}

func ReservePorts(s Interface, ip string, ports []int) error {
var cmd, errMessage string
func ReservePorts(s Interface, ip string, ports []int) (isInused bool, message string, err error) {
var cmd string
for _, port := range ports {
cmd += fmt.Sprintf(`bash -c "</dev/tcp/%s/%d" &>/dev/null; echo $?; `, ip, port)
}
out, _, _, _ := s.Exec(cmd)
out = strings.TrimSuffix(out, "\n")
results := strings.Split(out, "\n")
if len(results) != len(ports) {
return fmt.Errorf("check results length does not match need check ports length, get results output is: %s", out)
return false, "", fmt.Errorf("check results length does not match need check ports length, get results output is: %s", out)
}
for i, result := range results {
if result != "1" {
errMessage += fmt.Sprintf("%d ", ports[i])
message += fmt.Sprintf("%d ", ports[i])
}
}
if len(errMessage) != 0 {
return fmt.Errorf("ports %sis in used", errMessage)
if len(message) != 0 {
return true, fmt.Sprintf("ports %sis in used", message), nil
}
return nil
return false, "", nil
}

func FirewallEnabled(s Interface) (enabled bool, err error) {
Expand Down

0 comments on commit f5c03d3

Please sign in to comment.