/
exerciseset_controller.go
172 lines (149 loc) · 5.94 KB
/
exerciseset_controller.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Copyright 2021 Maximilian Geberl.
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 controllers
import (
"context"
"fmt"
"reflect"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
kubeteachv1alpha1 "github.com/dergeberl/kubeteach/api/v1alpha1"
)
// ExerciseSetReconciler reconciles a ExerciseSet object
type ExerciseSetReconciler struct {
client.Client
Scheme *runtime.Scheme
RequeueTime time.Duration
}
//+kubebuilder:rbac:groups=kubeteach.geberl.io,resources=taskdefinitions,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=kubeteach.geberl.io,resources=exercisesets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=kubeteach.geberl.io,resources=exercisesets/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=kubeteach.geberl.io,resources=exercisesets/finalizers,verbs=update
// Reconcile handles reconcile of an ExersiceSet
func (r *ExerciseSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
var exerciseSet kubeteachv1alpha1.ExerciseSet
err := r.Client.Get(ctx, req.NamespacedName, &exerciseSet)
if err != nil {
// ignore ExerciseSet that dose not exists
return ctrl.Result{}, client.IgnoreNotFound(err)
}
var newExerciseSetStatus kubeteachv1alpha1.ExerciseSetStatus
for _, taskDefinition := range exerciseSet.Spec.TaskDefinitions {
var taskDefinitionObject kubeteachv1alpha1.TaskDefinition
err = r.Client.Get(ctx, client.ObjectKey{Name: taskDefinition.Name, Namespace: req.Namespace}, &taskDefinitionObject)
if err != nil {
if client.IgnoreNotFound(err) != nil {
return ctrl.Result{}, err
}
// create taskDefinition
taskDefinitionObject = kubeteachv1alpha1.TaskDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: taskDefinition.Name,
Namespace: req.Namespace,
OwnerReferences: []metav1.OwnerReference{{
APIVersion: exerciseSet.APIVersion,
Kind: exerciseSet.Kind,
Name: exerciseSet.Name,
UID: exerciseSet.UID,
}},
},
Spec: taskDefinition.TaskDefinitionSpec,
}
err = r.Client.Create(ctx, &taskDefinitionObject)
if err != nil {
return ctrl.Result{}, err
}
}
// update TaskDefinition if needed
if !reflect.DeepEqual(taskDefinitionObject.Spec, taskDefinition.TaskDefinitionSpec) {
taskDefinitionObject.Spec = taskDefinition.TaskDefinitionSpec
err = r.Client.Update(ctx, &taskDefinitionObject)
if err != nil {
return ctrl.Result{}, err
}
}
// update OwnerReferences if needed
if !reflect.DeepEqual(taskDefinitionObject.OwnerReferences, []metav1.OwnerReference{{
APIVersion: exerciseSet.APIVersion,
Kind: exerciseSet.Kind,
Name: exerciseSet.Name,
UID: exerciseSet.UID,
}}) {
taskDefinitionObject.OwnerReferences = []metav1.OwnerReference{{
APIVersion: exerciseSet.APIVersion,
Kind: exerciseSet.Kind,
Name: exerciseSet.Name,
UID: exerciseSet.UID,
}}
err = r.Client.Update(ctx, &taskDefinitionObject)
if err != nil {
return ctrl.Result{}, err
}
}
// count total tasks
newExerciseSetStatus.NumberOfTasks++
// count tasks with state
if taskDefinitionObject.Status.State != nil {
switch *taskDefinitionObject.Status.State {
case StateActive:
newExerciseSetStatus.NumberOfActiveTasks++
case StatePending:
newExerciseSetStatus.NumberOfPendingTasks++
case StateSuccessful:
newExerciseSetStatus.NumberOfSuccessfulTasks++
}
} else {
newExerciseSetStatus.NumberOfUnknownTasks++
}
// count total sum of points
newExerciseSetStatus.PointsTotal += taskDefinition.TaskDefinitionSpec.Points
// count tasks without points
if taskDefinition.TaskDefinitionSpec.Points == 0 {
newExerciseSetStatus.NumberOfTasksWithoutPoints++
}
// count points from successful tasks
if taskDefinitionObject.Status.State != nil &&
*taskDefinitionObject.Status.State == StateSuccessful {
newExerciseSetStatus.PointsAchieved += taskDefinition.TaskDefinitionSpec.Points
}
}
// update status if needed
if !reflect.DeepEqual(exerciseSet.Status, newExerciseSetStatus) {
patch := []byte(`{"status": {` +
`"numberOfTasks": ` + fmt.Sprint(newExerciseSetStatus.NumberOfTasks) + `, ` +
`"numberOfActiveTasks": ` + fmt.Sprint(newExerciseSetStatus.NumberOfActiveTasks) + `, ` +
`"numberOfPendingTasks": ` + fmt.Sprint(newExerciseSetStatus.NumberOfPendingTasks) + `, ` +
`"numberOfSuccessfulTasks": ` + fmt.Sprint(newExerciseSetStatus.NumberOfSuccessfulTasks) + `, ` +
`"numberOfUnknownTasks": ` + fmt.Sprint(newExerciseSetStatus.NumberOfUnknownTasks) + `, ` +
`"numberOfTasksWithoutPoints": ` + fmt.Sprint(newExerciseSetStatus.NumberOfTasksWithoutPoints) + `, ` +
`"pointsTotal": ` + fmt.Sprint(newExerciseSetStatus.PointsTotal) + `, ` +
`"pointsAchieved": ` + fmt.Sprint(newExerciseSetStatus.PointsAchieved) + `}}`)
err = r.Client.Status().Patch(ctx, &exerciseSet, client.RawPatch(types.MergePatchType, patch))
if err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{RequeueAfter: r.RequeueTime}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *ExerciseSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&kubeteachv1alpha1.ExerciseSet{}).
Complete(r)
}