Skip to content

Commit 1db84d6

Browse files
committed
Implement dump-only mode
And by the way, waiting for the informer's caches to be fully dumped before stopping controllers is much saner.
1 parent 3dda689 commit 1db84d6

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

README.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@ This provides real time, continuous backups, and keeps detailled changes history
1010

1111
## Usage
1212

13+
Just dump the cluster's content and exit:
1314
```bash
14-
katafygio --git-url https://user:[email protected]/myorg/myrepos.git --local-dir /tmp/kfdump
15+
katafygio --dump-only --local-dir /tmp/clusterdump/
16+
```
17+
18+
Create a local git repository in /tmp/kfdump and continuously save the cluster content:
19+
```bash
20+
katafygio --local-dir /tmp/kfdump
1521
```
1622

17-
The remote git url is optional: if not specified, katafygio will create a local
18-
repository in the target dump directory. katafygio may be used that way to
19-
quickly dump a cluster's content.
23+
Same, but also continously push the local repository to a remote:
24+
```bash
25+
katafygio --git-url https://user:[email protected]/myorg/myrepos.git --local-dir /tmp/kfdump
26+
```
2027

21-
Filtering out irrelevant objects (esp. ReplicaSets and Pods) with `-x` and `-y`
22-
will help to keep memory usage low. Same for low value and frequently changing
23-
objects, that may bloat the git history.
28+
Filtering out irrelevant objects (esp. ReplicaSets and Pods) with `-x` or `-y`
29+
will help to keep resources usage low and git history concise.
2430

2531

2632
```bash
27-
# Filtering out replicasets and pods since they are managed by Deployments
33+
# Filtering out replicasets and pods since they are generated by Deployments
2834
# (already archived), endpoints (managed by Services), secrets (to keep them
2935
# confidential), events and node (irrelevant), and the leader-elector
3036
# configmap that has low value and changes a lot, causing commits churn.
@@ -55,6 +61,7 @@ Flags:
5561
-s, --api-server string Kubernetes api-server url
5662
-c, --config string Configuration file (default "/etc/katafygio/katafygio.yaml")
5763
-d, --dry-run Dry-run mode: don't store anything.
64+
-m, --dump-only Dump mode: dump everything and exit
5865
-x, --exclude-kind stringSlice Ressource kind to exclude. Eg. 'deployment'
5966
-y, --exclude-object stringSlice Object to exclude. Eg. 'configmap:kube-system/kube-dns'
6067
-l, --filter string Label filter. Select only objects matching the label.

cmd/execute.go

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
apiServer string
2626
kubeConf string
2727
dryRun bool
28+
dumpMode bool
2829
logLevel string
2930
logOutput string
3031
logServer string
@@ -54,6 +55,7 @@ var (
5455
RunE: func(cmd *cobra.Command, args []string) error {
5556
conf := &config.KfConfig{
5657
DryRun: viper.GetBool("dry-run"),
58+
DumpMode: viper.GetBool("dump-only"),
5759
Logger: klog.New(viper.GetString("log.level"), viper.GetString("log.server"), viper.GetString("log.output")),
5860
LocalDir: viper.GetString("local-dir"),
5961
GitURL: viper.GetString("git-url"),
@@ -105,6 +107,9 @@ func init() {
105107
RootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "Dry-run mode: don't store anything.")
106108
bindPFlag("dry-run", "dry-run")
107109

110+
RootCmd.PersistentFlags().BoolVarP(&dumpMode, "dump-only", "m", false, "Dump mode: dump everything and exit")
111+
bindPFlag("dump-only", "dump-only")
112+
108113
RootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "v", "info", "Log level")
109114
bindPFlag("log.level", "log-level")
110115

config/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type KfConfig struct {
1515
// When DryRun is true, we don't write to disk and we don't commit/push
1616
DryRun bool
1717

18+
// When DumpMode is true, we just dump everything once and exit
19+
DumpMode bool
20+
1821
// Logger should be used to send all logs
1922
Logger *logrus.Logger
2023

pkg/controller/controller.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import (
2525
"github.com/ghodss/yaml"
2626
)
2727

28-
const maxProcessRetry = 6
28+
var (
29+
maxProcessRetry = 6
30+
canaryKey = "$katafygio-canary"
31+
)
2932

3033
// Interface describe a standard kubernetes controller
3134
type Interface interface {
@@ -41,6 +44,7 @@ type Controller struct {
4144
name string
4245
stopCh chan struct{}
4346
doneCh chan struct{}
47+
syncCh chan struct{}
4448
notifier event.Notifier
4549
config *config.KfConfig
4650
queue workqueue.RateLimitingInterface
@@ -93,6 +97,7 @@ func New(client cache.ListerWatcher, notifier event.Notifier, name string, confi
9397
return &Controller{
9498
stopCh: make(chan struct{}),
9599
doneCh: make(chan struct{}),
100+
syncCh: make(chan struct{}, 1),
96101
notifier: notifier,
97102
name: name,
98103
config: config,
@@ -113,15 +118,18 @@ func (c *Controller) Start() {
113118
return
114119
}
115120

121+
c.queue.Add(canaryKey)
122+
116123
go wait.Until(c.runWorker, time.Second, c.stopCh)
117124
}
118125

119126
// Stop halts the controller
120127
func (c *Controller) Stop() {
128+
c.config.Logger.Infof("Stopping %s controller", c.name)
129+
<-c.syncCh
121130
close(c.stopCh)
122131
c.queue.ShutDown()
123132
<-c.doneCh
124-
c.config.Logger.Infof("Stopping %s controller", c.name)
125133
}
126134

127135
func (c *Controller) runWorker() {
@@ -138,6 +146,13 @@ func (c *Controller) processNextItem() bool {
138146
}
139147
defer c.queue.Done(key)
140148

149+
if strings.Compare(key.(string), canaryKey) == 0 {
150+
c.config.Logger.Infof("Initial sync completed for %s controller", c.name)
151+
c.syncCh <- struct{}{}
152+
c.queue.Forget(key)
153+
return true
154+
}
155+
141156
err := c.processItem(key.(string))
142157

143158
if err == nil {

pkg/run/run.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func Run(config *config.KfConfig) {
3131
sigterm := make(chan os.Signal, 1)
3232
signal.Notify(sigterm, syscall.SIGTERM)
3333
signal.Notify(sigterm, syscall.SIGINT)
34-
<-sigterm
34+
if !config.DumpMode {
35+
<-sigterm
36+
}
3537

3638
obsv.Stop()
3739
repo.Stop()

0 commit comments

Comments
 (0)