From 524671e3072d37e5da0cd485cb63724a2f19b22e Mon Sep 17 00:00:00 2001 From: Rohan CJ Date: Fri, 29 Nov 2024 17:23:58 +0530 Subject: [PATCH] fix(cli): platform destroy should remove .vcluster/config.json Signed-off-by: Rohan CJ --- cmd/vclusterctl/cmd/platform/destroy.go | 14 ++++++++- pkg/cli/config/config.go | 41 +++++++++++++++++++++++++ pkg/platform/client.go | 4 +++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/cmd/vclusterctl/cmd/platform/destroy.go b/cmd/vclusterctl/cmd/platform/destroy.go index 93fc264eb..e74f29ca0 100644 --- a/cmd/vclusterctl/cmd/platform/destroy.go +++ b/cmd/vclusterctl/cmd/platform/destroy.go @@ -39,7 +39,7 @@ func NewDestroyCmd(globalFlags *flags.GlobalFlags) *cobra.Command { Destroys a vCluster Platform instance in your Kubernetes cluster. -Important: This action is done against the cluster the the kube-context is pointing to, and not the vCluster Platform instance that is logged in. +IMPORTANT: This action is done against the cluster the the kube-context is pointing to, and not the vCluster Platform instance that is logged in. It does not require logging in to vCluster Platform. Please make sure you meet the following requirements @@ -121,5 +121,17 @@ func (cmd *DestroyCmd) Run(ctx context.Context) error { if err != nil { return fmt.Errorf("failed to destroy platform: %w", err) } + + cmd.Log.Infof("deleting platform config at %q", cmd.Config) + cliConfig := cmd.LoadedConfig(cmd.Log) + err = cliConfig.Delete() + if err != nil { + if errors.Is(err, os.ErrNotExist) && cmd.IgnoreNotFound { + cmd.Log.Info("no platform config detected") + return nil + } + return fmt.Errorf("failed to delete platform config: %w", err) + } + return nil } diff --git a/pkg/cli/config/config.go b/pkg/cli/config/config.go index 9593015bf..fafb4a940 100644 --- a/pkg/cli/config/config.go +++ b/pkg/cli/config/config.go @@ -1,7 +1,9 @@ package config import ( + "bytes" "encoding/json" + "errors" "fmt" "io" "os" @@ -53,6 +55,45 @@ func (c *CLI) Save() error { return Write(path, c) } +func (c *CLI) Delete() error { + if c == nil || c.path == "" { + return errors.New("nil config path") + } + + file, err := os.Open(c.path) + if err != nil { + return fmt.Errorf("failed to load vcluster configuration file from %q : %w", c.path, err) + } + stat, err := file.Stat() + if err != nil { + return fmt.Errorf("failed to load vcluster configuration file from %q: %w", c.path, err) + } + if stat.IsDir() { + return fmt.Errorf("failed to load vcluster configuration file %q", c.path) + } + defer file.Close() + + fileBytes, err := io.ReadAll(file) + if err != nil { + return fmt.Errorf("read all: %w", err) + } + + decoder := json.NewDecoder(bytes.NewReader(fileBytes)) + decoder.DisallowUnknownFields() + tryRead := &CLI{} + err = decoder.Decode(tryRead) + if err != nil { + return fmt.Errorf("failed to unmarshall vcluster configuration from %q: %w", c.path, err) + } + + // delete file at path + err = os.Remove(c.path) + if err != nil { + return fmt.Errorf("failed to delete configuration file at %q: %w", c.path, err) + } + return nil +} + // Read returns the current config by trying to read it from the given config path. // It returns a new default config if there have been any errors during the read. func Read(path string, log log.Logger) *CLI { diff --git a/pkg/platform/client.go b/pkg/platform/client.go index 92406f14b..2cba31484 100644 --- a/pkg/platform/client.go +++ b/pkg/platform/client.go @@ -157,6 +157,10 @@ func (c *client) Save() error { return c.config.Save() } +func (c *client) Delete() error { + return c.config.Delete() +} + func (c *client) ManagementConfig() (*rest.Config, error) { return c.restConfig("/kubernetes/management") }