Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding appProtocol to all services #3436

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/vendor/
tools
/testing/kuttl/e2e-generated*/
postgres_exporter.tar.gz
26 changes: 15 additions & 11 deletions internal/controller/postgrescluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/crunchydata/postgres-operator/internal/initialize"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/internal/patroni"
"github.com/crunchydata/postgres-operator/internal/pki"
Expand Down Expand Up @@ -143,10 +144,11 @@ func (r *Reconciler) generateClusterPrimaryService(
service.Spec.Selector = nil

service.Spec.Ports = []corev1.ServicePort{{
Name: naming.PortPostgreSQL,
Port: *cluster.Spec.Port,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPostgreSQL),
Name: naming.PortPostgreSQL,
Port: *cluster.Spec.Port,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPostgreSQL),
AppProtocol: initialize.String(postgres.IANAServiceName),
}}

// Resolve to the ClusterIP for which Patroni has configured the Endpoints.
Expand All @@ -158,9 +160,10 @@ func (r *Reconciler) generateClusterPrimaryService(
for _, sp := range service.Spec.Ports {
endpoints.Subsets[0].Ports = append(endpoints.Subsets[0].Ports,
corev1.EndpointPort{
Name: sp.Name,
Port: sp.Port,
Protocol: sp.Protocol,
Name: sp.Name,
Port: sp.Port,
Protocol: sp.Protocol,
AppProtocol: sp.AppProtocol,
})
}

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

err := errors.WithStack(r.setControllerReference(cluster, service))
Expand Down
9 changes: 6 additions & 3 deletions internal/controller/postgrescluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ ownerReferences:
uid: ""
`))
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: postgres
- appProtocol: postgresql
name: postgres
port: 2600
protocol: TCP
targetPort: postgres
Expand Down Expand Up @@ -665,7 +666,8 @@ subsets:
- addresses:
- ip: 1.9.8.3
ports:
- name: postgres
- appProtocol: postgresql
name: postgres
port: 2600
protocol: TCP
`))
Expand Down Expand Up @@ -753,7 +755,8 @@ ownerReferences:
`))
assert.Assert(t, marshalMatches(service.Spec, `
ports:
- name: postgres
- appProtocol: postgresql
name: postgres
port: 9876
protocol: TCP
targetPort: postgres
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/postgrescluster/patroni.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,11 @@ func (r *Reconciler) generatePatroniLeaderLeaseService(
// ContainerPort. This name allows the port number to differ between
// instances, which can happen during a rolling update.
servicePort := corev1.ServicePort{
Name: naming.PortPostgreSQL,
Port: *cluster.Spec.Port,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPostgreSQL),
Name: naming.PortPostgreSQL,
Port: *cluster.Spec.Port,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPostgreSQL),
AppProtocol: initialize.String(postgres.IANAServiceName),
}

if spec := cluster.Spec.Service; spec == nil {
Expand Down
12 changes: 8 additions & 4 deletions internal/controller/postgrescluster/patroni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ ownerReferences:
// Defaults to ClusterIP.
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeClusterIP)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: postgres
- appProtocol: postgresql
name: postgres
port: 9876
protocol: TCP
targetPort: postgres
Expand Down Expand Up @@ -182,7 +183,8 @@ ownerReferences:
alwaysExpect(t, service)
test.Expect(t, service)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: postgres
- appProtocol: postgresql
name: postgres
port: 9876
protocol: TCP
targetPort: postgres
Expand All @@ -207,7 +209,8 @@ ownerReferences:
alwaysExpect(t, service)
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeNodePort)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: postgres
- appProtocol: postgresql
name: postgres
nodePort: 32001
port: 9876
protocol: TCP
Expand All @@ -220,7 +223,8 @@ ownerReferences:
assert.NilError(t, err)
alwaysExpect(t, service)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: postgres
- appProtocol: postgresql
name: postgres
nodePort: 32002
port: 9876
protocol: TCP
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/postgrescluster/pgadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ func (r *Reconciler) generatePGAdminService(
// TODO(tjmoore4): A custom service port is not currently supported as this
// requires updates to the pgAdmin service configuration.
servicePort := corev1.ServicePort{
Name: naming.PortPGAdmin,
Port: *initialize.Int32(5050),
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPGAdmin),
Name: naming.PortPGAdmin,
Port: *initialize.Int32(5050),
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPGAdmin),
AppProtocol: initialize.String(naming.AppProtocolHTTP),
}

