Skip to content

Commit 07488c7

Browse files
adding appProtocol to Services/Endpoints
1 parent 8387698 commit 07488c7

File tree

11 files changed

+70
-41
lines changed

11 files changed

+70
-41
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
tools
44
/testing/kuttl/e2e-generated/
55
/testing/kuttl/e2e-generated-other/
6+
postgres_exporter.tar.gz

internal/controller/postgrescluster/cluster.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/util/intstr"
2727

28+
"github.com/crunchydata/postgres-operator/internal/initialize"
2829
"github.com/crunchydata/postgres-operator/internal/naming"
2930
"github.com/crunchydata/postgres-operator/internal/patroni"
3031
"github.com/crunchydata/postgres-operator/internal/pki"
@@ -143,10 +144,11 @@ func (r *Reconciler) generateClusterPrimaryService(
143144
service.Spec.Selector = nil
144145

145146
service.Spec.Ports = []corev1.ServicePort{{
146-
Name: naming.PortPostgreSQL,
147-
Port: *cluster.Spec.Port,
148-
Protocol: corev1.ProtocolTCP,
149-
TargetPort: intstr.FromString(naming.PortPostgreSQL),
147+
Name: naming.PortPostgreSQL,
148+
Port: *cluster.Spec.Port,
149+
Protocol: corev1.ProtocolTCP,
150+
TargetPort: intstr.FromString(naming.PortPostgreSQL),
151+
AppProtocol: initialize.String(postgres.IANAServiceName),
150152
}}
151153

152154
// Resolve to the ClusterIP for which Patroni has configured the Endpoints.
@@ -158,9 +160,10 @@ func (r *Reconciler) generateClusterPrimaryService(
158160
for _, sp := range service.Spec.Ports {
159161
endpoints.Subsets[0].Ports = append(endpoints.Subsets[0].Ports,
160162
corev1.EndpointPort{
161-
Name: sp.Name,
162-
Port: sp.Port,
163-
Protocol: sp.Protocol,
163+
Name: sp.Name,
164+
Port: sp.Port,
165+
Protocol: sp.Protocol,
166+
AppProtocol: sp.AppProtocol,
164167
})
165168
}
166169

@@ -221,10 +224,11 @@ func (r *Reconciler) generateClusterReplicaService(
221224
// ContainerPort. This name allows the port number to differ between Pods,
222225
// which can happen during a rolling update.
223226
service.Spec.Ports = []corev1.ServicePort{{
224-
Name: naming.PortPostgreSQL,
225-
Port: *cluster.Spec.Port,
226-
Protocol: corev1.ProtocolTCP,
227-
TargetPort: intstr.FromString(naming.PortPostgreSQL),
227+
Name: naming.PortPostgreSQL,
228+
Port: *cluster.Spec.Port,
229+
Protocol: corev1.ProtocolTCP,
230+
TargetPort: intstr.FromString(naming.PortPostgreSQL),
231+
AppProtocol: initialize.String(postgres.IANAServiceName),
228232
}}
229233

230234
err := errors.WithStack(r.setControllerReference(cluster, service))

internal/controller/postgrescluster/cluster_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,8 @@ ownerReferences:
634634
uid: ""
635635
`))
636636
assert.Assert(t, marshalMatches(service.Spec.Ports, `
637-
- name: postgres
637+
- appProtocol: postgresql
638+
name: postgres
638639
port: 2600
639640
protocol: TCP
640641
targetPort: postgres
@@ -665,7 +666,8 @@ subsets:
665666
- addresses:
666667
- ip: 1.9.8.3
667668
ports:
668-
- name: postgres
669+
- appProtocol: postgresql
670+
name: postgres
669671
port: 2600
670672
protocol: TCP
671673
`))
@@ -753,7 +755,8 @@ ownerReferences:
753755
`))
754756
assert.Assert(t, marshalMatches(service.Spec, `
755757
ports:
756-
- name: postgres
758+
- appProtocol: postgresql
759+
name: postgres
757760
port: 9876
758761
protocol: TCP
759762
targetPort: postgres

internal/controller/postgrescluster/patroni.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,11 @@ func (r *Reconciler) generatePatroniLeaderLeaseService(
262262
// ContainerPort. This name allows the port number to differ between
263263
// instances, which can happen during a rolling update.
264264
servicePort := corev1.ServicePort{
265-
Name: naming.PortPostgreSQL,
266-
Port: *cluster.Spec.Port,
267-
Protocol: corev1.ProtocolTCP,
268-
TargetPort: intstr.FromString(naming.PortPostgreSQL),
265+
Name: naming.PortPostgreSQL,
266+
Port: *cluster.Spec.Port,
267+
Protocol: corev1.ProtocolTCP,
268+
TargetPort: intstr.FromString(naming.PortPostgreSQL),
269+
AppProtocol: initialize.String(postgres.IANAServiceName),
269270
}
270271

271272
if spec := cluster.Spec.Service; spec == nil {

internal/controller/postgrescluster/patroni_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ ownerReferences:
9393
// Defaults to ClusterIP.
9494
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeClusterIP)
9595
assert.Assert(t, marshalMatches(service.Spec.Ports, `
96-
- name: postgres
96+
- appProtocol: postgresql
97+
name: postgres
9798
port: 9876
9899
protocol: TCP
99100
targetPort: postgres
@@ -182,7 +183,8 @@ ownerReferences:
182183
alwaysExpect(t, service)
183184
test.Expect(t, service)
184185
assert.Assert(t, marshalMatches(service.Spec.Ports, `
185-
- name: postgres
186+
- appProtocol: postgresql
187+
name: postgres
186188
port: 9876
187189
protocol: TCP
188190
targetPort: postgres
@@ -207,7 +209,8 @@ ownerReferences:
207209
alwaysExpect(t, service)
208210
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeNodePort)
209211
assert.Assert(t, marshalMatches(service.Spec.Ports, `
210-
- name: postgres
212+
- appProtocol: postgresql
213+
name: postgres
211214
nodePort: 32001
212215
port: 9876
213216
protocol: TCP
@@ -220,7 +223,8 @@ ownerReferences:
220223
assert.NilError(t, err)
221224
alwaysExpect(t, service)
222225
assert.Assert(t, marshalMatches(service.Spec.Ports, `
223-
- name: postgres
226+
- appProtocol: postgresql
227+
name: postgres
224228
nodePort: 32002
225229
port: 9876
226230
protocol: TCP

internal/controller/postgrescluster/pgadmin.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,11 @@ func (r *Reconciler) generatePGAdminService(
167167
// TODO(tjmoore4): A custom service port is not currently supported as this
168168
// requires updates to the pgAdmin service configuration.
169169
servicePort := corev1.ServicePort{
170-
Name: naming.PortPGAdmin,
171-
Port: *initialize.Int32(5050),
172-
Protocol: corev1.ProtocolTCP,
173-
TargetPort: intstr.FromString(naming.PortPGAdmin),
170+
Name: naming.PortPGAdmin,
171+
Port: *initialize.Int32(5050),
172+
Protocol: corev1.ProtocolTCP,
173+
TargetPort: intstr.FromString(naming.PortPGAdmin),
174+
AppProtocol: initialize.String(naming.AppProtocolHTTP),
174175
}
175176

176177
if spec := cluster.Spec.UserInterface.PGAdmin.Service; spec == nil {

internal/controller/postgrescluster/pgadmin_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ ownerReferences:
266266
// Defaults to ClusterIP.
267267
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeClusterIP)
268268
assert.Assert(t, marshalMatches(service.Spec.Ports, `
269-
- name: pgadmin
269+
- appProtocol: http
270+
name: pgadmin
270271
port: 5050
271272
protocol: TCP
272273
targetPort: pgadmin
@@ -299,7 +300,8 @@ ownerReferences:
299300
alwaysExpect(t, service)
300301
test.Expect(t, service)
301302
assert.Assert(t, marshalMatches(service.Spec.Ports, `
302-
- name: pgadmin
303+
- appProtocol: http
304+
name: pgadmin
303305
port: 5050
304306
protocol: TCP
305307
targetPort: pgadmin
@@ -324,7 +326,8 @@ ownerReferences:
324326
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeNodePort)
325327
alwaysExpect(t, service)
326328
assert.Assert(t, marshalMatches(service.Spec.Ports, `
327-
- name: pgadmin
329+
- appProtocol: http
330+
name: pgadmin
328331
nodePort: 32001
329332
port: 5050
330333
protocol: TCP
@@ -337,7 +340,8 @@ ownerReferences:
337340
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeLoadBalancer)
338341
alwaysExpect(t, service)
339342
assert.Assert(t, marshalMatches(service.Spec.Ports, `
340-
- name: pgadmin
343+
- appProtocol: http
344+
name: pgadmin
341345
nodePort: 32002
342346
port: 5050
343347
protocol: TCP

internal/controller/postgrescluster/pgbouncer.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,11 @@ func (r *Reconciler) generatePGBouncerService(
291291
// ContainerPort. This name allows the port number to differ between Pods,
292292
// which can happen during a rolling update.
293293
servicePort := corev1.ServicePort{
294-
Name: naming.PortPGBouncer,
295-
Port: *cluster.Spec.Proxy.PGBouncer.Port,
296-
Protocol: corev1.ProtocolTCP,
297-
TargetPort: intstr.FromString(naming.PortPGBouncer),
294+
Name: naming.PortPGBouncer,
295+
Port: *cluster.Spec.Proxy.PGBouncer.Port,
296+
Protocol: corev1.ProtocolTCP,
297+
TargetPort: intstr.FromString(naming.PortPGBouncer),
298+
AppProtocol: initialize.String(postgres.IANAServiceName),
298299
}
299300

300301
if spec := cluster.Spec.Proxy.PGBouncer.Service; spec == nil {

internal/controller/postgrescluster/pgbouncer_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ ownerReferences:
175175
// Defaults to ClusterIP.
176176
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeClusterIP)
177177
assert.Assert(t, marshalMatches(service.Spec.Ports, `
178-
- name: pgbouncer
178+
- appProtocol: postgresql
179+
name: pgbouncer
179180
port: 9651
180181
protocol: TCP
181182
targetPort: pgbouncer
@@ -208,7 +209,8 @@ ownerReferences:
208209
alwaysExpect(t, service)
209210
test.Expect(t, service)
210211
assert.Assert(t, marshalMatches(service.Spec.Ports, `
211-
- name: pgbouncer
212+
- appProtocol: postgresql
213+
name: pgbouncer
212214
port: 9651
213215
protocol: TCP
214216
targetPort: pgbouncer
@@ -233,7 +235,8 @@ ownerReferences:
233235
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeNodePort)
234236
alwaysExpect(t, service)
235237
assert.Assert(t, marshalMatches(service.Spec.Ports, `
236-
- name: pgbouncer
238+
- appProtocol: postgresql
239+
name: pgbouncer
237240
nodePort: 32001
238241
port: 9651
239242
protocol: TCP
@@ -246,7 +249,8 @@ ownerReferences:
246249
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeLoadBalancer)
247250
alwaysExpect(t, service)
248251
assert.Assert(t, marshalMatches(service.Spec.Ports, `
249-
- name: pgbouncer
252+
- appProtocol: postgresql
253+
name: pgbouncer
250254
nodePort: 32002
251255
port: 9651
252256
protocol: TCP

internal/naming/names.go

+5
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ const (
186186
RestoreConfigCopySuffix = "%s-restorecopy-%d"
187187
)
188188

189+
const (
190+
// AppProtocolHTTP is the name of the appProtocol for Services which use the HTTP protocol
191+
AppProtocolHTTP = "http"
192+
)
193+
189194
// AsObjectKey converts the ObjectMeta API type to a client.ObjectKey.
190195
// When you have a client.Object, use client.ObjectKeyFromObject() instead.
191196
func AsObjectKey(m metav1.ObjectMeta) client.ObjectKey {

internal/patroni/config.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,10 @@ func instanceEnvironment(
334334
for _, cp := range podContainers[i].Ports {
335335
if sp.TargetPort.StrVal == cp.Name {
336336
ports = append(ports, corev1.EndpointPort{
337-
Name: sp.Name,
338-
Port: cp.ContainerPort,
339-
Protocol: cp.Protocol,
337+
Name: sp.Name,
338+
Port: cp.ContainerPort,
339+
Protocol: cp.Protocol,
340+
AppProtocol: sp.AppProtocol,
340341
})
341342
}
342343
}

0 commit comments

Comments
 (0)