@@ -11,6 +11,8 @@ import (
1111metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212"k8s.io/apimachinery/pkg/util/wait"
1313elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
14+ "sigs.k8s.io/aws-load-balancer-controller/pkg/algorithm"
15+ "sigs.k8s.io/aws-load-balancer-controller/pkg/annotations"
1416"sigs.k8s.io/aws-load-balancer-controller/pkg/deploy/tracking"
1517"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
1618elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
@@ -68,12 +70,18 @@ func (m *defaultTargetGroupBindingManager) Create(ctx context.Context, resTGB *e
6870return elbv2model.TargetGroupBindingResourceStatus {}, err
6971}
7072
71- stackLabels := m .trackingProvider .StackLabels (resTGB .Stack ())
73+ labels := m .trackingProvider .StackLabels (resTGB .Stack ())
74+
75+ if resTGB .Spec .Template .Labels != nil {
76+ labels = algorithm .MergeStringMap (labels , resTGB .Spec .Template .Labels )
77+ }
78+
7279k8sTGB := & elbv2api.TargetGroupBinding {
7380ObjectMeta : metav1.ObjectMeta {
74- Namespace : resTGB .Spec .Template .Namespace ,
75- Name : resTGB .Spec .Template .Name ,
76- Labels : stackLabels ,
81+ Namespace : resTGB .Spec .Template .Namespace ,
82+ Name : resTGB .Spec .Template .Name ,
83+ Annotations : resTGB .Spec .Template .Annotations ,
84+ Labels : labels ,
7785},
7886Spec : k8sTGBSpec ,
7987}
@@ -96,16 +104,30 @@ func (m *defaultTargetGroupBindingManager) Update(ctx context.Context, resTGB *e
96104if err != nil {
97105return elbv2model.TargetGroupBindingResourceStatus {}, err
98106}
99- if equality .Semantic .DeepEqual (k8sTGB .Spec , k8sTGBSpec ) {
107+
108+ calculatedLabels := m .trackingProvider .StackLabels (resTGB .Stack ())
109+
110+ if resTGB .Spec .Template .Labels != nil {
111+ calculatedLabels = algorithm .MergeStringMap (calculatedLabels , resTGB .Spec .Template .Labels )
112+ }
113+
114+ specSame := equality .Semantic .DeepEqual (k8sTGB .Spec , k8sTGBSpec )
115+ labelsSame := equality .Semantic .DeepEqual (k8sTGB .Labels , calculatedLabels )
116+ annotationsSame := tgbAnnotationsSame (resTGB , k8sTGB )
117+
118+ if specSame && labelsSame && annotationsSame {
100119return buildResTargetGroupBindingStatus (k8sTGB ), nil
101120}
102121
103122oldK8sTGB := k8sTGB .DeepCopy ()
104123k8sTGB .Spec = k8sTGBSpec
124+ k8sTGB .Annotations = resTGB .Spec .Template .Annotations
125+ k8sTGB .Labels = calculatedLabels
105126m .logger .Info ("modifying targetGroupBinding" ,
106127"stackID" , resTGB .Stack ().StackID (),
107128"resourceID" , resTGB .ID (),
108129"targetGroupBinding" , k8s .NamespacedName (k8sTGB ))
130+
109131if err := m .k8sClient .Patch (ctx , k8sTGB , client .MergeFrom (oldK8sTGB )); err != nil {
110132return elbv2model.TargetGroupBindingResourceStatus {}, err
111133}
@@ -116,6 +138,7 @@ func (m *defaultTargetGroupBindingManager) Update(ctx context.Context, resTGB *e
116138"stackID" , resTGB .Stack ().StackID (),
117139"resourceID" , resTGB .ID (),
118140"targetGroupBinding" , k8s .NamespacedName (k8sTGB ))
141+
119142return buildResTargetGroupBindingStatus (k8sTGB ), nil
120143}
121144
@@ -242,3 +265,17 @@ func buildResTargetGroupBindingStatus(k8sTGB *elbv2api.TargetGroupBinding) elbv2
242265},
243266}
244267}
268+
269+ // tgbAnnotationsSame performs map equality with the two sets of annotations. Will ignore the check point annotations inserted by the TGB reconciler.
270+ func tgbAnnotationsSame (resTGB * elbv2model.TargetGroupBindingResource , k8sTGB * elbv2api.TargetGroupBinding ) bool {
271+ annotationsNoCheckpoint := make (map [string ]string )
272+
273+ if k8sTGB .Annotations != nil {
274+ for k , v := range k8sTGB .Annotations {
275+ if k != annotations .AnnotationCheckPointTimestamp && k != annotations .AnnotationCheckPoint {
276+ annotationsNoCheckpoint [k ] = v
277+ }
278+ }
279+ }
280+ return equality .Semantic .DeepEqual (annotationsNoCheckpoint , resTGB .Spec .Template .Annotations )
281+ }
0 commit comments