@@ -19,10 +19,7 @@ package pkg
19
19
import (
20
20
"context"
21
21
"fmt"
22
- "io/ioutil"
23
- "os"
24
22
"path/filepath"
25
- "strings"
26
23
"time"
27
24
28
25
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
@@ -33,7 +30,6 @@ import (
33
30
"github.com/spf13/cobra"
34
31
license "go.bytebuilders.dev/license-verifier/kubernetes"
35
32
"gomodules.xyz/flags"
36
- "gomodules.xyz/go-sh"
37
33
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38
34
"k8s.io/client-go/kubernetes"
39
35
"k8s.io/client-go/tools/clientcmd"
@@ -64,7 +60,7 @@ func NewCmdBackup() *cobra.Command {
64
60
Short : "Takes a backup of Elasticsearch DB" ,
65
61
DisableAutoGenTag : true ,
66
62
RunE : func (cmd * cobra.Command , args []string ) error {
67
- flags .EnsureRequiredFlags (cmd , "appbinding" , "provider" , "secret-dir " )
63
+ flags .EnsureRequiredFlags (cmd , "appbinding" , "provider" , "storage- secret-name" , "storage-secret-namespace " )
68
64
time .Sleep (time .Second * 5 )
69
65
70
66
// prepare client
@@ -126,13 +122,14 @@ func NewCmdBackup() *cobra.Command {
126
122
cmd .Flags ().StringVar (& opt .namespace , "namespace" , "default" , "Namespace of Backup/Restore Session" )
127
123
cmd .Flags ().StringVar (& opt .backupSessionName , "backupsession" , opt .backupSessionName , "Name of the Backup Session" )
128
124
cmd .Flags ().StringVar (& opt .appBindingName , "appbinding" , opt .appBindingName , "Name of the app binding" )
125
+ cmd .Flags ().StringVar (& opt .storageSecret .Name , "storage-secret-name" , opt .storageSecret .Name , "Name of the storage secret" )
126
+ cmd .Flags ().StringVar (& opt .storageSecret .Namespace , "storage-secret-namespace" , opt .storageSecret .Namespace , "Namespace of the storage secret" )
129
127
130
128
cmd .Flags ().StringVar (& opt .setupOptions .Provider , "provider" , opt .setupOptions .Provider , "Backend provider (i.e. gcs, s3, azure etc)" )
131
129
cmd .Flags ().StringVar (& opt .setupOptions .Bucket , "bucket" , opt .setupOptions .Bucket , "Name of the cloud bucket/container (keep empty for local backend)" )
132
130
cmd .Flags ().StringVar (& opt .setupOptions .Endpoint , "endpoint" , opt .setupOptions .Endpoint , "Endpoint for s3/s3 compatible backend or REST server URL" )
133
131
cmd .Flags ().StringVar (& opt .setupOptions .Region , "region" , opt .setupOptions .Region , "Region for s3/s3 compatible backend" )
134
132
cmd .Flags ().StringVar (& opt .setupOptions .Path , "path" , opt .setupOptions .Path , "Directory inside the bucket where backup will be stored" )
135
- cmd .Flags ().StringVar (& opt .setupOptions .SecretDir , "secret-dir" , opt .setupOptions .SecretDir , "Directory where storage secret has been mounted" )
136
133
cmd .Flags ().StringVar (& opt .setupOptions .ScratchDir , "scratch-dir" , opt .setupOptions .ScratchDir , "Temporary directory" )
137
134
cmd .Flags ().BoolVar (& opt .setupOptions .EnableCache , "enable-cache" , opt .setupOptions .EnableCache , "Specify whether to enable caching for restic" )
138
135
cmd .Flags ().Int64Var (& opt .setupOptions .MaxConnections , "max-connections" , opt .setupOptions .MaxConnections , "Specify maximum concurrent connections for GCS, Azure and B2 backend" )
@@ -156,6 +153,12 @@ func NewCmdBackup() *cobra.Command {
156
153
}
157
154
158
155
func (opt * esOptions ) backupElasticsearch (targetRef api_v1beta1.TargetRef ) (* restic.BackupOutput , error ) {
156
+ var err error
157
+ opt .setupOptions .StorageSecret , err = opt .kubeClient .CoreV1 ().Secrets (opt .storageSecret .Namespace ).Get (context .TODO (), opt .storageSecret .Name , metav1.GetOptions {})
158
+ if err != nil {
159
+ return nil , err
160
+ }
161
+
159
162
// if any pre-backup actions has been assigned to it, execute them
160
163
actionOptions := api_util.ActionOptions {
161
164
StashClient : opt .stashClient ,
@@ -164,7 +167,7 @@ func (opt *esOptions) backupElasticsearch(targetRef api_v1beta1.TargetRef) (*res
164
167
BackupSessionName : opt .backupSessionName ,
165
168
Namespace : opt .namespace ,
166
169
}
167
- err : = api_util .ExecutePreBackupActions (actionOptions )
170
+ err = api_util .ExecutePreBackupActions (actionOptions )
168
171
if err != nil {
169
172
return nil , err
170
173
}
@@ -183,70 +186,47 @@ func (opt *esOptions) backupElasticsearch(targetRef api_v1beta1.TargetRef) (*res
183
186
return nil , err
184
187
}
185
188
186
- // get app binding
187
189
appBinding , err := opt .catalogClient .AppcatalogV1alpha1 ().AppBindings (opt .namespace ).Get (context .TODO (), opt .appBindingName , metav1.GetOptions {})
188
190
if err != nil {
189
191
return nil , err
190
192
}
191
- // get secret
192
- appBindingSecret , err := opt .kubeClient .CoreV1 ().Secrets (opt .namespace ).Get (context .TODO (), appBinding .Spec .Secret .Name , metav1.GetOptions {})
193
- if err != nil {
194
- return nil , err
195
- }
196
193
197
- // transform secret
198
- err = appBinding .TransformSecret (opt .kubeClient , appBindingSecret .Data )
194
+ // clear directory before running multielasticdump
195
+ session := opt .newSessionWrapper (MultiElasticDumpCMD )
196
+
197
+ err = opt .setDatabaseCredentials (appBinding , session .cmd )
199
198
if err != nil {
200
199
return nil , err
201
200
}
202
-
203
- // clear directory before running multielasticdump
204
201
klog .Infoln ("Cleaning up directory: " , opt .interimDataDir )
205
202
if err := clearDir (opt .interimDataDir ); err != nil {
206
203
return nil , err
207
204
}
208
205
209
- // write the credential ifo into a file
210
- // TODO: support backup without authentication
211
- httpAuthFilePath := filepath .Join (opt .setupOptions .ScratchDir , ESAuthFile )
212
- err = writeAuthFile (httpAuthFilePath , appBindingSecret )
206
+ url , err := appBinding .URL ()
213
207
if err != nil {
214
208
return nil , err
215
209
}
210
+ session .cmd .Args = append (session .cmd .Args , []interface {}{
211
+ fmt .Sprintf (`--input=%v` , url ),
212
+ fmt .Sprintf (`--output=%v` , opt .interimDataDir ),
213
+ }... )
216
214
217
- var tlsArgs string
218
- if appBinding .Spec .ClientConfig .CABundle != nil {
219
- if err := ioutil .WriteFile (filepath .Join (opt .setupOptions .ScratchDir , ESCACertFile ), appBinding .Spec .ClientConfig .CABundle , os .ModePerm ); err != nil {
220
- return nil , err
221
- }
222
- tlsArgs = fmt .Sprintf ("--ca-input=%v" , filepath .Join (opt .setupOptions .ScratchDir , ESCACertFile ))
215
+ err = session .setTLSParameters (appBinding , opt .setupOptions .ScratchDir )
216
+ if err != nil {
217
+ return nil , err
223
218
}
224
219
225
- appSVC := appBinding .Spec .ClientConfig .Service
226
- esURL := fmt .Sprintf ("%v://%s:%d" , appSVC .Scheme , appSVC .Name , appSVC .Port )
227
-
228
- // wait for DB ready
229
- waitForDBReady (appBinding .Spec .ClientConfig .Service .Name , appBinding .Spec .ClientConfig .Service .Port , opt .waitTimeout )
230
-
231
- // run separate shell to dump indices
232
- klog .Infoln ("Performing multielasticdump on " , appSVC .Name )
233
- esShell := sh .NewSession ()
234
- esShell .ShowCMD = false
235
- esShell .SetEnv ("NODE_TLS_REJECT_UNAUTHORIZED" , "0" ) //xref: https://github.com/taskrabbit/elasticsearch-dump#bypassing-self-sign-certificate-errors
236
-
237
- args := []interface {}{
238
- fmt .Sprintf (`--input=%v` , esURL ),
239
- fmt .Sprintf (`--output=%v` , opt .interimDataDir ),
240
- fmt .Sprintf ("--httpAuthFile=%s" , httpAuthFilePath ),
241
- tlsArgs ,
242
- }
243
- for _ , arg := range strings .Fields (opt .esArgs ) {
244
- args = append (args , arg )
220
+ err = opt .waitForDBReady (appBinding )
221
+ if err != nil {
222
+ return nil , err
245
223
}
246
224
247
- esShell .Command (MultiElasticDumpCMD , args ... ) // xref: multielasticdump: https://github.com/taskrabbit/elasticsearch-dump#multielasticdump
225
+ session .sh .ShowCMD = false
226
+ session .setUserArgs (opt .esArgs )
227
+ session .sh .Command (session .cmd .Name , session .cmd .Args ... ) // xref: multielasticdump: https://github.com/taskrabbit/elasticsearch-dump#multielasticdump
248
228
249
- if err := esShell .Run (); err != nil {
229
+ if err := session . sh .Run (); err != nil {
250
230
return nil , err
251
231
}
252
232
@@ -259,6 +239,5 @@ func (opt *esOptions) backupElasticsearch(targetRef api_v1beta1.TargetRef) (*res
259
239
return nil , err
260
240
}
261
241
262
- // Run backup
263
242
return resticWrapper .RunBackup (opt .backupOptions , targetRef )
264
243
}
0 commit comments