-
Notifications
You must be signed in to change notification settings - Fork 900
/
rb_application_failover_controller.go
264 lines (229 loc) · 10.1 KB
/
rb_application_failover_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Copyright 2023 The Karmada Authors.
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 applicationfailover
import (
"context"
"fmt"
"math"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/features"
"github.com/karmada-io/karmada/pkg/resourceinterpreter"
"github.com/karmada-io/karmada/pkg/sharedcli/ratelimiterflag"
"github.com/karmada-io/karmada/pkg/util/helper"
)
// RBApplicationFailoverControllerName is the controller name that will be used when reporting events.
const RBApplicationFailoverControllerName = "resource-binding-application-failover-controller"
// RBApplicationFailoverController is to sync ResourceBinding's application failover behavior.
type RBApplicationFailoverController struct {
client.Client
EventRecorder record.EventRecorder
RateLimiterOptions ratelimiterflag.Options
// workloadUnhealthyMap records which clusters the specific resource is in an unhealthy state
workloadUnhealthyMap *workloadUnhealthyMap
ResourceInterpreter resourceinterpreter.ResourceInterpreter
}
// Reconcile performs a full reconciliation for the object referred to by the Request.
// The Controller will requeue the Request to be processed again if an error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (c *RBApplicationFailoverController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
klog.V(4).Infof("Reconciling ResourceBinding %s.", req.NamespacedName.String())
binding := &workv1alpha2.ResourceBinding{}
if err := c.Client.Get(ctx, req.NamespacedName, binding); err != nil {
if apierrors.IsNotFound(err) {
c.workloadUnhealthyMap.delete(req.NamespacedName)
return controllerruntime.Result{}, nil
}
return controllerruntime.Result{}, err
}
if !c.bindingFilter(binding) {
c.workloadUnhealthyMap.delete(req.NamespacedName)
return controllerruntime.Result{}, nil
}
retryDuration, err := c.syncBinding(ctx, binding)
if err != nil {
return controllerruntime.Result{}, err
}
if retryDuration > 0 {
klog.V(4).Infof("Retry to check health status of the workload after %v seconds.", retryDuration.Seconds())
return controllerruntime.Result{RequeueAfter: retryDuration}, nil
}
return controllerruntime.Result{}, nil
}
func (c *RBApplicationFailoverController) detectFailure(clusters []string, tolerationSeconds *int32, key types.NamespacedName) (int32, []string) {
var needEvictClusters []string
duration := int32(math.MaxInt32)
for _, cluster := range clusters {
if !c.workloadUnhealthyMap.hasWorkloadBeenUnhealthy(key, cluster) {
c.workloadUnhealthyMap.setTimeStamp(key, cluster)
if duration > *tolerationSeconds {
duration = *tolerationSeconds
}
continue
}
// When the workload in a cluster is in an unhealthy state for more than the tolerance time,
// and the cluster has not been in the GracefulEvictionTasks,
// the cluster will be added to the list that needs to be evicted.
unHealthyTimeStamp := c.workloadUnhealthyMap.getTimeStamp(key, cluster)
timeNow := metav1.Now()
if timeNow.After(unHealthyTimeStamp.Add(time.Duration(*tolerationSeconds) * time.Second)) {
needEvictClusters = append(needEvictClusters, cluster)
} else {
if duration > *tolerationSeconds-int32(timeNow.Sub(unHealthyTimeStamp.Time).Seconds()) {
duration = *tolerationSeconds - int32(timeNow.Sub(unHealthyTimeStamp.Time).Seconds())
}
}
}
if duration == int32(math.MaxInt32) {
duration = 0
}
return duration, needEvictClusters
}
func (c *RBApplicationFailoverController) syncBinding(ctx context.Context, binding *workv1alpha2.ResourceBinding) (time.Duration, error) {
key := types.NamespacedName{Name: binding.Name, Namespace: binding.Namespace}
tolerationSeconds := binding.Spec.Failover.Application.DecisionConditions.TolerationSeconds
allClusters := sets.New[string]()
for _, cluster := range binding.Spec.Clusters {
allClusters.Insert(cluster.Name)
}
unhealthyClusters, others := distinguishUnhealthyClustersWithOthers(binding.Status.AggregatedStatus, binding.Spec)
duration, needEvictClusters := c.detectFailure(unhealthyClusters, tolerationSeconds, key)
err := c.evictBinding(binding, needEvictClusters)
if err != nil {
klog.Errorf("Failed to evict binding(%s/%s), err: %v.", binding.Namespace, binding.Name, err)
return 0, err
}
if len(needEvictClusters) != 0 {
if err = c.updateBinding(ctx, binding, allClusters, needEvictClusters); err != nil {
return 0, err
}
}
// Cleanup clusters on which the application status is not unhealthy and clusters that have been evicted or removed in the workloadUnhealthyMap.
c.workloadUnhealthyMap.deleteIrrelevantClusters(key, allClusters, others)
return time.Duration(duration) * time.Second, nil
}
func (c *RBApplicationFailoverController) evictBinding(binding *workv1alpha2.ResourceBinding, clusters []string) error {
for _, cluster := range clusters {
switch binding.Spec.Failover.Application.PurgeMode {
case policyv1alpha1.Graciously:
if features.FeatureGate.Enabled(features.GracefulEviction) {
binding.Spec.GracefulEvictCluster(cluster, workv1alpha2.NewTaskOptions(workv1alpha2.WithProducer(RBApplicationFailoverControllerName),
workv1alpha2.WithReason(workv1alpha2.EvictionReasonApplicationFailure), workv1alpha2.WithGracePeriodSeconds(binding.Spec.Failover.Application.GracePeriodSeconds)))
} else {
err := fmt.Errorf("GracefulEviction featureGate must be enabled when purgeMode is %s", policyv1alpha1.Graciously)
klog.Error(err)
return err
}
case policyv1alpha1.Never:
if features.FeatureGate.Enabled(features.GracefulEviction) {
binding.Spec.GracefulEvictCluster(cluster, workv1alpha2.NewTaskOptions(workv1alpha2.WithProducer(RBApplicationFailoverControllerName),
workv1alpha2.WithReason(workv1alpha2.EvictionReasonApplicationFailure), workv1alpha2.WithSuppressDeletion(ptr.To[bool](true))))
} else {
err := fmt.Errorf("GracefulEviction featureGate must be enabled when purgeMode is %s", policyv1alpha1.Never)
klog.Error(err)
return err
}
case policyv1alpha1.Immediately:
binding.Spec.RemoveCluster(cluster)
}
}
return nil
}
func (c *RBApplicationFailoverController) updateBinding(ctx context.Context, binding *workv1alpha2.ResourceBinding, allClusters sets.Set[string], needEvictClusters []string) error {
if err := c.Update(ctx, binding); err != nil {
for _, cluster := range needEvictClusters {
helper.EmitClusterEvictionEventForResourceBinding(binding, cluster, c.EventRecorder, err)
}
klog.ErrorS(err, "Failed to update binding", "binding", klog.KObj(binding))
return err
}
for _, cluster := range needEvictClusters {
allClusters.Delete(cluster)
}
if !features.FeatureGate.Enabled(features.GracefulEviction) {
for _, cluster := range needEvictClusters {
helper.EmitClusterEvictionEventForResourceBinding(binding, cluster, c.EventRecorder, nil)
}
}
return nil
}
// SetupWithManager creates a controller and register to controller manager.
func (c *RBApplicationFailoverController) SetupWithManager(mgr controllerruntime.Manager) error {
c.workloadUnhealthyMap = newWorkloadUnhealthyMap()
resourceBindingPredicateFn := predicate.Funcs{
CreateFunc: func(createEvent event.CreateEvent) bool {
obj := createEvent.Object.(*workv1alpha2.ResourceBinding)
if obj.Spec.Failover == nil || obj.Spec.Failover.Application == nil {
return false
}
return true
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
oldObj := updateEvent.ObjectOld.(*workv1alpha2.ResourceBinding)
newObj := updateEvent.ObjectNew.(*workv1alpha2.ResourceBinding)
if (oldObj.Spec.Failover == nil || oldObj.Spec.Failover.Application == nil) &&
(newObj.Spec.Failover == nil || newObj.Spec.Failover.Application == nil) {
return false
}
return true
},
DeleteFunc: func(deleteEvent event.DeleteEvent) bool {
obj := deleteEvent.Object.(*workv1alpha2.ResourceBinding)
if obj.Spec.Failover == nil || obj.Spec.Failover.Application == nil {
return false
}
return true
},
GenericFunc: func(event.GenericEvent) bool { return false },
}
return controllerruntime.NewControllerManagedBy(mgr).
For(&workv1alpha2.ResourceBinding{}, builder.WithPredicates(resourceBindingPredicateFn)).
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter(c.RateLimiterOptions)}).
Complete(c)
}
func (c *RBApplicationFailoverController) bindingFilter(rb *workv1alpha2.ResourceBinding) bool {
if rb.Spec.Failover == nil || rb.Spec.Failover.Application == nil {
return false
}
if len(rb.Status.AggregatedStatus) == 0 {
return false
}
resourceKey, err := helper.ConstructClusterWideKey(rb.Spec.Resource)
if err != nil {
// Never reach
klog.Errorf("Failed to construct clusterWideKey from binding(%s/%s)", rb.Namespace, rb.Name)
return false
}
if !c.ResourceInterpreter.HookEnabled(resourceKey.GroupVersionKind(), configv1alpha1.InterpreterOperationInterpretHealth) {
return false
}
if !rb.Spec.PropagateDeps {
return false
}
return true
}