From f9c91e8b37bd435717cd387565a371bff74a54bc Mon Sep 17 00:00:00 2001 From: Lyndon-Li Date: Mon, 6 Jan 2025 17:35:57 +0800 Subject: [PATCH] recall repo maintenance history on restart Signed-off-by: Lyndon-Li --- pkg/cmd/cli/repomantenance/maintenance.go | 16 +++++++++++++--- pkg/cmd/server/server.go | 2 +- pkg/controller/backup_repository_controller.go | 4 ---- pkg/repository/keys/keys.go | 12 ++++-------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/pkg/cmd/cli/repomantenance/maintenance.go b/pkg/cmd/cli/repomantenance/maintenance.go index 5e83a079c4..7ca6d7505e 100644 --- a/pkg/cmd/cli/repomantenance/maintenance.go +++ b/pkg/cmd/cli/repomantenance/maintenance.go @@ -13,7 +13,10 @@ import ( "github.com/spf13/pflag" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/kubernetes" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log/zap" "github.com/vmware-tanzu/velero/internal/credentials" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" @@ -65,6 +68,8 @@ func (o *Options) Run(f velerocli.Factory) { logger := logging.DefaultLogger(o.LogLevelFlag.Parse(), o.FormatFlag.Parse()) logger.SetOutput(os.Stdout) + ctrl.SetLogger(zap.New(zap.UseDevMode(true))) + time.Sleep(time.Minute) pruneError := o.runRepoPrune(f, f.Namespace(), logger) @@ -116,9 +121,9 @@ func (o *Options) initClient(f velerocli.Factory) (client.Client, error) { return cli, nil } -func initRepoManager(namespace string, cli client.Client, logger logrus.FieldLogger) (repomanager.Manager, error) { +func initRepoManager(namespace string, cli client.Client, kubeClient kubernetes.Interface, logger logrus.FieldLogger) (repomanager.Manager, error) { // ensure the repo key secret is set up - if err := repokey.EnsureCommonRepositoryKey(cli, namespace); err != nil { + if err := repokey.EnsureCommonRepositoryKey(kubeClient.CoreV1(), namespace); err != nil { return nil, errors.Wrap(err, "failed to ensure repository key") } @@ -155,7 +160,12 @@ func (o *Options) runRepoPrune(f velerocli.Factory, namespace string, logger log return err } - manager, err := initRepoManager(namespace, cli, logger) + kubeClient, err := f.KubeClient() + if err != nil { + return err + } + + manager, err := initRepoManager(namespace, cli, kubeClient, logger) if err != nil { return err } diff --git a/pkg/cmd/server/server.go b/pkg/cmd/server/server.go index 82c876761d..396970b92a 100644 --- a/pkg/cmd/server/server.go +++ b/pkg/cmd/server/server.go @@ -491,7 +491,7 @@ func (s *server) checkNodeAgent() { func (s *server) initRepoManager() error { // ensure the repo key secret is set up - if err := repokey.EnsureCommonRepositoryKey(s.mgr.GetClient(), s.namespace); err != nil { + if err := repokey.EnsureCommonRepositoryKey(s.kubeClient.CoreV1(), s.namespace); err != nil { return err } diff --git a/pkg/controller/backup_repository_controller.go b/pkg/controller/backup_repository_controller.go index 92266ee1bf..3fa8f03ab3 100644 --- a/pkg/controller/backup_repository_controller.go +++ b/pkg/controller/backup_repository_controller.go @@ -438,10 +438,6 @@ func (r *BackupRepoReconciler) runMaintenanceIfDue(ctx context.Context, req *vel log.Info("Running maintenance on backup repository") - // prune failures should be displayed in the `.status.message` field but - // should not cause the repo to move to `NotReady`. - log.Debug("Pruning repo") - job, err := funcStartMaintenanceJob(r.Client, ctx, req, r.repoMaintenanceConfig, r.podResources, r.logLevel, r.logFormat, log) if err != nil { log.WithError(err).Warn("Starting repo maintenance failed") diff --git a/pkg/repository/keys/keys.go b/pkg/repository/keys/keys.go index 15f4dae9e0..21423afe09 100644 --- a/pkg/repository/keys/keys.go +++ b/pkg/repository/keys/keys.go @@ -24,11 +24,9 @@ import ( corev1api "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" "github.com/vmware-tanzu/velero/pkg/builder" - - "sigs.k8s.io/controller-runtime/pkg/client" ) const ( @@ -38,13 +36,11 @@ const ( encryptionKey = "static-passw0rd" ) -func EnsureCommonRepositoryKey(cli client.Client, namespace string) error { - existing := &corev1api.Secret{} - err := cli.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: credentialsSecretName}, existing) +func EnsureCommonRepositoryKey(secretClient corev1client.SecretsGetter, namespace string) error { + _, err := secretClient.Secrets(namespace).Get(context.TODO(), credentialsSecretName, metav1.GetOptions{}) if err != nil && !apierrors.IsNotFound(err) { return errors.WithStack(err) } - if err == nil { return nil } @@ -62,7 +58,7 @@ func EnsureCommonRepositoryKey(cli client.Client, namespace string) error { }, } - if err := cli.Create(context.TODO(), secret); err != nil { + if _, err = secretClient.Secrets(namespace).Create(context.TODO(), secret, metav1.CreateOptions{}); err != nil { return errors.Wrapf(err, "error creating %s secret", credentialsSecretName) }