@@ -2,7 +2,6 @@ package targetgroupbinding
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
7
6
"net/netip"
8
7
"time"
@@ -17,9 +16,7 @@ import (
17
16
corev1 "k8s.io/api/core/v1"
18
17
apierrors "k8s.io/apimachinery/pkg/api/errors"
19
18
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20
- "k8s.io/apimachinery/pkg/types"
21
19
"k8s.io/apimachinery/pkg/util/sets"
22
- "k8s.io/apimachinery/pkg/util/strategicpatch"
23
20
elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
24
21
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services"
25
22
"sigs.k8s.io/aws-load-balancer-controller/pkg/backend"
@@ -290,9 +287,9 @@ func (m *defaultResourceManager) updateTargetHealthPodConditionForPod(ctx contex
290
287
}
291
288
needFurtherProbe := targetHealthCondStatus != corev1 .ConditionTrue
292
289
293
- existingTargetHealthCond , exists := pod .GetPodCondition (targetHealthCondType )
290
+ existingTargetHealthCond , hasExistingTargetHealthCond := pod .GetPodCondition (targetHealthCondType )
294
291
// we skip patch pod if it matches current computed status/reason/message.
295
- if exists &&
292
+ if hasExistingTargetHealthCond &&
296
293
existingTargetHealthCond .Status == targetHealthCondStatus &&
297
294
existingTargetHealthCond .Reason == reason &&
298
295
existingTargetHealthCond .Message == message {
@@ -305,22 +302,30 @@ func (m *defaultResourceManager) updateTargetHealthPodConditionForPod(ctx contex
305
302
Reason : reason ,
306
303
Message : message ,
307
304
}
308
- if ! exists || existingTargetHealthCond .Status != targetHealthCondStatus {
305
+ if ! hasExistingTargetHealthCond || existingTargetHealthCond .Status != targetHealthCondStatus {
309
306
newTargetHealthCond .LastTransitionTime = metav1 .Now ()
307
+ } else {
308
+ newTargetHealthCond .LastTransitionTime = existingTargetHealthCond .LastTransitionTime
310
309
}
311
310
312
- patch , err := buildPodConditionPatch (pod , newTargetHealthCond )
313
- if err != nil {
314
- return false , err
315
- }
316
- k8sPod := & corev1.Pod {
311
+ podPatchSource := & corev1.Pod {
317
312
ObjectMeta : metav1.ObjectMeta {
318
313
Namespace : pod .Key .Namespace ,
319
314
Name : pod .Key .Name ,
320
- UID : pod .UID ,
315
+ },
316
+ Status : corev1.PodStatus {
317
+ Conditions : []corev1.PodCondition {},
321
318
},
322
319
}
323
- if err := m .k8sClient .Status ().Patch (ctx , k8sPod , patch ); err != nil {
320
+ if hasExistingTargetHealthCond {
321
+ podPatchSource .Status .Conditions = []corev1.PodCondition {existingTargetHealthCond }
322
+ }
323
+
324
+ podPatchTarget := podPatchSource .DeepCopy ()
325
+ podPatchTarget .UID = pod .UID // only put the uid in the new object to ensure it appears in the patch as a precondition
326
+ podPatchTarget .Status .Conditions = []corev1.PodCondition {newTargetHealthCond }
327
+
328
+ if err := m .k8sClient .Status ().Patch (ctx , podPatchTarget , client .StrategicMergeFrom (podPatchSource )); err != nil {
324
329
if apierrors .IsNotFound (err ) {
325
330
return false , nil
326
331
}
@@ -512,31 +517,6 @@ func matchNodePortEndpointWithTargets(endpoints []backend.NodePortEndpoint, targ
512
517
return matchedEndpointAndTargets , unmatchedEndpoints , unmatchedTargets
513
518
}
514
519
515
- func buildPodConditionPatch (pod k8s.PodInfo , condition corev1.PodCondition ) (client.Patch , error ) {
516
- oldData , err := json .Marshal (corev1.Pod {
517
- Status : corev1.PodStatus {
518
- Conditions : nil ,
519
- },
520
- })
521
- if err != nil {
522
- return nil , err
523
- }
524
- newData , err := json .Marshal (corev1.Pod {
525
- ObjectMeta : metav1.ObjectMeta {UID : pod .UID }, // only put the uid in the new object to ensure it appears in the patch as a precondition
526
- Status : corev1.PodStatus {
527
- Conditions : []corev1.PodCondition {condition },
528
- },
529
- })
530
- if err != nil {
531
- return nil , err
532
- }
533
- patchBytes , err := strategicpatch .CreateTwoWayMergePatch (oldData , newData , corev1.Pod {})
534
- if err != nil {
535
- return nil , err
536
- }
537
- return client .RawPatch (types .StrategicMergePatchType , patchBytes ), nil
538
- }
539
-
540
520
func isELBV2TargetGroupNotFoundError (err error ) bool {
541
521
var awsErr awserr.Error
542
522
if errors .As (err , & awsErr ) {
0 commit comments