-
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 <[email protected]>
- Loading branch information
1 parent
2a1ae0e
commit c8a8349
Showing
18 changed files
with
1,673 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,143 @@ | ||
package repomantenance | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"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 { | ||
file.WriteString(fmt.Sprintf("An error occurred: %v \n", err)) | ||
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) | ||
|
||
credentialFileStore, err := credentials.NewNamespacedFileStore( | ||
cli, | ||
f.Namespace(), | ||
"/tmp/credentials", | ||
filesystem.NewFileSystem(), | ||
) | ||
checkError(err, errorFile) | ||
|
||
credentialSecretStore, err := credentials.NewNamespacedSecretStore(cli, f.Namespace()) | ||
checkError(err, errorFile) | ||
|
||
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, f.Namespace(), | ||
repository.BackupRepositoryKey{ | ||
VolumeNamespace: o.RepoName, | ||
BackupLocation: o.BackupStorageLocation, | ||
RepositoryType: o.RepoType, | ||
}, true) | ||
|
||
checkError(err, errorFile) | ||
|
||
// bsl | ||
bsl := &velerov1api.BackupStorageLocation{} | ||
err = cli.Get(context.Background(), client.ObjectKey{Namespace: f.Namespace(), Name: repo.Spec.BackupStorageLocation}, bsl) | ||
checkError(err, errorFile) | ||
|
||
para := provider.RepoParam{ | ||
BackupRepo: repo, | ||
BackupLocation: bsl, | ||
} | ||
|
||
err = repoProvider.BoostRepoConnect(context.Background(), para) | ||
checkError(err, errorFile) | ||
|
||
err = repoProvider.PruneRepo(context.Background(), para) | ||
checkError(err, errorFile) | ||
} |
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.