@@ -84,12 +84,11 @@ type nlbPortMapping struct {
84
84
SSLPolicy string
85
85
}
86
86
87
- // getLoadBalancerAdditionalTags converts the comma separated list of key-value
88
- // pairs in the ServiceAnnotationLoadBalancerAdditionalTags annotation and returns
89
- // it as a map.
90
- func getLoadBalancerAdditionalTags (annotations map [string ]string ) map [string ]string {
87
+ // getKeyValuePropertiesFromAnnotation converts the comma separated list of key-value
88
+ // pairs from the specified annotation and returns it as a map.
89
+ func getKeyValuePropertiesFromAnnotation (annotations map [string ]string , annotation string ) map [string ]string {
91
90
additionalTags := make (map [string ]string )
92
- if additionalTagsList , ok := annotations [ServiceAnnotationLoadBalancerAdditionalTags ]; ok {
91
+ if additionalTagsList , ok := annotations [annotation ]; ok {
93
92
additionalTagsList = strings .TrimSpace (additionalTagsList )
94
93
95
94
// Break up list of "Key1=Val,Key2=Val2"
@@ -123,7 +122,7 @@ func (c *Cloud) ensureLoadBalancerv2(namespacedName types.NamespacedName, loadBa
123
122
dirty := false
124
123
125
124
// Get additional tags set by the user
126
- tags := getLoadBalancerAdditionalTags (annotations )
125
+ tags := getKeyValuePropertiesFromAnnotation (annotations , ServiceAnnotationLoadBalancerAdditionalTags )
127
126
// Add default tags
128
127
tags [TagNameKubernetesService ] = namespacedName .String ()
129
128
tags = c .tagging .buildTags (ResourceLifecycleOwned , tags )
@@ -939,7 +938,7 @@ func (c *Cloud) ensureLoadBalancer(namespacedName types.NamespacedName, loadBala
939
938
}
940
939
941
940
// Get additional tags set by the user
942
- tags := getLoadBalancerAdditionalTags (annotations )
941
+ tags := getKeyValuePropertiesFromAnnotation (annotations , ServiceAnnotationLoadBalancerAdditionalTags )
943
942
944
943
// Add default tags
945
944
tags [TagNameKubernetesService ] = namespacedName .String ()
@@ -1128,7 +1127,7 @@ func (c *Cloud) ensureLoadBalancer(namespacedName types.NamespacedName, loadBala
1128
1127
{
1129
1128
// Add additional tags
1130
1129
klog .V (2 ).Infof ("Creating additional load balancer tags for %s" , loadBalancerName )
1131
- tags := getLoadBalancerAdditionalTags (annotations )
1130
+ tags := getKeyValuePropertiesFromAnnotation (annotations , ServiceAnnotationLoadBalancerAdditionalTags )
1132
1131
if len (tags ) > 0 {
1133
1132
err := c .addLoadBalancerTags (loadBalancerName , tags )
1134
1133
if err != nil {
@@ -1521,9 +1520,12 @@ func proxyProtocolEnabled(backend *elb.BackendServerDescription) bool {
1521
1520
// findInstancesForELB gets the EC2 instances corresponding to the Nodes, for setting up an ELB
1522
1521
// We ignore Nodes (with a log message) where the instanceid cannot be determined from the provider,
1523
1522
// and we ignore instances which are not found
1524
- func (c * Cloud ) findInstancesForELB (nodes []* v1.Node ) (map [InstanceID ]* ec2.Instance , error ) {
1523
+ func (c * Cloud ) findInstancesForELB (nodes []* v1.Node , annotations map [string ]string ) (map [InstanceID ]* ec2.Instance , error ) {
1524
+
1525
+ targetNodes := filterTargetNodes (nodes , annotations )
1526
+
1525
1527
// Map to instance ids ignoring Nodes where we cannot find the id (but logging)
1526
- instanceIDs := mapToAWSInstanceIDsTolerant (nodes )
1528
+ instanceIDs := mapToAWSInstanceIDsTolerant (targetNodes )
1527
1529
1528
1530
cacheCriteria := cacheCriteria {
1529
1531
// MaxAge not required, because we only care about security groups, which should not change
@@ -1539,3 +1541,35 @@ func (c *Cloud) findInstancesForELB(nodes []*v1.Node) (map[InstanceID]*ec2.Insta
1539
1541
1540
1542
return instances , nil
1541
1543
}
1544
+
1545
+ // filterTargetNodes uses node labels to filter the nodes that should be targeted by the ELB,
1546
+ // checking if all the labels provided in an annotation are present in the nodes
1547
+ func filterTargetNodes (nodes []* v1.Node , annotations map [string ]string ) []* v1.Node {
1548
+
1549
+ targetNodeLabels := getKeyValuePropertiesFromAnnotation (annotations , ServiceAnnotationLoadBalancerTargetNodeLabels )
1550
+
1551
+ if len (targetNodeLabels ) == 0 {
1552
+ return nodes
1553
+ }
1554
+
1555
+ targetNodes := make ([]* v1.Node , 0 , len (nodes ))
1556
+
1557
+ for _ , node := range nodes {
1558
+ if node .Labels != nil && len (node .Labels ) > 0 {
1559
+ allFiltersMatch := true
1560
+
1561
+ for targetLabelKey , targetLabelValue := range targetNodeLabels {
1562
+ if nodeLabelValue , ok := node .Labels [targetLabelKey ]; ! ok || (nodeLabelValue != targetLabelValue && targetLabelValue != "" ) {
1563
+ allFiltersMatch = false
1564
+ break
1565
+ }
1566
+ }
1567
+
1568
+ if allFiltersMatch {
1569
+ targetNodes = append (targetNodes , node )
1570
+ }
1571
+ }
1572
+ }
1573
+
1574
+ return targetNodes
1575
+ }
0 commit comments