Skip to content
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

feat(connection_pool): expose pool name #561

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

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

## v0.16.1 - YYYY-MM-DD

- Check VPC for running services before deletion
- Expose `KAFKA_SCHEMA_REGISTRY_URI` and `KAFKA_REST_URI` to `Kafka` secret
- 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
17 changes: 13 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,12 @@ func (h ConnectionPoolHandler) get(ctx context.Context, avn *aiven.Client, obj c
return nil, fmt.Errorf("cannot get ConnectionPool: %w", err)
}

// The pool comes with its own port
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 +150,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 +178,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"],
byashimov marked this conversation as resolved.
Show resolved Hide resolved
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
Loading