-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate.go
More file actions
175 lines (145 loc) · 4.81 KB
/
migrate.go
File metadata and controls
175 lines (145 loc) · 4.81 KB
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
171
172
173
174
175
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkg
import (
"context"
"fmt"
"os"
"path/filepath"
"stash.appscode.dev/apimachinery/apis/stash/v1alpha1"
cs "stash.appscode.dev/apimachinery/client/clientset/versioned"
"stash.appscode.dev/apimachinery/pkg/restic"
"stash.appscode.dev/stash/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)
type migrateOptions struct {
kubeClient *kubernetes.Clientset
config *rest.Config
repo *v1alpha1.Repository
}
func NewCmdMigrateRepositoryToV2(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
opt := migrateOptions{}
cmd := &cobra.Command{
Use: "migrate",
Short: `Migrate restic repository to v2`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "" {
return fmt.Errorf("repository name not found")
}
repositoryName := args[0]
var err error
opt.config, err = clientGetter.ToRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to read kubeconfig")
}
namespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
stshclient, err := cs.NewForConfig(opt.config)
if err != nil {
return err
}
opt.repo, err = stshclient.StashV1alpha1().Repositories(namespace).Get(context.TODO(), repositoryName, metav1.GetOptions{})
if err != nil {
return err
}
opt.kubeClient, err = kubernetes.NewForConfig(opt.config)
if err != nil {
return err
}
if opt.repo.Spec.Backend.Local != nil {
// get the pod that mount this repository as volume
pod, err := getBackendMountingPod(opt.kubeClient, opt.repo)
if err != nil {
return err
}
return opt.migrateRepoFromPod(pod)
}
return opt.migrateRepo()
},
}
cmd.Flags().StringVar(&imgRestic.Registry, "docker-registry", imgRestic.Registry, "Docker image registry for restic cli")
cmd.Flags().StringVar(&imgRestic.Tag, "image-tag", imgRestic.Tag, "Restic docker image tag")
return cmd
}
func (opt *migrateOptions) migrateRepoFromPod(pod *core.Pod) error {
if err := opt.executeMigrateRepoCmdInPod(pod); err != nil {
return err
}
klog.Infof("Repository %s/%s upgraded to version 2", namespace, opt.repo.Name)
return nil
}
func (opt *migrateOptions) executeMigrateRepoCmdInPod(pod *core.Pod) error {
command := []string{"/stash-enterprise", "migrate"}
command = append(command, "--repo-name", opt.repo.Name, "--repo-namespace", opt.repo.Namespace)
out, err := execCommandOnPod(opt.kubeClient, opt.config, pod, command)
if string(out) != "" {
klog.Infoln("Output:", string(out))
}
return err
}
func (opt *migrateOptions) migrateRepo() error {
// get source repository secret
secret, err := opt.kubeClient.CoreV1().Secrets(namespace).Get(context.TODO(), opt.repo.Spec.Backend.StorageSecretName, metav1.GetOptions{})
if err != nil {
return err
}
if err = os.MkdirAll(ScratchDir, 0o755); err != nil {
return err
}
defer removeDirWithLogErr(ScratchDir)
// configure restic wrapper
extraOpt := util.ExtraOptions{
StorageSecret: secret,
ScratchDir: ScratchDir,
}
// configure setupOption
setupOpt, err := util.SetupOptionsForRepository(*opt.repo, extraOpt)
if err != nil {
return fmt.Errorf("setup option for repository failed")
}
resticWrapper, err := restic.NewResticWrapper(setupOpt)
if err != nil {
return err
}
localDirs := &cliLocalDirectories{
configDir: filepath.Join(ScratchDir, configDirName),
}
// dump restic's environments into `restic-env` file.
// we will pass this env file to restic docker container.
err = resticWrapper.DumpEnv(localDirs.configDir, ResticEnvs)
if err != nil {
return err
}
extraAgrs := []string{
"upgrade_repo_v2", "--no-cache",
}
// For TLS secured Minio/REST server, specify cert path
if resticWrapper.GetCaPath() != "" {
extraAgrs = append(extraAgrs, "--cacert", resticWrapper.GetCaPath())
}
if err = runCmdViaDocker(*localDirs, "migrate", extraAgrs); err != nil {
return err
}
klog.Infof("Repository %s/%s upgraded to version 2", namespace, opt.repo.Name)
return nil
}