Skip to content

Commit

Permalink
fix(cli): platform destroy should remove .vcluster/config.json
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan CJ <rohantmp@gmail.com>
  • Loading branch information
rohantmp committed Dec 2, 2024
1 parent 991ec3a commit 524671e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cmd/vclusterctl/cmd/platform/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
41 changes: 41 additions & 0 deletions pkg/cli/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/platform/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 524671e

Please sign in to comment.