@@ -21,7 +21,6 @@ package cmd
21
21
*/
22
22
23
23
import (
24
- "context"
25
24
"errors"
26
25
"fmt"
27
26
"os"
@@ -33,39 +32,19 @@ import (
33
32
"github.com/spf13/cobra"
34
33
35
34
appsv1 "k8s.io/api/apps/v1"
36
- corev1 "k8s.io/api/core/v1"
35
+ apiv1 "k8s.io/api/core/v1"
37
36
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38
- "k8s.io/client-go/kubernetes"
39
37
ctrl "sigs.k8s.io/controller-runtime"
40
38
)
41
39
42
- func prepareRestore (kmd * cobra.Command ) (string , * kubernetes.Clientset , string ) {
43
-
44
- cliCtx , err := cliutils .GetCLIContext (kmd )
45
- if err != nil {
46
- ctrl .Log .Error (err , "Error initializing CLI:" )
47
- os .Exit (1 )
48
- }
49
-
50
- kubeContext := cliCtx .KubeContext
51
- _ , kubeClientSet := cliutils .GetClientset (kubeContext )
52
- return cliCtx .Namespace , kubeClientSet , kubeContext
53
- }
54
-
55
- func restoreSecret (ns string , backupDir string , kubeContext string ) {
40
+ func restoreSecret (backupDir string , env cliutils.ENV ) {
56
41
ctrl .Log .Info ("Restoring secrets..." )
57
42
58
- env := cliutils.ENV {
59
- Cli : cliutils .CreateKubernetesClientOrDie (kubeContext ),
60
- Ctx : context .TODO (),
61
- Ns : ns ,
62
- }
63
-
64
43
for _ , sec := range SecretsToBackup {
65
44
pathToSecret := backupDir + "/" + SecretsBackupPath + "/" + sec + ".yaml"
66
45
secretContent := cliutils .ReadYAMLToMapOrDie (pathToSecret )
67
46
68
- var secret corev1 .Secret
47
+ secret := apiv1 .Secret {}
69
48
if cliutils .GetMOrDie (& env , sec , & secret ) {
70
49
secretMap := secretContent ["data" ].(map [string ]interface {})
71
50
for key , value := range secretMap {
@@ -88,16 +67,17 @@ func restoreSecret(ns string, backupDir string, kubeContext string) {
88
67
89
68
}
90
69
91
- func restoreDB (ns string , backupDir string , kubeClientSet * kubernetes. Clientset , kubeContext string ) {
70
+ func restoreDB (backupDir string , kubeContext string , env cliutils. ENV ) {
92
71
ctrl .Log .Info ("Restoring DB..." )
93
- pod := cliutils .GetPodByName (dbBackupPod , ns , kubeClientSet )
72
+ pod := apiv1.Pod {}
73
+ cliutils .GetMOrDie (& env , dbBackupPod , & pod )
94
74
95
75
kubectlPath := cliutils .GetKubectlPath ()
96
76
dropDBCMD := []string {
97
77
"mysql" ,
98
78
"-e DROP DATABASE zuul;" ,
99
79
}
100
- cliutils .RunRemoteCmd (kubeContext , ns , pod .Name , controllers .MariaDBIdent , dropDBCMD )
80
+ cliutils .RunRemoteCmd (kubeContext , env . Ns , pod .Name , controllers .MariaDBIdent , dropDBCMD )
101
81
102
82
mariadbBackupPath := backupDir + "/" + DBBackupPath
103
83
@@ -106,32 +86,22 @@ func restoreDB(ns string, backupDir string, kubeClientSet *kubernetes.Clientset,
106
86
// but in that case, we need to do it via system kubernetes client.
107
87
executeCommand := fmt .Sprintf (
108
88
"cat %s | %s -n %s exec -it %s -c %s -- sh -c \" mysql -h0\" " ,
109
- mariadbBackupPath , kubectlPath , ns , pod .Name , controllers .MariaDBIdent ,
89
+ mariadbBackupPath , kubectlPath , env . Ns , pod .Name , controllers .MariaDBIdent ,
110
90
)
111
91
112
- cliutils .ExecuteKubectlClient (ns , pod .Name , controllers .MariaDBIdent , executeCommand )
92
+ cliutils .ExecuteKubectlClient (env . Ns , pod .Name , controllers .MariaDBIdent , executeCommand )
113
93
114
94
ctrl .Log .Info ("Finished restoring DB from backup!" )
115
95
}
116
- func restoreZuul (ns string , backupDir string , kubeClientSet * kubernetes. Clientset , kubeContext string ) {
96
+ func restoreZuul (backupDir string , kubeContext string , env cliutils. ENV ) {
117
97
ctrl .Log .Info ("Restoring Zuul..." )
118
- pod := cliutils .GetPodByName (zuulBackupPod , ns , kubeClientSet )
98
+ pod := apiv1.Pod {}
99
+ cliutils .GetMOrDie (& env , zuulBackupPod , & pod )
119
100
120
101
// ensure that pod does not have any restore file
121
- restoreZuulRemoveCMD := []string {
122
- "rm" ,
123
- "-rf" ,
124
- "/tmp/zuul-import" ,
125
- }
126
- cliutils .RunRemoteCmd (kubeContext , ns , pod .Name , controllers .ZuulSchedulerIdent , restoreZuulRemoveCMD )
127
-
128
- // create empty directory for future restore
129
- restoreZuulCreateDirCMD := []string {
130
- "mkdir" ,
131
- "-p" ,
132
- "/tmp/zuul-import" ,
133
- }
134
- cliutils .RunRemoteCmd (kubeContext , ns , pod .Name , controllers .ZuulSchedulerIdent , restoreZuulCreateDirCMD )
102
+ cleanCMD := []string {
103
+ "bash" , "-c" , "rm -rf /tmp/zuul-import && mkdir -p /tmp/zuul-import" }
104
+ cliutils .RunRemoteCmd (kubeContext , env .Ns , pod .Name , controllers .ZuulSchedulerIdent , cleanCMD )
135
105
136
106
// copy the Zuul private keys backup to pod
137
107
// tar cf - -C /tmp/backup/zuul zuul.keys | /usr/bin/kubectl exec -i -n sf zuul-scheduler-0 -c zuul-scheduler -- tar xf - -C /tmp
@@ -140,38 +110,26 @@ func restoreZuul(ns string, backupDir string, kubeClientSet *kubernetes.Clientse
140
110
baseFile := filepath .Base (ZuulBackupPath )
141
111
executeCommand := fmt .Sprintf (
142
112
"tar cf - -C %s %s | %s exec -i -n %s %s -c %s -- tar xf - -C /tmp/zuul-import" ,
143
- basePath , baseFile , kubectlPath , ns , pod .Name , controllers .ZuulSchedulerIdent ,
113
+ basePath , baseFile , kubectlPath , env . Ns , pod .Name , controllers .ZuulSchedulerIdent ,
144
114
)
145
115
ctrl .Log .Info ("Executing " + executeCommand )
146
116
147
- cliutils .ExecuteKubectlClient (ns , pod .Name , controllers .ZuulSchedulerIdent , executeCommand )
117
+ cliutils .ExecuteKubectlClient (env . Ns , pod .Name , controllers .ZuulSchedulerIdent , executeCommand )
148
118
149
119
// https://zuul-ci.org/docs/zuul/latest/client.html
150
- restoreZuulCMD := []string {
151
- "zuul-admin" ,
152
- "import-keys" ,
153
- "--force" ,
154
- "/tmp/zuul-import/" + baseFile ,
155
- }
120
+ restoreCMD := []string {
121
+ "bash" , "-c" , "zuul-admin import-keys --force /tmp/zuul-import/" + baseFile + " && " +
122
+ "rm -rf /tmp/zuul-import" }
156
123
157
124
// Execute command for restore
158
- cliutils .RunRemoteCmd (kubeContext , ns , pod .Name , controllers .ZuulSchedulerIdent , restoreZuulCMD )
159
-
160
- // remove after all
161
- cliutils .RunRemoteCmd (kubeContext , ns , pod .Name , controllers .ZuulSchedulerIdent , restoreZuulRemoveCMD )
125
+ cliutils .RunRemoteCmd (kubeContext , env .Ns , pod .Name , controllers .ZuulSchedulerIdent , restoreCMD )
162
126
163
127
ctrl .Log .Info ("Finished doing Zuul private keys restore!" )
164
128
165
129
}
166
130
167
- func clearComponents (ns string , kubeContext string ) {
168
- ctrl .Log .Info ("Removing components requiring a complete restart ..." )
169
-
170
- env := cliutils.ENV {
171
- Cli : cliutils .CreateKubernetesClientOrDie (kubeContext ),
172
- Ctx : context .TODO (),
173
- Ns : ns ,
174
- }
131
+ func clearComponents (env cliutils.ENV ) {
132
+ ctrl .Log .Info ("Removing components requirering a complete restart ..." )
175
133
176
134
for _ , stsName := range []string {"zuul-scheduler" , "zuul-executor" , "zuul-merger" , "nodepool-builder" , "zookeeper" } {
177
135
cliutils .DeleteOrDie (& env , & appsv1.StatefulSet {
@@ -209,18 +167,17 @@ func restoreCmd(kmd *cobra.Command, args []string) {
209
167
210
168
}
211
169
212
- // prepare to make restore
213
- ns , kubeClientSet , kubeContext := prepareRestore (kmd )
170
+ kubeContext , env := cliutils .GetCLIENV (kmd )
214
171
215
- if ns == "" {
172
+ if env . Ns == "" {
216
173
ctrl .Log .Info ("You did not specify the namespace!" )
217
174
os .Exit (1 )
218
175
}
219
176
220
- restoreZuul (ns , backupDir , kubeClientSet , kubeContext )
221
- restoreSecret (ns , backupDir , kubeContext )
222
- restoreDB (ns , backupDir , kubeClientSet , kubeContext )
223
- clearComponents (ns , kubeContext )
177
+ restoreZuul (backupDir , kubeContext , env )
178
+ restoreSecret (backupDir , env )
179
+ restoreDB (backupDir , kubeContext , env )
180
+ clearComponents (env )
224
181
225
182
}
226
183
0 commit comments