forked from maorfr/helm-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (152 loc) · 4.49 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
utils "github.com/maorfr/helm-plugin-utils/pkg"
helm_restore "github.com/maorfr/helm-restore/pkg"
"github.com/mholt/archiver"
"github.com/spf13/cobra"
)
var (
releaseName string
tillerNamespace string
label string
file string
restore bool
)
func main() {
cmd := &cobra.Command{
Use: "backup [flags] NAMESPACE",
Short: "backup/restore releases in a namespace to/from a file",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("namespace to backup has to be defined")
}
return nil
},
RunE: run,
}
f := cmd.Flags()
f.StringVar(&tillerNamespace, "tiller-namespace", "kube-system", "namespace of Tiller")
f.StringVarP(&label, "label", "l", "OWNER=TILLER", "label to select tiller resources by")
f.StringVar(&file, "file", "", "file name to use (.tgz file). If not provided - will use <namespace>.tgz")
f.BoolVarP(&restore, "restore", "r", false, "restore instead of backup")
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
func run(cmd *cobra.Command, args []string) error {
namespace := args[0]
if restore {
if err := Restore(namespace); err != nil {
return err
}
} else {
if err := Backup(namespace); err != nil {
return err
}
}
return nil
}
// Backup performs a backup of all releases from provided namespace
func Backup(namespace string) error {
log.Println("getting tiller storage")
storage := utils.GetTillerStorage(tillerNamespace)
log.Printf("found tiller storage: %s", storage)
log.Printf("getting releases in namespace \"%s\"", namespace)
inReleases, err := utils.ListReleaseNamesInNamespace(utils.ListReleaseNamesInNamespaceOptions{
Namespace: namespace,
TillerNamespace: tillerNamespace,
})
if err != nil {
return err
}
if len(inReleases) == 0 {
return fmt.Errorf("no releases found in namespace \"%s\"", namespace)
}
log.Printf("found relases: %s", prettyPrint(inReleases))
backupCmd := []string{
"kubectl",
"--namespace", tillerNamespace,
"get", storage,
"-l", label,
"-l", "NAME in (" + inReleases + ")",
"-o", "yaml",
}
log.Println("getting backup data")
output := utils.Execute(backupCmd)
log.Println("successfully got backup data")
manifestsFileName := "manifests.yaml"
releasesFileName := "releases"
tarGzName := getTarGzFileName(namespace)
os.Remove(manifestsFileName)
os.Remove(releasesFileName)
os.Remove(tarGzName)
log.Printf("writing backup to file %s", tarGzName)
if err := ioutil.WriteFile(manifestsFileName, output, 0644); err != nil {
return err
}
if err := ioutil.WriteFile(releasesFileName, []byte(inReleases), 0644); err != nil {
return err
}
err = archiver.TarGz.Make(tarGzName, []string{manifestsFileName, releasesFileName})
if err != nil {
return err
}
os.Remove(manifestsFileName)
os.Remove(releasesFileName)
log.Printf("backup of namespace \"%s\" to file %s complete\n", namespace, tarGzName)
return nil
}
// Restore performs a restore of all releases to provided namespace
func Restore(namespace string) error {
untarDir := "restore"
manifestsFileName := untarDir + "/manifests.yaml"
releasesFileName := untarDir + "/releases"
os.RemoveAll(untarDir)
tarGzName := getTarGzFileName(namespace)
log.Printf("extracting backup from file %s", tarGzName)
if err := archiver.TarGz.Open(tarGzName, untarDir); err != nil {
return err
}
log.Println("reading backup data")
releases, err := ioutil.ReadFile(releasesFileName)
if err != nil {
return err
}
restoreCmd := []string{
"kubectl",
"--namespace", tillerNamespace,
"apply", "-f", manifestsFileName,
}
releasesToRestore := (string)(releases)
log.Printf("releases found to restore: %s", prettyPrint(releasesToRestore))
log.Println("applying backup data to tiller (this command will fail if releases exist)")
output := utils.ExecuteCombined(restoreCmd)
log.Print((string)(output))
label += ",STATUS=DEPLOYED"
for _, r := range strings.Split((string)(releasesToRestore), ",") {
log.Printf("restoring release \"%s\"", r)
helm_restore.Restore(r, tillerNamespace, label)
}
os.RemoveAll(untarDir)
log.Printf("restore file %s to namespace \"%s\" complete", tarGzName, namespace)
return nil
}
func getTarGzFileName(namespace string) string {
tarGzName := file
if tarGzName == "" {
tarGzName = namespace
}
if !strings.HasSuffix(tarGzName, ".tgz") {
tarGzName = tarGzName + ".tgz"
}
return tarGzName
}
func prettyPrint(in string) string {
return strings.Replace(in, ",", ", ", -1)
}