-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ming Qiu <mqiu@vmware.com>
- Loading branch information
1 parent
2a1ae0e
commit 5e4a396
Showing
18 changed files
with
1,633 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add repository maintenance job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package repomantenance | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/vmware-tanzu/velero/internal/credentials" | ||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" | ||
velerocli "github.com/vmware-tanzu/velero/pkg/client" | ||
"github.com/vmware-tanzu/velero/pkg/repository" | ||
"github.com/vmware-tanzu/velero/pkg/repository/provider" | ||
"github.com/vmware-tanzu/velero/pkg/util/filesystem" | ||
"github.com/vmware-tanzu/velero/pkg/util/logging" | ||
) | ||
|
||
type Options struct { | ||
RepoName string | ||
BackupStorageLocation string | ||
RepoType string | ||
KeepLatestMaitenanceJobs int | ||
LogLevelFlag *logging.LevelFlag | ||
FormatFlag *logging.FormatFlag | ||
} | ||
|
||
func (o *Options) BindFlags(flags *pflag.FlagSet) { | ||
flags.StringVar(&o.RepoName, "repo-name", "", "namespace of the pod/volume that the snapshot is for") | ||
flags.StringVar(&o.BackupStorageLocation, "backup-storage-location", "", "backup's storage location name") | ||
flags.StringVar(&o.RepoType, "repo-type", velerov1api.BackupRepositoryTypeKopia, "type of the repository where the snapshot is stored") | ||
flags.Var(o.LogLevelFlag, "log-level", fmt.Sprintf("The level at which to log. Valid values are %s.", strings.Join(o.LogLevelFlag.AllowedValues(), ", "))) | ||
flags.Var(o.FormatFlag, "log-format", fmt.Sprintf("The format for log output. Valid values are %s.", strings.Join(o.FormatFlag.AllowedValues(), ", "))) | ||
} | ||
|
||
func NewCommand(f velerocli.Factory) *cobra.Command { | ||
o := &Options{ | ||
LogLevelFlag: logging.LogLevelFlag(logrus.InfoLevel), | ||
FormatFlag: logging.NewFormatFlag(), | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "repo-mantenance", | ||
Hidden: true, | ||
Short: "VELERO INTERNAL COMMAND ONLY - not intended to be run directly by users", | ||
Run: func(c *cobra.Command, args []string) { | ||
o.Run(f) | ||
}, | ||
} | ||
|
||
o.BindFlags(cmd.Flags()) | ||
return cmd | ||
} | ||
|
||
func checkError(err error, file *os.File) { | ||
if err != nil { | ||
if err != context.Canceled { | ||
if _, errWrite := file.WriteString(fmt.Sprintf("An error occurred: %v \n", err)); errWrite != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to write error to termination log file: %v\n", errWrite) | ||
} | ||
file.Close() | ||
os.Exit(1) // indicate the command executed failed | ||
} | ||
} | ||
} | ||
|
||
func (o *Options) Run(f velerocli.Factory) { | ||
logger := logging.DefaultLogger(o.LogLevelFlag.Parse(), o.FormatFlag.Parse()) | ||
logger.SetOutput(os.Stdout) | ||
|
||
errorFile, err := os.Create("/dev/termination-log") | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to create termination log file: %v\n", err) | ||
return | ||
} | ||
defer errorFile.Close() | ||
|
||
scheme := runtime.NewScheme() | ||
err = velerov1api.AddToScheme(scheme) | ||
checkError(err, errorFile) | ||
|
||
err = v1.AddToScheme(scheme) | ||
checkError(err, errorFile) | ||
|
||
config, err := f.ClientConfig() | ||
checkError(err, errorFile) | ||
|
||
cli, err := client.New(config, client.Options{ | ||
Scheme: scheme, | ||
}) | ||
checkError(err, errorFile) | ||
|
||
err = o.runRepoPrune(cli, f.Namespace(), logger) | ||
checkError(err, errorFile) | ||
} | ||
|
||
func (o *Options) runRepoPrune(cli client.Client, namespace string, logger logrus.FieldLogger) error { | ||
credentialFileStore, err := credentials.NewNamespacedFileStore( | ||
cli, | ||
namespace, | ||
"/tmp/credentials", | ||
filesystem.NewFileSystem(), | ||
) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create namespaced file store") | ||
} | ||
|
||
credentialSecretStore, err := credentials.NewNamespacedSecretStore(cli, namespace) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create namespaced secret store") | ||
} | ||
|
||
var repoProvider provider.Provider | ||
if o.RepoType == velerov1api.BackupRepositoryTypeRestic { | ||
repoProvider = provider.NewResticRepositoryProvider(credentialFileStore, filesystem.NewFileSystem(), logger) | ||
} else { | ||
repoProvider = provider.NewUnifiedRepoProvider( | ||
credentials.CredentialGetter{ | ||
FromFile: credentialFileStore, | ||
FromSecret: credentialSecretStore, | ||
}, o.RepoType, cli, logger) | ||
} | ||
|
||
// backupRepository | ||
repo, err := repository.GetBackupRepository(context.Background(), cli, namespace, | ||
repository.BackupRepositoryKey{ | ||
VolumeNamespace: o.RepoName, | ||
BackupLocation: o.BackupStorageLocation, | ||
RepositoryType: o.RepoType, | ||
}, true) | ||
|
||
if err != nil { | ||
return errors.Wrap(err, "failed to get backup repository") | ||
} | ||
|
||
// bsl | ||
bsl := &velerov1api.BackupStorageLocation{} | ||
err = cli.Get(context.Background(), client.ObjectKey{Namespace: namespace, Name: repo.Spec.BackupStorageLocation}, bsl) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get backup storage location") | ||
} | ||
|
||
para := provider.RepoParam{ | ||
BackupRepo: repo, | ||
BackupLocation: bsl, | ||
} | ||
|
||
err = repoProvider.BoostRepoConnect(context.Background(), para) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to boost repo connect") | ||
} | ||
|
||
err = repoProvider.PruneRepo(context.Background(), para) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to prune repo") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.