chore: add flag to force talos cluster folder deletion

This is handy when the node with qemu went down, so you had to manually delete the folder after it restarted.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
This commit is contained in:
Dmitriy Matrenichev 2022-11-17 19:31:50 +03:00
parent d9c2c6f0a5
commit 1f1128028a
No known key found for this signature in database
GPG Key ID: D3363CF894E68892
5 changed files with 43 additions and 10 deletions

View File

@ -10,9 +10,14 @@ import (
"github.com/spf13/cobra"
"github.com/siderolabs/talos/pkg/cli"
"github.com/siderolabs/talos/pkg/provision"
"github.com/siderolabs/talos/pkg/provision/providers"
)
var destroyCmdFlags struct {
forceDelete bool
}
// destroyCmd represents the cluster destroy command.
var destroyCmd = &cobra.Command{
Use: "destroy",
@ -37,9 +42,11 @@ func destroy(ctx context.Context) error {
return err
}
return provisioner.Destroy(ctx, cluster)
return provisioner.Destroy(ctx, cluster, provision.WithDeleteOnErr(destroyCmdFlags.forceDelete))
}
func init() {
destroyCmd.PersistentFlags().BoolVarP(&destroyCmdFlags.forceDelete, "force", "f", false, "force deletion of cluster directory if there were errors")
Cmd.AddCommand(destroyCmd)
}

View File

@ -1 +1 @@
v1.3.0-alpha.1
v1.3.0-alpha.2

View File

@ -106,6 +106,15 @@ func WithDockerPortsHostIP(hostIP string) Option {
}
}
// WithDeleteOnErr informs the provisioner to delete cluster state folder on error.
func WithDeleteOnErr(v bool) Option {
return func(o *Options) error {
o.DeleteStateOnErr = v
return nil
}
}
// Options describes Provisioner parameters.
type Options struct {
LogWriter io.Writer
@ -125,6 +134,7 @@ type Options struct {
// Expose ports to worker machines in docker provisioner
DockerPorts []string
DockerPortsHostIP string
DeleteStateOnErr bool
}
// DefaultOptions returns default options.

View File

@ -14,6 +14,8 @@ import (
)
// Destroy Talos cluster as set of qemu VMs.
//
//nolint:gocyclo
func (p *provisioner) Destroy(ctx context.Context, cluster provision.Cluster, opts ...provision.Option) error {
options := provision.DefaultOptions()
@ -23,6 +25,24 @@ func (p *provisioner) Destroy(ctx context.Context, cluster provision.Cluster, op
}
}
complete := false
deleteStateDirectory := func(shouldDelete bool) error {
if complete || !shouldDelete {
return nil
}
complete = true
stateDirectoryPath, err := cluster.StatePath()
if err != nil {
return err
}
return os.RemoveAll(stateDirectoryPath)
}
defer deleteStateDirectory(options.DeleteStateOnErr) //nolint:errcheck
fmt.Fprintln(options.LogWriter, "stopping VMs")
if err := p.DestroyNodes(cluster.Info(), &options); err != nil {
@ -54,10 +74,5 @@ func (p *provisioner) Destroy(ctx context.Context, cluster provision.Cluster, op
fmt.Fprintln(options.LogWriter, "removing state directory")
stateDirectoryPath, err := cluster.StatePath()
if err != nil {
return err
}
return os.RemoveAll(stateDirectoryPath)
return deleteStateDirectory(true)
}

View File

@ -96,7 +96,7 @@ talosctl cluster create [flags]
--bad-rtc launch VM with bad RTC state (QEMU only)
--cidr string CIDR of the cluster network (IPv4, ULA network for IPv6 is derived in automated way) (default "10.5.0.0/24")
--cni-bin-path strings search path for CNI binaries (VM only) (default [/home/user/.talos/cni/bin])
--cni-bundle-url string URL to download CNI bundle from (VM only) (default "https://github.com/siderolabs/talos/releases/download/v1.3.0-alpha.1/talosctl-cni-bundle-${ARCH}.tar.gz")
--cni-bundle-url string URL to download CNI bundle from (VM only) (default "https://github.com/siderolabs/talos/releases/download/v1.3.0-alpha.2/talosctl-cni-bundle-${ARCH}.tar.gz")
--cni-cache-dir string CNI cache directory path (VM only) (default "/home/user/.talos/cni/cache")
--cni-conf-dir string CNI config directory path (VM only) (default "/home/user/.talos/cni/conf.d")
--config-patch stringArray patch generated machineconfigs (applied to all node types), use @file to read a patch from file
@ -185,7 +185,8 @@ talosctl cluster destroy [flags]
### Options
```
-h, --help help for destroy
-f, --force force deletion of cluster directory if there were errors
-h, --help help for destroy
```
### Options inherited from parent commands