Skip to content

Commit 0e69ee6

Browse files
authored
chore(ops): add externalTrafficPolicy and internalTrafficPolicy support to expose operations (#10287) (#10289)
1 parent 787bac7 commit 0e69ee6

7 files changed

Lines changed: 148 additions & 0 deletions

File tree

apis/operations/v1alpha1/opsrequest_types.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,35 @@ type OpsService struct {
801801
//
802802
// +optional
803803
IPFamilyPolicy *corev1.IPFamilyPolicy `json:"ipFamilyPolicy,omitempty" protobuf:"bytes,17,opt,name=ipFamilyPolicy,casttype=IPFamilyPolicy"`
804+
805+
// Specifies the external traffic policy of the Service. This controls how external traffic is routed to node-local or cluster-wide endpoints.
806+
//
807+
// Possible values:
808+
//
809+
// - `Cluster` (default for NodePort): Routes external traffic to all ready endpoints of the Service.
810+
// This preserves the original source IP and avoids a second hop for LoadBalancer and NodePort services.
811+
// - `Local`: Routes external traffic only to node-local endpoints.
812+
// If no node-local endpoints exist, traffic is dropped. This ensures that the source IP is exposed.
813+
//
814+
// This field is only applicable to LoadBalancer and NodePort services.
815+
//
816+
// +optional
817+
ExternalTrafficPolicy corev1.ServiceExternalTrafficPolicy `json:"externalTrafficPolicy,omitempty"`
818+
819+
// Specifies the internal traffic policy of the Service. This controls how traffic from internal sources
820+
// is routed.
821+
//
822+
// Possible values:
823+
//
824+
// - `Cluster` (default): Routes internal traffic to all ready endpoints of the Service.
825+
// This distributes traffic evenly across all endpoints.
826+
// - `Local`: Routes internal traffic only to node-local endpoints.
827+
// If no node-local endpoints exist, traffic is dropped.
828+
//
829+
// This field can be specified on any Service type.
830+
//
831+
// +optional
832+
InternalTrafficPolicy *corev1.ServiceInternalTrafficPolicy `json:"internalTrafficPolicy,omitempty"`
804833
}
805834

806835
type Backup struct {

apis/operations/v1alpha1/opsrequest_validation.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ func (r *OpsRequest) validateExpose(_ context.Context, cluster *appsv1.Cluster)
173173
if len(opssvc.Ports) == 0 {
174174
return fmt.Errorf("spec.expose.services.ports must be specified when componentName is empty")
175175
}
176+
if opssvc.ExternalTrafficPolicy != "" &&
177+
opssvc.ServiceType != corev1.ServiceTypeNodePort &&
178+
opssvc.ServiceType != corev1.ServiceTypeLoadBalancer {
179+
return fmt.Errorf("externalTrafficPolicy can only be set for NodePort or LoadBalancer service types, got %s", opssvc.ServiceType)
180+
}
176181
}
177182
}
178183
}

apis/operations/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/operations.kubeblocks.io_opsrequests.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,33 @@ spec:
310310
311311
More info: https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer.
312312
type: object
313+
externalTrafficPolicy:
314+
description: |-
315+
Specifies the external traffic policy of the Service. This controls how external traffic is routed to node-local or cluster-wide endpoints.
316+
317+
Possible values:
318+
319+
- `Cluster` (default for NodePort): Routes external traffic to all ready endpoints of the Service.
320+
This preserves the original source IP and avoids a second hop for LoadBalancer and NodePort services.
321+
- `Local`: Routes external traffic only to node-local endpoints.
322+
If no node-local endpoints exist, traffic is dropped. This ensures that the source IP is exposed.
323+
324+
This field is only applicable to LoadBalancer and NodePort services.
325+
type: string
326+
internalTrafficPolicy:
327+
description: |-
328+
Specifies the internal traffic policy of the Service. This controls how traffic from internal sources
329+
is routed.
330+
331+
Possible values:
332+
333+
- `Cluster` (default): Routes internal traffic to all ready endpoints of the Service.
334+
This distributes traffic evenly across all endpoints.
335+
- `Local`: Routes internal traffic only to node-local endpoints.
336+
If no node-local endpoints exist, traffic is dropped.
337+
338+
This field can be specified on any Service type.
339+
type: string
313340
ipFamilies:
314341
description: |-
315342
A list of IP families (e.g., IPv4, IPv6) assigned to this Service.

