Skip to content

Commit dfcc67a

Browse files
committed
fix: make service type None actually create an headless service
1 parent 935fed7 commit dfcc67a

6 files changed

Lines changed: 53 additions & 11 deletions

File tree

stackgres-k8s/install/operator-sdk/stackgres-operator/config/manifests/bases/stackgres.clusterserviceversion.template.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ EOF
100100
operatorframework.io/os.linux: supported
101101
relatedImages:
102102
- image: quay.io/ongres/kubectl:v1.31.3-build-6.38
103-
name: kubectl_1_30_0
104-
- image: gcr.io/kubebuilder/kube-rbac-proxy:v0.13.0
105-
name: kube-rbac-proxy_0_13_0
103+
name: kubectl_1_31_3
106104
- image: quay.io/stackgres/operator:${VERSION}
107105
name: stackgres-operator
108106
- image: quay.io/stackgres/restapi:${VERSION}

stackgres-k8s/src/common/src/main/java/io/stackgres/common/crd/postgres/service/StackGresPostgresService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.Objects;
1111

12+
import com.fasterxml.jackson.annotation.JsonIgnore;
1213
import com.fasterxml.jackson.annotation.JsonInclude;
1314
import io.fabric8.kubernetes.api.model.ServicePort;
1415
import io.fabric8.kubernetes.api.model.SessionAffinityConfig;
@@ -83,6 +84,14 @@ public void setEnabled(Boolean enabled) {
8384
this.enabled = enabled;
8485
}
8586

