Skip to content

Commit c80b61c

Browse files
committed
added support for per tg annotation
1 parent f9699da commit c80b61c

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

pkg/annotations/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const (
6868
SvcLBSuffixScheme = "aws-load-balancer-scheme"
6969
SvcLBSuffixInternal = "aws-load-balancer-internal"
7070
SvcLBSuffixProxyProtocol = "aws-load-balancer-proxy-protocol"
71+
SvcLBSuffixProxyProtocolPerTargetGroup = "aws-load-balancer-proxy-protocol-per-target-group"
7172
SvcLBSuffixIPAddressType = "aws-load-balancer-ip-address-type"
7273
SvcLBSuffixAccessLogEnabled = "aws-load-balancer-access-log-enabled"
7374
SvcLBSuffixAccessLogS3BucketName = "aws-load-balancer-access-log-s3-bucket-name"

pkg/service/model_build_target_group.go

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (t *defaultModelBuildTask) buildTargetGroup(ctx context.Context, port corev
4444
if err != nil {
4545
return nil, err
4646
}
47-
tgAttrs, err := t.buildTargetGroupAttributes(ctx)
47+
tgAttrs, err := t.buildTargetGroupAttributes(ctx, port)
4848
if err != nil {
4949
return nil, err
5050
}
@@ -204,41 +204,64 @@ func (t *defaultModelBuildTask) buildTargetGroupName(_ context.Context, svcPort
204204
return fmt.Sprintf("k8s-%.8s-%.8s-%.10s", sanitizedNamespace, sanitizedName, uuid)
205205
}
206206

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
242265
}
243266

244267
func (t *defaultModelBuildTask) buildPreserveClientIPFlag(_ context.Context, targetType elbv2model.TargetType, tgAttrs []elbv2model.TargetGroupAttribute) (bool, error) {
@@ -711,4 +734,4 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingMultiClusterFlag(svc *cor
711734
return rawEnabled, nil
712735
}
713736
return false, nil
714-
}
737+
}

0 commit comments

Comments
 (0)