Skip to content

Commit afaaef2

Browse files
committed
Get rid of run
No need for an intermediary, this should be easier for new readers. Also paves, the way for the removal of KfConfig (which in turns, will help for the pkg-ification of pkgs/).
1 parent 11c1792 commit afaaef2

File tree

3 files changed

+83
-79
lines changed

3 files changed

+83
-79
lines changed

cmd/execute.go

+63-35
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,88 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
6+
"os/signal"
7+
"syscall"
58
"time"
69

710
"github.com/spf13/cobra"
8-
"github.com/spf13/viper"
911

1012
"github.com/bpineau/katafygio/config"
1113
"github.com/bpineau/katafygio/pkg/client"
14+
"github.com/bpineau/katafygio/pkg/controller"
15+
"github.com/bpineau/katafygio/pkg/event"
16+
"github.com/bpineau/katafygio/pkg/health"
1217
"github.com/bpineau/katafygio/pkg/log"
13-
"github.com/bpineau/katafygio/pkg/run"
18+
"github.com/bpineau/katafygio/pkg/observer"
19+
"github.com/bpineau/katafygio/pkg/recorder"
20+
"github.com/bpineau/katafygio/pkg/store/git"
1421
)
1522

1623
const appName = "katafygio"
1724

25+
func runE(cmd *cobra.Command, args []string) (err error) {
26+
27+
resync := time.Duration(resyncInt) * time.Second
28+
logger := log.New(logLevel, logServer, logOutput)
29+
30+
if restcfg == nil {
31+
restcfg, err = client.New(apiServer, kubeConf)
32+
if err != nil {
33+
return fmt.Errorf("failed to create a client: %v", err)
34+
}
35+
}
36+
37+
conf := &config.KfConfig{
38+
DryRun: dryRun,
39+
DumpMode: dumpMode,
40+
Logger: logger,
41+
LocalDir: localDir,
42+
GitURL: gitURL,
43+
Filter: filter,
44+
ExcludeKind: exclkind,
45+
ExcludeObject: exclobj,
46+
HealthPort: healthP,
47+
Client: restcfg,
48+
ResyncIntv: resync,
49+
}
50+
51+
repo, err := git.New(conf).Start()
52+
if err != nil {
53+
conf.Logger.Fatalf("failed to start git repo handler: %v", err)
54+
}
55+
56+
evts := event.New()
57+
reco := recorder.New(conf, evts).Start()
58+
obsv := observer.New(conf, evts, &controller.Factory{}).Start()
59+
http := health.New(conf).Start()
60+
61+
sigterm := make(chan os.Signal, 1)
62+
signal.Notify(sigterm, syscall.SIGTERM)
63+
signal.Notify(sigterm, syscall.SIGINT)
64+
if !conf.DumpMode {
65+
<-sigterm
66+
}
67+
68+
obsv.Stop()
69+
repo.Stop()
70+
reco.Stop()
71+
http.Stop()
72+
73+
return nil
74+
}
75+
1876
var (
1977
restcfg client.Interface
2078

21-
// RootCmd is our main entry point, launching pkg/run.Run()
79+
// RootCmd is our main entry point, launching runE()
2280
RootCmd = &cobra.Command{
2381
Use: appName,
2482
Short: "Backup Kubernetes cluster as yaml files",
2583
Long: "Backup Kubernetes cluster as yaml files in a git repository.\n" +
2684
"--exclude-kind (-x) and --exclude-object (-y) may be specified several times.",
27-
28-
RunE: func(cmd *cobra.Command, args []string) (err error) {
29-
resync := time.Duration(viper.GetInt("resync-interval")) * time.Second
30-
logger := log.New(viper.GetString("log.level"),
31-
viper.GetString("log.server"),
32-
viper.GetString("log.output"))
33-
34-
if restcfg == nil {
35-
restcfg, err = client.New(viper.GetString("api-server"),
36-
viper.GetString("kube-config"))
37-
if err != nil {
38-
return fmt.Errorf("failed to create a client: %v", err)
39-
}
40-
}
41-
42-
conf := &config.KfConfig{
43-
DryRun: viper.GetBool("dry-run"),
44-
DumpMode: viper.GetBool("dump-only"),
45-
Logger: logger,
46-
LocalDir: viper.GetString("local-dir"),
47-
GitURL: viper.GetString("git-url"),
48-
Filter: viper.GetString("filter"),
49-
ExcludeKind: viper.GetStringSlice("exclude-kind"),
50-
ExcludeObject: viper.GetStringSlice("exclude-object"),
51-
HealthPort: viper.GetInt("healthcheck-port"),
52-
Client: restcfg,
53-
ResyncIntv: resync,
54-
}
55-
56-
run.Run(conf) // <- this is where things happen
57-
return nil
58-
},
85+
PreRun: bindConf,
86+
RunE: runE,
5987
}
6088
)
6189

cmd/flags.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
localDir string
2121
gitURL string
2222
healthP int
23-
resync int
23+
resyncInt int
2424
exclkind []string
2525
exclobj []string
2626
)
@@ -80,6 +80,24 @@ func init() {
8080
RootCmd.PersistentFlags().IntVarP(&healthP, "healthcheck-port", "p", 0, "Port for answering healthchecks on /health url")
8181
bindPFlag("healthcheck-port", "healthcheck-port")
8282

83-
RootCmd.PersistentFlags().IntVarP(&resync, "resync-interval", "i", 900, "Full resync interval in seconds (0 to disable)")
83+
RootCmd.PersistentFlags().IntVarP(&resyncInt, "resync-interval", "i", 900, "Full resync interval in seconds (0 to disable)")
8484
bindPFlag("resync-interval", "resync-interval")
8585
}
86+
87+
// for whatever the reason, viper don't auto bind values from config file so we have to tell him
88+
func bindConf(cmd *cobra.Command, args []string) {
89+
apiServer = viper.GetString("api-server")
90+
kubeConf = viper.GetString("kube-config")
91+
dryRun = viper.GetBool("dry-run")
92+
dumpMode = viper.GetBool("dump-only")
93+
logLevel = viper.GetString("log.level")
94+
logOutput = viper.GetString("log.output")
95+
logServer = viper.GetString("log.server")
96+
filter = viper.GetString("filter")
97+
localDir = viper.GetString("local-dir")
98+
gitURL = viper.GetString("git-url")
99+
healthP = viper.GetInt("healthcheck-port")
100+
resyncInt = viper.GetInt("resync-interval")
101+
exclkind = viper.GetStringSlice("exclude-kind")
102+
exclobj = viper.GetStringSlice("exclude-object")
103+
}

pkg/run/run.go

-42
This file was deleted.

0 commit comments

Comments
 (0)