From abe8d3e153ba644ffb9dbbe1461617b014258156 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 6 Oct 2022 13:24:15 -0400 Subject: [PATCH] scaffolding for zrok gc (#75) --- cmd/zrok/gc.go | 38 ++++++++++++++++++++++++++++++++++++++ controller/controller.go | 15 +++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 cmd/zrok/gc.go diff --git a/cmd/zrok/gc.go b/cmd/zrok/gc.go new file mode 100644 index 00000000..86e81281 --- /dev/null +++ b/cmd/zrok/gc.go @@ -0,0 +1,38 @@ +package main + +import ( + "github.com/michaelquigley/cf" + "github.com/openziti-test-kitchen/zrok/controller" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(newGcCmd().cmd) +} + +type gcCmd struct { + cmd *cobra.Command +} + +func newGcCmd() *gcCmd { + cmd := &cobra.Command{ + Use: "gc ", + Short: "Garbage collect a zrok instance", + Args: cobra.ExactArgs(1), + } + c := &gcCmd{cmd: cmd} + cmd.Run = c.run + return c +} + +func (c *gcCmd) run(_ *cobra.Command, args []string) { + cfg, err := controller.LoadConfig(args[0]) + if err != nil { + panic(err) + } + logrus.Infof(cf.Dump(cfg, cf.DefaultOptions())) + if err := controller.GC(cfg); err != nil { + panic(err) + } +} diff --git a/controller/controller.go b/controller/controller.go index 8e11cd96..38e830f0 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -9,6 +9,7 @@ import ( "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/metadata" "github.com/pkg/errors" + "github.com/sirupsen/logrus" ) var str *store.Store @@ -55,6 +56,20 @@ func Run(cfg *Config) error { return nil } +func GC(cfg *Config) error { + if v, err := store.Open(cfg.Store); err == nil { + str = v + } else { + return errors.Wrap(err, "error opening store") + } + defer func() { + if err := str.Close(); err != nil { + logrus.Errorf("error closing store: %v", err) + } + }() + return nil +} + func versionHandler(_ metadata.VersionParams) middleware.Responder { return metadata.NewVersionOK().WithPayload(version) }