@@ -41,6 +41,7 @@ import (
41
41
"github.com/openkruise/kruise/pkg/util/refmanager"
42
42
"github.com/openkruise/kruise/pkg/util/volumeclaimtemplate"
43
43
44
+ "github.com/prometheus/client_golang/prometheus"
44
45
apps "k8s.io/api/apps/v1"
45
46
v1 "k8s.io/api/core/v1"
46
47
"k8s.io/apimachinery/pkg/api/errors"
@@ -59,13 +60,16 @@ import (
59
60
"sigs.k8s.io/controller-runtime/pkg/event"
60
61
"sigs.k8s.io/controller-runtime/pkg/handler"
61
62
"sigs.k8s.io/controller-runtime/pkg/manager"
63
+ "sigs.k8s.io/controller-runtime/pkg/metrics"
62
64
"sigs.k8s.io/controller-runtime/pkg/predicate"
63
65
"sigs.k8s.io/controller-runtime/pkg/reconcile"
64
66
"sigs.k8s.io/controller-runtime/pkg/source"
65
67
)
66
68
67
69
func init () {
68
70
flag .IntVar (& concurrentReconciles , "cloneset-workers" , concurrentReconciles , "Max concurrent workers for CloneSet controller." )
71
+ // register prometheus
72
+ metrics .Registry .MustRegister (CloneSetScaleExpectationLeakageMetrics )
69
73
}
70
74
71
75
var (
75
79
minimumReplicasToPreDownloadImage int32 = 3
76
80
)
77
81
82
+ var (
83
+ CloneSetScaleExpectationLeakageMetrics = prometheus .NewCounterVec (
84
+ prometheus.CounterOpts {
85
+ Name : "cloneset_scale_expectation_leakage" ,
86
+ Help : "CloneSet Scale Expectation Leakage Metrics" ,
87
+ // cloneSet namespace, name
88
+ }, []string {"namespace" , "name" },
89
+ )
90
+ )
91
+
78
92
// Add creates a new CloneSet Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
79
93
// and Start it when the Manager is Started.
80
94
func Add (mgr manager.Manager ) error {
@@ -229,7 +243,31 @@ func (r *ReconcileCloneSet) doReconcile(request reconcile.Request) (res reconcil
229
243
// If scaling expectations have not satisfied yet, just skip this reconcile.
230
244
if scaleSatisfied , unsatisfiedDuration , scaleDirtyPods := clonesetutils .ScaleExpectations .SatisfiedExpectations (request .String ()); ! scaleSatisfied {
231
245
if unsatisfiedDuration >= expectations .ExpectationTimeout {
246
+ // In some extreme scenarios, if the Pod is created and then quickly deleted, there may be event loss.
247
+ // Therefore, a touting mechanism is needed to ensure that clonesets can continue to work.
232
248
klog .InfoS ("Expectation unsatisfied overtime" , "cloneSet" , request , "scaleDirtyPods" , scaleDirtyPods , "overTime" , unsatisfiedDuration )
249
+ CloneSetScaleExpectationLeakageMetrics .WithLabelValues (request .Namespace , request .Name ).Add (1 )
250
+ // TODO: check the existence of resource in apiserver using client-go directly
251
+ /*for _, pods := range scaleDirtyPods {
252
+ for _, name := range pods {
253
+ _, err = kubeClient.GetGenericClient().KubeClient.CoreV1().Pods(request.Namespace).Get(context.TODO(), name, metav1.GetOptions{})
254
+ if err == nil {
255
+ klog.Warningf("CloneSet(%s/%s) ScaleExpectations leakage, but Pod(%s) already exist", request.Namespace, request.Name, name)
256
+ return reconcile.Result{RequeueAfter: 30 * time.Second}, nil
257
+ } else if !errors.IsNotFound(err) {
258
+ klog.ErrorS(err, "Failed to get Pod", "cloneSet", request, "pod", name)
259
+ return reconcile.Result{RequeueAfter: 3 * time.Second}, nil
260
+ }
261
+ }
262
+ }
263
+ klog.InfoS("CloneSet ScaleExpectation DirtyPods no longer exists, and delete ScaleExpectation", "cloneSet", request)*/
264
+ if utilfeature .DefaultFeatureGate .Enabled (features .ForceDeleteTimeoutExpectationFeatureGate ) {
265
+ klog .InfoS ("Expectation unsatisfied overtime, and force delete the timeout Expectation" , "cloneSet" , request , "scaleDirtyPods" , scaleDirtyPods , "overTime" , unsatisfiedDuration )
266
+ clonesetutils .ScaleExpectations .DeleteExpectations (request .String ())
267
+ // In order to avoid the scale expectation timeout,
268
+ // there is no subsequent Pod, CloneSet event causing CloneSet not to be scheduled
269
+ return reconcile.Result {RequeueAfter : 10 * time .Second }, nil
270
+ }
233
271
return reconcile.Result {}, nil
234
272
}
235
273
klog .V (4 ).InfoS ("Not satisfied scale" , "cloneSet" , request , "scaleDirtyPods" , scaleDirtyPods )
0 commit comments