Skip to content

Commit

Permalink
recall repo maintenance history on restart
Browse files Browse the repository at this point in the history
Signed-off-by: Lyndon-Li <[email protected]>
  • Loading branch information
Lyndon-Li committed Jan 6, 2025
1 parent db69829 commit 4aa06f7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/8580-Lyndon-Li
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue #7753, recall repo maintenance history on Velero server restart
26 changes: 10 additions & 16 deletions pkg/cmd/cli/repomantenance/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/vmware-tanzu/velero/internal/credentials"
Expand Down Expand Up @@ -88,41 +87,36 @@ func (o *Options) Run(f velerocli.Factory) {
}
}

func (o *Options) initClient(f velerocli.Factory) (client.Client, kubernetes.Interface, error) {
func (o *Options) initClient(f velerocli.Factory) (client.Client, error) {
scheme := runtime.NewScheme()
err := velerov1api.AddToScheme(scheme)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to add velero scheme")
return nil, errors.Wrap(err, "failed to add velero scheme")
}

err = v1.AddToScheme(scheme)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to add api core scheme")
return nil, errors.Wrap(err, "failed to add api core scheme")
}

config, err := f.ClientConfig()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get client config")
return nil, errors.Wrap(err, "failed to get client config")
}

cli, err := client.New(config, client.Options{
Scheme: scheme,
})
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create client")
return nil, errors.Wrap(err, "failed to create client")
}

kubeClient, err := f.KubeClient()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create kube client")
}

return cli, kubeClient, nil
return cli, nil
}

func initRepoManager(namespace string, kubeClient kubernetes.Interface, cli client.Client, logger logrus.FieldLogger) (repomanager.Manager, error) {
func initRepoManager(namespace string, cli client.Client, logger logrus.FieldLogger) (repomanager.Manager, error) {
// ensure the repo key secret is set up
if err := repokey.EnsureCommonRepositoryKey(kubeClient.CoreV1(), namespace); err != nil {
if err := repokey.EnsureCommonRepositoryKey(cli, namespace); err != nil {
return nil, errors.Wrap(err, "failed to ensure repository key")

Check warning on line 120 in pkg/cmd/cli/repomantenance/maintenance.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/cli/repomantenance/maintenance.go#L117-L120

Added lines #L117 - L120 were not covered by tests
}

Expand Down Expand Up @@ -154,12 +148,12 @@ func initRepoManager(namespace string, kubeClient kubernetes.Interface, cli clie
}

func (o *Options) runRepoPrune(f velerocli.Factory, namespace string, logger logrus.FieldLogger) error {
cli, kubeClient, err := o.initClient(f)
cli, err := o.initClient(f)
if err != nil {
return err

Check warning on line 153 in pkg/cmd/cli/repomantenance/maintenance.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/cli/repomantenance/maintenance.go#L150-L153

Added lines #L150 - L153 were not covered by tests
}

manager, err := initRepoManager(namespace, kubeClient, cli, logger)
manager, err := initRepoManager(namespace, cli, logger)
if err != nil {
return err

Check warning on line 158 in pkg/cmd/cli/repomantenance/maintenance.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/cli/repomantenance/maintenance.go#L156-L158

Added lines #L156 - L158 were not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func (s *server) checkNodeAgent() {

func (s *server) initRepoManager() error {
// ensure the repo key secret is set up
if err := repokey.EnsureCommonRepositoryKey(s.kubeClient.CoreV1(), s.namespace); err != nil {
if err := repokey.EnsureCommonRepositoryKey(s.mgr.GetClient(), s.namespace); err != nil {

Check warning on line 483 in pkg/cmd/server/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/server/server.go#L483

Added line #L483 was not covered by tests
return err
}

Expand Down
9 changes: 3 additions & 6 deletions pkg/controller/backup_repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,9 @@ func (r *BackupRepoReconciler) recallMaintenance(ctx context.Context, req *veler

log.Warn("Updating backup repository because of unrecorded histories")

if lastMaintenanceTime.After(req.Status.LastMaintenanceTime.Time) {
log.Warnf("Updating backup repository last maintenance time (%v) from history (%v)", req.Status.LastMaintenanceTime.Time, lastMaintenanceTime.Time)
}

return r.patchBackupRepository(ctx, req, func(rr *velerov1api.BackupRepository) {
if lastMaintenanceTime.After(rr.Status.LastMaintenanceTime.Time) {
log.Warnf("Updating backup repository last maintenance time (%v) from history (%v)", rr.Status.LastMaintenanceTime.Time, lastMaintenanceTime.Time)
rr.Status.LastMaintenanceTime = lastMaintenanceTime
}

Expand Down Expand Up @@ -453,8 +450,8 @@ func (r *BackupRepoReconciler) runMaintenanceIfDue(ctx context.Context, req *vel
})
}

// when WaitMaintenanceJobComplete fails, the maintenance result will be left temporarily
// If the maintenenance still completes later, recallMaintenance recalls the left onces and update LastMaintenanceTime and history
// when WaitMaintenanceJobComplete fails, the maintenance result will be left aside temporarily
// If the maintenenance still completes later, recallMaintenance recalls the left once and update LastMaintenanceTime and history
status, err := funcWaitMaintenanceJobComplete(r.Client, ctx, job, r.namespace, log)
if err != nil {
return errors.Wrapf(err, "error waiting repo maintenance completion status")
Expand Down
12 changes: 8 additions & 4 deletions pkg/repository/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
corev1api "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/vmware-tanzu/velero/pkg/builder"

"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand All @@ -36,11 +38,13 @@ const (
encryptionKey = "static-passw0rd"
)

func EnsureCommonRepositoryKey(secretClient corev1client.SecretsGetter, namespace string) error {
_, err := secretClient.Secrets(namespace).Get(context.TODO(), credentialsSecretName, metav1.GetOptions{})
func EnsureCommonRepositoryKey(cli client.Client, namespace string) error {
existing := &corev1api.Secret{}
err := cli.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: credentialsSecretName}, existing)

Check warning on line 43 in pkg/repository/keys/keys.go

View check run for this annotation

Codecov / codecov/patch

pkg/repository/keys/keys.go#L41-L43

Added lines #L41 - L43 were not covered by tests
if err != nil && !apierrors.IsNotFound(err) {
return errors.WithStack(err)
}

if err == nil {
return nil
}
Expand All @@ -58,7 +62,7 @@ func EnsureCommonRepositoryKey(secretClient corev1client.SecretsGetter, namespac
},
}

if _, err = secretClient.Secrets(namespace).Create(context.TODO(), secret, metav1.CreateOptions{}); err != nil {
if err := cli.Create(context.TODO(), secret); err != nil {

Check warning on line 65 in pkg/repository/keys/keys.go

View check run for this annotation

Codecov / codecov/patch

pkg/repository/keys/keys.go#L65

Added line #L65 was not covered by tests
return errors.Wrapf(err, "error creating %s secret", credentialsSecretName)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/repository/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ func WaitAllMaintenanceJobComplete(ctx context.Context, cli client.Client, repo
return history, nil
}

// StartMaintenanceJob creates a new maintenance job
func StartMaintenanceJob(cli client.Client, ctx context.Context, repo *velerov1api.BackupRepository, repoMaintenanceJobConfig string,
podResources kube.PodResources, logLevel logrus.Level, logFormat *logging.FormatFlag, logger logrus.FieldLogger) (string, error) {
bsl := &velerov1api.BackupStorageLocation{}
Expand Down

0 comments on commit 4aa06f7

Please sign in to comment.