forked from banzaicloud/koperator
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcruisecontroloperation_ttl_controller.go
118 lines (101 loc) · 4.46 KB
/
cruisecontroloperation_ttl_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
// Copyright © 2022 Cisco Systems, Inc. and/or its affiliates
//
// 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"
"time"
"github.com/go-logr/logr"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
banzaiv1alpha1 "github.com/banzaicloud/koperator/api/v1alpha1"
)
// CruiseControlOperationTTLReconciler reconciles CruiseControlOperation custom resources
type CruiseControlOperationTTLReconciler struct {
client.Client
Scheme *runtime.Scheme
}
// +kubebuilder:rbac:groups=kafka.banzaicloud.io,resources=cruisecontroloperations,verbs=get;list;watch;create;update;patch;delete;deletecollection
// +kubebuilder:rbac:groups=kafka.banzaicloud.io,resources=cruisecontroloperations/status,verbs=get
//nolint:gocyclo
func (r *CruiseControlOperationTTLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := logr.FromContextOrDiscard(ctx)
log.V(1).Info("reconciling CruiseControlOperation custom resources")
ccOperation := &banzaiv1alpha1.CruiseControlOperation{}
if err := r.Get(ctx, client.ObjectKey{Name: req.Name, Namespace: req.Namespace}, ccOperation); err != nil {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
return reconciled()
}
// Error reading the object - requeue the request.
return requeueWithError(log, err.Error(), err)
}
if ccOperation.GetTTLSecondsAfterFinished() == nil || ccOperation.CurrentTaskFinished() == nil {
return reconciled()
}
operationTTL := time.Duration(*ccOperation.GetTTLSecondsAfterFinished()) * time.Second
finishedAt := ccOperation.CurrentTaskFinished()
cleanupTime := finishedAt.Time.Add(operationTTL)
if IsExpired(operationTTL, finishedAt.Time) {
log.V(1).Info("cleaning up finished CruiseControlOperation", "finished", finishedAt.Time, "clean-up time", cleanupTime)
return r.delete(ctx, ccOperation)
}
// +1 sec is needed to be sure, because double to int conversion round down
reqSec := int(time.Until(cleanupTime).Seconds() + 1)
log.V(1).Info("requeue later to clean up CruiseControlOperation", "clean-up time", cleanupTime)
return requeueAfter(reqSec)
}
// SetupCruiseControlWithManager registers cruise control operation controller to the manager
func SetupCruiseControlOperationTTLWithManager(mgr ctrl.Manager) *ctrl.Builder {
cruiseControlOperationTTLPredicate := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
obj := e.Object.(*banzaiv1alpha1.CruiseControlOperation)
if obj.IsFinished() && obj.GetTTLSecondsAfterFinished() != nil && obj.GetDeletionTimestamp().IsZero() {
return true
}
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
newObj := e.ObjectNew.(*banzaiv1alpha1.CruiseControlOperation)
if newObj.IsFinished() && newObj.GetTTLSecondsAfterFinished() != nil && newObj.GetDeletionTimestamp().IsZero() {
return true
}
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
}
builder := ctrl.NewControllerManagedBy(mgr).
For(&banzaiv1alpha1.CruiseControlOperation{}).
WithEventFilter(SkipClusterRegistryOwnedResourcePredicate{}).
WithEventFilter(cruiseControlOperationTTLPredicate).
Named("CruiseControlOperationTTL")
return builder
}
func IsExpired(ttl time.Duration, finishedAt time.Time) bool {
return time.Since(finishedAt) > ttl
}
func (r *CruiseControlOperationTTLReconciler) delete(ctx context.Context, ccOperation *banzaiv1alpha1.CruiseControlOperation) (reconcile.Result, error) {
log := logr.FromContextOrDiscard(ctx)
err := r.Delete(ctx, ccOperation)
if err != nil && !apierrors.IsNotFound(err) {
return requeueWithError(log, "error is occurred when deleting finished CruiseControlOperation ", err)
}
return reconcile.Result{}, nil
}