Skip to content

Commit 7f96b2a

Browse files
jkatzJonathan S. Katz
authored andcommitted
Only expose Service ports that are used for a PostgreSQL instance
The Service template would check for the presence of optional ports (e.g. pgBadger, collect) and expose those. However, this check is performed by seeing if the value of the pgBadger / collect port is present. The pgcluster CRD sets these values by default and, as such, would always expose the port regardless if pgBadger/collect were enabled. This adjusts the logic to only include the value of the collect/pgBadger port in the Service template if collect/pgBadger are actually enabled on the CR entry. Issue: #1611
1 parent f7634df commit 7f96b2a

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

operator/cluster/clusterlogic.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,33 @@ func AddCluster(clientset *kubernetes.Clientset, client *rest.RESTClient, cl *cr
5252
st = cl.Spec.UserLabels[config.LABEL_SERVICE_TYPE]
5353
}
5454

55-
//create the primary service
55+
// create the primary service
5656
serviceFields := ServiceTemplateFields{
57-
Name: cl.Spec.Name,
58-
ServiceName: cl.Spec.Name,
59-
ClusterName: cl.Spec.Name,
60-
Port: cl.Spec.Port,
61-
PGBadgerPort: cl.Spec.PGBadgerPort,
62-
ExporterPort: cl.Spec.ExporterPort,
63-
ServiceType: st,
57+
Name: cl.Spec.Name,
58+
ServiceName: cl.Spec.Name,
59+
ClusterName: cl.Spec.Name,
60+
Port: cl.Spec.Port,
61+
ServiceType: st,
62+
}
63+
64+
// only add references to the collect / pgBadger ports
65+
clusterLabels := cl.ObjectMeta.GetLabels()
66+
67+
if val, ok := clusterLabels[config.LABEL_BADGER]; ok && val == config.LABEL_TRUE {
68+
serviceFields.PGBadgerPort = cl.Spec.PGBadgerPort
69+
}
70+
71+
// ...due to legacy reasons, the collect label may not be available yet in the
72+
// main labels. so we will check here first, and then check the user labels
73+
if val, ok := clusterLabels[config.LABEL_COLLECT]; ok && val == config.LABEL_TRUE {
74+
serviceFields.ExporterPort = cl.Spec.ExporterPort
75+
}
76+
77+
// ...this condition should be targeted for removal in the future
78+
if cl.Spec.UserLabels != nil {
79+
if val, ok := cl.Spec.UserLabels[config.LABEL_COLLECT]; ok && val == config.LABEL_TRUE {
80+
serviceFields.ExporterPort = cl.Spec.ExporterPort
81+
}
6482
}
6583

6684
err = CreateService(clientset, &serviceFields, namespace)

0 commit comments

Comments
 (0)