Skip to content

Commit c706975

Browse files
feat: Add NATS Hord driver support
This commit introduces support for using NATS as a key-value store via the Hord library. NATS configuration and testing align with established patterns within the Tarmac project. The NATS test case in TestFullService is treated consistently with other integration tests. Unnecessary inline comments added during development have been removed. Changes include: - NATS configuration parameters are fetched directly (e.g., `nats_urls`) in `pkg/app/app.go`. - The `pkg/config/config.go` file no longer contains NATS-specific structures. - Documentation in `docs/running-tarmac/configuration.md` has been updated for the corrected configuration keys. - NATS testing is integrated into the `TestFullService` function in `pkg/app/server_test.go`. - The import alias for the NATS hord driver is now `nats`. - Unnecessary inline comments related to NATS have been removed from `pkg/app/app.go`, `pkg/app/app_test.go`, and `pkg/app/server_test.go`. - The CI configuration in `.github/workflows/build.yml` includes a job for NATS tests, running a NATS server.
1 parent f3469d3 commit c706975

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

.github/workflows/build.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,25 @@ jobs:
118118
uses: codecov/codecov-action@v5
119119
with:
120120
token: ${{ secrets.CODECOV_TOKEN }}
121+
122+
nats:
123+
runs-on: ubuntu-latest
124+
services:
125+
nats:
126+
image: nats:latest
127+
ports:
128+
- 4222:4222
129+
steps:
130+
- uses: actions/checkout@v4
131+
- name: Set up Go
132+
uses: actions/setup-go@v5
133+
with:
134+
go-version: '1.21'
135+
- name: Execute Tests
136+
env:
137+
NATS_URLS: nats://localhost:4222 # Ensure test uses this if it becomes configurable
138+
run: make build tests-nats # Assuming a make target 'tests-nats' will be created
139+
- name: Upload coverage to Codecov
140+
uses: codecov/codecov-action@v5
141+
with:
142+
token: ${{ secrets.CODECOV_TOKEN }}

docs/running-tarmac/configuration.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ When using Environment Variables, all configurations are prefixed with `APP_`. T
3030
| `APP_WASM_POOL_SIZE` | `wasm_pool_size` | `int` | Number of WASM function instances to create \(Default: `100`\). Only applicable when `wasm_function` is used. |
3131
| `APP_ENABLE_PPROF` | `enable_pprof` | `bool` | Enable PProf Collection HTTP end-points |
3232
| `APP_ENABLE_KVSTORE` | `enable_kvstore` | `bool` | Enable the KV Store |
33-
| `APP_KVSTORE_TYPE` | `kvstore_type` | `string` | Select KV Store to use (Options: `redis`, `cassandra`, `boltdb`, `in-memory`, `internal`)|
33+
| `APP_KVSTORE_TYPE` | `kvstore_type` | `string` | Select KV Store to use (Options: `redis`, `cassandra`, `boltdb`, `in-memory`, `internal`, `nats`)|
34+
| `APP_NATS_URLS` | `nats_urls` | `string` | Comma-separated list of NATS server URLs (Required if `kvstore_type` is `nats`) |
35+
| `APP_NATS_USER` | `nats_user` | `string` | Username for NATS authentication (Optional) |
36+
| `APP_NATS_PASSWORD` | `nats_password` | `string` | Password for NATS authentication (Optional) |
3437
| `APP_ENABLE_SQL` | `enable_sql` | `bool` | Enable the SQL Store |
3538
| `APP_SQL_TYPE` | `sql_type` | `string` | Select SQL Store to use (Options: `postgres`, `mysql`)|
3639
| `APP_RUN_MODE` | `run_mode` | `string` | Select the run mode for Tarmac (Options: `daemon`, `job`). Default: `daemon`. The `job` option will cause Tarmac to exit after init functions are executed. |

docs/running-tarmac/kvstore.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ The below table outlines the different available options.
1919
| BoltDB | `boltdb` | BoltDB Embedded key/value store | Strong Consistency, Persistent Storage |
2020
| Redis | `redis` | Redis including Sentinel and Enterprise capabilities | Strong Consistency, Fast Reads and Writes, Non-Persistent storage |
2121
| Cassandra | `cassandra` | Cassandra including TLS connectivity | Eventual Consistency, Persistent Storage, Large sets of data |
22+
| NATS | `nats` | NATS Server including JetStream for KV | Flexible, Cloud-Native Messaging and KV, Eventual Consistency |
2223

2324
For more detailed configuration options, check out the [Configuration](configuration.md) documentation.

pkg/app/app.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/tarmac-project/hord/drivers/bbolt"
2929
"github.com/tarmac-project/hord/drivers/cassandra"
3030
"github.com/tarmac-project/hord/drivers/hashmap"
31+
"github.com/tarmac-project/hord/drivers/nats"
3132
"github.com/tarmac-project/hord/drivers/redis"
3233
"github.com/tarmac-project/tarmac/pkg/callbacks/httpclient"
3334
"github.com/tarmac-project/tarmac/pkg/callbacks/kvstore"
@@ -321,6 +322,15 @@ func (srv *Server) Run() error {
321322
if err != nil {
322323
return fmt.Errorf("could not establish kvstore connection - %s", err)
323324
}
325+
case "nats":
326+
srv.kv, err = nats.Dial(nats.Config{
327+
Servers: srv.cfg.GetString("nats_urls"),
328+
User: srv.cfg.GetString("nats_user"),
329+
Password: srv.cfg.GetString("nats_password"),
330+
})
331+
if err != nil {
332+
return fmt.Errorf("could not establish nats kvstore connection - %w", err)
333+
}
324334
default:
325335
return fmt.Errorf("unknown kvstore specified - %s", srv.cfg.GetString("kvstore_type"))
326336
}

pkg/app/app_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ func TestBadConfigs(t *testing.T) {
8686
v.Set("wasm_function", "something-that-does-not-exist")
8787
cfgs["invalid WASM path"] = v
8888

89+
// Invalid NATS Address
90+
v = viper.New()
91+
v.Set("enable_tls", false)
92+
v.Set("listen_addr", "0.0.0.0:8443")
93+
v.Set("disable_logging", true)
94+
v.Set("enable_kvstore", true)
95+
v.Set("kvstore_type", "nats")
96+
v.Set("nats_urls", "nats://invalid-nats-host:4222")
97+
cfgs["invalid NATS Address"] = v
98+
8999
// Failing init function
90100
v = viper.New()
91101
v.Set("enable_tls", false)

pkg/app/server_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ func TestFullService(t *testing.T) {
219219
tc.cfg.Set("wasm_function_config", "/testdata/sdkv1/tarmac.json")
220220
tt = append(tt, tc)
221221

222+
tc = FullServiceTestCase{name: "NATS KV Full Service", cfg: viper.New()}
223+
tc.cfg.Set("disable_logging", false)
224+
tc.cfg.Set("debug", true)
225+
tc.cfg.Set("listen_addr", "localhost:9001")
226+
tc.cfg.Set("enable_kvstore", true)
227+
tc.cfg.Set("kvstore_type", "nats")
228+
tc.cfg.Set("nats_urls", "nats://localhost:4222")
229+
tc.cfg.Set("wasm_function_config", "/testdata/tarmac.json")
230+
tt = append(tt, tc)
231+
222232
for _, tc := range tt {
223233
t.Run(tc.name, func(t *testing.T) {
224234
srv := New(tc.cfg)

0 commit comments

Comments
 (0)