From 0cfa842375df0431def14706b830faeb1e6c6d1b Mon Sep 17 00:00:00 2001 From: jiangpengfei Date: Thu, 11 Mar 2021 01:33:27 +0800 Subject: [PATCH] feat(sync): support sync config --- cmd/import.go | 2 +- cmd/rename.go | 6 ++-- cmd/sync.go | 72 ++++++++++++++++++++++++++++++++++++++++++ cmd/use.go | 6 ++-- pkg/manager/manager.go | 53 +++++++++++++++++++++++++++---- 5 files changed, 126 insertions(+), 13 deletions(-) create mode 100644 cmd/sync.go diff --git a/cmd/import.go b/cmd/import.go index f56e233..828cc9c 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -16,10 +16,10 @@ limitations under the License. package cmd import ( - "github.com/joyme123/kubecm/pkg/types" "log" "github.com/joyme123/kubecm/pkg/loader" + "github.com/joyme123/kubecm/pkg/types" "github.com/spf13/cobra" ) diff --git a/cmd/rename.go b/cmd/rename.go index cd37b9b..478de30 100644 --- a/cmd/rename.go +++ b/cmd/rename.go @@ -31,9 +31,9 @@ var renameOpt renameOptions // renameCmd represents the rename command var renameCmd = &cobra.Command{ Use: "rename", - Short: "rename set config name", - Long: `rename set config name`, - Args: cobra.MinimumNArgs(2), + Short: "rename config name", + Long: `rename config name`, + Args: cobra.MinimumNArgs(2), Run: func(cmd *cobra.Command, args []string) { renameOpt.name = args[0] renameOpt.to = args[1] diff --git a/cmd/sync.go b/cmd/sync.go new file mode 100644 index 0000000..061b830 --- /dev/null +++ b/cmd/sync.go @@ -0,0 +1,72 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "github.com/spf13/cobra" + "log" +) + +var syncOpt syncOptions + +type syncOptions struct { + name string +} + +// syncCmd represents the sync command +var syncCmd = &cobra.Command{ + Use: "sync", + Short: "sync refresh config from ssh", + Long: `sync refresh config from ssh. For example: +kubecm sync k8s_cluster_1 +`, + Run: func(cmd *cobra.Command, args []string) { + opt := syncOptions{} + if len(args) > 0 { + opt.name = args[0] + } + sync(opt) + }, +} + +func sync(opt syncOptions) { + m, err := newManagerInterface() + if err != nil { + log.Fatalf("fatal: %v", err) + } + res := m.Sync(opt.name) + for name, err := range res { + msg := "ok" + if err != nil { + msg = err.Error() + } + log.Printf("%s: sync %s", name, msg) + } +} + +func init() { + rootCmd.AddCommand(syncCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // syncCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // syncCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/use.go b/cmd/use.go index 3da115a..c47772b 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -30,9 +30,9 @@ var useOpt useOptions // useCmd represents the use command var useCmd = &cobra.Command{ Use: "use", - Short: "use set kube config file to specified one", - Long: `use set kube config file to specified one`, - Args: cobra.MinimumNArgs(1), + Short: "set kube config file to specified one", + Long: `set kube config file to specified one`, + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { useOpt.name = args[0] use(useOpt) diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index e8a7a2b..f6b9a5b 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -3,6 +3,7 @@ package manager import ( "fmt" "github.com/google/uuid" + "github.com/joyme123/kubecm/pkg/loader" "github.com/joyme123/kubecm/pkg/types" "io/ioutil" "os" @@ -22,6 +23,7 @@ type Interface interface { Rename(src string, dst string) error Use(name string) error SaveSyncInfo(name string, syncInfo *types.Sync) error + Sync(name string) map[string]error } type impl struct { @@ -85,7 +87,11 @@ func (i *impl) Import(name string, configData []byte, override bool) error { if !override && index >= 0 { return NameConflictError } + return i.importByIndex(index, name, configData) +} +// if index < 0, config will append +func (i *impl) importByIndex(index int, name string, configData []byte) error { var newLocation string if index >= 0 { newLocation = i.conf.Items[index].Location @@ -97,14 +103,14 @@ func (i *impl) Import(name string, configData []byte, override bool) error { return err } - item := types.ConfigItem{ - Name: name, - Location: newLocation, - TimeStamp: time.Now(), - } if index >= 0 { - i.conf.Items[index] = item + i.conf.Items[index].TimeStamp = time.Now() } else { + item := types.ConfigItem{ + Name: name, + Location: newLocation, + TimeStamp: time.Now(), + } i.conf.Items = append(i.conf.Items, item) } return i.write() @@ -200,6 +206,41 @@ func (i *impl) SaveSyncInfo(name string, syncInfo *types.Sync) error { return i.write() } +// Sync sync target config. if name is empty, sync all +func (i *impl) Sync(name string) map[string]error { + syncFunc := func(index int, name string, info *types.Sync) error { + if info == nil { + return nil + } + data, err := loader.Load(info) + if err != nil { + return err + } + return i.importByIndex(index, name, data) + } + + res := map[string]error{} + + if len(name) == 0 { + for index, item := range i.conf.Items { + if item.Sync == nil { + continue + } + res[item.Name] = syncFunc(index, item.Name, item.Sync) + } + return res + } + + index := i.search(name) + if index < 0 { + res[name] = fmt.Errorf("target name not exist") + return res + } + item := i.conf.Items[index] + res[name] = syncFunc(index, item.Name, item.Sync) + return res +} + func (i *impl) search(name string) int { for k, item := range i.conf.Items { if item.Name == name {