Skip to content

Commit

Permalink
feat(sync): support sync config
Browse files Browse the repository at this point in the history
  • Loading branch information
joyme123 authored and Pengfei Jiang committed Mar 11, 2021
1 parent 7f53997 commit 0cfa842
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
6 changes: 3 additions & 3 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
72 changes: 72 additions & 0 deletions cmd/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
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")
}
6 changes: 3 additions & 3 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 47 additions & 6 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0cfa842

Please sign in to comment.