Skip to content

Commit ae72e15

Browse files
authored
Merge pull request #1844 from fluxcd/svc-traffic-distribution
Add support for setting traffic distribution
2 parents 60cb38a + c30e655 commit ae72e15

File tree

8 files changed

+76
-6
lines changed

8 files changed

+76
-6
lines changed

artifacts/flagger/crd.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,18 @@ spec:
191191
appProtocol:
192192
description: Application protocol of the port
193193
type: string
194+
trafficDistribution:
195+
description: Traffic distribution of the service
196+
type: string
197+
enum:
198+
- PreferClose
199+
- PreferSameZone
200+
- PreferSameNode
194201
targetPort:
195202
description: Container target port name
196203
x-kubernetes-int-or-string: true
197204
portDiscovery:
198-
description: Enable port dicovery
205+
description: Enable port discovery
199206
type: boolean
200207
headless:
201208
description: Headless if set to true, generates headless Kubernetes services.

charts/flagger/crds/crd.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,18 @@ spec:
191191
appProtocol:
192192
description: Application protocol of the port
193193
type: string
194+
trafficDistribution:
195+
description: Traffic distribution of the service
196+
type: string
197+
enum:
198+
- PreferClose
199+
- PreferSameZone
200+
- PreferSameNode
194201
targetPort:
195202
description: Container target port name
196203
x-kubernetes-int-or-string: true
197204
portDiscovery:
198-
description: Enable port dicovery
205+
description: Enable port discovery
199206
type: boolean
200207
headless:
201208
description: Headless if set to true, generates headless Kubernetes services.

docs/gitbook/usage/how-it-works.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,15 @@ spec:
148148
targetPort: 9898
149149
portDiscovery: true
150150
headless: false
151+
trafficDistribution: PreferClose
151152
```
152153

153154
The container port from the target workload should match the `service.port` or `service.targetPort`.
154155
The `service.name` is optional, defaults to `spec.targetRef.name`.
155156
The `service.targetPort` can be a container port number or name.
156157
The `service.portName` is optional (defaults to `http`), if your workload uses gRPC then set the port name to `grpc`.
157158
The `service.appProtocol` is optional, more details can be found [here](https://kubernetes.io/docs/concepts/services-networking/service/#application-protocol).
158-
159+
The `service.trafficDistribution` is optional, more details can be found [here](https://kubernetes.io/docs/concepts/services-networking/service/#traffic-distribution).
159160

160161
If port discovery is enabled, Flagger scans the target workload and extracts the containers ports
161162
excluding the port specified in the canary service and service mesh sidecar ports.

kustomize/base/flagger/crd.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,18 @@ spec:
191191
appProtocol:
192192
description: Application protocol of the port
193193
type: string
194+
trafficDistribution:
195+
description: Traffic distribution of the service
196+
type: string
197+
enum:
198+
- PreferClose
199+
- PreferSameZone
200+
- PreferSameNode
194201
targetPort:
195202
description: Container target port name
196203
x-kubernetes-int-or-string: true
197204
portDiscovery:
198-
description: Enable port dicovery
205+
description: Enable port discovery
199206
type: boolean
200207
headless:
201208
description: Headless if set to true, generates headless Kubernetes services.

pkg/apis/flagger/v1beta1/canary.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ import (
2020
"fmt"
2121
"time"
2222

23-
"github.com/fluxcd/flagger/pkg/apis/gatewayapi/v1beta1"
24-
istiov1beta1 "github.com/fluxcd/flagger/pkg/apis/istio/v1beta1"
2523
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2624
"k8s.io/apimachinery/pkg/util/intstr"
25+
26+
"github.com/fluxcd/flagger/pkg/apis/gatewayapi/v1beta1"
27+
istiov1beta1 "github.com/fluxcd/flagger/pkg/apis/istio/v1beta1"
2728
)
2829

2930
const (
@@ -150,6 +151,11 @@ type CanaryService struct {
150151
// +optional
151152
AppProtocol string `json:"appProtocol,omitempty"`
152153

154+
// TrafficDistribution of the service
155+
// https://kubernetes.io/docs/concepts/services-networking/service/#traffic-distribution
156+
// +optional
157+
TrafficDistribution string `json:"trafficDistribution,omitempty"`
158+
153159
// PortDiscovery adds all container ports to the generated Kubernetes service
154160
PortDiscovery bool `json:"portDiscovery"`
155161

pkg/canary/service_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ func buildService(canary *flaggerv1.Canary, name string, src *corev1.Service) *c
165165
// Operation cannot be fulfilled on services "mysvc-canary": the object has been modified; please apply your changes to the latest version and try again
166166
delete(svc.ObjectMeta.Annotations, "kubectl.kubernetes.io/last-applied-configuration")
167167
}
168+
169+
if v := canary.Spec.Service.TrafficDistribution; v != "" {
170+
svc.Spec.TrafficDistribution = &v
171+
}
172+
168173
return svc
169174
}
170175

pkg/router/kubernetes_default.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
121121
svcSpec.Ports[0].AppProtocol = &v
122122
}
123123

124+
if v := canary.Spec.Service.TrafficDistribution; v != "" {
125+
svcSpec.TrafficDistribution = &v
126+
}
127+
124128
// set additional ports
125129
for n, p := range c.ports {
126130
cp := corev1.ServicePort{

pkg/router/kubernetes_default_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,39 @@ func TestServiceRouter_Create(t *testing.T) {
6464
assert.Equal(t, "None", primarySvc.Spec.ClusterIP)
6565
}
6666

67+
func TestServiceRouter_TrafficDistribution(t *testing.T) {
68+
mocks := newFixture(nil)
69+
trafficDistribution := "PreferClose"
70+
mocks.canary.Spec.Service.TrafficDistribution = trafficDistribution
71+
72+
router := &KubernetesDefaultRouter{
73+
kubeClient: mocks.kubeClient,
74+
flaggerClient: mocks.flaggerClient,
75+
logger: mocks.logger,
76+
}
77+
78+
err := router.Initialize(mocks.canary)
79+
require.NoError(t, err)
80+
81+
err = router.Reconcile(mocks.canary)
82+
require.NoError(t, err)
83+
84+
canarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-canary", metav1.GetOptions{})
85+
require.NoError(t, err)
86+
require.NotNil(t, canarySvc.Spec.TrafficDistribution)
87+
assert.Equal(t, trafficDistribution, *canarySvc.Spec.TrafficDistribution)
88+
89+
primarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
90+
require.NoError(t, err)
91+
require.NotNil(t, primarySvc.Spec.TrafficDistribution)
92+
assert.Equal(t, trafficDistribution, *primarySvc.Spec.TrafficDistribution)
93+
94+
apexSvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo", metav1.GetOptions{})
95+
require.NoError(t, err)
96+
require.NotNil(t, apexSvc.Spec.TrafficDistribution)
97+
assert.Equal(t, trafficDistribution, *apexSvc.Spec.TrafficDistribution)
98+
}
99+
67100
func TestServiceRouter_Update(t *testing.T) {
68101
mocks := newFixture(nil)
69102
router := &KubernetesDefaultRouter{

0 commit comments

Comments
 (0)