Skip to content

Commit

Permalink
feat: create kube config backup before overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiondic committed Aug 30, 2022
1 parent 835e030 commit 5636955
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions internal/connection/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -51,9 +52,10 @@ func Connect() {
fmt.Println(configYaml)
} else {
if !viper.GetBool(flags.AutoApprove) {
confirmOperation("Proceeding will overwrite your local $HOME/.kube/config file. Impact of this is that you will lose " +
"existing connections to other clusters. Type 'yes' to proceed. You can also consider using the " + flags.PrintToStdout + " " +
"flag to see instructions on merging the kube config files")
confirmOperation("Proceeding will overwrite your local $HOME/.kube/config file. Your old config will be backed up, but the impact of " +
"this is that you will lose existing connections to other clusters. You can manually restore your connections by renaming the config " +
"backup (in .kube directory) back to 'config' file name. Type 'yes' to proceed. You can also consider using the " +
flags.PrintToStdout + "flag to see instructions on merging the kube config files")
}

saveKubeConfigToFile(configYaml)
Expand Down Expand Up @@ -93,10 +95,42 @@ func saveKubeConfigToFile(configYaml string) {
panicOnError(err)

configFilePath := filepath.Join(home, ".kube", "config")

// check if file already there, make a backup
if _, err := os.Stat(configFilePath); err == nil {
copyFile(configFilePath, filepath.Join(home, ".kube", "copsctl_backup_config"))
}

err = ioutil.WriteFile(configFilePath, []byte(configYaml), 0600)
panicOnError(err)
}

func copyFile(sourceFile, destinationFile string) error {
in, err := os.Open(sourceFile)

if err != nil {
return fmt.Errorf("could not open file which was expected to exist: %v, error: %w", sourceFile, err)
}

defer in.Close()

out, err := os.Create(destinationFile)

if err != nil {
return fmt.Errorf("could not create the destination file: %v, error: %w", destinationFile, err)
}

defer out.Close()

_, err = io.Copy(out, in)

if err != nil {
return fmt.Errorf("problem copying the file from %v to %v, error: %w", sourceFile, destinationFile, err)
}

return out.Close()
}

type KubeConfigsContainerV1 struct {
Version string `yaml:"version"`
ModifiedAt string `yaml:"modifiedAt"`
Expand Down

0 comments on commit 5636955

Please sign in to comment.