Skip to content

Commit de74090

Browse files
committed
refactor(waltr): add kv to the application
1 parent a9b843d commit de74090

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

cmd/waltr/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"github.com/fmjstudios/gopskit/internal/waltr/app"
55
"github.com/fmjstudios/gopskit/internal/waltr/cmd"
66
_ "github.com/fmjstudios/gopskit/pkg/stamp"
7+
"golang.org/x/sync/errgroup"
78
"log"
9+
"os"
810
)
911

1012
func main() {
@@ -17,4 +19,16 @@ func main() {
1719
if err := cmdRoot.Execute(); err != nil {
1820
kern.Log.Fatalf("%s exited with error: %v\n", kern.Name, err)
1921
}
22+
23+
g := new(errgroup.Group)
24+
g.Go(func() error {
25+
return kern.KV.Close()
26+
})
27+
28+
err = g.Wait()
29+
if err != nil {
30+
log.Fatalf("could not shut down waltr database connection: %v", err)
31+
}
32+
33+
os.Exit(0)
2034
}

internal/waltr/app/app.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"fmt"
55
"github.com/fmjstudios/gopskit/pkg/core"
66
"github.com/fmjstudios/gopskit/pkg/fs"
7+
"github.com/fmjstudios/gopskit/pkg/kv"
78
"github.com/fmjstudios/gopskit/pkg/log"
89
"github.com/fmjstudios/gopskit/pkg/proc"
910
"github.com/spf13/cobra"
11+
"path/filepath"
1012
"time"
1113

1214
"github.com/fmjstudios/gopskit/pkg/kube"
@@ -60,6 +62,13 @@ func New(opts ...Opt) (*State, error) {
6062
return nil, fmt.Errorf("could not create kubernetes client: %v", err)
6163
}
6264

65+
// embedded BadgerDB database
66+
dbpath := filepath.Join(platf.Data, "data")
67+
db, err := kv.New(dbpath)
68+
if err != nil {
69+
return nil, err
70+
}
71+
6372
// enforce HTTPS
6473
va := fmt.Sprintf("https://127.0.0.1:%s", kube.DefaultLocalPort)
6574
vc, err := vault.New(vault.WithAddress(va), vault.WithRequestTimeout(60*time.Second), vault.WithTLS(vault.TLSConfiguration{
@@ -77,6 +86,7 @@ func New(opts ...Opt) (*State, error) {
7786
Exec: exec,
7887
Kube: kc,
7988
Log: lgr,
89+
KV: db,
8090
Paths: platf,
8191
Stamp: stamps,
8292
},

pkg/core/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"github.com/fmjstudios/gopskit/pkg/fs"
55
"github.com/fmjstudios/gopskit/pkg/kube"
6+
"github.com/fmjstudios/gopskit/pkg/kv"
67
"github.com/fmjstudios/gopskit/pkg/log"
78
"github.com/fmjstudios/gopskit/pkg/proc"
89
"github.com/fmjstudios/gopskit/pkg/stamp"
@@ -34,6 +35,11 @@ type API struct {
3435
// Log is a wrapper object around Uber's `zap` logger
3536
Log *log.Logger
3637

38+
// KV is the embedded BadgerDB database wrapper which we us to persist
39+
// Vault tokens, unseal keys and recovery keys, e.g. after Vault initialization.
40+
// These values may or may not be made accessible via the CLIs via a subcommand.
41+
KV *kv.Database
42+
3743
// Stamp is build-time information that is linked into the final executable
3844
// by LD. Our Bazel builds stamps builds via LD using the 'x_defs' attribute
3945
// on the 'go_library' rule

pkg/kv/op.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ func (d *Database) delete(key []byte) error {
190190
return nil
191191
}
192192

193+
// Close closes an active connection to a database. This method must be called
194+
// during program shutdown or else you risk corrupting the data. Additionally,
195+
// we run garbage collection across the entire database before finally shutting
196+
// down.
193197
func (d *Database) Close() error {
194198
d.lock.Lock()
195199
defer d.lock.Unlock()
@@ -201,13 +205,15 @@ func (d *Database) Close() error {
201205
return d.kv.Close()
202206
}
203207

208+
// Path returns the filesystem path to the database
204209
func (d *Database) Path() string {
205210
d.lock.Lock()
206211
defer d.lock.Unlock()
207212

208213
return d.path
209214
}
210215

216+
// Config returns the badger.Options the database was initialized with
211217
func (d *Database) Config() badger.Options {
212218
d.lock.Lock()
213219
defer d.lock.Unlock()

0 commit comments

Comments
 (0)