if spec := cluster.Spec.UserInterface.PGAdmin.Service; spec == nil {
Expand Down
12 changes: 8 additions & 4 deletions internal/controller/postgrescluster/pgadmin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ ownerReferences:
// Defaults to ClusterIP.
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeClusterIP)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: pgadmin
- appProtocol: http
name: pgadmin
port: 5050
protocol: TCP
targetPort: pgadmin
Expand Down Expand Up @@ -299,7 +300,8 @@ ownerReferences:
alwaysExpect(t, service)
test.Expect(t, service)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: pgadmin
- appProtocol: http
name: pgadmin
port: 5050
protocol: TCP
targetPort: pgadmin
Expand All @@ -324,7 +326,8 @@ ownerReferences:
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeNodePort)
alwaysExpect(t, service)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: pgadmin
- appProtocol: http
name: pgadmin
nodePort: 32001
port: 5050
protocol: TCP
Expand All @@ -337,7 +340,8 @@ ownerReferences:
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeLoadBalancer)
alwaysExpect(t, service)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: pgadmin
- appProtocol: http
name: pgadmin
nodePort: 32002
port: 5050
protocol: TCP
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/postgrescluster/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,11 @@ func (r *Reconciler) generatePGBouncerService(
// ContainerPort. This name allows the port number to differ between Pods,
// which can happen during a rolling update.
servicePort := corev1.ServicePort{
Name: naming.PortPGBouncer,
Port: *cluster.Spec.Proxy.PGBouncer.Port,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPGBouncer),
Name: naming.PortPGBouncer,
Port: *cluster.Spec.Proxy.PGBouncer.Port,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPGBouncer),
AppProtocol: initialize.String(postgres.IANAServiceName),
}

if spec := cluster.Spec.Proxy.PGBouncer.Service; spec == nil {
Expand Down
12 changes: 8 additions & 4 deletions internal/controller/postgrescluster/pgbouncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ ownerReferences:
// Defaults to ClusterIP.
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeClusterIP)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: pgbouncer
- appProtocol: postgresql
name: pgbouncer
port: 9651
protocol: TCP
targetPort: pgbouncer
Expand Down Expand Up @@ -208,7 +209,8 @@ ownerReferences:
alwaysExpect(t, service)
test.Expect(t, service)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: pgbouncer
- appProtocol: postgresql
name: pgbouncer
port: 9651
protocol: TCP
targetPort: pgbouncer
Expand All @@ -233,7 +235,8 @@ ownerReferences:
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeNodePort)
alwaysExpect(t, service)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: pgbouncer
- appProtocol: postgresql
name: pgbouncer
nodePort: 32001
port: 9651
protocol: TCP
Expand All @@ -246,7 +249,8 @@ ownerReferences:
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeLoadBalancer)
alwaysExpect(t, service)
assert.Assert(t, marshalMatches(service.Spec.Ports, `
- name: pgbouncer
- appProtocol: postgresql
name: pgbouncer
nodePort: 32002
port: 9651
protocol: TCP
Expand Down
5 changes: 5 additions & 0 deletions internal/naming/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ const (
RestoreConfigCopySuffix = "%s-restorecopy-%d"
)

const (
// AppProtocolHTTP is the name of the appProtocol for Services which use the HTTP protocol
AppProtocolHTTP = "http"
)

// AsObjectKey converts the ObjectMeta API type to a client.ObjectKey.
// When you have a client.Object, use client.ObjectKeyFromObject() instead.
func AsObjectKey(m metav1.ObjectMeta) client.ObjectKey {
Expand Down
7 changes: 4 additions & 3 deletions internal/patroni/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,10 @@ func instanceEnvironment(
for _, cp := range podContainers[i].Ports {
if sp.TargetPort.StrVal == cp.Name {
ports = append(ports, corev1.EndpointPort{
Name: sp.Name,
Port: cp.ContainerPort,
Protocol: cp.Protocol,
Name: sp.Name,
Port: cp.ContainerPort,
Protocol: cp.Protocol,
AppProtocol: sp.AppProtocol,
})
}
}
Expand Down