-
Notifications
You must be signed in to change notification settings - Fork 76
K8SPG-901: allow running without pgbouncer #1443
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
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
172c718
allow running withoug pgbouncer
mayankshah1607 3ff7fb3
use primary service
mayankshah1607 2ec2260
update comment
mayankshah1607 8523f43
Merge branch 'main' into K8SPG-901
mayankshah1607 84bcbc5
add unit test
mayankshah1607 4cb911c
potential nil pointer deref
mayankshah1607 188c2d4
update ToCrunchy unit tests
mayankshah1607 e28166a
comments
mayankshah1607 c494bf7
add test for getHost
mayankshah1607 a0474fe
imports
mayankshah1607 d4a602c
linting
mayankshah1607 fe4c5fc
fix tests
mayankshah1607 a0a87ac
backward compatibility
mayankshah1607 67ecb0f
improvements
mayankshah1607 e1cb385
bug fixes
mayankshah1607 82b4dd1
linting
mayankshah1607 384bda1
linting
mayankshah1607 4e20c0e
test fix
mayankshah1607 49eb7f9
Merge branch 'main' into K8SPG-901
mayankshah1607 a6263ba
Merge branch 'main' into K8SPG-901
mayankshah1607 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| package pgcluster | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
|
||
| v2 "github.com/percona/percona-postgresql-operator/v2/pkg/apis/pgv2.percona.com/v2" | ||
| "github.com/percona/percona-postgresql-operator/v2/pkg/apis/postgres-operator.crunchydata.com/v1beta1" | ||
| ) | ||
|
|
||
| func TestGetHost(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| const ( | ||
| clusterName = "test-cluster" | ||
| ns = "test-ns" | ||
| ) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| proxy *v2.PGProxySpec | ||
| svc *corev1.Service | ||
| expectedHost string | ||
| expectErr bool | ||
| }{ | ||
| { | ||
| name: "proxy is nil", | ||
| proxy: nil, | ||
| expectedHost: clusterName + "-primary." + ns + ".svc", | ||
| }, | ||
| { | ||
| name: "proxy set but PGBouncer is nil", | ||
| proxy: &v2.PGProxySpec{PGBouncer: nil}, | ||
| expectedHost: clusterName + "-primary." + ns + ".svc", | ||
| }, | ||
| { | ||
| name: "PGBouncer configured, no ServiceExpose", | ||
| proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{}}, | ||
| expectedHost: clusterName + "-pgbouncer." + ns + ".svc", | ||
| }, | ||
| { | ||
| name: "PGBouncer configured, ServiceExpose is ClusterIP", | ||
| proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{ | ||
| ServiceExpose: &v2.ServiceExpose{Type: string(corev1.ServiceTypeClusterIP)}, | ||
| }}, | ||
| expectedHost: clusterName + "-pgbouncer." + ns + ".svc", | ||
| }, | ||
| { | ||
| name: "PGBouncer configured, ServiceExpose is NodePort", | ||
| proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{ | ||
| ServiceExpose: &v2.ServiceExpose{Type: string(corev1.ServiceTypeNodePort)}, | ||
| }}, | ||
| expectedHost: clusterName + "-pgbouncer." + ns + ".svc", | ||
| }, | ||
| { | ||
| name: "PGBouncer LoadBalancer with IP", | ||
| proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{ | ||
| ServiceExpose: &v2.ServiceExpose{Type: string(corev1.ServiceTypeLoadBalancer)}, | ||
| }}, | ||
| svc: &corev1.Service{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: clusterName + "-pgbouncer", | ||
| Namespace: ns, | ||
| }, | ||
| Status: corev1.ServiceStatus{ | ||
| LoadBalancer: corev1.LoadBalancerStatus{ | ||
| Ingress: []corev1.LoadBalancerIngress{ | ||
| {IP: "10.0.0.1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedHost: "10.0.0.1", | ||
| }, | ||
| { | ||
| name: "PGBouncer LoadBalancer with hostname", | ||
| proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{ | ||
| ServiceExpose: &v2.ServiceExpose{Type: string(corev1.ServiceTypeLoadBalancer)}, | ||
| }}, | ||
| svc: &corev1.Service{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: clusterName + "-pgbouncer", | ||
| Namespace: ns, | ||
| }, | ||
| Status: corev1.ServiceStatus{ | ||
| LoadBalancer: corev1.LoadBalancerStatus{ | ||
| Ingress: []corev1.LoadBalancerIngress{ | ||
| {Hostname: "my-lb.example.com"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedHost: "my-lb.example.com", | ||
| }, | ||
| { | ||
| name: "PGBouncer LoadBalancer with both IP and hostname prefers hostname", | ||
| proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{ | ||
| ServiceExpose: &v2.ServiceExpose{Type: string(corev1.ServiceTypeLoadBalancer)}, | ||
| }}, | ||
| svc: &corev1.Service{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: clusterName + "-pgbouncer", | ||
| Namespace: ns, | ||
| }, | ||
| Status: corev1.ServiceStatus{ | ||
| LoadBalancer: corev1.LoadBalancerStatus{ | ||
| Ingress: []corev1.LoadBalancerIngress{ | ||
| {IP: "10.0.0.1", Hostname: "my-lb.example.com"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedHost: "my-lb.example.com", | ||
| }, | ||
| { | ||
| name: "PGBouncer LoadBalancer with no ingress returns empty host", | ||
| proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{ | ||
| ServiceExpose: &v2.ServiceExpose{Type: string(corev1.ServiceTypeLoadBalancer)}, | ||
| }}, | ||
| svc: &corev1.Service{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: clusterName + "-pgbouncer", | ||
| Namespace: ns, | ||
| }, | ||
| }, | ||
| expectedHost: "", | ||
| }, | ||
| { | ||
| name: "PGBouncer LoadBalancer service not found", | ||
| proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{ | ||
| ServiceExpose: &v2.ServiceExpose{Type: string(corev1.ServiceTypeLoadBalancer)}, | ||
| }}, | ||
| expectErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cr := &v2.PerconaPGCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: clusterName, | ||
| Namespace: ns, | ||
| }, | ||
| Spec: v2.PerconaPGClusterSpec{ | ||
| Proxy: tt.proxy, | ||
| }, | ||
| } | ||
|
|
||
| var objs []client.Object | ||
| if tt.svc != nil { | ||
| objs = append(objs, tt.svc) | ||
| } | ||
|
|
||
| s := scheme.Scheme | ||
| err := v1beta1.AddToScheme(s) | ||
| require.NoError(t, err) | ||
| err = v2.AddToScheme(s) | ||
| require.NoError(t, err) | ||
|
|
||
| cl := fake.NewClientBuilder(). | ||
| WithScheme(s). | ||
| WithObjects(objs...). | ||
| WithStatusSubresource(objs...). | ||
| Build() | ||
|
|
||
| r := &PGClusterReconciler{ | ||
| Client: cl, | ||
| } | ||
|
|
||
| host, err := r.getHost(ctx, cr) | ||
| if tt.expectErr { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.expectedHost, host) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -212,21 +212,23 @@ func (cr *PerconaPGCluster) Default() { | |||||
| cr.Spec.InstanceSets[i].Metadata.Labels[LabelOperatorVersion] = cr.Spec.CRVersion | ||||||
| } | ||||||
|
|
||||||
| if cr.Spec.Proxy == nil { | ||||||
| cr.Spec.Proxy = new(PGProxySpec) | ||||||
| } | ||||||
| if cr.CompareVersion("2.9.0") < 0 || cr.Spec.Proxy.IsSet() { | ||||||
| if cr.Spec.Proxy == nil { | ||||||
| cr.Spec.Proxy = &PGProxySpec{} | ||||||
| } | ||||||
|
|
||||||
| if cr.Spec.Proxy.PGBouncer == nil { | ||||||
| cr.Spec.Proxy.PGBouncer = new(PGBouncerSpec) | ||||||
| } | ||||||
| if cr.Spec.Proxy.PGBouncer == nil { | ||||||
| cr.Spec.Proxy.PGBouncer = &PGBouncerSpec{} | ||||||
| } | ||||||
|
|
||||||
| if cr.Spec.Proxy.PGBouncer.Metadata == nil { | ||||||
| cr.Spec.Proxy.PGBouncer.Metadata = new(crunchyv1beta1.Metadata) | ||||||
| } | ||||||
| if cr.Spec.Proxy.PGBouncer.Metadata.Labels == nil { | ||||||
| cr.Spec.Proxy.PGBouncer.Metadata.Labels = make(map[string]string) | ||||||
| if cr.Spec.Proxy.PGBouncer.Metadata == nil { | ||||||
| cr.Spec.Proxy.PGBouncer.Metadata = &crunchyv1beta1.Metadata{} | ||||||
| } | ||||||
| if cr.Spec.Proxy.PGBouncer.Metadata.Labels == nil { | ||||||
| cr.Spec.Proxy.PGBouncer.Metadata.Labels = make(map[string]string) | ||||||
| } | ||||||
| cr.Spec.Proxy.PGBouncer.Metadata.Labels[LabelOperatorVersion] = cr.Spec.CRVersion | ||||||
| } | ||||||
| cr.Spec.Proxy.PGBouncer.Metadata.Labels[LabelOperatorVersion] = cr.Spec.CRVersion | ||||||
|
|
||||||
| t := true | ||||||
| f := false | ||||||
|
|
@@ -1040,6 +1042,10 @@ type PGProxySpec struct { | |||||
| PGBouncer *PGBouncerSpec `json:"pgBouncer"` | ||||||
| } | ||||||
|
|
||||||
| func (p *PGProxySpec) IsSet() bool { | ||||||
| return p != nil && p.PGBouncer != nil | ||||||
| } | ||||||
|
|
||||||
| func (p *PGProxySpec) ToCrunchy(version string) *crunchyv1beta1.PostgresProxySpec { | ||||||
| if p == nil { | ||||||
|
||||||
| if p == nil { | |
| if p == nil || p.PGBouncer == nil { |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getHostalways returns the in-cluster primary Service FQDN, ignoring whether the primary Service is exposed as a LoadBalancer..status.hostis printed as the user-facing “Endpoint”; without pgBouncer, users exposing Postgres via.spec.expose.type=LoadBalancerwill still see an internal-only DNS name..spec.exposeis LoadBalancer, fetch the primary Service and prefer its ingress hostname/IP; otherwise return the Service FQDN.