Repository: kubeclipper/kubeclipper
Pull request #381 (2022-11-14 05:30:39)

[release-1.3] update the code of month-end cronbackup
This is an automated cherry-pick of #379

```release-note
None
```

kubeclipper-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubeclipper/kubeclipper/pull/381#" title="Author self-approved">kubeclipper-bot</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **x893675** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @x893675` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubeclipper%2Fkubeclipper).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubeclipper/kubeclipper/blob/release-1.3/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["x893675"]} -->
codecov-commenter: # [Codecov](https://codecov.io/gh/kubeclipper/kubeclipper/pull/381?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper) Report
> Merging [#381](https://codecov.io/gh/kubeclipper/kubeclipper/pull/381?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper) (f6c2d19) into [release-1.3](https://codecov.io/gh/kubeclipper/kubeclipper/commit/0b106ebadda3b0bf37f33596639d4799a8064fa4?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper) (0b106eb) will **decrease** coverage by `0.02%`.
> The diff coverage is `5.88%`.

[![Impacted file tree graph](https://codecov.io/gh/kubeclipper/kubeclipper/pull/381/graphs/tree.svg?width=650&height=150&src=pr&token=3PWFG2MHTE&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper)](https://codecov.io/gh/kubeclipper/kubeclipper/pull/381?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper)

```diff
@@               Coverage Diff               @@
##           release-1.3     #381      +/-   ##
===============================================
- Coverage        12.97%   12.94%   -0.03%     
===============================================
  Files              102      102              
  Lines            15697    15723      +26     
===============================================
- Hits              2037     2036       -1     
- Misses           13415    13442      +27     
  Partials           245      245              
```


| [Impacted Files](https://codecov.io/gh/kubeclipper/kubeclipper/pull/381?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper) | Coverage Δ | |
|---|---|---|
| [pkg/apis/core/v1/handler.go](https://codecov.io/gh/kubeclipper/kubeclipper/pull/381/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper#diff-cGtnL2FwaXMvY29yZS92MS9oYW5kbGVyLmdv) | `0.30% <0.00%> (-0.01%)` | :arrow_down: |
| [pkg/component/nfs/nfs.go](https://codecov.io/gh/kubeclipper/kubeclipper/pull/381/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper#diff-cGtnL2NvbXBvbmVudC9uZnMvbmZzLmdv) | `0.60% <0.00%> (-0.05%)` | :arrow_down: |
| [pkg/controller/cronbackupcontroller/controller.go](https://codecov.io/gh/kubeclipper/kubeclipper/pull/381/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper#diff-cGtnL2NvbnRyb2xsZXIvY3JvbmJhY2t1cGNvbnRyb2xsZXIvY29udHJvbGxlci5nbw==) | `2.22% <50.00%> (-0.25%)` | :arrow_down: |

x893675: /lgtm
/approve
kubeclipper-bot: LGTM label has been added.  <details>Git tree hash: 4fb3ee03823894bf5a422b1e9741dc89ed3df706</details>
kubeclipper-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubeclipper/kubeclipper/pull/381#" title="Author self-approved">kubeclipper-bot</a>*, *<a href="https://github.com/kubeclipper/kubeclipper/pull/381#issuecomment-1313129844" title="Approved">x893675</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubeclipper%2Fkubeclipper).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubeclipper/kubeclipper/blob/release-1.3/OWNERS)~~ [x893675]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/apis/core/v1/handler.go b/pkg/apis/core/v1/handler.go
index fab43f7..1449a74 100644
--- a/pkg/apis/core/v1/handler.go
+++ b/pkg/apis/core/v1/handler.go
@@ -3001,11 +3001,10 @@ func (h *handler) UpdateCronBackup(req *restful.Request, resp *restful.Response)
 	}
 
 	if cb.Spec.Schedule != "" {
-		r := cronbackupcontroller.CronBackupReconciler{}
-		r.Now = func() time.Time {
+		now := func() time.Time {
 			return time.Now()
 		}
-		ocb.Spec.Schedule = r.ParseSchedule(cb.Spec.Schedule)
+		ocb.Spec.Schedule = cronbackupcontroller.ParseSchedule(cb.Spec.Schedule, now())
 		// update the next schedule time
 		s, _ := cron.NewParser(4 | 8 | 16 | 32 | 64).Parse(ocb.Spec.Schedule)
 		nextRunAt := metav1.NewTime(s.Next(time.Now()))
diff --git a/pkg/controller/cronbackupcontroller/controller.go b/pkg/controller/cronbackupcontroller/controller.go
index 6b1e5f2..d712f09 100644
--- a/pkg/controller/cronbackupcontroller/controller.go
+++ b/pkg/controller/cronbackupcontroller/controller.go
@@ -130,7 +130,7 @@ func (r *CronBackupReconciler) Reconcile(ctx context.Context, req ctrl.Request)
 	if cronBackup.Spec.Schedule != "" {
 		// first created cron backup, update next schedule time
 		if cronBackup.Status.NextScheduleTime == nil {
-			schedule := r.ParseSchedule(cronBackup.Spec.Schedule)
+			schedule := ParseSchedule(cronBackup.Spec.Schedule, r.Now())
 			s, _ := cron.NewParser(4 | 8 | 16 | 32 | 64).Parse(schedule)
 			// update the next schedule time
 			nextRunAt := metav1.NewTime(s.Next(time.Now()))
@@ -158,7 +158,7 @@ func (r *CronBackupReconciler) Reconcile(ctx context.Context, req ctrl.Request)
 			cronBackup.Status.LastScheduleTime = &now
 			cronBackup.Status.LastSuccessfulTime = cronBackup.Status.LastScheduleTime
 
-			schedule := r.ParseSchedule(cronBackup.Spec.Schedule)
+			schedule := ParseSchedule(cronBackup.Spec.Schedule, r.Now())
 			s, _ := cron.NewParser(4 | 8 | 16 | 32 | 64).Parse(schedule)
 			// update the next schedule time
 			nextRunAt := metav1.NewTime(s.Next(now.Time))
@@ -540,11 +540,10 @@ func (r *CronBackupReconciler) deleteBackup(log logger.Logging, clusterName stri
 	return nil
 }
 
-func (r *CronBackupReconciler) ParseSchedule(schedule string) string {
-	currentTime := r.Now()
+func ParseSchedule(schedule string, now time.Time) string {
 	arr := strings.Split(schedule, " ")
 	if arr[2] == "L" {
-		first := currentTime.AddDate(0, 0, -currentTime.Day()+1)
+		first := now.AddDate(0, 0, -now.Day()+1)
 		next := first.AddDate(0, 1, -1)
 		lastDay := strconv.Itoa(next.Day())
 		return strings.Replace(schedule, "L", lastDay, 1)


Test patch:
diff --git a/pkg/controller/cronbackupcontroller/controller_test.go b/pkg/controller/cronbackupcontroller/controller_test.go
index 0872b56..ac25c5d 100644
--- a/pkg/controller/cronbackupcontroller/controller_test.go
+++ b/pkg/controller/cronbackupcontroller/controller_test.go
@@ -78,7 +78,7 @@ func TestCronBackupReconciler_parseSchedule(t *testing.T) {
 			s := &CronBackupReconciler{
 				Now: tt.fields.Now,
 			}
-			if got := s.ParseSchedule(tt.args.schedule); got != tt.want {
+			if got := ParseSchedule(tt.args.schedule, s.Now()); got != tt.want {
 				t.Errorf("CronBackupReconciler.parseSchedule() = %v, want %v, %v", got, tt.want, tt.fields.Now().String())
 			}
 		})