diff --git a/pkg/platform/provider/baremetal/validation/cluster.go b/pkg/platform/provider/baremetal/validation/cluster.go index 3a7366180..405b628cb 100644 --- a/pkg/platform/provider/baremetal/validation/cluster.go +++ b/pkg/platform/provider/baremetal/validation/cluster.go @@ -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" @@ -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 @@ -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 diff --git a/pkg/util/ssh/os.go b/pkg/util/ssh/os.go index 5d26662ae..edec780c8 100644 --- a/pkg/util/ssh/os.go +++ b/pkg/util/ssh/os.go @@ -156,8 +156,8 @@ 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/null; echo $?; `, ip, port) } @@ -165,17 +165,17 @@ func ReservePorts(s Interface, ip string, ports []int) error { 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) {