Skip to content

Commit fe212ae

Browse files
authored
Merge tags from alb.ingress.kubernetes.io/tags (#2480)
* Merge tags on service and ingress * update doc
1 parent 2e8cf3f commit fe212ae

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

docs/guide/ingress/annotations.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ The AWS Load Balancer Controller automatically applies following tags to the AWS
741741
In addition, you can use annotations to specify additional tags
742742

743743
- <a name="tags">`alb.ingress.kubernetes.io/tags`</a> specifies additional tags that will be applied to AWS resources created.
744+
In case of target group, the controller will merge the tags from the ingress and the backend service giving precedence
745+
to the values specified on the service when there is conflict.
744746

745747
!!!example
746748
```

pkg/ingress/model_build_tags.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,19 @@ func (t *defaultModelBuildTask) buildIngressResourceTags(ing ClassifiedIngress)
4949

5050
// buildIngressBackendResourceTags builds the AWS Tags used for a single Ingress and Backend. e.g. TargetGroup.
5151
// Note: the Tags specified via IngressClass takes higher priority than tags specified via annotation on Ingress or Service.
52-
// the Tags annotation of Service takes higher priority than annotation of Ingress. (TODO: we might consider change this behavior to merge tags instead)
52+
// the target group will have the merged tags specified by the annotations of both Ingress and Service
53+
// the Tags annotation of Service takes higher priority if there is conflict between the tags of Ingress and Service
5354
func (t *defaultModelBuildTask) buildIngressBackendResourceTags(ing ClassifiedIngress, backend *corev1.Service) (map[string]string, error) {
54-
mergedAnnotations := algorithm.MergeStringMap(backend.Annotations, ing.Ing.Annotations)
55-
var annotationTags map[string]string
56-
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixTags, &annotationTags, mergedAnnotations); err != nil {
55+
var backendAnnotationTags map[string]string
56+
var ingressAnnotationTags map[string]string
57+
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixTags, &backendAnnotationTags, backend.Annotations); err != nil {
5758
return nil, err
5859
}
59-
if err := t.validateTagCollisionWithExternalManagedTags(annotationTags); err != nil {
60+
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixTags, &ingressAnnotationTags, ing.Ing.Annotations); err != nil {
61+
return nil, err
62+
}
63+
mergedAnnotationTags := algorithm.MergeStringMap(backendAnnotationTags, ingressAnnotationTags)
64+
if err := t.validateTagCollisionWithExternalManagedTags(mergedAnnotationTags); err != nil {
6065
return nil, errors.Wrapf(err, "failed build tags for Ingress %v and Service %v",
6166
k8s.NamespacedName(ing.Ing).String(), k8s.NamespacedName(backend).String())
6267
}
@@ -66,7 +71,7 @@ func (t *defaultModelBuildTask) buildIngressBackendResourceTags(ing ClassifiedIn
6671
return nil, err
6772
}
6873

69-
return algorithm.MergeStringMap(ingClassTags, annotationTags), nil
74+
return algorithm.MergeStringMap(ingClassTags, mergedAnnotationTags), nil
7075
}
7176

7277
// buildIngressClassResourceTags builds the AWS Tags for a IngressClass.

pkg/ingress/model_build_tags_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ func Test_defaultModelBuildTask_buildIngressBackendResourceTags(t *testing.T) {
276276
want: map[string]string{
277277
"tag-c": "value-c1",
278278
"tag-d": "value-d1",
279+
"tag-e": "value-e",
279280
},
280281
},
281282
{
@@ -403,6 +404,59 @@ func Test_defaultModelBuildTask_buildIngressBackendResourceTags(t *testing.T) {
403404
"tag-e": "value-e",
404405
},
405406
},
407+
{
408+
name: "non-empty annotation tags from Ingress and Service, non-empty IngressClass tags",
409+
fields: fields{
410+
externalManagedTags: sets.NewString("tag-a", "tag-b"),
411+
},
412+
args: args{
413+
ing: ClassifiedIngress{
414+
Ing: &networking.Ingress{
415+
ObjectMeta: metav1.ObjectMeta{
416+
Namespace: "awesome-ns",
417+
Name: "awesome-ing",
418+
Annotations: map[string]string{
419+
"alb.ingress.kubernetes.io/tags": "tag-c=value-c,tag-d=value-d, tag-f=value-f",
420+
},
421+
},
422+
},
423+
IngClassConfig: ClassConfiguration{
424+
IngClassParams: &elbv2api.IngressClassParams{
425+
ObjectMeta: metav1.ObjectMeta{
426+
Name: "awesome-class",
427+
},
428+
Spec: elbv2api.IngressClassParamsSpec{
429+
Tags: []elbv2api.Tag{
430+
{
431+
Key: "tag-d",
432+
Value: "value-d1",
433+
},
434+
{
435+
Key: "tag-e",
436+
Value: "value-e",
437+
},
438+
},
439+
},
440+
},
441+
},
442+
},
443+
backend: &corev1.Service{
444+
ObjectMeta: metav1.ObjectMeta{
445+
Namespace: "awesome-ns",
446+
Name: "awesome-svc",
447+
Annotations: map[string]string{
448+
"alb.ingress.kubernetes.io/tags": "tag-c=value-c,tag-d=value-d2",
449+
},
450+
},
451+
},
452+
},
453+
want: map[string]string{
454+
"tag-c": "value-c",
455+
"tag-d": "value-d1",
456+
"tag-e": "value-e",
457+
"tag-f": "value-f",
458+
},
459+
},
406460
{
407461
name: "empty tags from Ingress & Service, empty tags from IngressClass",
408462
fields: fields{

test/e2e/ingress/multi_path_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (s *multiPathBackendStack) buildIngressResource(ns *corev1.Namespace, ingID
257257
backendSVC := svcByBackendID[pathCFG.BackendID]
258258
exact := networking.PathTypeExact
259259
ing.Spec.Rules[0].HTTP.Paths = append(ing.Spec.Rules[0].HTTP.Paths, networking.HTTPIngressPath{
260-
Path: pathCFG.Path,
260+
Path: pathCFG.Path,
261261
PathType: &exact,
262262
Backend: networking.IngressBackend{
263263
Service: &networking.IngressServiceBackend{

0 commit comments

Comments
 (0)