Skip to content

Commit 71c45f5

Browse files
authored
Merge pull request #3373 from M00nF1sh/main
remove unnecessary patch requests when PodReadinessGate is enabled.
2 parents 074beee + a6decce commit 71c45f5

File tree

2 files changed

+26
-85
lines changed

2 files changed

+26
-85
lines changed

pkg/targetgroupbinding/resource_manager.go

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package targetgroupbinding
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"net/netip"
87
"time"
@@ -17,9 +16,7 @@ import (
1716
corev1 "k8s.io/api/core/v1"
1817
apierrors "k8s.io/apimachinery/pkg/api/errors"
1918
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20-
"k8s.io/apimachinery/pkg/types"
2119
"k8s.io/apimachinery/pkg/util/sets"
22-
"k8s.io/apimachinery/pkg/util/strategicpatch"
2320
elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
2421
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services"
2522
"sigs.k8s.io/aws-load-balancer-controller/pkg/backend"
@@ -297,9 +294,9 @@ func (m *defaultResourceManager) updateTargetHealthPodConditionForPod(ctx contex
297294
}
298295
needFurtherProbe := targetHealthCondStatus != corev1.ConditionTrue
299296

300-
existingTargetHealthCond, exists := pod.GetPodCondition(targetHealthCondType)
297+
existingTargetHealthCond, hasExistingTargetHealthCond := pod.GetPodCondition(targetHealthCondType)
301298
// we skip patch pod if it matches current computed status/reason/message.
302-
if exists &&
299+
if hasExistingTargetHealthCond &&
303300
existingTargetHealthCond.Status == targetHealthCondStatus &&
304301
existingTargetHealthCond.Reason == reason &&
305302
existingTargetHealthCond.Message == message {
@@ -312,22 +309,30 @@ func (m *defaultResourceManager) updateTargetHealthPodConditionForPod(ctx contex
312309
Reason: reason,
313310
Message: message,
314311
}
315-
if !exists || existingTargetHealthCond.Status != targetHealthCondStatus {
312+
if !hasExistingTargetHealthCond || existingTargetHealthCond.Status != targetHealthCondStatus {
316313
newTargetHealthCond.LastTransitionTime = metav1.Now()
314+
} else {
315+
newTargetHealthCond.LastTransitionTime = existingTargetHealthCond.LastTransitionTime
317316
}
318317

319-
patch, err := buildPodConditionPatch(pod, newTargetHealthCond)
320-
if err != nil {
321-
return false, err
322-
}
323-
k8sPod := &corev1.Pod{
318+
podPatchSource := &corev1.Pod{
324319
ObjectMeta: metav1.ObjectMeta{
325320
Namespace: pod.Key.Namespace,
326321
Name: pod.Key.Name,
327-
UID: pod.UID,
322+
},
323+
Status: corev1.PodStatus{
324+
Conditions: []corev1.PodCondition{},
328325
},
329326
}
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 {
331336
if apierrors.IsNotFound(err) {
332337
return false, nil
333338
}
@@ -519,31 +524,6 @@ func matchNodePortEndpointWithTargets(endpoints []backend.NodePortEndpoint, targ
519524
return matchedEndpointAndTargets, unmatchedEndpoints, unmatchedTargets
520525
}
521526

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-
547527
func isELBV2TargetGroupNotFoundError(err error) bool {
548528
var awsErr awserr.Error
549529
if errors.As(err, &awsErr) {

pkg/targetgroupbinding/resource_manager_test.go

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ func Test_defaultResourceManager_updateTargetHealthPodConditionForPod(t *testing
137137
Status: corev1.PodStatus{
138138
Conditions: []corev1.PodCondition{
139139
{
140-
Type: "target-health.elbv2.k8s.aws/my-tgb",
141-
Status: corev1.ConditionFalse,
140+
Type: "target-health.elbv2.k8s.aws/my-tgb",
141+
Message: elbv2sdk.TargetHealthReasonEnumElbRegistrationInProgress,
142+
Reason: "Elb.RegistrationInProgress",
143+
Status: corev1.ConditionFalse,
142144
},
143145
{
144146
Type: corev1.ContainersReady,
@@ -160,8 +162,10 @@ func Test_defaultResourceManager_updateTargetHealthPodConditionForPod(t *testing
160162
},
161163
Conditions: []corev1.PodCondition{
162164
{
163-
Type: "target-health.elbv2.k8s.aws/my-tgb",
164-
Status: corev1.ConditionFalse,
165+
Type: "target-health.elbv2.k8s.aws/my-tgb",
166+
Message: elbv2sdk.TargetHealthReasonEnumElbRegistrationInProgress,
167+
Reason: "Elb.RegistrationInProgress",
168+
Status: corev1.ConditionFalse,
165169
},
166170
{
167171
Type: corev1.ContainersReady,
@@ -459,46 +463,3 @@ func Test_containsTargetsInInitialState(t *testing.T) {
459463
})
460464
}
461465
}
462-
463-
func Test_buildPodConditionPatch(t *testing.T) {
464-
type args struct {
465-
pod k8s.PodInfo
466-
condition corev1.PodCondition
467-
}
468-
tests := []struct {
469-
name string
470-
args args
471-
wantPatch []byte
472-
wantErr error
473-
}{
474-
{
475-
name: "standard case",
476-
args: args{
477-
pod: k8s.PodInfo{
478-
Key: types.NamespacedName{Namespace: "ns-1", Name: "pod-1"},
479-
UID: "pod-uuid",
480-
},
481-
condition: corev1.PodCondition{
482-
Type: "custom-condition",
483-
Status: corev1.ConditionTrue,
484-
Reason: "some-reason",
485-
Message: "some-msg",
486-
},
487-
},
488-
wantPatch: []byte(`{"metadata":{"uid":"pod-uuid"},"status":{"conditions":[{"lastProbeTime":null,"lastTransitionTime":null,"message":"some-msg","reason":"some-reason","status":"True","type":"custom-condition"}]}}`),
489-
},
490-
}
491-
for _, tt := range tests {
492-
t.Run(tt.name, func(t *testing.T) {
493-
got, err := buildPodConditionPatch(tt.args.pod, tt.args.condition)
494-
if tt.wantErr != nil {
495-
assert.EqualError(t, err, tt.wantErr.Error())
496-
} else {
497-
assert.NoError(t, err)
498-
gotPatch, _ := got.Data(nil)
499-
assert.Equal(t, tt.wantPatch, gotPatch)
500-
assert.Equal(t, types.StrategicMergePatchType, got.Type())
501-
}
502-
})
503-
}
504-
}

0 commit comments

Comments
 (0)