Skip to content

Commit

Permalink
Merge pull request kubernetes#1138 from mcastellin/nodekiller-provide…
Browse files Browse the repository at this point in the history
…r-decouple

Decouple SSH command execution in util.SSH to enable unit testing
  • Loading branch information
k8s-ci-robot committed Apr 1, 2020
2 parents 074b859 + 1d65628 commit 5681edf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ clusterloader2/clusterloader

# Vscode files
.vscode

# GoLand files
.idea
8 changes: 5 additions & 3 deletions clusterloader2/pkg/chaos/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type NodeKiller struct {
// killedNodes stores names of the nodes that have been killed by NodeKiller.
killedNodes sets.String
recorder *eventRecorder
ssh util.SSHExecutor
}

type nodeAction string
Expand Down Expand Up @@ -85,7 +86,8 @@ func NewNodeKiller(config api.NodeFailureConfig, client clientset.Interface, pro
if provider != "gce" && provider != "gke" {
return nil, fmt.Errorf("provider %q is not supported by NodeKiller", provider)
}
return &NodeKiller{config, client, provider, sets.NewString(), newEventRecorder()}, nil
sshExecutor := &util.GCloudSSHExecutor{}
return &NodeKiller{config, client, provider, sets.NewString(), newEventRecorder(), sshExecutor}, nil
}

// Run starts NodeKiller until stopCh is closed.
Expand Down Expand Up @@ -154,7 +156,7 @@ func (k *NodeKiller) kill(nodes []v1.Node, stopCh <-chan struct{}) {

klog.Infof("%s: Stopping docker and kubelet on %q to simulate failure", k, node.Name)
k.addStopServicesEvent(node.Name)
err := util.SSH("sudo systemctl stop docker kubelet", &node, nil)
err := k.ssh.Exec("sudo systemctl stop docker kubelet", &node, nil)
if err != nil {
klog.Errorf("%s: ERROR while stopping node %q: %v", k, node.Name, err)
return
Expand All @@ -165,7 +167,7 @@ func (k *NodeKiller) kill(nodes []v1.Node, stopCh <-chan struct{}) {

klog.Infof("%s: Rebooting %q to repair the node", k, node.Name)
k.addRebootEvent(node.Name)
err = util.SSH("sudo reboot", &node, nil)
err = k.ssh.Exec("sudo reboot", &node, nil)
if err != nil {
klog.Errorf("%s: Error while rebooting node %q: %v", k, node.Name, err)
return
Expand Down
6 changes: 5 additions & 1 deletion clusterloader2/pkg/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type PrometheusController struct {
templateMapping map[string]interface{}
// diskMetadata store name and zone of Prometheus persistent disk.
diskMetadata prometheusDiskMetadata
// ssh executor to run commands in cluster nodes via ssh
ssh util.SSHExecutor
}

// NewPrometheusController creates a new instance of PrometheusController for the given config.
Expand Down Expand Up @@ -121,6 +123,8 @@ func NewPrometheusController(clusterLoaderConfig *config.ClusterLoaderConfig) (p

pc.templateMapping = mapping

pc.ssh = &util.GCloudSSHExecutor{}

return pc, nil
}

Expand Down Expand Up @@ -267,7 +271,7 @@ func (pc *PrometheusController) runNodeExporter() error {
return fmt.Errorf("Unable to open manifest file: %v", err)
}
defer f.Close()
return util.SSH("sudo tee /etc/kubernetes/manifests/node-exporter.yaml > /dev/null", &node, f)
return pc.ssh.Exec("sudo tee /etc/kubernetes/manifests/node-exporter.yaml > /dev/null", &node, f)
})
}
}
Expand Down
10 changes: 9 additions & 1 deletion clusterloader2/pkg/util/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ import (
"k8s.io/klog"
)

// SSHExecutor interface can run commands in cluster nodes via SSH
type SSHExecutor interface {
Exec(command string, node *v1.Node, stdin io.Reader) error
}

// GCloudSSHExecutor runs commands in GCloud cluster nodes
type GCloudSSHExecutor struct{}

// SSH executes command on a given node with stdin provided.
// If stdin is nil, the process reads from null device.
func SSH(command string, node *v1.Node, stdin io.Reader) error {
func (e *GCloudSSHExecutor) Exec(command string, node *v1.Node, stdin io.Reader) error {
zone, ok := node.Labels["failure-domain.beta.kubernetes.io/zone"]
if !ok {
return fmt.Errorf("unknown zone for %q node: no failure-domain.beta.kubernetes.io/zone label", node.Name)
Expand Down

0 comments on commit 5681edf

Please sign in to comment.