@@ -44,7 +44,7 @@ func (t *defaultModelBuildTask) buildTargetGroup(ctx context.Context, port corev
44
44
if err != nil {
45
45
return nil , err
46
46
}
47
- tgAttrs , err := t .buildTargetGroupAttributes (ctx )
47
+ tgAttrs , err := t .buildTargetGroupAttributes (ctx , port )
48
48
if err != nil {
49
49
return nil , err
50
50
}
@@ -204,41 +204,64 @@ func (t *defaultModelBuildTask) buildTargetGroupName(_ context.Context, svcPort
204
204
return fmt .Sprintf ("k8s-%.8s-%.8s-%.10s" , sanitizedNamespace , sanitizedName , uuid )
205
205
}
206
206
207
- func (t * defaultModelBuildTask ) buildTargetGroupAttributes (_ context.Context ) ([]elbv2model.TargetGroupAttribute , error ) {
208
- var rawAttributes map [string ]string
209
- if _ , err := t .annotationParser .ParseStringMapAnnotation (annotations .SvcLBSuffixTargetGroupAttributes , & rawAttributes , t .service .Annotations ); err != nil {
210
- return nil , err
211
- }
212
- if rawAttributes == nil {
213
- rawAttributes = make (map [string ]string )
214
- }
215
- if _ , ok := rawAttributes [tgAttrsProxyProtocolV2Enabled ]; ! ok {
216
- rawAttributes [tgAttrsProxyProtocolV2Enabled ] = strconv .FormatBool (t .defaultProxyProtocolV2Enabled )
217
- }
218
- proxyV2Annotation := ""
219
- if exists := t .annotationParser .ParseStringAnnotation (annotations .SvcLBSuffixProxyProtocol , & proxyV2Annotation , t .service .Annotations ); exists {
220
- if proxyV2Annotation != "*" {
221
- return []elbv2model.TargetGroupAttribute {}, errors .Errorf ("invalid value %v for Load Balancer proxy protocol v2 annotation, only value currently supported is *" , proxyV2Annotation )
222
- }
223
- rawAttributes [tgAttrsProxyProtocolV2Enabled ] = "true"
224
- }
225
- if rawPreserveIPEnabled , ok := rawAttributes [tgAttrsPreserveClientIPEnabled ]; ok {
226
- _ , err := strconv .ParseBool (rawPreserveIPEnabled )
227
- if err != nil {
228
- return nil , errors .Wrapf (err , "failed to parse attribute %v=%v" , tgAttrsPreserveClientIPEnabled , rawPreserveIPEnabled )
229
- }
230
- }
231
- attributes := make ([]elbv2model.TargetGroupAttribute , 0 , len (rawAttributes ))
232
- for attrKey , attrValue := range rawAttributes {
233
- attributes = append (attributes , elbv2model.TargetGroupAttribute {
234
- Key : attrKey ,
235
- Value : attrValue ,
236
- })
237
- }
238
- sort .Slice (attributes , func (i , j int ) bool {
239
- return attributes [i ].Key < attributes [j ].Key
240
- })
241
- return attributes , nil
207
+ func (t * defaultModelBuildTask ) buildTargetGroupAttributes (_ context.Context , port corev1.ServicePort ) ([]elbv2model.TargetGroupAttribute , error ) {
208
+ var rawAttributes map [string ]string
209
+ if _ , err := t .annotationParser .ParseStringMapAnnotation (annotations .SvcLBSuffixTargetGroupAttributes , & rawAttributes , t .service .Annotations ); err != nil {
210
+ return nil , err
211
+ }
212
+ if rawAttributes == nil {
213
+ rawAttributes = make (map [string ]string )
214
+ }
215
+ if _ , ok := rawAttributes [tgAttrsProxyProtocolV2Enabled ]; ! ok {
216
+ rawAttributes [tgAttrsProxyProtocolV2Enabled ] = strconv .FormatBool (t .defaultProxyProtocolV2Enabled )
217
+ }
218
+
219
+ var proxyProtocolPerTG string
220
+ if t .annotationParser .ParseStringAnnotation (annotations .SvcLBSuffixProxyProtocolPerTargetGroup , & proxyProtocolPerTG , t .service .Annotations ) {
221
+ pairs := strings .Split (proxyProtocolPerTG , "," )
222
+ if len (pairs )% 2 != 0 {
223
+ return nil , errors .Errorf ("invalid format for proxy-protocol-per-target-group: %v" , proxyProtocolPerTG )
224
+ }
225
+ for i := 0 ; i < len (pairs ); i += 2 {
226
+ portStr := strings .TrimSpace (pairs [i ])
227
+ enabledStr := strings .TrimSpace (pairs [i + 1 ])
228
+ if portStr == strconv .FormatInt (int64 (port .Port ), 10 ) {
229
+ enabled , err := strconv .ParseBool (enabledStr )
230
+ if err != nil {
231
+ return nil , errors .Errorf ("invalid boolean value for port %v: %v" , portStr , enabledStr )
232
+ }
233
+ rawAttributes [tgAttrsProxyProtocolV2Enabled ] = strconv .FormatBool (enabled )
234
+ break
235
+ }
236
+ }
237
+ }
238
+
239
+ proxyV2Annotation := ""
240
+ if exists := t .annotationParser .ParseStringAnnotation (annotations .SvcLBSuffixProxyProtocol , & proxyV2Annotation , t .service .Annotations ); exists {
241
+ if proxyV2Annotation != "*" {
242
+ return []elbv2model.TargetGroupAttribute {}, errors .Errorf ("invalid value %v for Load Balancer proxy protocol v2 annotation, only value currently supported is *" , proxyV2Annotation )
243
+ }
244
+ rawAttributes [tgAttrsProxyProtocolV2Enabled ] = "true"
245
+ }
246
+
247
+ if rawPreserveIPEnabled , ok := rawAttributes [tgAttrsPreserveClientIPEnabled ]; ok {
248
+ _ , err := strconv .ParseBool (rawPreserveIPEnabled )
249
+ if err != nil {
250
+ return nil , errors .Wrapf (err , "failed to parse attribute %v=%v" , tgAttrsPreserveClientIPEnabled , rawPreserveIPEnabled )
251
+ }
252
+ }
253
+
254
+ attributes := make ([]elbv2model.TargetGroupAttribute , 0 , len (rawAttributes ))
255
+ for attrKey , attrValue := range rawAttributes {
256
+ attributes = append (attributes , elbv2model.TargetGroupAttribute {
257
+ Key : attrKey ,
258
+ Value : attrValue ,
259
+ })
260
+ }
261
+ sort .Slice (attributes , func (i , j int ) bool {
262
+ return attributes [i ].Key < attributes [j ].Key
263
+ })
264
+ return attributes , nil
242
265
}
243
266
244
267
func (t * defaultModelBuildTask ) buildPreserveClientIPFlag (_ context.Context , targetType elbv2model.TargetType , tgAttrs []elbv2model.TargetGroupAttribute ) (bool , error ) {
@@ -711,4 +734,4 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingMultiClusterFlag(svc *cor
711
734
return rawEnabled , nil
712
735
}
713
736
return false , nil
714
- }
737
+ }
0 commit comments