@@ -2,76 +2,51 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
- "log"
6
- "os"
7
- "strings"
8
5
"time"
9
6
10
7
"github.com/spf13/cobra"
11
8
"github.com/spf13/viper"
12
- "k8s.io/client-go/util/homedir"
13
9
14
10
"github.com/bpineau/katafygio/config"
15
- klog "github.com/bpineau/katafygio/pkg/log"
11
+ "github.com/bpineau/katafygio/pkg/log"
16
12
"github.com/bpineau/katafygio/pkg/run"
17
13
)
18
14
19
15
const appName = "katafygio"
20
16
21
17
var (
22
- version = "0.3.0"
23
-
24
- cfgFile string
25
- apiServer string
26
- kubeConf string
27
- dryRun bool
28
- dumpMode bool
29
- logLevel string
30
- logOutput string
31
- logServer string
32
- filter string
33
- localDir string
34
- gitURL string
35
- healthP int
36
- resync int
37
- exclkind []string
38
- exclobj []string
39
-
40
- versionCmd = &cobra.Command{
41
- Use: "version",
42
- Short: "Print the version number",
43
- Run: func(cmd *cobra.Command, args []string) {
44
- RootCmd.Printf("%s version %s\n", appName, version)
45
- },
46
- }
47
-
48
- // RootCmd represents the base command when called without any subcommands
18
+ // RootCmd is our main entry point, launching pkg/run.Run()
49
19
RootCmd = &cobra.Command{
50
20
Use: appName,
51
21
Short: "Backup Kubernetes cluster as yaml files",
52
22
Long: "Backup Kubernetes cluster as yaml files in a git repository.\n" +
53
- "--exclude-kind (x) and --exclude-object (-y) may be specified several times.",
23
+ "--exclude-kind (- x) and --exclude-object (-y) may be specified several times.",
54
24
55
25
RunE: func(cmd *cobra.Command, args []string) error {
26
+ resync := time.Duration(viper.GetInt("resync-interval")) * time.Second
27
+ logger := log.New(viper.GetString("log.level"),
28
+ viper.GetString("log.server"),
29
+ viper.GetString("log.output"))
30
+
56
31
conf := &config.KfConfig{
57
32
DryRun: viper.GetBool("dry-run"),
58
33
DumpMode: viper.GetBool("dump-only"),
59
- Logger: klog.New(viper.GetString("log.level"), viper.GetString("log.server"), viper.GetString("log.output")) ,
34
+ Logger: logger ,
60
35
LocalDir: viper.GetString("local-dir"),
61
36
GitURL: viper.GetString("git-url"),
62
37
Filter: viper.GetString("filter"),
63
38
ExcludeKind: viper.GetStringSlice("exclude-kind"),
64
39
ExcludeObject: viper.GetStringSlice("exclude-object"),
65
40
HealthPort: viper.GetInt("healthcheck-port"),
66
- ResyncIntv: time.Duration(viper.GetInt(" resync-interval")) * time.Second ,
41
+ ResyncIntv: resync,
67
42
}
68
43
69
44
err := conf.Init(viper.GetString("api-server"), viper.GetString("kube-config"))
70
45
if err != nil {
71
46
return fmt.Errorf("Failed to initialize the configuration: %v", err)
72
47
}
73
48
74
- run.Run(conf)
49
+ run.Run(conf) // <- this is where things happens
75
50
return nil
76
51
},
77
52
}
81
56
func Execute() error {
82
57
return RootCmd.Execute()
83
58
}
84
-
85
- func bindPFlag(key string, cmd string) {
86
- if err := viper.BindPFlag(key, RootCmd.PersistentFlags().Lookup(cmd)); err != nil {
87
- log.Fatal("Failed to bind cli argument:", err)
88
- }
89
- }
90
-
91
- func init() {
92
- cobra.OnInitialize(initConfig)
93
- RootCmd.AddCommand(versionCmd)
94
-
95
- defaultCfg := "/etc/katafygio/" + appName + ".yaml"
96
- RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfg, "Configuration file")
97
-
98
- RootCmd.PersistentFlags().StringVarP(&apiServer, "api-server", "s", "", "Kubernetes api-server url")
99
- bindPFlag("api-server", "api-server")
100
-
101
- RootCmd.PersistentFlags().StringVarP(&kubeConf, "kube-config", "k", "", "Kubernetes config path")
102
- bindPFlag("kube-config", "kube-config")
103
- if err := viper.BindEnv("kube-config", "KUBECONFIG"); err != nil {
104
- log.Fatal("Failed to bind cli argument:", err)
105
- }
106
-
107
- RootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "Dry-run mode: don't store anything")
108
- bindPFlag("dry-run", "dry-run")
109
-
110
- RootCmd.PersistentFlags().BoolVarP(&dumpMode, "dump-only", "m", false, "Dump mode: dump everything once and exit")
111
- bindPFlag("dump-only", "dump-only")
112
-
113
- RootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "v", "info", "Log level")
114
- bindPFlag("log.level", "log-level")
115
-
116
- RootCmd.PersistentFlags().StringVarP(&logOutput, "log-output", "o", "stderr", "Log output")
117
- bindPFlag("log.output", "log-output")
118
-
119
- RootCmd.PersistentFlags().StringVarP(&logServer, "log-server", "r", "", "Log server (if using syslog)")
120
- bindPFlag("log.server", "log-server")
121
-
122
- RootCmd.PersistentFlags().StringVarP(&localDir, "local-dir", "e", "./kubernetes-backup", "Where to dump yaml files")
123
- bindPFlag("local-dir", "local-dir")
124
-
125
- RootCmd.PersistentFlags().StringVarP(&gitURL, "git-url", "g", "", "Git repository URL")
126
- bindPFlag("git-url", "git-url")
127
-
128
- RootCmd.PersistentFlags().StringSliceVarP(&exclkind, "exclude-kind", "x", nil, "Ressource kind to exclude. Eg. 'deployment'")
129
- bindPFlag("exclude-kind", "exclude-kind")
130
-
131
- RootCmd.PersistentFlags().StringSliceVarP(&exclobj, "exclude-object", "y", nil, "Object to exclude. Eg. 'configmap:kube-system/kube-dns'")
132
- bindPFlag("exclude-object", "exclude-object")
133
-
134
- RootCmd.PersistentFlags().StringVarP(&filter, "filter", "l", "", "Label filter. Select only objects matching the label.")
135
- bindPFlag("filter", "filter")
136
-
137
- RootCmd.PersistentFlags().IntVarP(&healthP, "healthcheck-port", "p", 0, "Port for answering healthchecks on /health url")
138
- bindPFlag("healthcheck-port", "healthcheck-port")
139
-
140
- RootCmd.PersistentFlags().IntVarP(&resync, "resync-interval", "i", 900, "Full resync interval in seconds (0 to disable)")
141
- bindPFlag("resync-interval", "resync-interval")
142
- }
143
-
144
- func initConfig() {
145
- viper.SetConfigType("yaml")
146
- viper.SetConfigName(appName)
147
-
148
- // all possible config file paths, by priority
149
- viper.AddConfigPath("/etc/katafygio/")
150
- if home := homedir.HomeDir(); home != "" {
151
- viper.AddConfigPath(home)
152
- }
153
- viper.AddConfigPath(".")
154
-
155
- // prefer the config file path provided by cli flag, if any
156
- if _, err := os.Stat(cfgFile); !os.IsNotExist(err) {
157
- viper.SetConfigFile(cfgFile)
158
- }
159
-
160
- // allow config params through prefixed env variables
161
- viper.SetEnvPrefix("KF")
162
- replacer := strings.NewReplacer("-", "_", ".", "_DOT_")
163
- viper.SetEnvKeyReplacer(replacer)
164
- viper.AutomaticEnv()
165
-
166
- if err := viper.ReadInConfig(); err == nil {
167
- RootCmd.Printf("Using config file: %s", viper.ConfigFileUsed())
168
- }
169
- }
0 commit comments