Skip to content

Commit

Permalink
feat(internal): Add 'stopped' flag to rm & 'force' to stop (#1401)
Browse files Browse the repository at this point in the history
Reviewed-by: Antoine Cotten <antoine@unikraft.io>
Approved-by: Antoine Cotten <antoine@unikraft.io>
  • Loading branch information
craciunoiuc authored Mar 12, 2024
2 parents c2d0909 + d96809e commit 2ec8ab4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ require (
k8s.io/apimachinery v0.29.2
k8s.io/apiserver v0.29.2
oras.land/oras-go/v2 v2.4.0
sdk.kraft.cloud v0.5.3
sdk.kraft.cloud v0.5.4
sigs.k8s.io/kustomize/kyaml v0.14.3
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1673,8 +1673,8 @@ oras.land/oras-go/v2 v2.4.0/go.mod h1:osvtg0/ClRq1KkydMAEu/IxFieyjItcsQ4ut4PPF+f
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sdk.kraft.cloud v0.5.3 h1:IcueltPtK7d+n6AGofC4cbpPqHvdRZby1o96s9mHikk=
sdk.kraft.cloud v0.5.3/go.mod h1:lgGLp9jSBwSdeQkwrcQZHuejQbkgE3AqbIN+mnaKDkg=
sdk.kraft.cloud v0.5.4 h1:g6XdkbNYAnozy5siNbZKRPwigYGdofvCc3WvZUOqMGI=
sdk.kraft.cloud v0.5.4/go.mod h1:lgGLp9jSBwSdeQkwrcQZHuejQbkgE3AqbIN+mnaKDkg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.14/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0 h1:TgtAeesdhpm2SGwkQasmbeqDo8th5wOBA5h/AjTKA4I=
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/kraft/cloud/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (opts *DeployOptions) Run(ctx context.Context, args []string) error {
}

if _, err := opts.Client.Instances().WithMetro(opts.Metro).
StopByUUIDs(ctx, int(time.Minute.Milliseconds()), instance); err != nil {
StopByUUIDs(ctx, int(time.Minute.Milliseconds()), false, instance); err != nil {
return fmt.Errorf("could not stop the old instance: %w", err)
}

Expand Down Expand Up @@ -312,7 +312,7 @@ func (opts *DeployOptions) Run(ctx context.Context, args []string) error {
}

if _, err := opts.Client.Instances().WithMetro(opts.Metro).
StopByUUIDs(ctx, int(time.Minute.Milliseconds()), instance); err != nil {
StopByUUIDs(ctx, int(time.Minute.Milliseconds()), false, instance); err != nil {
return fmt.Errorf("could not stop the old instance: %w", err)
}

Expand Down
48 changes: 41 additions & 7 deletions internal/cli/kraft/cloud/instance/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"sdk.kraft.cloud/instances"

kraftcloud "sdk.kraft.cloud"

Expand All @@ -21,8 +22,9 @@ import (
)

type RemoveOptions struct {
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list" default:"table"`
All bool `long:"all" usage:"Remove all instances"`
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list" default:"table"`
All bool `long:"all" short:"a" usage:"Remove all instances"`
Stopped bool `long:"stopped" short:"s" usage:"Remove all stopped instances"`

metro string
token string
Expand Down Expand Up @@ -55,6 +57,9 @@ func NewCmd() *cobra.Command {
# Remove all KraftCloud instances
$ kraft cloud instance remove --all
# Remove all stopped KraftCloud instances
$ kraft cloud instance remove --stopped
`),
Long: heredoc.Doc(`
Remove a KraftCloud instance.
Expand All @@ -71,9 +76,18 @@ func NewCmd() *cobra.Command {
}

func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
if !opts.All && len(args) == 0 {
if !opts.Stopped && !opts.All && len(args) == 0 {
return fmt.Errorf("either specify an instance name or UUID, or use the --all flag")
}
if opts.Stopped && opts.All {
return fmt.Errorf("cannot use --stopped and --all together")
}
if opts.Stopped && len(args) > 0 {
return fmt.Errorf("cannot specify instances and use --stopped together")
}
if opts.All && len(args) > 0 {
return fmt.Errorf("cannot specify instances and use --all together")
}

err := utils.PopulateMetroToken(cmd, &opts.metro, &opts.token)
if err != nil {
Expand All @@ -93,21 +107,41 @@ func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)),
)

if opts.All {
if opts.All || opts.Stopped {
instListResp, err := client.WithMetro(opts.metro).List(ctx)
if err != nil {
return fmt.Errorf("could not list instances: %w", err)
}

log.G(ctx).Infof("Removing %d instance(s)", len(instListResp))

uuids := make([]string, 0, len(instListResp))
for _, instItem := range instListResp {
uuids = append(uuids, instItem.UUID)
}

if opts.Stopped {
instInfos, err := client.WithMetro(opts.metro).GetByUUIDs(ctx, uuids...)
if err != nil {
return fmt.Errorf("could not get instances: %w", err)
}

var stoppedUuids []string
for _, instInfo := range instInfos {
if instances.State(instInfo.State) == instances.StateStopped {
stoppedUuids = append(stoppedUuids, instInfo.UUID)
}
}

uuids = stoppedUuids
}

if len(uuids) == 0 {
return nil
}

log.G(ctx).Infof("Removing %d instance(s)", len(uuids))

if _, err := client.WithMetro(opts.metro).DeleteByUUIDs(ctx, uuids...); err != nil {
return fmt.Errorf("removing %d instance(s): %w", len(instListResp), err)
return fmt.Errorf("removing %d instance(s): %w", len(uuids), err)
}
return nil
}
Expand Down
11 changes: 6 additions & 5 deletions internal/cli/kraft/cloud/instance/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type StopOptions struct {
DrainTimeout time.Duration `local:"true" long:"drain-timeout" short:"d" usage:"Timeout for the instance to stop (ms/s/m/h)"`
Output string `long:"output" short:"o" usage:"Set output format. Options: table,yaml,json,list" default:"table"`
All bool `long:"all" usage:"Stop all instances"`
Force bool `long:"force" short:"f" usage:"Force stop the instance(s)"`
Metro string `noattribute:"true"`
Token string `noattribute:"true"`
}
Expand Down Expand Up @@ -113,7 +114,7 @@ func (opts *StopOptions) Run(ctx context.Context, args []string) error {
uuids = append(uuids, instItem.UUID)
}

if _, err = client.WithMetro(opts.Metro).StopByUUIDs(ctx, timeout, uuids...); err != nil {
if _, err = client.WithMetro(opts.Metro).StopByUUIDs(ctx, timeout, opts.Force, uuids...); err != nil {
log.G(ctx).Error("could not stop instance: %w", err)
}

Expand All @@ -137,21 +138,21 @@ func (opts *StopOptions) Run(ctx context.Context, args []string) error {

switch {
case allUUIDs:
if _, err := client.WithMetro(opts.Metro).StopByUUIDs(ctx, timeout, args...); err != nil {
if _, err := client.WithMetro(opts.Metro).StopByUUIDs(ctx, timeout, opts.Force, args...); err != nil {
return fmt.Errorf("stopping %d instance(s): %w", len(args), err)
}
case allNames:
if _, err := client.WithMetro(opts.Metro).StopByNames(ctx, timeout, args...); err != nil {
if _, err := client.WithMetro(opts.Metro).StopByNames(ctx, timeout, opts.Force, args...); err != nil {
return fmt.Errorf("stopping %d instance(s): %w", len(args), err)
}
default:
for _, arg := range args {
log.G(ctx).Infof("Stopping instance %s", arg)

if utils.IsUUID(arg) {
_, err = client.WithMetro(opts.Metro).StopByUUIDs(ctx, timeout, arg)
_, err = client.WithMetro(opts.Metro).StopByUUIDs(ctx, timeout, opts.Force, arg)
} else {
_, err = client.WithMetro(opts.Metro).StopByNames(ctx, timeout, arg)
_, err = client.WithMetro(opts.Metro).StopByNames(ctx, timeout, opts.Force, arg)
}

if err != nil {
Expand Down

0 comments on commit 2ec8ab4

Please sign in to comment.