Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform): validate storage addr failed #2103

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
wl-chen marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
return false, "", nil
}

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