Skip to content

Commit 737eb8a

Browse files
authored
Various bug fixes (#42)
Signed-off-by: Fazle Rabbi Sarker <[email protected]>
1 parent 7978fae commit 737eb8a

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

apis/supervisor/v1alpha1/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package v1alpha1
1919
const (
2020
DefaultMaintenanceWindowKey = "supervisor.appscode.com/is-default-maintenance-window"
2121
DefaultClusterMaintenanceWindowKey = "supervisor.appscode.com/is-default-cluster-maintenance-window"
22-
DefaultBackoffLimit = 5
22+
DefaultBackoffLimit = 10
2323
)
2424

2525
// List of Condition and Phase reasons

pkg/controllers/supervisor/recommendation_controller.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,16 @@ func (r *RecommendationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
7474
_ = log.FromContext(ctx)
7575

7676
key := req.NamespacedName
77-
klog.Info("got event for Recommendation: ", key.String())
7877

7978
obj := &api.Recommendation{}
8079
if err := r.Client.Get(ctx, key, obj); err != nil {
81-
klog.Infof("Recommendation %q doesn't exist anymore", key.String())
8280
return ctrl.Result{}, client.IgnoreNotFound(err)
8381
}
8482
obj = obj.DeepCopy()
8583

8684
// Skipped outdated Recommendation
8785
if obj.Status.Outdated {
86+
klog.Infof("skipped outdated recommendation: %s", obj.Name)
8887
_, err := kmc.PatchStatus(ctx, r.Client, obj, func(obj client.Object) client.Object {
8988
in := obj.(*api.Recommendation)
9089
in.Status.ObservedGeneration = in.Generation
@@ -107,6 +106,7 @@ func (r *RecommendationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
107106
}
108107

109108
if obj.Status.FailedAttempt > pointer.Int32(obj.Spec.BackoffLimit) {
109+
klog.Infof("backoff limit exceeded for recommendation: %s", key.String())
110110
_, err := kmc.PatchStatus(ctx, r.Client, obj, func(obj client.Object) client.Object {
111111
in := obj.(*api.Recommendation)
112112
in.Status.ObservedGeneration = in.Generation
@@ -129,6 +129,23 @@ func (r *RecommendationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
129129
}
130130
}
131131

132+
if obj.Status.Phase == api.Pending && obj.Status.ApprovalStatus == api.ApprovalPending {
133+
if obj.Spec.Deadline != nil && obj.Spec.Deadline.UTC().Before(r.Clock.Now()) {
134+
_, err := kmc.PatchStatus(ctx, r.Client, obj, func(obj client.Object) client.Object {
135+
in := obj.(*api.Recommendation)
136+
in.Status.ApprovalStatus = api.ApprovalApproved
137+
in.Status.ApprovedWindow = &api.ApprovedWindow{
138+
Window: api.Immediate,
139+
}
140+
return in
141+
})
142+
if err != nil {
143+
return ctrl.Result{}, err
144+
}
145+
return ctrl.Result{RequeueAfter: r.RequeueAfterDuration}, nil
146+
}
147+
}
148+
132149
if obj.Status.ApprovalStatus == api.ApprovalApproved {
133150
if obj.Status.Phase == api.InProgress && obj.Status.CreatedOperationRef != nil {
134151
return r.checkOpsRequestStatus(ctx, obj)
@@ -157,8 +174,14 @@ func (r *RecommendationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
157174
// If WaitingForMaintenanceWindow, but certificate deadline is reached,
158175
// trigger maintenanceWork anyway, requeue otherwise
159176
if obj.Status.Phase == api.Waiting && obj.Status.Reason == api.WaitingForMaintenanceWindow {
160-
if obj.Spec.Deadline != nil && obj.Spec.Deadline.UTC().After(r.Clock.Now()) {
161-
return ctrl.Result{RequeueAfter: r.RequeueAfterDuration}, nil
177+
if obj.Spec.Deadline != nil {
178+
if obj.Spec.Deadline.UTC().After(r.Clock.Now()) && !isMaintenanceTime {
179+
return ctrl.Result{RequeueAfter: r.RequeueAfterDuration}, nil
180+
}
181+
} else {
182+
if !isMaintenanceTime {
183+
return ctrl.Result{RequeueAfter: r.RequeueAfterDuration}, nil
184+
}
162185
}
163186
}
164187

@@ -220,10 +243,12 @@ func (r *RecommendationReconciler) checkOpsRequestStatus(ctx context.Context, rc
220243
}
221244

222245
if success == nil {
246+
klog.Infof("not successful operation retry again %q", key.String())
223247
return ctrl.Result{RequeueAfter: r.RequeueAfterDuration}, nil
224248
}
225249

226250
if pointer.Bool(success) {
251+
klog.Infof("successful operation %q", key.String())
227252
_, err = kmc.PatchStatus(ctx, r.Client, rcmd, func(obj client.Object) client.Object {
228253
in := obj.(*api.Recommendation)
229254
in.Status.Phase = api.Succeeded
@@ -240,6 +265,7 @@ func (r *RecommendationReconciler) checkOpsRequestStatus(ctx context.Context, rc
240265
})
241266
return ctrl.Result{}, err
242267
} else {
268+
klog.Infof("record failed operation %q", key.String())
243269
return r.recordFailedAttempt(ctx, rcmd, errors.New("operation has been failed"))
244270
}
245271
}
@@ -256,7 +282,6 @@ func (r *RecommendationReconciler) runMaintenanceWork(ctx context.Context, rcmd
256282

257283
deadlineMgr := deadline_manager.NewManager(rcmd, r.Clock)
258284
deadlineKnocking := deadlineMgr.IsDeadlineLessThan(r.BeforeDeadlineDuration)
259-
260285
if !(maintainParallelism || deadlineKnocking) {
261286
_, err = kmc.PatchStatus(ctx, r.Client, rcmd, func(obj client.Object) client.Object {
262287
in := obj.(*api.Recommendation)
@@ -272,21 +297,24 @@ func (r *RecommendationReconciler) runMaintenanceWork(ctx context.Context, rcmd
272297

273298
unObj, err := shared.GetUnstructuredObj(rcmd.Spec.Operation)
274299
if err != nil {
300+
klog.Infof("error getting unstructured object: %v", err)
275301
return r.handleErr(ctx, rcmd, err, api.Failed)
276302
}
277303

278304
opsReqName, err := generateOpsRequestName(unObj)
279305
if err != nil {
306+
klog.Errorf("error generating ops request name: %v", err)
280307
return ctrl.Result{}, err
281308
}
282309
unObj.SetName(opsReqName)
283-
284310
err = r.Client.Create(ctx, unObj)
285311
if err != nil {
312+
klog.Infof("error creating unstructured object: %v", err)
286313
return r.handleErr(ctx, rcmd, err, api.Failed)
287314
}
288315

289316
_, err = kmc.PatchStatus(ctx, r.Client, rcmd, func(obj client.Object) client.Object {
317+
klog.Info("Created Opsrequest name is", opsReqName)
290318
in := obj.(*api.Recommendation)
291319
in.Status.Phase = api.InProgress
292320
in.Status.Reason = api.StartedExecutingOperation

pkg/maintenance/recommendation_maintenance.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/jonboulle/clockwork"
2828
"gomodules.xyz/pointer"
29+
"k8s.io/klog/v2"
2930
kmapi "kmodules.xyz/client-go/api/v1"
3031
"sigs.k8s.io/controller-runtime/pkg/client"
3132
)
@@ -70,7 +71,6 @@ func (r *RecommendationMaintenance) IsMaintenanceTime() (bool, error) {
7071
if len(mwList.Items) == 0 {
7172
return false, errors.New("no available MaintenanceWindow is found")
7273
}
73-
7474
mwPassedFlag := true
7575

7676
for _, mw := range mwList.Items {
@@ -84,6 +84,7 @@ func (r *RecommendationMaintenance) IsMaintenanceTime() (bool, error) {
8484
day := getCurrentDay(r.clock, loc)
8585

8686
mTimes, found := mw.Spec.Days[api.DayOfWeek(day)]
87+
klog.Info("DayOfWeek:", day, ":", mTimes)
8788
if found {
8889
if r.isMaintenanceTimeWindow(mTimes, loc) {
8990
return true, nil
@@ -236,11 +237,25 @@ func (r *RecommendationMaintenance) getAvailableMaintenanceWindowList() (*api.Ma
236237
}
237238
}
238239
} else if aw.MaintenanceWindow != nil {
239-
mw, err := r.getMaintenanceWindow(client.ObjectKey{Namespace: aw.MaintenanceWindow.Namespace, Name: aw.MaintenanceWindow.Name})
240-
if err != nil {
241-
return nil, err
240+
klog.Info("Found existing maintenance window")
241+
if aw.MaintenanceWindow.Kind == api.ResourceKindClusterMaintenanceWindow {
242+
clusterMWList, err := r.getMWListFromClusterMWList()
243+
if err != nil {
244+
klog.Error(err)
245+
return nil, err
246+
}
247+
mw := &api.MaintenanceWindow{
248+
Spec: clusterMWList.Items[0].Spec,
249+
Status: clusterMWList.Items[0].Status,
250+
}
251+
mwList.Items = append(mwList.Items, *mw)
252+
} else {
253+
mw, err := r.getMaintenanceWindow(client.ObjectKey{Namespace: aw.MaintenanceWindow.Namespace, Name: aw.MaintenanceWindow.Name})
254+
if err != nil {
255+
return nil, err
256+
}
257+
mwList.Items = append(mwList.Items, *mw)
242258
}
243-
mwList.Items = append(mwList.Items, *mw)
244259
} else if aw.Window == api.NextAvailable {
245260
var err error
246261
mwList, err = r.getMaintenanceWindows()

0 commit comments

Comments
 (0)