87+
@JsonIgnore
88+
public String getServiceType() {
89+
if (Objects.equals(getType(), StackGresPostgresServiceType.NONE.toString())) {
90+
return "ClusterIP";
91+
}
92+
return getType();
93+
}
94+
8695
@Override
8796
public String getType() {
8897
return type;
@@ -103,6 +112,14 @@ public void setAllocateLoadBalancerNodePorts(Boolean allocateLoadBalancerNodePor
103112
super.setAllocateLoadBalancerNodePorts(allocateLoadBalancerNodePorts);
104113
}
105114

115+
@JsonIgnore
116+
public String getServiceClusterIP() {
117+
if (Objects.equals(getType(), StackGresPostgresServiceType.NONE.toString())) {
118+
return "None";
119+
}
120+
return null;
121+
}
122+
106123
@Override
107124
public String getClusterIP() {
108125
return super.getClusterIP();

stackgres-k8s/src/common/src/main/resources/crds/SGCluster.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,15 +674,20 @@ spec:
674674
default: true
675675
type:
676676
type: string
677-
enum: ["ClusterIP", "LoadBalancer", "NodePort"]
677+
enum: ["ClusterIP", "LoadBalancer", "NodePort", "None"]
678678
description: |
679679
type determines how the Service is exposed. Defaults to ClusterIP. Valid
680-
options are ClusterIP, NodePort, and LoadBalancer. "ClusterIP" allocates
680+
options are ClusterIP, NodePort, LoadBalancer and None. "ClusterIP" allocates
681681
a cluster-internal IP address for load-balancing to endpoints.
682682
"NodePort" builds on ClusterIP and allocates a port on every node.
683683
"LoadBalancer" builds on NodePort and creates
684684
an external load-balancer (if supported in the current cloud).
685-
More info: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
685+
"None" creates an headless service that can be use in conjunction with `.spec.pods.disableEnvoy`
686+
set to `true` in order to acces the database using a DNS.
687+
More info:
688+
* https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
689+
* https://kubernetes.io/docs/concepts/services-networking/service/#headless-services
690+
* https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/
686691
default: ClusterIP
687692
customPorts:
688693
type: array

stackgres-k8s/src/common/src/main/resources/crds/SGDistributedLogs.yaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,21 @@ spec:
111111
properties: &service-properties
112112
type:
113113
type: string
114-
enum: ["ClusterIP", "LoadBalancer", "NodePort"]
115-
description: Specifies the type of Kubernetes service(`ClusterIP`, `LoadBalancer`, `NodePort`)
114+
enum: ["ClusterIP", "LoadBalancer", "NodePort", "None"]
115+
description: |
116+
type determines how the Service is exposed. Defaults to ClusterIP. Valid
117+
options are ClusterIP, NodePort, LoadBalancer and None. "ClusterIP" allocates
118+
a cluster-internal IP address for load-balancing to endpoints.
119+
"NodePort" builds on ClusterIP and allocates a port on every node.
120+
"LoadBalancer" builds on NodePort and creates
121+
an external load-balancer (if supported in the current cloud).
122+
"None" creates an headless service that can be use in conjunction with `.spec.pods.disableEnvoy`
123+
set to `true` in order to acces the database using a DNS.
124+
More info:
125+
* https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
126+
* https://kubernetes.io/docs/concepts/services-networking/service/#headless-services
127+
* https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/
128+
default: ClusterIP
116129
allocateLoadBalancerNodePorts: #!jq_placeholder .definitions["io.k8s.api.core.v1.ServiceSpec"].properties.allocateLoadBalancerNodePorts
117130
{"description":"allocateLoadBalancerNodePorts defines if NodePorts will be automatically allocated for services with type LoadBalancer. Default is \"true\". It may be set to \"false\" if the cluster load-balancer does not rely on NodePorts. If the caller requests specific NodePorts (by specifying a value), those requests will be respected, regardless of this field. This field may only be set for services with type LoadBalancer and will be cleared if the type is changed to any other type.","type":"boolean"}
118131
externalIPs: #!jq_placeholder .definitions["io.k8s.api.core.v1.ServiceSpec"].properties.externalIPs #allocateloadbalancernodeports-v1-core

stackgres-k8s/src/common/src/main/resources/crds/SGShardedCluster.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,20 @@ spec:
401401
description: Specify if the service should be created or not.
402402
type:
403403
type: string
404-
enum: ["ClusterIP", "LoadBalancer", "NodePort"]
404+
enum: ["ClusterIP", "LoadBalancer", "NodePort", "None"]
405405
description: |
406406
type determines how the Service is exposed. Defaults to ClusterIP. Valid
407-
options are ClusterIP, NodePort, and LoadBalancer. "ClusterIP" allocates
407+
options are ClusterIP, NodePort, LoadBalancer and None. "ClusterIP" allocates
408408
a cluster-internal IP address for load-balancing to endpoints.
409409
"NodePort" builds on ClusterIP and allocates a port on every node.
410410
"LoadBalancer" builds on NodePort and creates
411411
an external load-balancer (if supported in the current cloud).
412-
More info: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
412+
"None" creates an headless service that can be use in conjunction with `.spec.pods.disableEnvoy`
413+
set to `true` in order to acces the database using a DNS.
414+
More info:
415+
* https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
416+
* https://kubernetes.io/docs/concepts/services-networking/service/#headless-services
417+
* https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/
413418
allocateLoadBalancerNodePorts: #!jq_placeholder .definitions["io.k8s.api.core.v1.ServiceSpec"].properties.allocateLoadBalancerNodePorts
414419
{"description":"allocateLoadBalancerNodePorts defines if NodePorts will be automatically allocated for services with type LoadBalancer. Default is \"true\". It may be set to \"false\" if the cluster load-balancer does not rely on NodePorts. If the caller requests specific NodePorts (by specifying a value), those requests will be respected, regardless of this field. This field may only be set for services with type LoadBalancer and will be cleared if the type is changed to any other type.","type":"boolean"}
415420
externalIPs: #!jq_placeholder .definitions["io.k8s.api.core.v1.ServiceSpec"].properties.externalIPs #allocateloadbalancernodeports-v1-core

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/cluster/patroni/PatroniServices.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ private Service createPrimaryService(StackGresClusterContext context) {
164164
.endMetadata()
165165
.withSpec(cluster.getSpec().getPostgresServices().getPrimary())
166166
.editSpec()
167+
.withType(cluster.getSpec().getPostgresServices().getPrimary().getServiceType())
168+
.withClusterIP(cluster.getSpec().getPostgresServices().getPrimary().getServiceClusterIP())
167169
.withPorts(getPrimaryServicePorts(cluster))
168170
.endSpec()
169171
.build();
@@ -298,6 +300,8 @@ private Service createReplicaService(StackGresClusterContext context) {
298300
.endMetadata()
299301
.withSpec(cluster.getSpec().getPostgresServices().getReplicas())
300302
.editSpec()
303+
.withType(cluster.getSpec().getPostgresServices().getReplicas().getServiceType())
304+
.withClusterIP(cluster.getSpec().getPostgresServices().getReplicas().getServiceClusterIP())
301305
.withSelector(labelFactory.clusterReplicaLabels(cluster))
302306
.withPorts(getReplicasPorts(cluster))
303307
.endSpec()

0 commit comments

Comments
 (0)