Skip to content

Commit

Permalink
refactor(cockroachdb): to use request driven options (#2883)
Browse files Browse the repository at this point in the history
* feat: use recommended cluster settings for cockroachdb

* fix: wrap errors

* feat: add WithStatements option and expose recommended settings as a list of statements

* Update modules/cockroachdb/options.go

Co-authored-by: Steven Hartland <[email protected]>

* Update modules/cockroachdb/cockroachdb.go

Co-authored-by: Steven Hartland <[email protected]>

* fix: return close error on defer

* chore: improvement documentation and set default value for statements in defaultOptions

* Update modules/cockroachdb/cockroachdb.go

Co-authored-by: Manuel de la Peña <[email protected]>

* chore: remove unnecessary nolint directive and rename ClusterDefaults to DefaultStatements

* fix: only use recommended settings in root user tests

* feat!(cockroachdb): simplify connection handling

Simplify the connection handling in cockroachdb so that ConnectionString
can be used without the user doing extra work to handle TLS if enabled.

Deprecate TLSConfig which is no longer needed separately.

BREAKING_CHANGE: This now returns a registered connection string so is
no longer compatible with pgx.ParseConfig, use ConnectionConfig for this
use case instead.

* refactor(cockroachdb): to use request driven options

Refactor cockroachdb module to to use request driven options,
simplifying the flow.

* fix: various fixes and remove certs

Various fixes and remove the ability to configure certs using the
default generated ones instead.

* fix: add missing data files

Add the missing data file and remove unused constants.

* chore: add reference to ErrTLSNotEnabled

* fix: lint failure for missing test helper

* feat: tls strategy and strategy walk

Extract TLS certificate wait strategy into a dedicated wait type so it
can be reused.

Implement walk method which can be used to identify wait strategies in
a request chain.

Use embed to simplify wait test loading of certs.

* chore(cockroachdb): remove customizer

Remove the now unused customizer.

* fix: check setOptions error return

* fix: lint style

Fix lint style issue for blank line.

* chore: various clean ups

* docs(cockroachdb): clarified run defaults

Clarified the impact of run defaults.

---------

Co-authored-by: Martin Asquino <[email protected]>
Co-authored-by: martskins <[email protected]>
Co-authored-by: Manuel de la Peña <[email protected]>
  • Loading branch information
4 people authored Dec 2, 2024
1 parent 4d0f7b7 commit c28f0f8
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 483 deletions.
4 changes: 2 additions & 2 deletions docs/features/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ The example will also create a client that will connect to the server using the
demonstrating how to use the generated certificate to communicate with a service.

<!--codeinclude-->
[Create a self-signed certificate](../../modules/cockroachdb/certs.go) inside_block:exampleSelfSignedCert
[Sign a self-signed certificate](../../modules/cockroachdb/certs.go) inside_block:exampleSignSelfSignedCert
[Create a self-signed certificate](../../modules/rabbitmq/examples_test.go) inside_block:exampleSelfSignedCert
[Sign a self-signed certificate](../../modules/rabbitmq/examples_test.go) inside_block:exampleSignSelfSignedCert
<!--/codeinclude-->
33 changes: 25 additions & 8 deletions docs/modules/cockroachdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The Testcontainers module for CockroachDB.

Please run the following command to add the CockroachDB module to your Go dependencies:

```
```shell
go get github.com/testcontainers/testcontainers-go/modules/cockroachdb
```

Expand Down Expand Up @@ -54,23 +54,33 @@ E.g. `Run(context.Background(), "cockroachdb/cockroach:latest-v23.1")`.

Set the database that is created & dialled with `cockroachdb.WithDatabase`.

#### Password authentication
#### User and Password

You can configured the container to create a user with a password by setting `cockroachdb.WithUser` and `cockroachdb.WithPassword`.

Disable insecure mode and connect with password authentication by setting `cockroachdb.WithUser` and `cockroachdb.WithPassword`.
`cockroachdb.WithPassword` is incompatible with `cockroachdb.WithInsecure`.

#### Store size

Control the maximum amount of memory used for storage, by default this is 100% but can be changed by provided a valid option to `WithStoreSize`. Checkout https://www.cockroachlabs.com/docs/stable/cockroach-start#store for the full range of options available.

#### TLS authentication

`cockroachdb.WithTLS` lets you provide the CA certificate along with the certicate and key for the node & clients to connect with.
Internally CockroachDB requires a client certificate for the user to connect with.
`cockroachdb.WithInsecure` lets you disable the use of TLS on connections.

`cockroachdb.WithInsecure` is incompatible with `cockroachdb.WithPassword`.

#### Initialization Scripts

`cockroachdb.WithInitScripts` adds the given scripts to those automatically run when the container starts.
These will be ignored if data exists in the `/cockroach/cockroach-data` directory within the container.

A helper `cockroachdb.NewTLSConfig` exists to generate all of this for you.
`cockroachdb.WithNoClusterDefaults` disables the default cluster settings script.

!!!warning
When TLS is enabled there's a very small, unlikely chance that the underlying driver can panic when registering the driver as part of waiting for CockroachDB to be ready to accept connections. If this is repeatedly happening please open an issue.
Without this option Cockroach containers run `data/cluster-defaults.sql` on startup
which configures the settings recommended by Cockroach Labs for
[local testing clusters](https://www.cockroachlabs.com/docs/stable/local-testing)
unless data exists in the `/cockroach/cockroach-data` directory within the container.

### Container Methods

Expand All @@ -87,3 +97,10 @@ Same as `ConnectionString` but any error to generate the address will raise a pa
#### TLSConfig

Returns `*tls.Config` setup to allow you to dial your client over TLS, if enabled, else this will error with `cockroachdb.ErrTLSNotEnabled`.

!!!info
The `TLSConfig()` function is deprecated and will be removed in the next major release of _Testcontainers for Go_.

#### ConnectionConfig

Returns `*pgx.ConnConfig` which can be passed to `pgx.ConnectConfig` to open a new connection.
67 changes: 0 additions & 67 deletions modules/cockroachdb/certs.go

This file was deleted.

Loading

0 comments on commit c28f0f8

Please sign in to comment.