Skip to content

Commit e481ae7

Browse files
authored
Merge pull request #131 from MrFreezeex/traffic-distribution
apis: conformance: add traffic distribution and internal traffic policies fields
2 parents a93ef01 + c2e6739 commit e481ae7

9 files changed

+92
-5
lines changed

config/crd-base/multicluster.x-k8s.io_serviceexports.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ metadata:
2020
# The revision is updated on each CRD change and reset back to 0 on every new version.
2121
# It can be used together with the version label when installing those CRDs
2222
# and prevent any downgrades.
23-
multicluster.x-k8s.io/crd-schema-revision: "0"
23+
multicluster.x-k8s.io/crd-schema-revision: "1"
2424
spec:
2525
group: multicluster.x-k8s.io
2626
scope: Namespaced

config/crd-base/multicluster.x-k8s.io_serviceimports.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ metadata:
2020
# The revision is updated on each CRD change and reset back to 0 on every new version.
2121
# It can be used together with the version label when installing those CRDs
2222
# and prevent any downgrades.
23-
multicluster.x-k8s.io/crd-schema-revision: "0"
23+
multicluster.x-k8s.io/crd-schema-revision: "1"
2424
spec:
2525
group: multicluster.x-k8s.io
2626
scope: Namespaced

config/crd/multicluster.x-k8s.io_serviceexports.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ metadata:
2020
# The revision is updated on each CRD change and reset back to 0 on every new version.
2121
# It can be used together with the version label when installing those CRDs
2222
# and prevent any downgrades.
23-
multicluster.x-k8s.io/crd-schema-revision: "0"
23+
multicluster.x-k8s.io/crd-schema-revision: "1"
2424
spec:
2525
group: multicluster.x-k8s.io
2626
scope: Namespaced

config/crd/multicluster.x-k8s.io_serviceimports.yaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ metadata:
2020
# The revision is updated on each CRD change and reset back to 0 on every new version.
2121
# It can be used together with the version label when installing those CRDs
2222
# and prevent any downgrades.
23-
multicluster.x-k8s.io/crd-schema-revision: "0"
23+
multicluster.x-k8s.io/crd-schema-revision: "1"
2424
spec:
2525
group: multicluster.x-k8s.io
2626
scope: Namespaced
@@ -78,6 +78,15 @@ spec:
7878
- ports
7979
- type
8080
properties:
81+
internalTrafficPolicy:
82+
description: |-
83+
InternalTrafficPolicy describes how nodes distribute service traffic they
84+
receive on the ClusterIP. If set to "Local", the proxy will assume that pods
85+
only want to talk to endpoints of the service on the same node as the pod,
86+
dropping the traffic if there are no local endpoints. The default value,
87+
"Cluster", uses the standard behavior of routing to all endpoints evenly
88+
(possibly modified by topology and other features).
89+
type: string
8190
ipFamilies:
8291
description: IPFamilies identifies all the IPFamilies assigned for this ServiceImport.
8392
type: array
@@ -160,6 +169,15 @@ spec:
160169
Default value is 10800(for 3 hours).
161170
type: integer
162171
format: int32
172+
trafficDistribution:
173+
description: |-
174+
TrafficDistribution offers a way to express preferences for how traffic
175+
is distributed to Service endpoints. Implementations can use this field
176+
as a hint, but are not required to guarantee strict adherence. If the
177+
field is not set, the implementation will apply its default routing
178+
strategy. If set to "PreferClose", implementations should prioritize
179+
endpoints that are in the same zone.
180+
type: string
163181
type:
164182
description: |-
165183
type defines the type of this service.

conformance/service_import.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
. "github.com/onsi/gomega"
2626
corev1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/utils/ptr"
2829
"sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
2930
)
3031

@@ -179,6 +180,38 @@ func testClusterIPServiceImport() {
179180
})
180181
})
181182

