Skip to content

Commit f634bb0

Browse files
Add annotation to preserve client IP
(cherry picked from commit 65dae83)
1 parent 914f202 commit f634bb0

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

docs/huawei-cloud-controller-manager-configuration.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,27 @@ The following arguments are supported:
184184
* `timeout` Required. Specifies the health check timeout duration in the unit of second.
185185
The value ranges from `1` to `50`. Defaults to `3`.
186186

187+
* `enable-transparent-client-ip` Specifies whether to pass source IP addresses of the clients to backend servers.
188+
Valid values are `'true'` and `'false'`.
189+
190+
TCP or UDP listeners of shared load balancers:
191+
The value can be **true** or **false**, and the default value is **false** if this annotation is not passed.
192+
193+
HTTP or HTTPS listeners of shared load balancers:
194+
The value can only be **true**, and the default value is **true** if this annotation is not passed.
195+
196+
All listeners of dedicated load balancers:
197+
The value can only be **true**, and the default value is **true** if this annotation is not passed.
198+
199+
> Note:
200+
>
201+
> If this function is enabled, the load balancer communicates with backend servers using their real IP addresses.
202+
> Ensure that security group rules and access control policies are correctly configured.
203+
>
204+
> If this function is enabled, a server cannot serve as both a backend server and a client.
205+
>
206+
> If this function is enabled, backend server specifications cannot be changed.
207+
187208
* `enable-cross-vpc` Optional. Specifies whether to enable cross-VPC backend.
188209
The value can be `true` (enable cross-VPC backend) or `false` (disable cross-VPC backend).
189210
The value can only be updated to `true`.

docs/usage-guide.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ will be used, otherwise use the set value.
141141
* `timeout` Required. Specifies the health check timeout duration in the unit of second.
142142
The value ranges from `1` to `50`. Defaults to `3`.
143143

144+
* `kubernetes.io/elb.enable-transparent-client-ip` Optional. Specifies whether to pass source IP addresses of the clients to backend servers.
145+
Valid values are `'true'` and `'false'`.
146+
147+
TCP or UDP listeners of shared load balancers:
148+
The value can be **true** or **false**, and the default value is **false** if this annotation is not passed.
149+
150+
HTTP or HTTPS listeners of shared load balancers:
151+
The value can only be **true**, and the default value is **true** if this annotation is not passed.
152+
153+
All listeners of dedicated load balancers:
154+
The value can only be **true**, and the default value is **true** if this annotation is not passed.
155+
156+
> Note:
157+
>
158+
> If this function is enabled, the load balancer communicates with backend servers using their real IP addresses.
159+
> Ensure that security group rules and access control policies are correctly configured.
160+
>
161+
> If this function is enabled, a server cannot serve as both a backend server and a client.
162+
>
163+
> If this function is enabled, backend server specifications cannot be changed.
164+
144165
* `kubernetes.io/elb.x-forwarded-host` Optional. Specifies whether to rewrite the `X-Forwarded-Host` header.
145166
If this function is enabled, `X-Forwarded-Host` is rewritten based on Host in the request and sent to backend servers.
146167
Valid values are `'true'` and `'false'`, defaults to `'false'`.
@@ -216,7 +237,7 @@ kind: Service
216237
metadata:
217238
annotations:
218239
kubernetes.io/elb.class: shared
219-
kubernetes.io/elb.id: xxxx # Please fill your ELB service ID.
240+
kubernetes.io/elb.id: xx # Please replace xx with your ELB instance ID.
220241
kubernetes.io/elb.lb-algorithm: ROUND_ROBIN
221242
labels:
222243
app: nginx
@@ -263,6 +284,7 @@ metadata:
263284
annotations:
264285
kubernetes.io/elb.class: shared
265286
kubernetes.io/elb.lb-algorithm: ROUND_ROBIN
287+
kubernetes.io/elb.enable-transparent-client-ip: 'true' # Preserve client IP to backend servers.
266288
labels:
267289
app: nginx
268290
name: loadbalancer-service-demo-02

pkg/cloudprovider/huaweicloud/sharedloadbalancer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,13 @@ func (l *SharedLoadBalancer) createListener(loadbalancerID string, service *v1.S
676676
}
677677
}
678678

679+
if protocol == ProtocolTCP || protocol == ProtocolUDP {
680+
// TCP or UDP listeners transparent_client_ip_enable can be true or false.
681+
transparentClientIPEnable := getBoolFromSvsAnnotation(service, ElbEnableTransparentClientIP,
682+
l.loadbalancerOpts.EnableTransparentClientIP)
683+
createOpt.TransparentClientIpEnable = &transparentClientIPEnable
684+
}
685+
679686
listener, err := l.dedicatedELBClient.CreateListener(createOpt)
680687
if err != nil {
681688
return nil, status.Errorf(codes.Internal, "Failed to create listener for loadbalancer %s: %v",
@@ -708,6 +715,13 @@ func (l *SharedLoadBalancer) updateListener(listener *elbmodel.ListenerResp, ser
708715
}
709716
}
710717

718+
if listener.Protocol.Value() == ProtocolTCP || listener.Protocol.Value() == ProtocolUDP {
719+
// TCP or UDP listeners transparent_client_ip_enable can be true or false.
720+
transparentClientIPEnable := getBoolFromSvsAnnotation(service, ElbEnableTransparentClientIP,
721+
l.loadbalancerOpts.EnableTransparentClientIP)
722+
updateOpt.TransparentClientIpEnable = &transparentClientIPEnable
723+
}
724+
711725
err := l.dedicatedELBClient.UpdateListener(listener.Id, updateOpt)
712726
if err != nil {
713727
return err

test/e2e/shared_loadbalancer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ var _ = ginkgo.Describe("Shared loadbalancer(TCP) service testing", func() {
7676
annotations[huaweicloud.ElbSessionAffinityOption] = `{"type":"SOURCE_IP", "persistence_timeout": 3}`
7777
annotations[huaweicloud.ElbHealthCheckFlag] = "on"
7878
annotations[huaweicloud.ElbHealthCheckOptions] = `{"delay": 4, "timeout": 16, "max_retries": 4}`
79+
annotations[huaweicloud.ElbHealthCheckOptions] = `{"delay": 3, "timeout": 15, "max_retries": 3}`
80+
annotations[huaweicloud.ElbEnableTransparentClientIP] = "true"
7981

8082
service = newLoadbalancerAutoService(testNamespace, serviceName, 80, annotations)
8183
framework.CreateService(kubeClient, service)

0 commit comments

Comments
 (0)