Skip to content

Commit

Permalink
refactor: optimize error handling for ssh.ReadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
QianChenglong committed May 15, 2020
1 parent 60d3f14 commit 90aad74
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
12 changes: 6 additions & 6 deletions pkg/platform/provider/baremetal/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,37 +734,37 @@ func (p *Provider) EnsureStoreCredential(ctx context.Context, c *v1.Cluster) err

data, err := machineSSH.ReadFile(constants.CACertName)
if err != nil {
return errors.Wrapf(err, "read %s error", constants.CACertName)
return err
}
c.ClusterCredential.CACert = data

data, err = machineSSH.ReadFile(constants.CAKeyName)
if err != nil {
return errors.Wrapf(err, "read %s error", constants.CAKeyName)
return err
}
c.ClusterCredential.CAKey = data

data, err = machineSSH.ReadFile(constants.EtcdCACertName)
if err != nil {
return errors.Wrapf(err, "read %s error", constants.EtcdCACertName)
return err
}
c.ClusterCredential.ETCDCACert = data

data, err = machineSSH.ReadFile(constants.EtcdCAKeyName)
if err != nil {
return errors.Wrapf(err, "read %s error", constants.EtcdCAKeyName)
return err
}
c.ClusterCredential.ETCDCAKey = data

data, err = machineSSH.ReadFile(constants.APIServerEtcdClientCertName)
if err != nil {
return errors.Wrapf(err, "read %s error", constants.APIServerEtcdClientCertName)
return err
}
c.ClusterCredential.ETCDAPIClientCert = data

data, err = machineSSH.ReadFile(constants.APIServerEtcdClientKeyName)
if err != nil {
return errors.Wrapf(err, "read %s error", constants.APIServerEtcdClientKeyName)
return err
}
c.ClusterCredential.ETCDAPIClientKey = data

Expand Down
2 changes: 1 addition & 1 deletion pkg/platform/provider/baremetal/preflight/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (fcc FileContentCheck) Check() (warnings, errorList []error) {
log.Infof("validating the contents of file %s", fcc.Path)
data, err := fcc.ReadFile(fcc.Path)
if err != nil {
return nil, []error{errors.Errorf("%s could not be read", fcc.Path)}
return nil, []error{err}
}

if !bytes.Equal(data[:len(fcc.Content)], fcc.Content) {
Expand Down
17 changes: 10 additions & 7 deletions pkg/util/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ import (
"path"
"time"

"tkestack.io/tke/pkg/util/hash"

"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"gopkg.in/go-playground/validator.v9"
"k8s.io/apimachinery/pkg/util/wait"
"tkestack.io/tke/pkg/util/hash"
"tkestack.io/tke/pkg/util/log"
)

Expand Down Expand Up @@ -326,29 +325,33 @@ func (s *SSH) ReadFile(filename string) ([]byte, error) {
if err != nil {
err = wait.Poll(5*time.Second, time.Duration(s.Retry)*5*time.Second, func() (bool, error) {
if client, err = s.dialer.Dial("tcp", s.addr, config); err != nil {
return false, err
return false, fmt.Errorf("read file %s error: %w", filename, err)
}
return true, nil
})
}
if err != nil {
return nil, err
return nil, fmt.Errorf("read file %s error: %w", filename, err)
}
defer client.Close()

sftpClient, err := sftp.NewClient(client)
if err != nil {
return nil, err
return nil, fmt.Errorf("read file %s error: %w", filename, err)
}
defer sftpClient.Close()

f, err := sftpClient.Open(filename)
if err != nil {
return nil, err
return nil, fmt.Errorf("read file %s error: %w", filename, err)
}
data := new(bytes.Buffer)
_, err = f.WriteTo(data)
return data.Bytes(), err
if err != nil {
return nil, fmt.Errorf("read file %s error: %w", filename, err)
}

return data.Bytes(), nil
}

func (s *SSH) LookPath(file string) (string, error) {
Expand Down

0 comments on commit 90aad74

Please sign in to comment.