@@ -23,18 +23,19 @@ import (
23
23
"strings"
24
24
"time"
25
25
26
- "k8s.io/apimachinery/pkg/util/sets"
27
-
28
26
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
27
+ "github.com/openkruise/kruise/pkg/features"
29
28
"github.com/openkruise/kruise/pkg/util"
30
29
utilclient "github.com/openkruise/kruise/pkg/util/client"
31
30
"github.com/openkruise/kruise/pkg/util/controllerfinder"
32
31
utildiscovery "github.com/openkruise/kruise/pkg/util/discovery"
32
+ utilfeature "github.com/openkruise/kruise/pkg/util/feature"
33
33
"github.com/openkruise/kruise/pkg/util/ratelimiter"
34
34
corev1 "k8s.io/api/core/v1"
35
35
"k8s.io/apimachinery/pkg/api/errors"
36
36
"k8s.io/apimachinery/pkg/runtime"
37
37
"k8s.io/apimachinery/pkg/types"
38
+ "k8s.io/apimachinery/pkg/util/sets"
38
39
"k8s.io/client-go/util/retry"
39
40
"k8s.io/klog/v2"
40
41
kubecontroller "k8s.io/kubernetes/pkg/controller"
67
68
// Add creates a new NodePodProbe Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
68
69
// and Start it when the Manager is Started.
69
70
func Add (mgr manager.Manager ) error {
70
- if ! utildiscovery .DiscoverGVK (controllerKind ) {
71
+ if ! utildiscovery .DiscoverGVK (controllerKind ) || ! utilfeature . DefaultFeatureGate . Enabled ( features . PodProbeMarkerGate ) {
71
72
return nil
72
73
}
73
74
return add (mgr , newReconciler (mgr ))
@@ -220,9 +221,10 @@ func (r *ReconcileNodePodProbe) syncPodFromNodePodProbe(npp *appsv1alpha1.NodePo
220
221
221
222
func (r * ReconcileNodePodProbe ) updatePodProbeStatus (pod * corev1.Pod , status appsv1alpha1.PodProbeStatus ) error {
222
223
// map[probe.name]->probeState
223
- currentConditions := make (map [string ]appsv1alpha1.ProbeState )
224
- for _ , condition := range pod .Status .Conditions {
225
- currentConditions [string (condition .Type )] = appsv1alpha1 .ProbeState (condition .Status )
224
+ currentConditions := make (map [string ]* corev1.PodCondition )
225
+ for i := range pod .Status .Conditions {
226
+ condition := & pod .Status .Conditions [i ]
227
+ currentConditions [string (condition .Type )] = condition
226
228
}
227
229
type metadata struct {
228
230
Labels map [string ]interface {} `json:"labels,omitempty"`
@@ -239,11 +241,9 @@ func (r *ReconcileNodePodProbe) updatePodProbeStatus(pod *corev1.Pod, status app
239
241
validConditionTypes := sets .NewString ()
240
242
for i := range status .ProbeStates {
241
243
probeState := status .ProbeStates [i ]
242
- // ignore the probe state
243
- if probeState .State == "" || probeState .State == currentConditions [probeState .Name ] {
244
+ if probeState .State == "" {
244
245
continue
245
246
}
246
-
247
247
// fetch podProbeMarker
248
248
ppmName , probeName := strings .Split (probeState .Name , "#" )[0 ], strings .Split (probeState .Name , "#" )[1 ]
249
249
ppm := & appsv1alpha1.PodProbeMarker {}
@@ -267,7 +267,6 @@ func (r *ReconcileNodePodProbe) updatePodProbeStatus(pod *corev1.Pod, status app
267
267
break
268
268
}
269
269
}
270
-
271
270
if conditionType != "" && validConditionTypes .Has (conditionType ) {
272
271
klog .Warningf ("NodePodProbe(%s) pod(%s/%s) condition(%s) is conflict" , ppmName , pod .Namespace , pod .Name , conditionType )
273
272
// patch pod condition
@@ -287,11 +286,9 @@ func (r *ReconcileNodePodProbe) updatePodProbeStatus(pod *corev1.Pod, status app
287
286
Message : probeState .Message ,
288
287
})
289
288
}
290
-
291
289
if len (policy ) == 0 {
292
290
continue
293
291
}
294
-
295
292
// matchedPolicy is when policy.state is equal to probeState.State, otherwise oppositePolicy
296
293
// 1. If policy[0].state = Succeeded, policy[1].state = Failed. probeState.State = Succeeded.
297
294
// So policy[0] is matchedPolicy, policy[1] is oppositePolicy
@@ -328,15 +325,14 @@ func (r *ReconcileNodePodProbe) updatePodProbeStatus(pod *corev1.Pod, status app
328
325
if len (probeConditions ) == 0 && len (probeMetadata .Labels ) == 0 && len (probeMetadata .Annotations ) == 0 {
329
326
return nil
330
327
}
331
-
332
328
//update pod metadata and status condition
333
329
podClone := pod .DeepCopy ()
334
330
if err = retry .RetryOnConflict (retry .DefaultBackoff , func () error {
335
331
if err = r .Client .Get (context .TODO (), types.NamespacedName {Namespace : pod .Namespace , Name : pod .Name }, podClone ); err != nil {
336
332
klog .Errorf ("error getting updated pod(%s/%s) from client" , pod .Namespace , pod .Name )
337
333
return err
338
334
}
339
- oldStatus := podClone .DeepCopy ()
335
+ oldStatus := podClone .Status . DeepCopy ()
340
336
for i := range probeConditions {
341
337
condition := probeConditions [i ]
342
338
util .SetPodCondition (podClone , condition )
@@ -363,7 +359,7 @@ func (r *ReconcileNodePodProbe) updatePodProbeStatus(pod *corev1.Pod, status app
363
359
podClone .Annotations [k ] = v .(string )
364
360
}
365
361
}
366
- if reflect .DeepEqual (oldStatus , podClone .Status ) && reflect .DeepEqual (oldMetadata .Labels , podClone .Labels ) &&
362
+ if reflect .DeepEqual (oldStatus . Conditions , podClone .Status . Conditions ) && reflect .DeepEqual (oldMetadata .Labels , podClone .Labels ) &&
367
363
reflect .DeepEqual (oldMetadata .Annotations , podClone .Annotations ) {
368
364
return nil
369
365
}
@@ -372,6 +368,7 @@ func (r *ReconcileNodePodProbe) updatePodProbeStatus(pod *corev1.Pod, status app
372
368
klog .Errorf ("NodePodProbe patch pod(%s/%s) status failed: %s" , podClone .Namespace , podClone .Name , err .Error ())
373
369
return err
374
370
}
375
- klog .V (3 ).Infof ("NodePodProbe update pod(%s/%s) status conditions(%s) success" , podClone .Namespace , podClone .Name , util .DumpJSON (probeConditions ))
371
+ klog .V (3 ).Infof ("NodePodProbe update pod(%s/%s) metadata(%s) conditions(%s) success" , podClone .Namespace , podClone .Name ,
372
+ util .DumpJSON (probeMetadata ), util .DumpJSON (probeConditions ))
376
373
return nil
377
374
}
0 commit comments