Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(virtualization): add evict subcommand #44

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.1

require (
github.com/Masterminds/semver/v3 v3.3.0
github.com/deckhouse/virtualization/api v0.0.0-20240326113112-979c4f4f17b9
github.com/deckhouse/virtualization/api v0.0.0-20241101085803-1002322cdb92
github.com/google/go-containerregistry v0.20.0
github.com/gorilla/websocket v1.5.3
github.com/hashicorp/go-cleanhttp v0.5.2
Expand Down Expand Up @@ -342,7 +342,7 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/errgo.v2 v2.1.0 // indirect
gopkg.in/evanphx/json-patch.v5 v5.8.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/daviddengcn/go-colortext v1.0.0 h1:ANqDyC0ys6qCSvuEK7l3g5RaehL/Xck9EX8ATG8oKsE=
github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c=
github.com/deckhouse/virtualization/api v0.0.0-20240326113112-979c4f4f17b9 h1:UoyY1oAqGNp7UKi1x7UKYJKUDl3SQgNT1aAN2G01osA=
github.com/deckhouse/virtualization/api v0.0.0-20240326113112-979c4f4f17b9/go.mod h1:1tfoFeZmlKqq6jEuSfIpdrxsBpOcMajYaCbO94pVQLs=
github.com/deckhouse/virtualization/api v0.0.0-20241101085803-1002322cdb92 h1:EeO0Ly13a3DhoY9Q/Fz39MdhE6q2A4+cLi/d3nAGjpQ=
github.com/deckhouse/virtualization/api v0.0.0-20241101085803-1002322cdb92/go.mod h1:t+6i4NC43RfNLqcZqkEc5vxY1ypKceqmOOKlVEq0cYA=
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
Expand Down Expand Up @@ -1504,8 +1504,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/cenkalti/backoff.v2 v2.2.1 h1:eJ9UAg01/HIHG987TwxvnzK2MgxXq97YY6rYDpY9aII=
Expand Down
6 changes: 6 additions & 0 deletions internal/virtualization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Manages virtual machine-related operations in your Kubernetes cluster.
* start - Start a virtual machine.
* stop - Stop a virtual machine.
* restart - Restart a virtual machine.
* evict - Evict a virtual machine.

### Examples
#### console
Expand Down Expand Up @@ -52,4 +53,9 @@ d8 virtualization stop myvm -n mynamespace
```shell
d8 virtualization restart myvm.mynamespace --timeout=1m
d8 virtualization restart myvm -n mynamespace
```
#### evict
```shell
d8 virtualization evict myvm.mynamespace
d8 virtualization evict myvm -n mynamespace
```
24 changes: 24 additions & 0 deletions internal/virtualization/cmd/lifecycle/evict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lifecycle

import (
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"

"github.com/deckhouse/deckhouse-cli/internal/virtualization/templates"
)

func NewEvictCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
lifecycle := NewLifecycle(Evict, clientConfig)
cmd := &cobra.Command{
Use: "evict (VirtualMachine)",
Short: "Evict a virtual machine.",
Example: lifecycle.Usage(),
Args: templates.ExactArgs("evict", 1),
RunE: func(cmd *cobra.Command, args []string) error {
return lifecycle.Run(args)
},
}
AddCommandlineArgs(cmd.Flags(), &lifecycle.opts)
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package powerstate
package lifecycle

import (
"context"
Expand All @@ -25,10 +25,13 @@ import (

"github.com/deckhouse/virtualization/api/client/kubeclient"
"github.com/spf13/pflag"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"

"github.com/deckhouse/deckhouse-cli/internal/virtualization/cmd/powerstate/vmop"
"github.com/deckhouse/deckhouse-cli/internal/virtualization/cmd/lifecycle/vmop"

"github.com/deckhouse/deckhouse-cli/internal/virtualization/templates"
)

Expand All @@ -38,12 +41,14 @@ const (
Stop Command = "stop"
Start Command = "start"
Restart Command = "restart"
Evict Command = "evict"
)

type Manager interface {
Stop(ctx context.Context, name, namespace string, wait, force bool) (msg string, err error)
Start(ctx context.Context, name, namespace string, wait bool) (msg string, err error)
Restart(ctx context.Context, name, namespace string, wait, force bool) (msg string, err error)
Evict(ctx context.Context, name, namespace string, wait bool) (msg string, err error)
}

type Type string
Expand All @@ -59,15 +64,15 @@ func NewManager(t Type, client kubeclient.Client) (Manager, error) {
}
}

func NewPowerState(cmd Command, clientConfig clientcmd.ClientConfig) *PowerState {
return &PowerState{
func NewLifecycle(cmd Command, clientConfig clientcmd.ClientConfig) *Lifecycle {
return &Lifecycle{
cmd: cmd,
clientConfig: clientConfig,
opts: DefaultOptions(),
}
}

type PowerState struct {
type Lifecycle struct {
cmd Command
clientConfig clientcmd.ClientConfig
opts Options
Expand All @@ -87,74 +92,77 @@ type Options struct {
Timeout time.Duration
}

func (ps *PowerState) Run(args []string) error {
name, namespace, err := ps.getNameNamespace(args)
func (l *Lifecycle) Run(args []string) error {
name, namespace, err := l.getNameNamespace(args)
key := types.NamespacedName{Namespace: namespace, Name: name}
if err != nil {
return err
}
mgr, err := ps.getManager()
mgr, err := l.getManager()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), ps.opts.Timeout)
ctx, cancel := context.WithTimeout(context.Background(), l.opts.Timeout)
defer cancel()
writer := os.Stdout
var msg string
switch ps.cmd {
switch l.cmd {
case Stop:
fmt.Fprint(writer, fmt.Sprintf("Stopping virtual machine %q\n", key.String()))
msg, err = mgr.Stop(ctx, name, namespace, ps.opts.Wait, ps.opts.Force)
msg, err = mgr.Stop(ctx, name, namespace, l.opts.Wait, l.opts.Force)
case Start:
fmt.Fprint(writer, fmt.Sprintf("Starting virtual machine %q\n", key.String()))
msg, err = mgr.Start(ctx, name, namespace, ps.opts.Wait)
msg, err = mgr.Start(ctx, name, namespace, l.opts.Wait)
case Restart:
fmt.Fprint(writer, fmt.Sprintf("Restarting virtual machine %q\n", key.String()))
msg, err = mgr.Restart(ctx, name, namespace, ps.opts.Wait, ps.opts.Force)
msg, err = mgr.Restart(ctx, name, namespace, l.opts.Wait, l.opts.Force)
case Evict:
fmt.Fprint(writer, fmt.Sprintf("Evicting virtual machine %q\n", key.String()))
msg, err = mgr.Evict(ctx, name, namespace, l.opts.Wait)
default:
return fmt.Errorf("invalid command %q", ps.cmd)
return fmt.Errorf("invalid command %q", l.cmd)
}
if msg != "" {
fmt.Fprint(os.Stdout, msg)
}
return err
}

func (ps *PowerState) Usage() string {
func (l *Lifecycle) Usage() string {
opts := DefaultOptions()
usage := fmt.Sprintf(` # %s VirtualMachine 'myvm':`, strings.Title(string(ps.cmd)))
usage := fmt.Sprintf(` # %s VirtualMachine 'myvm':`, cases.Title(language.English).String(string(l.cmd)))
usage += strings.Replace(fmt.Sprintf(`
{{ProgramName}} {{operation}} myvm
{{ProgramName}} {{operation}} myvm.mynamespace
{{ProgramName}} {{operation}} myvm -n mynamespace
# Configure one minute timeout (default: timeout=%v)
{{ProgramName}} {{operation}} --%s=1m myvm
# Configure wait vm phase (default: wait=%v)
{{ProgramName}} {{operation}} --%s myvm`, opts.Timeout, timeoutFlag, opts.Wait, waitFlag), "{{operation}}", string(ps.cmd), -1)
if ps.cmd != Start {
{{ProgramName}} {{operation}} --%s myvm`, opts.Timeout, timeoutFlag, opts.Wait, waitFlag), "{{operation}}", string(l.cmd), -1)
if l.cmd != Start && l.cmd != Evict {
usage += fmt.Sprintf(`
# Configure shutdown policy (default: force=%v)
{{ProgramName}} %s --%s myvm`, opts.Force, ps.cmd, forceFlag)
{{ProgramName}} %s --%s myvm`, opts.Force, l.cmd, forceFlag)
}
return usage
}

func (ps *PowerState) getNameNamespace(args []string) (string, string, error) {
func (l *Lifecycle) getNameNamespace(args []string) (string, string, error) {
namespace, name, err := templates.ParseTarget(args[0])
if err != nil {
return "", "", err
}
if namespace == "" {
namespace, _, err = ps.clientConfig.Namespace()
namespace, _, err = l.clientConfig.Namespace()
if err != nil {
return "", "", err
}
}
return name, namespace, nil
}

func (ps *PowerState) getManager() (Manager, error) {
virtCli, err := kubeclient.GetClientFromClientConfig(ps.clientConfig)
func (l *Lifecycle) getManager() (Manager, error) {
virtCli, err := kubeclient.GetClientFromClientConfig(l.clientConfig)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,27 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package powerstate
package lifecycle

import (
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/client-go/tools/clientcmd"

"github.com/deckhouse/deckhouse-cli/internal/virtualization/templates"
)

func NewRestartCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
ps := NewPowerState(Restart, clientConfig)
restart := string(Restart)
lifecycle := NewLifecycle(Restart, clientConfig)
cmd := &cobra.Command{
Use: restart + " (VirtualMachine)",
Short: cases.Title(language.English).String(restart) + " a virtual machine.",
Example: ps.Usage(),
Args: templates.ExactArgs(restart, 1),
Use: "restart (VirtualMachine)",
Short: "Restart a virtual machine.",
Example: lifecycle.Usage(),
Args: templates.ExactArgs("restart", 1),
RunE: func(cmd *cobra.Command, args []string) error {
return ps.Run(args)
return lifecycle.Run(args)
},
}
AddCommandlineArgs(cmd.Flags(), &ps.opts)
AddCommandlineArgs(cmd.Flags(), &lifecycle.opts)
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,27 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package powerstate
package lifecycle

import (
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/client-go/tools/clientcmd"

"github.com/deckhouse/deckhouse-cli/internal/virtualization/templates"
)

func NewStartCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
ps := NewPowerState(Start, clientConfig)
start := string(Start)
lifecycle := NewLifecycle(Start, clientConfig)
cmd := &cobra.Command{
Use: start + " (VirtualMachine)",
Short: cases.Title(language.English).String(start) + " a virtual machine.",
Example: ps.Usage(),
Args: templates.ExactArgs(start, 1),
Use: "start (VirtualMachine)",
Short: "Start a virtual machine.",
Example: lifecycle.Usage(),
Args: templates.ExactArgs("start", 1),
RunE: func(cmd *cobra.Command, args []string) error {
return ps.Run(args)
return lifecycle.Run(args)
},
}
AddCommandlineArgs(cmd.Flags(), &ps.opts)
AddCommandlineArgs(cmd.Flags(), &lifecycle.opts)
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,27 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package powerstate
package lifecycle

import (
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/client-go/tools/clientcmd"

"github.com/deckhouse/deckhouse-cli/internal/virtualization/templates"
)

func NewStopCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
ps := NewPowerState(Stop, clientConfig)
stop := string(Stop)
lifecycle := NewLifecycle(Stop, clientConfig)
cmd := &cobra.Command{
Use: stop + " (VirtualMachine)",
Short: cases.Title(language.English).String(stop) + " a virtual machine.",
Example: ps.Usage(),
Args: templates.ExactArgs(stop, 1),
Use: "stop (VirtualMachine)",
Short: "Stop a virtual machine.",
Example: lifecycle.Usage(),
Args: templates.ExactArgs("stop", 1),
RunE: func(cmd *cobra.Command, args []string) error {
return ps.Run(args)
return lifecycle.Run(args)
},
}
AddCommandlineArgs(cmd.Flags(), &ps.opts)
AddCommandlineArgs(cmd.Flags(), &lifecycle.opts)
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}
Loading