@@ -2,60 +2,88 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
+ "os"
6
+ "os/signal"
7
+ "syscall"
5
8
"time"
6
9
7
10
"github.com/spf13/cobra"
8
- "github.com/spf13/viper"
9
11
10
12
"github.com/bpineau/katafygio/config"
11
13
"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"
12
17
"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"
14
21
)
15
22
16
23
const appName = "katafygio"
17
24
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
+
18
76
var (
19
77
restcfg client.Interface
20
78
21
- // RootCmd is our main entry point, launching pkg/run.Run ()
79
+ // RootCmd is our main entry point, launching runE ()
22
80
RootCmd = & cobra.Command {
23
81
Use : appName ,
24
82
Short : "Backup Kubernetes cluster as yaml files" ,
25
83
Long : "Backup Kubernetes cluster as yaml files in a git repository.\n " +
26
84
"--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 ,
59
87
}
60
88
)
61
89
0 commit comments