Skip to content

Commit

Permalink
feat(connection_pool): expose pool name
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Dec 11, 2023
1 parent 52b43a7 commit 9fadc69
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## [MAJOR.MINOR.PATCH] - YYYY-MM-DD

## v0.16.1 - YYYY-MM-DD

- Check VPC for running services before deletion
- Expose `CONNECTIONPOOL_NAME` in `ConnectionPool` secret
- Fix `CONNECTIONPOOL_PORT` exposes service port instead of pool port

## v0.16.0 - 2023-12-07

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/connectionpool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ConnectionPoolSpec struct {
PoolMode string `json:"poolMode,omitempty"`

// Information regarding secret creation.
// Exposed keys: `CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`, `CONNECTIONPOOL_DATABASE`, `CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`, `CONNECTIONPOOL_SSLMODE`, `CONNECTIONPOOL_DATABASE_URI`
// Exposed keys: `CONNECTIONPOOL_NAME`, `CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`, `CONNECTIONPOOL_DATABASE`, `CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`, `CONNECTIONPOOL_SSLMODE`, `CONNECTIONPOOL_DATABASE_URI`
ConnInfoSecretTarget ConnInfoSecretTarget `json:"connInfoSecretTarget,omitempty"`

// Authentication reference to Aiven token in a secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ spec:
type: object
connInfoSecretTarget:
description: 'Information regarding secret creation. Exposed keys:
`CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`, `CONNECTIONPOOL_DATABASE`,
`CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`, `CONNECTIONPOOL_SSLMODE`,
`CONNECTIONPOOL_DATABASE_URI`'
`CONNECTIONPOOL_NAME`, `CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`,
`CONNECTIONPOOL_DATABASE`, `CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`,
`CONNECTIONPOOL_SSLMODE`, `CONNECTIONPOOL_DATABASE_URI`'
properties:
annotations:
additionalProperties:
Expand Down
6 changes: 3 additions & 3 deletions config/crd/bases/aiven.io_connectionpools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ spec:
type: object
connInfoSecretTarget:
description: 'Information regarding secret creation. Exposed keys:
`CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`, `CONNECTIONPOOL_DATABASE`,
`CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`, `CONNECTIONPOOL_SSLMODE`,
`CONNECTIONPOOL_DATABASE_URI`'
`CONNECTIONPOOL_NAME`, `CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`,
`CONNECTIONPOOL_DATABASE`, `CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`,
`CONNECTIONPOOL_SSLMODE`, `CONNECTIONPOOL_DATABASE_URI`'
properties:
annotations:
additionalProperties:
Expand Down
16 changes: 12 additions & 4 deletions controllers/connectionpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package controllers
import (
"context"
"fmt"
"net/url"
"strconv"

"github.com/aiven/aiven-go-client/v2"
Expand Down Expand Up @@ -129,6 +130,11 @@ func (h ConnectionPoolHandler) get(ctx context.Context, avn *aiven.Client, obj c
return nil, fmt.Errorf("cannot get ConnectionPool: %w", err)
}

poolURI, err := url.Parse(cp.ConnectionURI)
if err != nil {
return nil, fmt.Errorf("can't parse ConnectionPool URI: %w", err)
}

s, err := avn.Services.Get(ctx, connPool.Spec.Project, connPool.Spec.ServiceName)
if err != nil {
return nil, fmt.Errorf("cannot get service: %w", err)
Expand All @@ -143,16 +149,17 @@ func (h ConnectionPoolHandler) get(ctx context.Context, avn *aiven.Client, obj c
if len(connPool.Spec.Username) == 0 {
prefix := getSecretPrefix(connPool)
stringData := map[string]string{
prefix + "NAME": connPool.Name,
prefix + "HOST": s.URIParams["host"],
prefix + "PORT": s.URIParams["port"],
prefix + "PORT": poolURI.Port(),
prefix + "DATABASE": cp.Database,
prefix + "USER": s.URIParams["user"],
prefix + "PASSWORD": s.URIParams["password"],
prefix + "SSLMODE": s.URIParams["sslmode"],
prefix + "DATABASE_URI": cp.ConnectionURI,
// todo: remove in future releases
"PGHOST": s.URIParams["host"],
"PGPORT": s.URIParams["port"],
"PGPORT": poolURI.Port(),
"PGDATABASE": cp.Database,
"PGUSER": s.URIParams["user"],
"PGPASSWORD": s.URIParams["password"],
Expand All @@ -170,16 +177,17 @@ func (h ConnectionPoolHandler) get(ctx context.Context, avn *aiven.Client, obj c

prefix := getSecretPrefix(connPool)
stringData := map[string]string{
prefix + "NAME": connPool.Name,
prefix + "HOST": s.URIParams["host"],
prefix + "PORT": s.URIParams["port"],
prefix + "PORT": poolURI.Port(),
prefix + "DATABASE": cp.Database,
prefix + "USER": cp.Username,
prefix + "PASSWORD": u.Password,
prefix + "SSLMODE": s.URIParams["sslmode"],
prefix + "DATABASE_URI": cp.ConnectionURI,
// todo: remove in future releases
"PGHOST": s.URIParams["host"],
"PGPORT": s.URIParams["port"],
"PGPORT": poolURI.Port(),
"PGDATABASE": cp.Database,
"PGUSER": cp.Username,
"PGPASSWORD": u.Password,
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/api-reference/connectionpool.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ConnectionPoolSpec defines the desired state of ConnectionPool.
**Optional**

- [`authSecretRef`](#spec.authSecretRef-property){: name='spec.authSecretRef-property'} (object). Authentication reference to Aiven token in a secret. See below for [nested schema](#spec.authSecretRef).
- [`connInfoSecretTarget`](#spec.connInfoSecretTarget-property){: name='spec.connInfoSecretTarget-property'} (object). Information regarding secret creation. Exposed keys: `CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`, `CONNECTIONPOOL_DATABASE`, `CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`, `CONNECTIONPOOL_SSLMODE`, `CONNECTIONPOOL_DATABASE_URI`. See below for [nested schema](#spec.connInfoSecretTarget).
- [`connInfoSecretTarget`](#spec.connInfoSecretTarget-property){: name='spec.connInfoSecretTarget-property'} (object). Information regarding secret creation. Exposed keys: `CONNECTIONPOOL_NAME`, `CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`, `CONNECTIONPOOL_DATABASE`, `CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`, `CONNECTIONPOOL_SSLMODE`, `CONNECTIONPOOL_DATABASE_URI`. See below for [nested schema](#spec.connInfoSecretTarget).
- [`poolMode`](#spec.poolMode-property){: name='spec.poolMode-property'} (string, Enum: `session`, `transaction`, `statement`). Mode the pool operates in (session, transaction, statement).
- [`poolSize`](#spec.poolSize-property){: name='spec.poolSize-property'} (integer). Number of connections the pool may create towards the backend server.

Expand All @@ -76,7 +76,7 @@ Authentication reference to Aiven token in a secret.

_Appears on [`spec`](#spec)._

Information regarding secret creation. Exposed keys: `CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`, `CONNECTIONPOOL_DATABASE`, `CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`, `CONNECTIONPOOL_SSLMODE`, `CONNECTIONPOOL_DATABASE_URI`.
Information regarding secret creation. Exposed keys: `CONNECTIONPOOL_NAME`, `CONNECTIONPOOL_HOST`, `CONNECTIONPOOL_PORT`, `CONNECTIONPOOL_DATABASE`, `CONNECTIONPOOL_USER`, `CONNECTIONPOOL_PASSWORD`, `CONNECTIONPOOL_SSLMODE`, `CONNECTIONPOOL_DATABASE_URI`.

**Required**

Expand Down
6 changes: 6 additions & 0 deletions tests/connectionpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func TestConnectionPool(t *testing.T) {
assert.NotEmpty(t, secret.Data["DATABASE_URI"])

// New secrets
assert.Equal(t, poolName, string(secret.Data["CONNECTIONPOOL_NAME"]))
assert.NotEmpty(t, secret.Data["CONNECTIONPOOL_HOST"])
assert.NotEmpty(t, secret.Data["CONNECTIONPOOL_PORT"])
assert.NotEmpty(t, secret.Data["CONNECTIONPOOL_DATABASE"])
Expand All @@ -165,6 +166,11 @@ func TestConnectionPool(t *testing.T) {
assert.NotEmpty(t, secret.Data["CONNECTIONPOOL_SSLMODE"])
assert.NotEmpty(t, secret.Data["CONNECTIONPOOL_DATABASE_URI"])

// URI contains valid values
uri := string(secret.Data["CONNECTIONPOOL_DATABASE_URI"])
assert.Contains(t, uri, string(secret.Data["CONNECTIONPOOL_HOST"]))
assert.Contains(t, uri, string(secret.Data["CONNECTIONPOOL_PORT"]))

// We need to validate deletion,
// because we can get false positive here:
// if service is deleted, pool is destroyed in Aiven. No service — no pool. No pool — no pool.
Expand Down

0 comments on commit 9fadc69

Please sign in to comment.