/
schedule.go
98 lines (78 loc) · 3.47 KB
/
schedule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package controllers
import (
"errors"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"time"
"k8s.io/apimachinery/pkg/runtime"
ref "k8s.io/client-go/tools/reference"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
operatorV1 "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1"
chaosTypes "github.com/litmuschaos/chaos-scheduler/pkg/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func schedule(schedulerReconcile *reconcileScheduler, scheduler *chaosTypes.SchedulerInfo, request reconcile.Request) (reconcile.Result, error) {
if scheduler.Instance.Spec.Schedule.Now {
schedulerReconcile.reqLogger.Info("Current scheduler type derived is ", "schedulerType", "now")
return schedulerReconcile.createForNowAndOnce(scheduler, request)
} else if scheduler.Instance.Spec.Schedule.Once != nil {
schedulerReconcile.reqLogger.Info("Current scheduler type derived is ", "schedulerType", "once")
scheduleTime := time.Now()
startDuration := scheduler.Instance.Spec.Schedule.Once.ExecutionTime.Local().Sub(scheduleTime)
if startDuration.Seconds() < 0 {
if scheduler.Instance.Spec.Schedule.Once.ExecutionTime.Time.Before(scheduleTime) {
return schedulerReconcile.createForNowAndOnce(scheduler, request)
}
}
schedulerReconcile.reqLogger.Info("Time left to schedule the engine", "Duration", startDuration)
return reconcile.Result{RequeueAfter: startDuration}, nil
} else if scheduler.Instance.Spec.Schedule.Repeat != nil {
schedulerReconcile.reqLogger.Info("Current scheduler type derived is ", "schedulerType", "repeat")
/* StartDuration is the duration between current time
* and the scheduled time to start the chaos which is
* being used by reconciler to reque this resource after
* that much duration
* Chaos is being started 1 min before the scheduled time
*/
var startTime *metav1.Time
if scheduler.Instance.Spec.Schedule.Repeat.TimeRange != nil {
startTime = scheduler.Instance.Spec.Schedule.Repeat.TimeRange.StartTime
}
if startTime == nil {
startTime = &scheduler.Instance.CreationTimestamp
}
scheduleTime := time.Now()
startDuration := startTime.Local().Sub(scheduleTime)
if startDuration.Seconds() < 0 {
return schedulerReconcile.createEngineRepeat(scheduler, request)
}
schedulerReconcile.reqLogger.Info("Time left to schedule the engine", "Duration", startDuration)
return reconcile.Result{RequeueAfter: startDuration}, nil
}
return reconcile.Result{}, errors.New("ScheduleType should be one of ('now', 'once', 'repeat')")
}
func (r *ChaosScheduleReconciler) getRef(object runtime.Object) (*corev1.ObjectReference, error) {
return ref.GetReference(r.Scheme, object)
}
// getEngineFromTemplate makes an Engine from a Schedule
func (r *ChaosScheduleReconciler) getEngineFromTemplate(cs *chaosTypes.SchedulerInfo) (*operatorV1.ChaosEngine, error) {
labels := map[string]string{
"app": "chaos-engine",
"chaosUID": string(cs.Instance.UID),
}
for index, element := range cs.Instance.Labels {
labels[index] = element
}
engine := &operatorV1.ChaosEngine{}
engine.SetLabels(labels)
engine.Name = cs.Instance.Name
engine.Namespace = cs.Instance.Namespace
engine.Labels = labels
engine.Annotations = cs.Instance.Annotations
engine.Spec = cs.Instance.Spec.EngineTemplateSpec
engine.Spec.EngineState = operatorV1.EngineStateActive
if err := controllerutil.SetControllerReference(cs.Instance, engine, r.Scheme); err != nil {
return nil, err
}
return engine, nil
}