Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 8dfb446

Browse files
author
Yifan Gu
authored
Merge pull request #516 from yifan-gu/ssh_client
e2e: Add a simple ssh client and node type
2 parents 06b25bd + 60da9fb commit 8dfb446

File tree

557 files changed

+64223
-2536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

557 files changed

+64223
-2536
lines changed

e2e/main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"testing"
88
"time"
99

10+
"golang.org/x/crypto/ssh"
11+
1012
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113
"k8s.io/client-go/kubernetes"
1214
"k8s.io/client-go/pkg/api/v1"

e2e/node.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package e2e
2+
3+
import (
4+
"fmt"
5+
6+
"k8s.io/client-go/pkg/api/v1"
7+
8+
"golang.org/x/crypto/ssh"
9+
)
10+
11+
type Node struct {
12+
apiNode *v1.Node
13+
}
14+
15+
func NewNode(n *v1.Node) *Node {
16+
return &Node{apiNode: n}
17+
}
18+
19+
func (n *Node) ExternalIP() string {
20+
var host string
21+
for _, addr := range n.apiNode.Status.Addresses {
22+
if addr.Type == v1.NodeExternalIP {
23+
host = addr.Address
24+
break
25+
}
26+
}
27+
return host
28+
}
29+
30+
func (n *Node) SSH(cmd string) (stdout, stderr []byte, err error) {
31+
host := n.ExternalIP()
32+
if host == "" {
33+
return nil, nil, fmt.Errorf("cannot find external IP for node %q", n.apiNode.Name)
34+
}
35+
return sshClient.SSH(host, cmd)
36+
}
37+
38+
func (n *Node) Reboot() error {
39+
stdout, stderr, err := n.SSH("sudo reboot")
40+
if _, ok := err.(*ssh.ExitMissingError); ok {
41+
// A terminated session is perfectly normal during reboot.
42+
err = nil
43+
}
44+
45+
if err != nil {
46+
return fmt.Errorf("issuing reboot command failed\nstdout:%s\nstderr:%s", stdout, stderr)
47+
}
48+
return nil
49+
}

e2e/smoke_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestSmoke(t *testing.T) {
1919
}
2020
d, ok := di.(*v1beta1.Deployment)
2121
if !ok {
22-
t.Fatalf("expected manifest to decode into *api.Service, got %T", di)
22+
t.Fatalf("expected manifest to decode into *api.deployment, got %T", di)
2323
}
2424
_, err = client.ExtensionsV1beta1().Deployments(namespace).Create(d)
2525
if err != nil {

e2e/ssh_client.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"log"
7+
"net"
8+
"os"
9+
10+
"golang.org/x/crypto/ssh"
11+
"golang.org/x/crypto/ssh/agent"
12+
)
13+
14+
// The global SSH client used by the tests.
15+
var sshClient *SSHClient
16+
17+
type SSHClient struct {
18+
*ssh.ClientConfig
19+
}
20+
21+
func InitSSHClient(keypath string) {
22+
sshClient = NewSSHClientOrDie(keypath)
23+
}
24+
25+
// NewSSHClientOrDie tries to create an ssh client.
26+
// If $SSH_AUTH_SOCK is set, the use the ssh agent to create the client,
27+
// otherwise read the private key directly.
28+
func NewSSHClientOrDie(keypath string) *SSHClient {
29+
var authMethod ssh.AuthMethod
30+
31+
sock := os.Getenv("SSH_AUTH_SOCK")
32+
if sock != "" {
33+
log.Println("Creating ssh client with ssh agent")
34+
sshAgent, err := net.Dial("unix", sock)
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
authMethod = ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
40+
} else {
41+
log.Println("Creating ssh client with private key")
42+
key, err := ioutil.ReadFile(keypath)
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
signer, err := ssh.ParsePrivateKey(key)
48+
if err != nil {
49+
panic(err)
50+
}
51+
52+
authMethod = ssh.PublicKeys(signer)
53+
}
54+
55+
sshConfig := &ssh.ClientConfig{
56+
User: "core", // TODO(yifan): Assume all nodes are container linux nodes for now.
57+
Auth: []ssh.AuthMethod{authMethod},
58+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
59+
}
60+
61+
return &SSHClient{sshConfig}
62+
}
63+
64+
func (c *SSHClient) SSH(host, cmd string) (stdout, stderr []byte, err error) {
65+
client, err := ssh.Dial("tcp", host+":22", c.ClientConfig) // TODO(yifan): Assume all nodes are listening on :22 for ssh requests for now.
66+
if err != nil {
67+
return nil, nil, err
68+
}
69+
defer client.Conn.Close()
70+
71+
session, err := client.NewSession()
72+
if err != nil {
73+
return nil, nil, err
74+
}
75+
defer session.Close()
76+
77+
outBuf := bytes.NewBuffer(nil)
78+
errBuf := bytes.NewBuffer(nil)
79+
session.Stdout = outBuf
80+
session.Stderr = errBuf
81+
82+
err = session.Run(cmd)
83+
84+
stdout = bytes.TrimSpace(outBuf.Bytes())
85+
stderr = bytes.TrimSpace(errBuf.Bytes())
86+
87+
return stdout, stderr, err
88+
}

glide.lock

Lines changed: 15 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import:
1111
- package: github.com/ghodss/yaml
1212
- package: github.com/golang/glog
1313
- package: github.com/spf13/cobra
14+
version: 4cdb38c072b86bf795d2c81de50784d9fdd6eb77
15+
- package: github.com/spf13/pflag
16+
version: e57e3eeb33f795204c1ca35f56c44f83227c6e66
1417
- package: k8s.io/apimachinery
1518
version: 75b8dd260ef0469d96d578705a87cffd0e09dab8
1619
subpackages:
@@ -34,3 +37,10 @@ import:
3437
subpackages:
3538
- pkg/kubectl/cmd/util
3639
- pkg/kubectl/resource
40+
- package: golang.org/x/net
41+
version: e90d6d0afc4c315a0d87a568ae68577cc15149a0
42+
- package: golang.org/x/crypto/
43+
version: 0fe963104e9d1877082f8fb38f816fcd97eb1d10
44+
subpackages:
45+
- ssh
46+
- ssh/agent

vendor/cloud.google.com/go/.travis.yml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/cloud.google.com/go/AUTHORS

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)