Skip to content

Commit

Permalink
Merge pull request #847 from Mirantis/jell/validate
Browse files Browse the repository at this point in the history
Introduce virtletctl validate command
  • Loading branch information
ivan4th authored Jan 23, 2019
2 parents 39e00bc + 7038634 commit b4c3643
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/virtletctl/virtletctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func newRootCmd() *cobra.Command {
cmd.AddCommand(tools.NewGenCmd(os.Stdout))
cmd.AddCommand(tools.NewVersionCommand(client, os.Stdout, nil))
cmd.AddCommand(tools.NewDiagCommand(client, os.Stdin, os.Stdout))
cmd.AddCommand(tools.NewValidateCommand(client, os.Stdin))

for _, c := range cmd.Commands() {
c.PreRunE = func(*cobra.Command, []string) error {
Expand Down
13 changes: 13 additions & 0 deletions docs/virtletctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Kubernetes cluster.
* [virtletctl gendoc](#virtletctl-gendoc) - Generate Markdown documentation for the commands
* [virtletctl install](#virtletctl-install) - Install virtletctl as a kubectl plugin
* [virtletctl ssh](#virtletctl-ssh) - Connect to a VM pod using ssh
* [virtletctl validate](#virtletctl-validate) - Validate cluster readiness for Virtlet deployment
* [virtletctl version](#virtletctl-version) - Display Virtlet version information
* [virtletctl virsh](#virtletctl-virsh) - Execute a virsh command
* [virtletctl vnc](#virtletctl-vnc) - Provide access to the VNC console of a VM pod
Expand Down Expand Up @@ -170,6 +171,18 @@ This command runs ssh and makes it connect to a VM pod.
virtletctl ssh [flags] user@pod -- [ssh args...]
```

## virtletctl validate

Validate cluster readiness for Virtlet deployment

**Synopsis**

Check configuration of cluster nodes valiating its readiness for Virtlet deployment

```
virtletctl validate [flags]
```

## virtletctl version

Display Virtlet version information
Expand Down
18 changes: 18 additions & 0 deletions pkg/tools/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.
package tools

import (
"errors"
"fmt"
"io"
"sort"
"strings"
"testing"

"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
)

type fakeKubeClient struct {
Expand Down Expand Up @@ -125,6 +127,22 @@ func (c *fakeKubeClient) PodLogs(podName, containerName, namespace string, tailL
return []byte(l), nil
}

func (c *fakeKubeClient) GetVirtletNodeNames() ([]string, error) {
return nil, errors.New("not implemented")
}

func (c *fakeKubeClient) CreatePod(pod *v1.Pod) (*v1.Pod, error) {
return nil, errors.New("not implemented")
}

func (c *fakeKubeClient) GetPod(name, namespace string) (*v1.Pod, error) {
return nil, errors.New("not implemented")
}

func (c *fakeKubeClient) DeletePod(pod, namespace string) error {
return errors.New("not implemented")
}

func fakeCobraCommand() *cobra.Command {
topCmd := &cobra.Command{
Use: "topcmd",
Expand Down
47 changes: 47 additions & 0 deletions pkg/tools/kubeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func ParsePortForwardOutput(out string, ports []*ForwardedPort) error {

// KubeClient contains methods for interfacing with Kubernetes clusters.
type KubeClient interface {
// GetVirtletNodeNames returns a list of node names for nodes labeled
// with virtlet as extra runtime
GetVirtletNodeNames() (nodeNames []string, err error)
// GetVirtletPodAndNodeNames returns a list of names of the
// virtlet pods present in the cluster and a list of
// corresponding node names that contain these pods.
Expand All @@ -131,6 +134,12 @@ type KubeClient interface {
// GetVMPodInfo returns then name of the virtlet pod and the vm container name for
// the specified VM pod.
GetVMPodInfo(podName string) (*VMPodInfo, error)
// CreatePod given a pod specification calls api to create it
CreatePod(pod *v1.Pod) (*v1.Pod, error)
// GetPod given a pod returns its status
GetPod(name, namespace string) (*v1.Pod, error)
// DeletePod given a pod and its namespace removes it
DeletePod(pod, namespace string) error
// ExecInContainer given a pod, a container, a namespace and a command
// executes that command inside the pod's container returning stdout and stderr output
// as strings and an error if it has occurred.
Expand Down Expand Up @@ -274,6 +283,26 @@ func (c *RealKubeClient) getVMPod(podName string) (*v1.Pod, error) {
return pod, nil
}

// GetVirtletNodeNames implements GetVirtletNodeNames methor of KubeClient interface.
func (c *RealKubeClient) GetVirtletNodeNames() ([]string, error) {
if err := c.setup(); err != nil {
return nil, err
}
opts := meta_v1.ListOptions{
LabelSelector: "extraRuntime=virtlet",
}
nodes, err := c.client.CoreV1().Nodes().List(opts)
if err != nil {
return nil, err
}

var nodeNames []string
for _, item := range nodes.Items {
nodeNames = append(nodeNames, item.Name)
}
return nodeNames, nil
}

// GetVirtletPodAndNodeNames implements GetVirtletPodAndNodeNames method of KubeClient interface.
func (c *RealKubeClient) GetVirtletPodAndNodeNames() (podNames []string, nodeNames []string, err error) {
return c.getVirtletPodAndNodeNames("")
Expand Down Expand Up @@ -327,6 +356,24 @@ func (c *RealKubeClient) GetVMPodInfo(podName string) (*VMPodInfo, error) {
}, nil
}

// CreatePod implements CreatePod method of KubeClient interface
func (c *RealKubeClient) CreatePod(pod *v1.Pod) (*v1.Pod, error) {
if err := c.setup(); err != nil {
return nil, err
}
return c.client.CoreV1().Pods(pod.Namespace).Create(pod)
}

// GetPod implements GetPod method of KubeClient interface
func (c *RealKubeClient) GetPod(name, namespace string) (*v1.Pod, error) {
return c.client.CoreV1().Pods(namespace).Get(name, meta_v1.GetOptions{})
}

// DeletePod implements DeletePod method of KubeClient interface
func (c *RealKubeClient) DeletePod(name, namespace string) error {
return c.client.CoreV1().Pods(namespace).Delete(name, &meta_v1.DeleteOptions{})
}

// ExecInContainer implements ExecInContainer method of KubeClient interface
func (c *RealKubeClient) ExecInContainer(podName, containerName, namespace string, stdin io.Reader, stdout, stderr io.Writer, command []string) (int, error) {
if err := c.setup(); err != nil {
Expand Down
Loading

0 comments on commit b4c3643

Please sign in to comment.