forked from kubevela/kube-trigger
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add cronjob source (kubevela#42)
* Refactor: allow the use of singleton for one Source, while also allowing non-singleton Sources, making it more extensible Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * Feat: add cronjob source Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * Chore: update cron lib Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * Feat: do not exit if config contains invalid sources Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * Chore: add tests and fix linter Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * Chore: go mod tidy Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * Docs: add cronjob example Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * fix comments Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * Chore: force lint to use the exact version Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * remove unnecessary dep Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * fix comments Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * use codecov token Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> * update lint script Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> --------- Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com> Signed-off-by: Amit Singh <singhamitch@outlook.com>
- Loading branch information
1 parent
f93644d
commit fe07c45
Showing
22 changed files
with
424 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
triggers: | ||
- source: | ||
type: cronjob | ||
properties: | ||
schedule: "* * * * *" | ||
timeZone: "Asia/Shanghai" # Optional | ||
filter: "" | ||
action: | ||
# TODO: add your action here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2023 The KubeVela Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cronjob | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Config is the config for CronJob. | ||
type Config struct { | ||
Schedule string `json:"schedule"` | ||
TimeZone string `json:"timeZone"` | ||
} | ||
|
||
func (c *Config) String() string { | ||
// When TZ is set in schedule, ignore timeZone, just use schedule as is. | ||
// This is not the intended use case, but we want to support it. | ||
if strings.Contains(c.Schedule, "TZ") { | ||
return c.Schedule | ||
} | ||
|
||
if c.TimeZone != "" { | ||
// We don't check if the timezone is valid here. | ||
// cron lib will do it. | ||
return fmt.Sprintf("TZ=%s %s", c.TimeZone, c.Schedule) | ||
} | ||
|
||
return c.Schedule | ||
} | ||
|
||
func formatSchedule(c Config) string { | ||
// When TZ is set in schedule, warn the user. This is not the intended use case. | ||
// However, it should still work, so we can continue. | ||
if strings.Contains(c.Schedule, "TZ") { | ||
logger.Warnf("do NOT set 'TZ' in schedule, setting 'timeZone' is the preferred way. With 'TZ' set, any 'timeZone' setting will be ignored.") | ||
} | ||
|
||
return c.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2023 The KubeVela Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cronjob | ||
|
||
import "testing" | ||
|
||
func TestFormatSchedule(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
schedule string | ||
timeZone string | ||
want string | ||
}{ | ||
{ | ||
name: "schedule_without_timezone", | ||
schedule: "* * * * *", | ||
timeZone: "", | ||
want: "* * * * *", | ||
}, | ||
{ | ||
name: "schedule_with_timezone", | ||
schedule: "* * * * *", | ||
timeZone: "Asia/Shanghai", | ||
want: "TZ=Asia/Shanghai * * * * *", | ||
}, | ||
{ | ||
name: "schedule_with_timezone_prefixed_unsupported_but_will_work", | ||
schedule: "TZ=Asia/Shanghai * * * * *", | ||
timeZone: "", | ||
want: "TZ=Asia/Shanghai * * * * *", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := &Config{ | ||
Schedule: tt.schedule, | ||
TimeZone: tt.timeZone, | ||
} | ||
if got := formatSchedule(*c); got != tt.want { | ||
t.Errorf("String() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.