Merge pull request #106621 from shuheiktgw/check_private_ssh_key_existence

Check the private SSH key existence in the node e2e tests
This commit is contained in:
Kubernetes Prow Robot 2022-01-05 18:06:35 -08:00 committed by GitHub
commit 2b0ddeb23b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -103,9 +103,12 @@ func SSHNoSudo(host string, cmd ...string) (string, error) {
// runSSHCommand executes the ssh or scp command, adding the flag provided --ssh-options
func runSSHCommand(cmd string, args ...string) (string, error) {
if *sshKey != "" {
args = append([]string{"-i", *sshKey}, args...)
} else if key, found := sshDefaultKeyMap[*sshEnv]; found {
if key, err := getPrivateSSHKey(); len(key) != 0 {
if err != nil {
klog.Errorf("private SSH key (%s) not found. Check if the SSH key is configured properly:, err: %v", key, err)
return "", fmt.Errorf("private SSH key (%s) does not exist", key)
}
args = append([]string{"-i", key}, args...)
}
if env, found := sshOptionsMap[*sshEnv]; found {
@ -122,3 +125,24 @@ func runSSHCommand(cmd string, args ...string) (string, error) {
}
return string(output), nil
}
// getPrivateSSHKey returns the path to ssh private key
func getPrivateSSHKey() (string, error) {
if *sshKey != "" {
if _, err := os.Stat(*sshKey); err != nil {
return *sshKey, err
}
return *sshKey, nil
}
if key, found := sshDefaultKeyMap[*sshEnv]; found {
if _, err := os.Stat(key); err != nil {
return key, err
}
return key, nil
}
return "", nil
}