-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
61 lines (52 loc) · 1.32 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/spf13/cobra"
)
var (
rootCommand = &cobra.Command{
Use: "etcd-k8s-extract",
Short: "etcd-k8s-extract reads an etcd db and writes all the kubernetes resources to file.",
Run: etcdKubernetesExtract,
}
)
var (
flockTimeout time.Duration
iterateBucketLimit uint64
outputPath string
)
func init() {
rootCommand.PersistentFlags().DurationVar(&flockTimeout, "timeout", 10*time.Second, "time to wait to obtain a file lock on db file, 0 to block indefinitely")
rootCommand.PersistentFlags().StringVar(&outputPath, "output-path", "", "path where the kuberegistry will be written")
}
func main() {
if err := rootCommand.Execute(); err != nil {
fmt.Fprintln(os.Stdout, err)
os.Exit(1)
}
}
func etcdKubernetesExtract(_ *cobra.Command, args []string) {
if len(args) != 1 {
log.Fatalf("Must provide 1 argument pointing at the etcd data directory or db file (got %d)", len(args))
}
dp := args[0]
if !strings.HasSuffix(dp, "db") {
dp = filepath.Join(snapDir(dp), "db")
}
if !existFileOrDir(dp) {
log.Fatalf("%q does not exist", dp)
}
err := iterateEtcdKeys(dp, outputPath, iterateBucketLimit)
if err != nil {
log.Fatal(err)
}
}
func existFileOrDir(name string) bool {
_, err := os.Stat(name)
return err == nil
}