deploy/helm/crds/operations.kubeblocks.io_opsrequests.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,33 @@ spec:
310310
311311
More info: https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer.
312312
type: object
313+
externalTrafficPolicy:
314+
description: |-
315+
Specifies the external traffic policy of the Service. This controls how external traffic is routed to node-local or cluster-wide endpoints.
316+
317+
Possible values:
318+
319+
- `Cluster` (default for NodePort): Routes external traffic to all ready endpoints of the Service.
320+
This preserves the original source IP and avoids a second hop for LoadBalancer and NodePort services.
321+
- `Local`: Routes external traffic only to node-local endpoints.
322+
If no node-local endpoints exist, traffic is dropped. This ensures that the source IP is exposed.
323+
324+
This field is only applicable to LoadBalancer and NodePort services.
325+
type: string
326+
internalTrafficPolicy:
327+
description: |-
328+
Specifies the internal traffic policy of the Service. This controls how traffic from internal sources
329+
is routed.
330+
331+
Possible values:
332+
333+
- `Cluster` (default): Routes internal traffic to all ready endpoints of the Service.
334+
This distributes traffic evenly across all endpoints.
335+
- `Local`: Routes internal traffic only to node-local endpoints.
336+
If no node-local endpoints exist, traffic is dropped.
337+
338+
This field can be specified on any Service type.
339+
type: string
313340
ipFamilies:
314341
description: |-
315342
A list of IP families (e.g., IPv4, IPv6) assigned to this Service.

docs/developer_docs/api-reference/operations.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,51 @@ If the cluster is not configured for dual-stack, the Service creation fails.</li
28772877
</ul>
28782878
</td>
28792879
</tr>
2880+
<tr>
2881+
<td>
2882+
<code>externalTrafficPolicy</code><br/>
2883+
<em>
2884+
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#serviceexternaltrafficpolicy-v1-core">
2885+
Kubernetes core/v1.ServiceExternalTrafficPolicy
2886+
</a>
2887+
</em>
2888+
</td>
2889+
<td>
2890+
<em>(Optional)</em>
2891+
<p>Specifies the external traffic policy of the Service. This controls how external traffic is routed to node-local or cluster-wide endpoints.</p>
2892+
<p>Possible values:</p>
2893+
<ul>
2894+
<li><code>Cluster</code> (default for NodePort): Routes external traffic to all ready endpoints of the Service.
2895+
This preserves the original source IP and avoids a second hop for LoadBalancer and NodePort services.</li>
2896+
<li><code>Local</code>: Routes external traffic only to node-local endpoints.
2897+
If no node-local endpoints exist, traffic is dropped. This ensures that the source IP is exposed.</li>
2898+
</ul>
2899+
<p>This field is only applicable to LoadBalancer and NodePort services.</p>
2900+
</td>
2901+
</tr>
2902+
<tr>
2903+
<td>
2904+
<code>internalTrafficPolicy</code><br/>
2905+
<em>
2906+
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#serviceinternaltrafficpolicy-v1-core">
2907+
Kubernetes core/v1.ServiceInternalTrafficPolicy
2908+
</a>
2909+
</em>
2910+
</td>
2911+
<td>
2912+
<em>(Optional)</em>
2913+
<p>Specifies the internal traffic policy of the Service. This controls how traffic from internal sources
2914+
is routed.</p>
2915+
<p>Possible values:</p>
2916+
<ul>
2917+
<li><code>Cluster</code> (default): Routes internal traffic to all ready endpoints of the Service.
2918+
This distributes traffic evenly across all endpoints.</li>
2919+
<li><code>Local</code>: Routes internal traffic only to node-local endpoints.
2920+
If no node-local endpoints exist, traffic is dropped.</li>
2921+
</ul>
2922+
<p>This field can be specified on any Service type.</p>
2923+
</td>
2924+
</tr>
28802925
</tbody>
28812926
</table>
28822927
<h3 id="operations.kubeblocks.io/v1alpha1.OpsType">OpsType

pkg/operations/expose.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,16 @@ func (e ExposeOpsHandler) buildClusterServices(reqCtx intctrlutil.RequestCtx,
390390
clusterService.Spec.IPFamilies = exposeService.IPFamilies
391391
}
392392

393+
// set external traffic policy
394+
if exposeService.ExternalTrafficPolicy != "" {
395+
clusterService.Spec.ExternalTrafficPolicy = exposeService.ExternalTrafficPolicy
396+
}
397+
398+
// set internal traffic policy
399+
if exposeService.InternalTrafficPolicy != nil {
400+
clusterService.Spec.InternalTrafficPolicy = exposeService.InternalTrafficPolicy
401+
}
402+
393403
// set service selector
394404
if exposeService.PodSelector != nil {
395405
clusterService.Spec.Selector = exposeService.PodSelector

0 commit comments

Comments
 (0)