183+
Context("", func() {
184+
BeforeEach(func() {
185+
t.helloService.Spec.InternalTrafficPolicy = ptr.To(corev1.ServiceInternalTrafficPolicyCluster)
186+
})
187+
Specify("The InternalTrafficPolicy for a ClusterSetIP ServiceImport should match the exported service's InternalTrafficPolicy",
188+
Label(RequiredLabel), func() {
189+
AddReportEntry(SpecRefReportEntry, "https://github.com/kubernetes/enhancements/tree/master/keps/sig-multicluster/1645-multi-cluster-services-api#internal-traffic-policy")
190+
191+
t.awaitServiceImport(&clients[0], helloServiceName, false, func(g Gomega, serviceImport *v1alpha1.ServiceImport) {
192+
g.Expect(serviceImport.Spec.InternalTrafficPolicy).To(Equal(t.helloService.Spec.InternalTrafficPolicy), reportNonConformant(
193+
"The InternalTrafficPolicy of the ServiceImport does not match the exported Service's InternalTrafficPolicy"))
194+
})
195+
},
196+
)
197+
})
198+
199+
Context("", func() {
200+
BeforeEach(func() {
201+
t.helloService.Spec.TrafficDistribution = ptr.To(corev1.ServiceTrafficDistributionPreferClose)
202+
})
203+
Specify("The TrafficDistribution for a ClusterSetIP ServiceImport should match the exported service's TrafficDistribution",
204+
Label(RequiredLabel), func() {
205+
AddReportEntry(SpecRefReportEntry, "https://github.com/kubernetes/enhancements/tree/master/keps/sig-multicluster/1645-multi-cluster-services-api#traffic-distribution")
206+
207+
t.awaitServiceImport(&clients[0], helloServiceName, false, func(g Gomega, serviceImport *v1alpha1.ServiceImport) {
208+
g.Expect(serviceImport.Spec.TrafficDistribution).To(Equal(t.helloService.Spec.TrafficDistribution), reportNonConformant(
209+
"The TrafficDistribution of the ServiceImport does not match the exported Service's TrafficDistribution"))
210+
})
211+
},
212+
)
213+
})
214+
182215
Specify("An IP should be allocated for a ClusterSetIP ServiceImport", Label(RequiredLabel), func() {
183216
AddReportEntry(SpecRefReportEntry, "https://github.com/kubernetes/enhancements/tree/master/keps/sig-multicluster/1645-multi-cluster-services-api#clustersetip")
184217

hack/verify-crd-bump-revision.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ BASE_REF="${PULL_BASE_SHA:-master}"
2020
crd_changed="$(git diff --name-only "${BASE_REF}" | grep -c "^config/crd/.*\.yaml$")"
2121
version_label_changed="$(git diff -U0 "${BASE_REF}" -- "config/crd-base/" | grep -c "multicluster.x-k8s.io/crd-schema-revision")"
2222

23-
if [ "${crd_changed}" -gt 0 ] && [ "${version_label_changed}" -ne 2 ]; then
23+
if [ "${crd_changed}" -gt 0 ] && [ "${version_label_changed}" -ne 4 ]; then
2424
echo "❌ CRDs were modified, but the CRD revision labels were not changed in 'config/crd-base/'. Please bump the CRDs revision."
2525
exit 1
2626
fi

pkg/apis/v1alpha1/serviceexport.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ const (
265265
// annotations.
266266
ServiceExportReasonAnnotationsConflict ServiceExportConditionReason = "AnnotationsConflict"
267267

268+
// ServiceExportReasonInternalTrafficPolicyConflict is used with the "Conflict"
269+
// condition when the exported service has a conflict related to internal traffic policy.
270+
ServiceExportReasonInternalTrafficPolicyConflict ServiceExportConditionReason = "InternalTrafficPolicyConflict"
271+
272+
// ServiceExportReasonTrafficDistributionConflict is used with the "Conflict"
273+
// condition when the exported service has a conflict related to traffic distribution.
274+
ServiceExportReasonTrafficDistributionConflict ServiceExportConditionReason = "TrafficDistributionConflict"
275+
268276
// ServiceExportReasonNoConflicts is used with the "Conflict" condition
269277
// when the condition is False.
270278
ServiceExportReasonNoConflicts ServiceExportConditionReason = "NoConflicts"

pkg/apis/v1alpha1/serviceimport.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ type ServiceImportSpec struct {
8888
// +kubebuilder:validation:MaxItems:=2
8989
// +optional
9090
IPFamilies []v1.IPFamily `json:"ipFamilies,omitempty"`
91+
92+
// InternalTrafficPolicy describes how nodes distribute service traffic they
93+
// receive on the ClusterIP. If set to "Local", the proxy will assume that pods
94+
// only want to talk to endpoints of the service on the same node as the pod,
95+
// dropping the traffic if there are no local endpoints. The default value,
96+
// "Cluster", uses the standard behavior of routing to all endpoints evenly
97+
// (possibly modified by topology and other features).
98+
// +optional
99+
InternalTrafficPolicy *v1.ServiceInternalTrafficPolicy `json:"internalTrafficPolicy,omitempty"`
100+
101+
// TrafficDistribution offers a way to express preferences for how traffic
102+
// is distributed to Service endpoints. Implementations can use this field
103+
// as a hint, but are not required to guarantee strict adherence. If the
104+
// field is not set, the implementation will apply its default routing
105+
// strategy. If set to "PreferClose", implementations should prioritize
106+
// endpoints that are in the same zone.
107+
// +optional
108+
TrafficDistribution *string `json:"trafficDistribution,omitempty"`
91109
}
92110

93111
// ServicePort represents the port on which the service is exposed

pkg/apis/v1alpha1/zz_generated.deepcopy.go

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

0 commit comments

Comments
 (0)