Skip to content

Commit

Permalink
move maintenance to a separate folder
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 7, 2025
1 parent 4ce7361 commit cb26980
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
10 changes: 5 additions & 5 deletions pkg/controller/backup_repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ import (
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/constant"
"github.com/vmware-tanzu/velero/pkg/label"
"github.com/vmware-tanzu/velero/pkg/repository"
repoconfig "github.com/vmware-tanzu/velero/pkg/repository/config"
"github.com/vmware-tanzu/velero/pkg/repository/maintenance"
repomanager "github.com/vmware-tanzu/velero/pkg/repository/manager"
"github.com/vmware-tanzu/velero/pkg/util/kube"
"github.com/vmware-tanzu/velero/pkg/util/logging"
Expand Down Expand Up @@ -229,7 +229,7 @@ func (r *BackupRepoReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, errors.Wrap(err, "error check and run repo maintenance jobs")
}

Check warning on line 230 in pkg/controller/backup_repository_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/backup_repository_controller.go#L228-L230

Added lines #L228 - L230 were not covered by tests

if err := repository.DeleteOldMaintenanceJobs(r.Client, req.Name, r.keepLatestMaintenanceJobs); err != nil {
if err := maintenance.DeleteOldJobs(r.Client, req.Name, r.keepLatestMaintenanceJobs); err != nil {
log.WithError(err).Warn("Failed to delete old maintenance jobs")
}

Check warning on line 234 in pkg/controller/backup_repository_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/backup_repository_controller.go#L232-L234

Added lines #L232 - L234 were not covered by tests
}
Expand Down Expand Up @@ -325,7 +325,7 @@ func ensureRepo(repo *velerov1api.BackupRepository, repoManager repomanager.Mana
}

func (r *BackupRepoReconciler) recallMaintenance(ctx context.Context, req *velerov1api.BackupRepository, log logrus.FieldLogger) error {
history, err := repository.WaitAllMaintenanceJobComplete(ctx, r.Client, req, defaultMaintenanceStatusQueueLength, log)
history, err := maintenance.WaitAllJobsComplete(ctx, r.Client, req, defaultMaintenanceStatusQueueLength, log)
if err != nil {
return errors.Wrapf(err, "error waiting incomplete repo maintenance job for repo %s", req.Name)
}
Expand Down Expand Up @@ -424,8 +424,8 @@ func isIdenticalHistory(a, b []velerov1api.BackupRepositoryMaintenanceStatus) bo
return true
}

var funcStartMaintenanceJob = repository.StartMaintenanceJob
var funcWaitMaintenanceJobComplete = repository.WaitMaintenanceJobComplete
var funcStartMaintenanceJob = maintenance.StartNewJob
var funcWaitMaintenanceJobComplete = maintenance.WaitJobComplete

func (r *BackupRepoReconciler) runMaintenanceIfDue(ctx context.Context, req *velerov1api.BackupRepository, log logrus.FieldLogger) error {
startTime := r.clock.Now()
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/backup_repository_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/builder"
"github.com/vmware-tanzu/velero/pkg/repository"
"github.com/vmware-tanzu/velero/pkg/repository/maintenance"
repomokes "github.com/vmware-tanzu/velero/pkg/repository/mocks"
repotypes "github.com/vmware-tanzu/velero/pkg/repository/types"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
Expand Down Expand Up @@ -756,7 +756,7 @@ func TestRecallMaintenance(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "job1",
Namespace: velerov1api.DefaultNamespace,
Labels: map[string]string{repository.RepositoryNameLabel: "repo"},
Labels: map[string]string{maintenance.RepositoryNameLabel: "repo"},
CreationTimestamp: metav1.Time{Time: now.Add(time.Hour)},
},
Status: batchv1.JobStatus{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package repository
package maintenance

import (
"context"
Expand Down Expand Up @@ -67,8 +67,8 @@ func GenerateJobName(repo string) string {
return jobName
}

// DeleteOldMaintenanceJobs deletes old maintenance jobs and keeps the latest N jobs
func DeleteOldMaintenanceJobs(cli client.Client, repo string, keep int) error {
// DeleteOldJobs deletes old maintenance jobs and keeps the latest N jobs
func DeleteOldJobs(cli client.Client, repo string, keep int) error {
// Get the maintenance job list by label
jobList := &batchv1.JobList{}
err := cli.List(context.TODO(), jobList, client.MatchingLabels(map[string]string{RepositoryNameLabel: repo}))
Expand Down Expand Up @@ -119,7 +119,7 @@ func waitForJobComplete(ctx context.Context, client client.Client, ns string, jo
return ret, err
}

func getMaintenanceResultFromJob(cli client.Client, job *batchv1.Job) (string, error) {
func getResultFromJob(cli client.Client, job *batchv1.Job) (string, error) {
// Get the maintenance job related pod by label selector
podList := &v1.PodList{}
err := cli.List(context.TODO(), podList, client.InNamespace(job.Namespace), client.MatchingLabels(map[string]string{"job-name": job.Name}))
Expand Down Expand Up @@ -148,7 +148,7 @@ func getMaintenanceResultFromJob(cli client.Client, job *batchv1.Job) (string, e
return terminated.Message, nil
}

// getMaintenanceJobConfig is called to get the Maintenance Job Config for the
// getJobConfig is called to get the Maintenance Job Config for the
// BackupRepository specified by the repo parameter.
//
// Params:
Expand All @@ -159,7 +159,7 @@ func getMaintenanceResultFromJob(cli client.Client, job *batchv1.Job) (string, e
// veleroNamespace: the Velero-installed namespace. It's used to retrieve the BackupRepository.
// repoMaintenanceJobConfig: the repository maintenance job ConfigMap name.
// repo: the BackupRepository needs to run the maintenance Job.
func getMaintenanceJobConfig(
func getJobConfig(
ctx context.Context,
client client.Client,
logger logrus.FieldLogger,
Expand Down Expand Up @@ -241,8 +241,8 @@ func getMaintenanceJobConfig(
return result, nil
}

// WaitMaintenanceJobComplete waits the completion of the specified maintenance job and return the BackupRepositoryMaintenanceStatus
func WaitMaintenanceJobComplete(cli client.Client, ctx context.Context, jobName, ns string, logger logrus.FieldLogger) (velerov1api.BackupRepositoryMaintenanceStatus, error) {
// WaitJobComplete waits the completion of the specified maintenance job and return the BackupRepositoryMaintenanceStatus
func WaitJobComplete(cli client.Client, ctx context.Context, jobName, ns string, logger logrus.FieldLogger) (velerov1api.BackupRepositoryMaintenanceStatus, error) {
log := logger.WithField("job name", jobName)

maintenanceJob, err := waitForJobComplete(ctx, cli, ns, jobName)
Expand All @@ -252,17 +252,17 @@ func WaitMaintenanceJobComplete(cli client.Client, ctx context.Context, jobName,

log.Info("Maintenance repo complete")

result, err := getMaintenanceResultFromJob(cli, maintenanceJob)
result, err := getResultFromJob(cli, maintenanceJob)
if err != nil {
log.WithError(err).Warn("Failed to get maintenance job result")
}

Check warning on line 258 in pkg/repository/maintenance/maintenance.go

View check run for this annotation

Codecov / codecov/patch

pkg/repository/maintenance/maintenance.go#L253-L258

Added lines #L253 - L258 were not covered by tests

return composeMaintenanceStatusFromJob(maintenanceJob, result), nil
return composeStatusFromJob(maintenanceJob, result), nil

Check warning on line 260 in pkg/repository/maintenance/maintenance.go

View check run for this annotation

Codecov / codecov/patch

pkg/repository/maintenance/maintenance.go#L260

Added line #L260 was not covered by tests
}

// WaitAllMaintenanceJobComplete checks all the incomplete maintenance jobs of the specified repo and wait for them to complete,
// WaitAllJobsComplete checks all the incomplete maintenance jobs of the specified repo and wait for them to complete,
// and then return the maintenance jobs' status in the range of limit
func WaitAllMaintenanceJobComplete(ctx context.Context, cli client.Client, repo *velerov1api.BackupRepository, limit int, log logrus.FieldLogger) ([]velerov1api.BackupRepositoryMaintenanceStatus, error) {
func WaitAllJobsComplete(ctx context.Context, cli client.Client, repo *velerov1api.BackupRepository, limit int, log logrus.FieldLogger) ([]velerov1api.BackupRepositoryMaintenanceStatus, error) {
jobList := &batchv1.JobList{}
err := cli.List(context.TODO(), jobList, &client.ListOptions{
Namespace: repo.Namespace,
Expand Down Expand Up @@ -303,19 +303,19 @@ func WaitAllMaintenanceJobComplete(ctx context.Context, cli client.Client, repo
job = updated

Check warning on line 303 in pkg/repository/maintenance/maintenance.go

View check run for this annotation

Codecov / codecov/patch

pkg/repository/maintenance/maintenance.go#L303

Added line #L303 was not covered by tests
}

message, err := getMaintenanceResultFromJob(cli, job)
message, err := getResultFromJob(cli, job)
if err != nil {
return nil, errors.Wrapf(err, "error getting maintenance job[%s] result", job.Name)
}

history = append(history, composeMaintenanceStatusFromJob(job, message))
history = append(history, composeStatusFromJob(job, message))
}

return history, nil
}

// StartMaintenanceJob creates a new maintenance job
func StartMaintenanceJob(cli client.Client, ctx context.Context, repo *velerov1api.BackupRepository, repoMaintenanceJobConfig string,
// StartNewJob creates a new maintenance job
func StartNewJob(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{}
if err := cli.Get(ctx, client.ObjectKey{Namespace: repo.Namespace, Name: repo.Spec.BackupStorageLocation}, bsl); err != nil {
Expand All @@ -329,7 +329,7 @@ func StartMaintenanceJob(cli client.Client, ctx context.Context, repo *velerov1a
"repo UID": repo.UID,
})

jobConfig, err := getMaintenanceJobConfig(
jobConfig, err := getJobConfig(
ctx,
cli,
log,
Expand All @@ -346,7 +346,7 @@ func StartMaintenanceJob(cli client.Client, ctx context.Context, repo *velerov1a

log.Info("Starting maintenance repo")

maintenanceJob, err := buildMaintenanceJob(cli, ctx, repo, bsl.Name, jobConfig, podResources, logLevel, logFormat)
maintenanceJob, err := buildJob(cli, ctx, repo, bsl.Name, jobConfig, podResources, logLevel, logFormat)
if err != nil {
return "", errors.Wrap(err, "error to build maintenance job")
}

Check warning on line 352 in pkg/repository/maintenance/maintenance.go

View check run for this annotation

Codecov / codecov/patch

pkg/repository/maintenance/maintenance.go#L347-L352

Added lines #L347 - L352 were not covered by tests
Expand All @@ -362,7 +362,7 @@ func StartMaintenanceJob(cli client.Client, ctx context.Context, repo *velerov1a
return maintenanceJob.Name, nil

Check warning on line 362 in pkg/repository/maintenance/maintenance.go

View check run for this annotation

Codecov / codecov/patch

pkg/repository/maintenance/maintenance.go#L360-L362

Added lines #L360 - L362 were not covered by tests
}

func buildMaintenanceJob(cli client.Client, ctx context.Context, repo *velerov1api.BackupRepository, bslName string, config *JobConfigs,
func buildJob(cli client.Client, ctx context.Context, repo *velerov1api.BackupRepository, bslName string, config *JobConfigs,
podResources kube.PodResources, logLevel logrus.Level, logFormat *logging.FormatFlag) (*batchv1.Job, error) {
// Get the Velero server deployment
deployment := &appsv1.Deployment{}
Expand Down Expand Up @@ -479,7 +479,7 @@ func buildMaintenanceJob(cli client.Client, ctx context.Context, repo *velerov1a
return job, nil
}

func composeMaintenanceStatusFromJob(job *batchv1.Job, message string) velerov1api.BackupRepositoryMaintenanceStatus {
func composeStatusFromJob(job *batchv1.Job, message string) velerov1api.BackupRepositoryMaintenanceStatus {
result := velerov1api.BackupRepositoryMaintenanceSucceeded
if job.Status.Failed > 0 {
result = velerov1api.BackupRepositoryMaintenanceFailed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package repository
package maintenance

import (
"context"
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestGenerateJobName1(t *testing.T) {
})
}
}
func TestDeleteOldMaintenanceJobs(t *testing.T) {
func TestDeleteOldJobs(t *testing.T) {
// Set up test repo and keep value
repo := "test-repo"
keep := 2
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestDeleteOldMaintenanceJobs(t *testing.T) {
cli := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()

// Call the function
err := DeleteOldMaintenanceJobs(cli, repo, keep)
err := DeleteOldJobs(cli, repo, keep)
assert.NoError(t, err)

// Get the remaining jobs
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestWaitForJobComplete(t *testing.T) {
}
}

func TestGetMaintenanceResultFromJob(t *testing.T) {
func TestGetResultFromJob(t *testing.T) {
// Set up test job
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -230,7 +230,7 @@ func TestGetMaintenanceResultFromJob(t *testing.T) {
cli := fake.NewClientBuilder().WithObjects(job, pod).Build()

// test an error should be returned
result, err := getMaintenanceResultFromJob(cli, job)
result, err := getResultFromJob(cli, job)
assert.Error(t, err)
assert.Equal(t, "", result)

Expand All @@ -245,7 +245,7 @@ func TestGetMaintenanceResultFromJob(t *testing.T) {

// Test an error should be returned
cli = fake.NewClientBuilder().WithObjects(job, pod).Build()
result, err = getMaintenanceResultFromJob(cli, job)
result, err = getResultFromJob(cli, job)
assert.Error(t, err)
assert.Equal(t, "", result)

Expand All @@ -264,12 +264,12 @@ func TestGetMaintenanceResultFromJob(t *testing.T) {

// This call should return the termination message with no error
cli = fake.NewClientBuilder().WithObjects(job, pod).Build()
result, err = getMaintenanceResultFromJob(cli, job)
result, err = getResultFromJob(cli, job)
assert.NoError(t, err)
assert.Equal(t, "test message", result)
}

func TestGetMaintenanceJobConfig(t *testing.T) {
func TestGetJobConfig(t *testing.T) {
ctx := context.Background()
logger := logrus.New()
veleroNamespace := "velero"
Expand Down Expand Up @@ -425,7 +425,7 @@ func TestGetMaintenanceJobConfig(t *testing.T) {
fakeClient = velerotest.NewFakeControllerRuntimeClient(t)
}

jobConfig, err := getMaintenanceJobConfig(
jobConfig, err := getJobConfig(
ctx,
fakeClient,
logger,
Expand All @@ -444,7 +444,7 @@ func TestGetMaintenanceJobConfig(t *testing.T) {
}
}

func TestWaitAllMaintenanceJobComplete(t *testing.T) {
func TestWaitAlJobsComplete(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)

veleroNamespace := "velero"
Expand Down Expand Up @@ -712,7 +712,7 @@ func TestWaitAllMaintenanceJobComplete(t *testing.T) {

fakeClient := fakeClientBuilder.WithRuntimeObjects(test.kubeClientObj...).Build()

history, err := WaitAllMaintenanceJobComplete(test.ctx, fakeClient, repo, 3, velerotest.NewLogger())
history, err := WaitAllJobsComplete(test.ctx, fakeClient, repo, 3, velerotest.NewLogger())

if test.expectedError != "" {
assert.EqualError(t, err, test.expectedError)
Expand All @@ -733,7 +733,7 @@ func TestWaitAllMaintenanceJobComplete(t *testing.T) {
cancel()
}

func TestBuildMaintenanceJob(t *testing.T) {
func TestBuildJob(t *testing.T) {
testCases := []struct {
name string
m *JobConfigs
Expand Down Expand Up @@ -872,7 +872,7 @@ func TestBuildMaintenanceJob(t *testing.T) {
cli := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).Build()

// Call the function to test
job, err := buildMaintenanceJob(cli, context.TODO(), param.BackupRepo, param.BackupLocation.Name, tc.m, *tc.m.PodResources, tc.logLevel, tc.logFormat)
job, err := buildJob(cli, context.TODO(), param.BackupRepo, param.BackupLocation.Name, tc.m, *tc.m.PodResources, tc.logLevel, tc.logFormat)

// Check the error
if tc.expectedError {
Expand Down

0 comments on commit cb26980

Please sign in to comment.