Skip to content

Commit

Permalink
Merge branch 'main' into stronghold
Browse files Browse the repository at this point in the history
  • Loading branch information
trublast committed Nov 8, 2024
2 parents 81f8cd2 + 1e22bfd commit e09a4cc
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 78 deletions.
2 changes: 1 addition & 1 deletion 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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,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/denisenkom/go-mssqldb v0.12.2 h1:1OcPn5GBIobjWNd+8yjfHNIaFX14B1pWI3F9HZy5KXw=
github.com/denisenkom/go-mssqldb v0.12.2/go.mod h1:lnIw1mZukFRZDJYQ0Pb833QS2IaC3l5HkEfra2LJ+sk=
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

0 comments on commit e09a4cc

Please sign in to comment.