Skip to content

Commit d99d37b

Browse files
authored
Merge pull request #1755 from fluxcd/headless-svc
2 parents ff4051f + 45618b9 commit d99d37b

File tree

7 files changed

+26
-0
lines changed

7 files changed

+26
-0
lines changed

artifacts/flagger/crd.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ spec:
198198
portDiscovery:
199199
description: Enable port dicovery
200200
type: boolean
201+
headless:
202+
description: Headless if set to true, generates headless Kubernetes services.
203+
type: boolean
201204
timeout:
202205
description: HTTP or gRPC request timeout
203206
type: string

charts/flagger/crds/crd.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ spec:
198198
portDiscovery:
199199
description: Enable port dicovery
200200
type: boolean
201+
headless:
202+
description: Headless if set to true, generates headless Kubernetes services.
203+
type: boolean
201204
timeout:
202205
description: HTTP or gRPC request timeout
203206
type: string

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

+5
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ spec:
147147
appProtocol: http
148148
targetPort: 9898
149149
portDiscovery: true
150+
headless: false
150151
```
151152

152153
The container port from the target workload should match the `service.port` or `service.targetPort`.
@@ -155,6 +156,7 @@ The `service.targetPort` can be a container port number or name.
155156
The `service.portName` is optional (defaults to `http`), if your workload uses gRPC then set the port name to `grpc`.
156157
The `service.appProtocol` is optional, more details can be found [here](https://kubernetes.io/docs/concepts/services-networking/service/#application-protocol).
157158

159+
158160
If port discovery is enabled, Flagger scans the target workload and extracts the containers ports
159161
excluding the port specified in the canary service and service mesh sidecar ports.
160162
These ports will be used when generating the ClusterIP services.
@@ -204,6 +206,9 @@ Note that the `apex` annotations are added to both the generated Kubernetes Serv
204206
generated service mesh/ingress object. This allows using external-dns with Istio `VirtualServices`
205207
and `TraefikServices`. Beware of configuration conflicts [here](../faq.md#ExternalDNS).
206208

209+
If you want for the generated Kubernetes ClusterIP services to be [headless](https://kubernetes.io/docs/concepts/services-networking/service/#headless-services),
210+
then set `service.headless` to true.
211+
207212
Besides port mapping and metadata, the service specification can
208213
contain URI match and rewrite rules, timeout and retry polices:
209214

kustomize/base/flagger/crd.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ spec:
198198
portDiscovery:
199199
description: Enable port dicovery
200200
type: boolean
201+
headless:
202+
description: Headless if set to true, generates headless Kubernetes services.
203+
type: boolean
201204
timeout:
202205
description: HTTP or gRPC request timeout
203206
type: string

pkg/apis/flagger/v1beta1/canary.go

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ type CanaryService struct {
146146
// PortDiscovery adds all container ports to the generated Kubernetes service
147147
PortDiscovery bool `json:"portDiscovery"`
148148

149+
// Headless if set to true, generates headless Kubernetes services.
150+
// ref: https://kubernetes.io/docs/concepts/services-networking/service/#headless-services
151+
// +optional
152+
Headless bool `json:"headless,omitempty"`
153+
149154
// Timeout of the HTTP or gRPC request
150155
// +optional
151156
Timeout string `json:"timeout,omitempty"`

pkg/router/kubernetes_default.go

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
113113
},
114114
},
115115
}
116+
if canary.Spec.Service.Headless {
117+
svcSpec.ClusterIP = "None"
118+
}
116119

117120
if v := canary.Spec.Service.AppProtocol; v != "" {
118121
svcSpec.Ports[0].AppProtocol = &v

pkg/router/kubernetes_default_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434

3535
func TestServiceRouter_Create(t *testing.T) {
3636
mocks := newFixture(nil)
37+
mocks.canary.Spec.Service.Headless = true
38+
3739
router := &KubernetesDefaultRouter{
3840
kubeClient: mocks.kubeClient,
3941
flaggerClient: mocks.flaggerClient,
@@ -53,11 +55,13 @@ func TestServiceRouter_Create(t *testing.T) {
5355
assert.Equal(t, &appProtocol, canarySvc.Spec.Ports[0].AppProtocol)
5456
assert.Equal(t, "http", canarySvc.Spec.Ports[0].Name)
5557
assert.Equal(t, int32(9898), canarySvc.Spec.Ports[0].Port)
58+
assert.Equal(t, "None", canarySvc.Spec.ClusterIP)
5659

5760
primarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
5861
require.NoError(t, err)
5962
assert.Equal(t, "http", primarySvc.Spec.Ports[0].Name)
6063
assert.Equal(t, int32(9898), primarySvc.Spec.Ports[0].Port)
64+
assert.Equal(t, "None", primarySvc.Spec.ClusterIP)
6165
}
6266

6367
func TestServiceRouter_Update(t *testing.T) {

0 commit comments

Comments
 (0)