From 483ece0c4ed9530fc1b74ce3f418efb91b62f059 Mon Sep 17 00:00:00 2001 From: Dinar Valeev Date: Wed, 27 May 2020 10:39:49 +0200 Subject: [PATCH] Allow NodePort type for master service This commit introduces new property to a postgresql spec serviceNodePort. It enable NodePort type service for master role with automatic port allocation if value is 0. Othewise it takes desired port number like 30025. fixes: https://github.com/zalando/postgres-operator/issues/983 Signed-off-by: Dinar Valeev --- pkg/apis/acid.zalan.do/v1/postgresql_type.go | 2 ++ pkg/cluster/k8sres.go | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/pkg/apis/acid.zalan.do/v1/postgresql_type.go b/pkg/apis/acid.zalan.do/v1/postgresql_type.go index 7346fb0e5..e5b2de9f0 100644 --- a/pkg/apis/acid.zalan.do/v1/postgresql_type.go +++ b/pkg/apis/acid.zalan.do/v1/postgresql_type.go @@ -45,6 +45,8 @@ type PostgresSpec struct { EnableMasterLoadBalancer *bool `json:"enableMasterLoadBalancer,omitempty"` EnableReplicaLoadBalancer *bool `json:"enableReplicaLoadBalancer,omitempty"` + // User nodeport as service type + ServiceNodePort *int32 `json:"serviceNodePort,omitempty"` // deprecated load balancer settings maintained for backward compatibility // see "Load balancers" operator docs UseLoadBalancer *bool `json:"useLoadBalancer,omitempty"` diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index c02a64df0..d679e0573 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -1659,6 +1659,13 @@ func (c *Cluster) generateService(role PostgresRole, spec *acidv1.PostgresSpec) Type: v1.ServiceTypeClusterIP, } + if spec.ServiceNodePort != nil && role == Master { + serviceSpec.Type = v1.ServiceTypeNodePort + if *spec.ServiceNodePort > 0 { + servicePort := &serviceSpec + servicePort.Ports[0].NodePort = *spec.ServiceNodePort + } + } if role == Replica || c.patroniKubernetesUseConfigMaps() { serviceSpec.Selector = c.roleLabelsSet(false, role) }