|
| 1 | +/* |
| 2 | +Copyright AppsCode Inc. and Contributors |
| 3 | +
|
| 4 | +Licensed under the AppsCode Community License 1.0.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package pkg |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + "stash.appscode.dev/apimachinery/apis/stash/v1alpha1" |
| 26 | + cs "stash.appscode.dev/apimachinery/client/clientset/versioned" |
| 27 | + "stash.appscode.dev/apimachinery/pkg/restic" |
| 28 | + "stash.appscode.dev/stash/pkg/util" |
| 29 | + |
| 30 | + "github.com/pkg/errors" |
| 31 | + "github.com/spf13/cobra" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 34 | + "k8s.io/client-go/kubernetes" |
| 35 | + "k8s.io/client-go/rest" |
| 36 | + "k8s.io/klog/v2" |
| 37 | +) |
| 38 | + |
| 39 | +type checkOptions struct { |
| 40 | + kubeClient *kubernetes.Clientset |
| 41 | + config *rest.Config |
| 42 | + repo *v1alpha1.Repository |
| 43 | + |
| 44 | + // All restic options for the 'check' command. |
| 45 | + readData bool |
| 46 | + readDataSubset string |
| 47 | + withCache bool |
| 48 | +} |
| 49 | + |
| 50 | +func NewCmdCheckRepository(clientGetter genericclioptions.RESTClientGetter) *cobra.Command { |
| 51 | + opt := checkOptions{} |
| 52 | + cmd := &cobra.Command{ |
| 53 | + Use: "check", |
| 54 | + Short: `Check the repository for errors`, |
| 55 | + DisableAutoGenTag: true, |
| 56 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 57 | + if len(args) == 0 || args[0] == "" { |
| 58 | + return fmt.Errorf("repository name not found") |
| 59 | + } |
| 60 | + repositoryName := args[0] |
| 61 | + |
| 62 | + var err error |
| 63 | + opt.config, err = clientGetter.ToRESTConfig() |
| 64 | + if err != nil { |
| 65 | + return errors.Wrap(err, "failed to read kubeconfig") |
| 66 | + } |
| 67 | + namespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace() |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + opt.kubeClient, err = kubernetes.NewForConfig(opt.config) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + stashClient, err = cs.NewForConfig(opt.config) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + // get source repository |
| 83 | + opt.repo, err = stashClient.StashV1alpha1().Repositories(namespace).Get(context.TODO(), repositoryName, metav1.GetOptions{}) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + extraArgs := opt.getUserExtraArguments() |
| 89 | + if opt.repo.Spec.Backend.Local != nil { |
| 90 | + return opt.checkLocalRepository(extraArgs) |
| 91 | + } |
| 92 | + |
| 93 | + return opt.checkRepository(extraArgs) |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + cmd.Flags().BoolVar(&opt.readData, "read-data", false, "read all data blobs") |
| 98 | + cmd.Flags().BoolVar(&opt.withCache, "with-cache", false, "use existing cache, only read uncached data from repository") |
| 99 | + cmd.Flags().StringVar(&opt.readDataSubset, "read-data-subset", "", "read a `subset` of data packs, specified as 'n/t' for specific part, or either 'x%' or 'x.y%' or a size in bytes with suffixes k/K, m/M, g/G, t/T for a random subset") |
| 100 | + return cmd |
| 101 | +} |
| 102 | + |
| 103 | +func (opt *checkOptions) checkLocalRepository(extraArgs []string) error { |
| 104 | + // get the pod that mount this repository as volume |
| 105 | + pod, err := getBackendMountingPod(opt.kubeClient, opt.repo) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + command := []string{"/stash-enterprise", "check"} |
| 111 | + command = append(command, extraArgs...) |
| 112 | + command = append(command, "--repo-name="+opt.repo.Name, "--repo-namespace="+opt.repo.Namespace) |
| 113 | + |
| 114 | + out, err := execCommandOnPod(opt.kubeClient, opt.config, pod, command) |
| 115 | + if string(out) != "" { |
| 116 | + klog.Infoln("Output:", string(out)) |
| 117 | + } |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + klog.Infof("Repository %s/%s has been checked successfully", opt.repo.Namespace, opt.repo.Name) |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func (opt *checkOptions) checkRepository(extraArgs []string) error { |
| 126 | + // get source repository secret |
| 127 | + secret, err := opt.kubeClient.CoreV1().Secrets(namespace).Get(context.TODO(), opt.repo.Spec.Backend.StorageSecretName, metav1.GetOptions{}) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + if err = os.MkdirAll(ScratchDir, 0o755); err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + defer os.RemoveAll(ScratchDir) |
| 136 | + |
| 137 | + // configure restic wrapper |
| 138 | + extraOpt := util.ExtraOptions{ |
| 139 | + StorageSecret: secret, |
| 140 | + ScratchDir: ScratchDir, |
| 141 | + } |
| 142 | + // configure setupOption |
| 143 | + setupOpt, err := util.SetupOptionsForRepository(*opt.repo, extraOpt) |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("setup option for repository failed") |
| 146 | + } |
| 147 | + // init restic wrapper |
| 148 | + resticWrapper, err := restic.NewResticWrapper(setupOpt) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + localDirs := &cliLocalDirectories{ |
| 154 | + configDir: filepath.Join(ScratchDir, configDirName), |
| 155 | + } |
| 156 | + |
| 157 | + // dump restic's environments into `restic-env` file. |
| 158 | + // we will pass this env file to restic docker container. |
| 159 | + |
| 160 | + err = resticWrapper.DumpEnv(localDirs.configDir, ResticEnvs) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + // For TLS secured Minio/REST server, specify cert path |
| 166 | + if resticWrapper.GetCaPath() != "" { |
| 167 | + extraArgs = append(extraArgs, "--cacert", resticWrapper.GetCaPath()) |
| 168 | + } |
| 169 | + |
| 170 | + // run unlock inside docker |
| 171 | + if err = runCmdViaDocker(*localDirs, "check", extraArgs); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + klog.Infof("Repository %s/%s has been checked successfully", opt.repo.Namespace, opt.repo.Name) |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +func (opt *checkOptions) getUserExtraArguments() []string { |
| 179 | + var extraArgs []string |
| 180 | + if opt.readData { |
| 181 | + extraArgs = append(extraArgs, "--read-data") |
| 182 | + } |
| 183 | + if opt.readDataSubset != "" { |
| 184 | + extraArgs = append(extraArgs, |
| 185 | + fmt.Sprintf("--read-data-subset=%s", opt.readDataSubset)) |
| 186 | + } |
| 187 | + if opt.withCache { |
| 188 | + extraArgs = append(extraArgs, "--with-cache") |
| 189 | + } |
| 190 | + return extraArgs |
| 191 | +} |
0 commit comments