Skip to content

Commit

Permalink
(cron): Adding separate mins and hours for minChaosInterval (#23)
Browse files Browse the repository at this point in the history
* (cron): Adding separate mins and hours for minChaosInterval

Signed-off-by: shubham chaudhary <[email protected]>

* (engine): updating minchaosinterval schema

Signed-off-by: shubham chaudhary <[email protected]>
  • Loading branch information
Shubham Chaudhary authored Aug 28, 2021
1 parent b1062b6 commit 7c64c81
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 52 deletions.
4 changes: 3 additions & 1 deletion deploy/crds/chaosschedule_cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ spec:
startTime: "2020-05-12T05:47:00Z" #should be modified according to current UTC Time, for type=repeat
endTime: "2020-09-13T02:58:00Z" #should be modified according to current UTC Time, for type=repeat
properties:
minChaosInterval: "2m" #format should be like "10m" or "2h" accordingly for minutes and hours, for type=repeat
minChaosInterval:
minute:
everyNthMinute: 2
workHours:
includedHours: 0-12
workDays:
Expand Down
18 changes: 16 additions & 2 deletions deploy/crds/chaosschedule_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,22 @@ spec:
properties:
properties:
minChaosInterval:
pattern: (([1-6][0-9]|[1-9])m)|(\d+h)
type: string
properties:
hour:
properties:
everyNthHour:
type: integer
minuteOfTheHour:
type: integer
type: object
minute:
properties:
everyNthMinute:
type: integer
type: object
type: object
minProperties: 1
maxProperties: 1
random:
type: boolean
type: object
Expand Down
19 changes: 18 additions & 1 deletion pkg/apis/litmuschaos/v1alpha1/chaosschedule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,28 @@ type TimeRange struct {
//ScheduleRepeatProperties will define the properties needed by the schedule to inject chaos
type ScheduleRepeatProperties struct {
//Minimum Period b/w two iterations of chaos experiments batch run
MinChaosInterval string `json:"minChaosInterval,omitempty"`
MinChaosInterval *MinChaosInterval `json:"minChaosInterval,omitempty"`
//Whether the chaos is to be scheduled at a random time or not
Random bool `json:"random,omitempty"`
}

// MinChaosInterval contains hours and minutes b/w each iterations
type MinChaosInterval struct {
Hour *Hour `json:"hour,omitempty"`
Minute *Minute `json:"minute,omitempty"`
}

// Hour contains hours and minutes b/w each schedule
type Hour struct {
EveryNthHour int `json:"everyNthHour,omitempty"`
MinuteOfTheHour int `json:"minuteOfTheHour,omitempty"`
}

// Minute contains minute b/w each schedule
type Minute struct {
EveryNthMinute int `json:"everyNthMinute,omitempty"`
}

//WorkHours specify in which hours of the day chaos is to be injected
type WorkHours struct {
//Hours of the day when experiments batch run is scheduled
Expand Down
87 changes: 69 additions & 18 deletions pkg/apis/litmuschaos/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 11 additions & 30 deletions pkg/controller/chaosscheduler/createEngineForRepeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (schedulerReconcile *reconcileScheduler) createNewEngine(cs *types.Schedule
return reconcile.Result{}, err
}
time.Sleep(1 * time.Second)

schedulerReconcile.reqLogger.Info("ChaosEngine has been created", "ChaosEngine Name", engineReq.Name)
return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -301,11 +301,6 @@ func parseCronData(data string) (int, int, error) {

func (schedulerReconcile *reconcileScheduler) scheduleRepeat(cs *types.SchedulerInfo) (string, time.Duration, error) {

interval, err := fetchInterval(cs.Instance.Spec.Schedule.Repeat.Properties.MinChaosInterval)
if err != nil {
return "", time.Duration(0), errors.New("error in parsing minChaosInterval(make sure to include 'm' or 'h' suffix for minutes and hours respectively)")
}

/* includedDays will be given in form comma seperated
* list such as 0,2,4 or Mon,Wed,Sat
* or in the range form such as 2-4 or Mon-Wed
Expand All @@ -326,36 +321,22 @@ func (schedulerReconcile *reconcileScheduler) scheduleRepeat(cs *types.Scheduler
}

// One of the minChaosInterval or instances is mandatory to be given
if interval != 0 {
/* MinChaosInterval will be in form of "10m" or "2h"
* where 'm' or 'h' indicating "minutes" or "hours" respectively
*/
if strings.Contains(cs.Instance.Spec.Schedule.Repeat.Properties.MinChaosInterval, "m") {
cron := fmt.Sprintf("*/%d %s * * %s", interval, includedHours, includedDays)
minChaosInterval := cs.Instance.Spec.Schedule.Repeat.Properties.MinChaosInterval
if minChaosInterval != nil && (minChaosInterval.Hour != nil || minChaosInterval.Minute != nil) {
if minChaosInterval.Minute != nil {
cron := fmt.Sprintf("*/%d %s * * %s", minChaosInterval.Minute.EveryNthMinute, includedHours, includedDays)
schedulerReconcile.reqLogger.Info("CronString formed ", "Cron String", cron)
return cron, time.Minute * time.Duration(minChaosInterval.Minute.EveryNthMinute), nil
}
if minChaosInterval.Hour != nil {
cron := fmt.Sprintf("%d %s/%d * * %s", minChaosInterval.Hour.MinuteOfTheHour, includedHours, minChaosInterval.Hour.EveryNthHour, includedDays)
schedulerReconcile.reqLogger.Info("CronString formed ", "Cron String", cron)
return cron, time.Minute * time.Duration(interval), nil
return cron, time.Hour * time.Duration(minChaosInterval.Hour.EveryNthHour), nil
}
cron := fmt.Sprintf("0 %s/%d * * %s", includedHours, interval, includedDays)
schedulerReconcile.reqLogger.Info("CronString formed ", "Cron String", cron)
return cron, time.Hour * time.Duration(interval), nil
}
return "", time.Duration(0), errors.New("MinChaosInterval not found")
}

func fetchInterval(minChaosInterval string) (int, error) {
/* MinChaosInterval will be in form of "10m" or "2h"
* where 'm' or 'h' indicating "minutes" or "hours" respectively
*/
if minChaosInterval == "" {
return 0, nil
} else if strings.Contains(minChaosInterval, "h") {
return strconv.Atoi(strings.Split(minChaosInterval, "h")[0])
} else if strings.Contains(minChaosInterval, "m") {
return strconv.Atoi(strings.Split(minChaosInterval, "m")[0])
}
return 0, errors.New("minChaosInterval should be in either minutes or hours and the prefix should be 'm' or 'h' respectively")
}

// getTimeHash returns Unix Epoch Time
func getTimeHash(scheduledTime time.Time) int64 {
return scheduledTime.Unix()
Expand Down

0 comments on commit 7c64c81

Please sign in to comment.