Repository: kubeclipper/kubeclipper
Pull request #379 (2022-11-14 03:23:25)

update the code of month-end cronbackup
Signed-off-by: Liucw <liu.chuwei@99cloud.net>

<!-- Thanks for sending a pull request! Here are some tips for you:

1. If you want **faster** PR reviews, read how: https://github.com/kubeclipper/community/blob/master/developer-guide/development/the-pr-author-guide-to-getting-through-code-review.md
2. In case you want to know how your PR got reviewed, read: https://github.com/kubeclipper/community/blob/master/developer-guide/development/code-review-guide.md
3. Here are some coding convetions followed by Kubeclipper community: https://github.com/kubeclipper/community/blob/master/developer-guide/development/coding-conventions.md
-->

### What type of PR is this?
/kind bug
<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind feature
/kind design

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->


### What this PR does / why we need it:

### Which issue(s) this PR fixes:
<!--
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes https://github.com/kubeclipper/kubeclipper/issues/365

### Special notes for reviewers:
```
```

### Does this PR introduced a user-facing change?
<!--
If no, just write "None" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".

For more information on release notes see: https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md
-->
```release-note
None
```

### Additional documentation, usage docs, etc.:
<!--
This section can be blank if this pull request does not require a release note.
Please use the following format for linking documentation or pass the
section below:
- [KEP]: <link>
- [Usage]: <link>
- [Other doc]: <link>
-->
```docs

```
@zhuzhenfan @x893675 

codecov-commenter: # [Codecov](https://codecov.io/gh/kubeclipper/kubeclipper/pull/379?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper) Report
> Merging [#379](https://codecov.io/gh/kubeclipper/kubeclipper/pull/379?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper) (fe10a8f) into [master](https://codecov.io/gh/kubeclipper/kubeclipper/commit/50c9254339d702729c0fa18c959a0d0c869ff9bc?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper) (50c9254) will **decrease** coverage by `0.00%`.
> The diff coverage is `33.33%`.

[![Impacted file tree graph](https://codecov.io/gh/kubeclipper/kubeclipper/pull/379/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/379?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper)

```diff
@@            Coverage Diff             @@
##           master     #379      +/-   ##
==========================================
- Coverage   12.26%   12.26%   -0.01%     
==========================================
  Files         102      102              
  Lines       16853    16851       -2     
==========================================
- Hits         2067     2066       -1     
+ Misses      14539    14538       -1     
  Partials      247      247              
```


| [Impacted Files](https://codecov.io/gh/kubeclipper/kubeclipper/pull/379?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/379/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubeclipper#diff-cGtnL2FwaXMvY29yZS92MS9oYW5kbGVyLmdv) | `0.32% <0.00%> (+<0.01%)` | :arrow_up: |
| [pkg/controller/cronbackupcontroller/controller.go](https://codecov.io/gh/kubeclipper/kubeclipper/pull/379/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: f6c50e803560b9e357b6b4d1bd27b8edb930bce1</details>
kubeclipper-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubeclipper/kubeclipper/pull/379#" title="Author self-approved">Metrora</a>*, *<a href="https://github.com/kubeclipper/kubeclipper/pull/379#issuecomment-1313118863" 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/master/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":[]} -->
x893675: /cherrypick release-1.3
kubeclipper-bot: @x893675: once the present PR merges, I will cherry-pick it on top of release-1.3 in a new PR and assign it to you.

<details>

In response to [this](https://github.com/kubeclipper/kubeclipper/pull/379#issuecomment-1313118966):

>/cherrypick release-1.3


Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>

Patch:
diff --git a/pkg/apis/core/v1/handler.go b/pkg/apis/core/v1/handler.go
index f4e052e..5909f2f 100644
--- a/pkg/apis/core/v1/handler.go
+++ b/pkg/apis/core/v1/handler.go
@@ -3512,11 +3512,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())
 			}
 		})