-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
112 lines (106 loc) · 2.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/urfave/cli/v2"
"github.com/viveksyngh/kube-sync/cmd"
"github.com/viveksyngh/kube-sync/pkg/syncer"
"github.com/viveksyngh/kube-sync/pkg/version"
"k8s.io/client-go/util/homedir"
)
var (
kubeconfig *string
namespace *string
targetNamespace *string
)
func defaultKubeconfig() string {
if home := homedir.HomeDir(); home != "" {
return filepath.Join(home, ".kube", "config")
}
return ""
}
func main() {
app := &cli.App{
Name: "kube-sync",
Usage: "A CLI to sync Kubernetes resources",
Description: "A CLI to sync resources in a Kubernetes cluster",
HelpName: "kube-sync",
Commands: []*cli.Command{
{
Name: "configmap",
Aliases: []string{"cm"},
ArgsUsage: "NAME",
Usage: "Copy a ConfigMap from one namespace to another",
Action: func(c *cli.Context) error {
err := cmd.Validate(c)
if err != nil {
return err
}
client, err := cmd.CreateClientset(c.String("kubeconfig"))
if err != nil {
return err
}
cs := &syncer.ConfigMapSyncer{}
return cs.Sync(client, c.Args().First(), c.String("namespace"), c.String("target-namespace"))
},
},
{
Name: "secret",
Aliases: []string{"sec"},
ArgsUsage: "NAME",
Usage: "Copy a secret from one namespace to another",
Action: func(c *cli.Context) error {
err := cmd.Validate(c)
if err != nil {
return err
}
client, err := cmd.CreateClientset(c.String("kubeconfig"))
if err != nil {
return err
}
cs := &syncer.SecretSyncer{}
return cs.Sync(client, c.Args().First(), c.String("namespace"), c.String("target-namespace"))
},
},
{
Name: "version",
Aliases: []string{"v"},
Usage: "print version",
Action: func(c *cli.Context) error {
fmt.Printf("Version: %s\n", version.Version)
fmt.Printf("SHA: %s\n", version.GitCommit)
return nil
},
},
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "kubeconfig",
Usage: "path to the kubeconfig file to use for CLI requests",
EnvVars: []string{"KUBECONFIG"},
Destination: kubeconfig,
Value: defaultKubeconfig(),
},
&cli.StringFlag{
Name: "namespace",
Aliases: []string{"n"},
Value: "default",
Usage: "namespace of the source resource",
Destination: namespace,
},
&cli.StringFlag{
Name: "target-namespace",
Aliases: []string{"tn"},
Value: "default",
Usage: "target namespace where resource will be copied",
Destination: targetNamespace,
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}