Skip to content

Commit

Permalink
feat(save-load): support save and load kubeconfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
joyme123 authored and Pengfei Jiang committed Mar 14, 2023
1 parent a759ece commit 05278e7
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"log"

"github.com/spf13/cobra"
)

var loadOpt saveOption

type loadOption struct {
filePath string
}

// loadCmd represents the load command
var loadCmd = &cobra.Command{
Use: "load",
Short: "load kubeconfigs from saved file",
Long: `load kubeconfigs from saved file`,
Run: func(cmd *cobra.Command, args []string) {
m, err := newManagerInterface()
if err != nil {
log.Fatalf("fatal: %v", err)
}
err = m.Load(loadOpt.filePath)
if err != nil {
log.Fatalf("load failed: %v", err)
}
},
}

func init() {
rootCmd.AddCommand(loadCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// loadCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
loadCmd.Flags().StringVarP(&loadOpt.filePath, "file-path", "f", "", "load all kubeconfigs from file path")
}
47 changes: 47 additions & 0 deletions cmd/save.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"log"

"github.com/spf13/cobra"
)

var saveOpt saveOption

type saveOption struct {
filePath string
}

// saveCmd represents the save command
var saveCmd = &cobra.Command{
Use: "save",
Short: "save all kubeconfigs into a single file",
Long: `you can use save command to export all kubeconfigs into a single file. And also you can use load command to import all of these kubeconfigs`,
Run: func(cmd *cobra.Command, args []string) {
m, err := newManagerInterface()
if err != nil {
log.Fatalf("fatal: %v", err)
}
err = m.Save(saveOpt.filePath)
if err != nil {
log.Fatalf("save failed: %v", err)
}
},
}

func init() {
rootCmd.AddCommand(saveCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// saveCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
saveCmd.Flags().StringVarP(&saveOpt.filePath, "file-path", "f", "", "saved file path for all kubeconfigs")
}
81 changes: 81 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Interface interface {
Use(name string, currentSession bool) error
SaveSyncInfo(name string, syncInfo *types.Sync) error
Sync(name string) map[string]error
Save(filePath string) error
Load(filePath string) error
}

type impl struct {
Expand Down Expand Up @@ -262,6 +264,85 @@ func (i *impl) Sync(name string) map[string]error {
return res
}

func (i *impl) Save(filePath string) error {
_, err := os.Stat(filePath)
if err == nil {
return fmt.Errorf("file %s is already exist", filePath)
}

if !os.IsNotExist(err) {
return err
}

var savedConfig types.SavedConfig
savedConfig.Current = i.conf.Current

for _, item := range i.conf.Items {
data, err := ioutil.ReadFile(item.Location)
if err != nil {
fmt.Printf("kubeconfig %s is missing on location: %s", item.Name, item.Location)
continue
}
savedConfig.Items = append(savedConfig.Items, types.SavedConfigItem{
Name: item.Name,
Content: string(data),
})
}

f, err := os.Create(filePath)
if err != nil {
return err
}

confData, err := yaml.Marshal(savedConfig)
if err != nil {
return err
}
_, err = f.Write(confData)
if err != nil {
return err
}

return nil
}

func (i *impl) Load(filePath string) error {
confData, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}

var savedConf types.SavedConfig
if err := yaml.Unmarshal(confData, &savedConf); err != nil {
return err
}

// merge with exist configurations
exists := make(map[string]struct{})
for _, item := range i.conf.Items {
exists[item.Name] = struct{}{}
}

for _, item := range savedConf.Items {
if _, ok := exists[item.Name]; ok {
fmt.Printf("%s exists, skip\n", item.Name)
continue
}
if err := i.Import(item.Name, []byte(item.Content), false, ""); err != nil {
fmt.Printf("import %s failed, err: %v\n", item.Name, err)
continue
} else {
fmt.Printf("import %s completed\n", item.Name)
}
}

if i.conf.Current == "" {
i.conf.Current = savedConf.Current
}

return nil
}

func (i *impl) search(name string) int {
for k, item := range i.conf.Items {
if item.Name == name {
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions pkg/types/saved_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package types

// SavedConfig ...
type SavedConfig struct {
Current string `json:"current" yaml:"current"`
Items []SavedConfigItem `json:"items" yaml:"items"`
}

// SavedConfigItem ...
type SavedConfigItem struct {
Name string `json:"name" yaml:"name"`
Content string `json:"content" yaml:"content"`
}

0 comments on commit 05278e7

Please sign in to comment.