@@ -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"
@@ -297,9 +294,9 @@ func (m *defaultResourceManager) updateTargetHealthPodConditionForPod(ctx contex
297
294
}
298
295
needFurtherProbe := targetHealthCondStatus != corev1 .ConditionTrue
299
296
300
- existingTargetHealthCond , exists := pod .GetPodCondition (targetHealthCondType )
297
+ existingTargetHealthCond , hasExistingTargetHealthCond := pod .GetPodCondition (targetHealthCondType )
301
298
// we skip patch pod if it matches current computed status/reason/message.
302
- if exists &&
299
+ if hasExistingTargetHealthCond &&
303
300
existingTargetHealthCond .Status == targetHealthCondStatus &&
304
301
existingTargetHealthCond .Reason == reason &&
305
302
existingTargetHealthCond .Message == message {
@@ -312,22 +309,30 @@ func (m *defaultResourceManager) updateTargetHealthPodConditionForPod(ctx contex
312
309
Reason : reason ,
313
310
Message : message ,
314
311
}
315
- if ! exists || existingTargetHealthCond .Status != targetHealthCondStatus {
312
+ if ! hasExistingTargetHealthCond || existingTargetHealthCond .Status != targetHealthCondStatus {
316
313
newTargetHealthCond .LastTransitionTime = metav1 .Now ()
314
+ } else {
315
+ newTargetHealthCond .LastTransitionTime = existingTargetHealthCond .LastTransitionTime
317
316
}
318
317
319
- patch , err := buildPodConditionPatch (pod , newTargetHealthCond )
320
- if err != nil {
321
- return false , err
322
- }
323
- k8sPod := & corev1.Pod {
318
+ podPatchSource := & corev1.Pod {
324
319
ObjectMeta : metav1.ObjectMeta {
325
320
Namespace : pod .Key .Namespace ,
326
321
Name : pod .Key .Name ,
327
- UID : pod .UID ,
322
+ },
323
+ Status : corev1.PodStatus {
324
+ Conditions : []corev1.PodCondition {},
328
325
},
329
326
}
330
- if err := m .k8sClient .Status ().Patch (ctx , k8sPod , patch ); err != nil {
327
+ if hasExistingTargetHealthCond {
328
+ podPatchSource .Status .Conditions = []corev1.PodCondition {existingTargetHealthCond }
329
+ }
330
+
331
+ podPatchTarget := podPatchSource .DeepCopy ()
332
+ podPatchTarget .UID = pod .UID // only put the uid in the new object to ensure it appears in the patch as a precondition
333
+ podPatchTarget .Status .Conditions = []corev1.PodCondition {newTargetHealthCond }
334
+
335
+ if err := m .k8sClient .Status ().Patch (ctx , podPatchTarget , client .StrategicMergeFrom (podPatchSource )); err != nil {
331
336
if apierrors .IsNotFound (err ) {
332
337
return false , nil
333
338
}
@@ -519,31 +524,6 @@ func matchNodePortEndpointWithTargets(endpoints []backend.NodePortEndpoint, targ
519
524
return matchedEndpointAndTargets , unmatchedEndpoints , unmatchedTargets
520
525
}
521
526
522
- func buildPodConditionPatch (pod k8s.PodInfo , condition corev1.PodCondition ) (client.Patch , error ) {
523
- oldData , err := json .Marshal (corev1.Pod {
524
- Status : corev1.PodStatus {
525
- Conditions : nil ,
526
- },
527
- })
528
- if err != nil {
529
- return nil , err
530
- }
531
- newData , err := json .Marshal (corev1.Pod {
532
- ObjectMeta : metav1.ObjectMeta {UID : pod .UID }, // only put the uid in the new object to ensure it appears in the patch as a precondition
533
- Status : corev1.PodStatus {
534
- Conditions : []corev1.PodCondition {condition },
535
- },
536
- })
537
- if err != nil {
538
- return nil , err
539
- }
540
- patchBytes , err := strategicpatch .CreateTwoWayMergePatch (oldData , newData , corev1.Pod {})
541
- if err != nil {
542
- return nil , err
543
- }
544
- return client .RawPatch (types .StrategicMergePatchType , patchBytes ), nil
545
- }
546
-
547
527
func isELBV2TargetGroupNotFoundError (err error ) bool {
548
528
var awsErr awserr.Error
549
529
if errors .As (err , & awsErr ) {
0 commit comments