Skip to content

Commit

Permalink
add migrate subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat committed Oct 2, 2024
1 parent 5c4ad95 commit f910cc7
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 68 deletions.
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module github.com/deckhouse/deckhouse-cli

go 1.22.0
go 1.22.7

toolchain go1.22.8

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/deckhouse/virtualization/api v0.0.0-20240326113112-979c4f4f17b9
github.com/deckhouse/virtualization/api v0.0.0-20241001121350-42e10ef60ce3
github.com/google/go-containerregistry v0.19.1
github.com/gookit/color v1.5.4
github.com/gorilla/websocket v1.5.1
github.com/int128/kubelogin v1.28.0
github.com/pkg/errors v0.9.1
Expand All @@ -20,6 +21,7 @@ require (
github.com/werf/logboek v0.6.1
github.com/werf/nelm v0.0.0-20240806160049-119410ac7901
github.com/werf/werf/v2 v2.10.1-0.20240806161101-2bc58b7bad1c
gitlab.com/greyxor/slogor v1.2.11
go.cypherpunks.ru/gogost/v5 v5.13.0
golang.org/x/crypto v0.21.0
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
Expand Down Expand Up @@ -172,6 +174,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
Expand Down Expand Up @@ -310,7 +313,6 @@ require (
github.com/xlab/treeprint v1.2.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
gitlab.com/greyxor/slogor v1.2.11 // indirect
go.etcd.io/bbolt v1.3.9 // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
Expand Down Expand Up @@ -343,7 +345,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.62.1 // indirect
google.golang.org/protobuf v1.33.0 // 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/go-jose/go-jose.v2 v2.6.3 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,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-20241001121350-42e10ef60ce3 h1:TF00E4F4kbDpZs7fMxIimU7AaTLkJ+/C9s2yI75nmnI=
github.com/deckhouse/virtualization/api v0.0.0-20241001121350-42e10ef60ce3/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 @@ -1315,8 +1315,6 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand Down Expand Up @@ -1496,8 +1494,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.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/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
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"
Migrate Command = "migrate"
)

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)
Migrate(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 Migrate:
fmt.Fprint(writer, fmt.Sprintf("Migrating virtual machine %q\n", key.String()))
msg, err = mgr.Migrate(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 != Migrate {
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
27 changes: 27 additions & 0 deletions internal/virtualization/cmd/lifecycle/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 NewMigrateCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
lifecycle := NewLifecycle(Migrate, clientConfig)
migrate := string(Migrate)
cmd := &cobra.Command{
Use: migrate + " (VirtualMachine)",
Short: cases.Title(language.English).String(migrate) + " a virtual machine.",
Example: lifecycle.Usage(),
Args: templates.ExactArgs(migrate, 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 (
"github.com/spf13/cobra"
Expand All @@ -26,18 +26,18 @@ import (
)

func NewRestartCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
ps := NewPowerState(Restart, clientConfig)
lifecycle := NewLifecycle(Restart, clientConfig)
restart := string(Restart)
cmd := &cobra.Command{
Use: restart + " (VirtualMachine)",
Short: cases.Title(language.English).String(restart) + " a virtual machine.",
Example: ps.Usage(),
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,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package powerstate
package lifecycle

import (
"github.com/spf13/cobra"
Expand All @@ -26,18 +26,18 @@ import (
)

func NewStartCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
ps := NewPowerState(Start, clientConfig)
lifecycle := NewLifecycle(Start, clientConfig)
start := string(Start)
cmd := &cobra.Command{
Use: start + " (VirtualMachine)",
Short: cases.Title(language.English).String(start) + " a virtual machine.",
Example: ps.Usage(),
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,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package powerstate
package lifecycle

import (
"github.com/spf13/cobra"
Expand All @@ -26,18 +26,18 @@ import (
)

func NewStopCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
ps := NewPowerState(Stop, clientConfig)
lifecycle := NewLifecycle(Stop, clientConfig)
stop := string(Stop)
cmd := &cobra.Command{
Use: stop + " (VirtualMachine)",
Short: cases.Title(language.English).String(stop) + " a virtual machine.",
Example: ps.Usage(),
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

0 comments on commit f910cc7

Please sign in to comment.