From 2a042b65ece8b2ce50b9c29517fa7a2c9c9c8576 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Wed, 25 Oct 2023 15:03:24 -0500 Subject: [PATCH] chore: Improve namespace filter --- cmd/dump/dump.go | 9 ++---- cmd/exec/exec.go | 5 ++++ cmd/port_forward/port_forward.go | 3 ++ cmd/restore/restore.go | 3 ++ cmd/status/status.go | 7 +++-- internal/config/flags/kubernetes.go | 4 +-- .../namespace_filter.go | 30 ++++++++++++++----- .../namespace_filter_test.go | 19 +++++++----- internal/util/cmd_setup.go | 3 +- 9 files changed, 56 insertions(+), 27 deletions(-) rename internal/config/{ => namespace_filter}/namespace_filter.go (54%) rename internal/config/{ => namespace_filter}/namespace_filter_test.go (82%) diff --git a/cmd/dump/dump.go b/cmd/dump/dump.go index d63071a1..bd67f593 100644 --- a/cmd/dump/dump.go +++ b/cmd/dump/dump.go @@ -1,12 +1,12 @@ package dump import ( + "context" "os" - "strconv" "github.com/clevyr/kubedb/internal/actions/dump" - "github.com/clevyr/kubedb/internal/config" "github.com/clevyr/kubedb/internal/config/flags" + "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/util" "github.com/spf13/cobra" @@ -33,10 +33,6 @@ the generated filename might look like "` + dump.HelpFilename() + `"`, PreRunE: preRun, RunE: run, - - Annotations: map[string]string{ - "access": strconv.Itoa(config.ReadOnly), - }, } flags.JobPodLabels(cmd) @@ -57,6 +53,7 @@ the generated filename might look like "` + dump.HelpFilename() + `"`, flags.RemoteGzip(cmd) flags.Spinner(cmd, &action.Spinner) + cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadOnly)) return cmd } diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index 38e88be4..51453153 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -1,8 +1,11 @@ package exec import ( + "context" + "github.com/clevyr/kubedb/internal/actions/exec" "github.com/clevyr/kubedb/internal/config/flags" + "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/util" "github.com/spf13/cobra" @@ -28,6 +31,8 @@ func New() *cobra.Command { flags.Username(cmd) flags.Password(cmd) cmd.Flags().StringVarP(&action.Command, consts.CommandFlag, "c", "", "Run a single command and exit") + + cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadWrite)) return cmd } diff --git a/cmd/port_forward/port_forward.go b/cmd/port_forward/port_forward.go index dcc48fbf..a0cb367d 100644 --- a/cmd/port_forward/port_forward.go +++ b/cmd/port_forward/port_forward.go @@ -1,10 +1,12 @@ package port_forward import ( + "context" "strconv" "github.com/clevyr/kubedb/internal/actions/port_forward" "github.com/clevyr/kubedb/internal/config/flags" + "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/util" "github.com/spf13/cobra" @@ -55,6 +57,7 @@ func New() *cobra.Command { panic(err) } + cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadWrite)) return cmd } diff --git a/cmd/restore/restore.go b/cmd/restore/restore.go index 58937070..4ceb5c18 100644 --- a/cmd/restore/restore.go +++ b/cmd/restore/restore.go @@ -1,12 +1,14 @@ package restore import ( + "context" "errors" "os" "github.com/AlecAivazis/survey/v2" "github.com/clevyr/kubedb/internal/actions/restore" "github.com/clevyr/kubedb/internal/config/flags" + "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/util" "github.com/spf13/cobra" @@ -53,6 +55,7 @@ Supported Input Filetypes: flags.Spinner(cmd, &action.Spinner) cmd.Flags().BoolVarP(&action.Force, consts.ForceFlag, "f", false, "Do not prompt before restore") + cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadWrite)) return cmd } diff --git a/cmd/status/status.go b/cmd/status/status.go index 720349b7..8a52e49b 100644 --- a/cmd/status/status.go +++ b/cmd/status/status.go @@ -1,6 +1,7 @@ package status import ( + "context" "errors" "fmt" "os" @@ -10,14 +11,13 @@ import ( "github.com/clevyr/kubedb/internal/config" "github.com/clevyr/kubedb/internal/config/flags" - "github.com/clevyr/kubedb/internal/consts" + "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/database" "github.com/clevyr/kubedb/internal/kubernetes" "github.com/clevyr/kubedb/internal/util" "github.com/fatih/color" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -34,6 +34,8 @@ func New() *cobra.Command { flags.Database(cmd) flags.Username(cmd) flags.Password(cmd) + + cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadOnly)) return cmd } @@ -51,7 +53,6 @@ func run(cmd *cobra.Command, args []string) error { prefixErr := color.RedString(" ✗") bold := color.New(color.Bold).Sprintf - viper.Set(consts.NamespaceFilterKey, false) defaultSetupErr := util.DefaultSetup(cmd, &conf, util.SetupOptions{Name: "status"}) fmt.Println(bold("Cluster Info")) diff --git a/internal/config/flags/kubernetes.go b/internal/config/flags/kubernetes.go index 36bf35d1..a6048b9f 100644 --- a/internal/config/flags/kubernetes.go +++ b/internal/config/flags/kubernetes.go @@ -4,7 +4,7 @@ import ( "os" "path/filepath" - "github.com/clevyr/kubedb/internal/config" + "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/kubernetes" "github.com/spf13/cobra" @@ -68,7 +68,7 @@ func Namespace(cmd *cobra.Command) { return nil, cobra.ShellCompDirectiveError } names := make([]string, 0, len(namespaces.Items)) - access := config.NewNamespaceRegexp(cmd.Annotations["access"]) + access := namespace_filter.NewFromContext(cmd.Context()) for _, namespace := range namespaces.Items { if access.Match(namespace.Name) { names = append(names, namespace.Name) diff --git a/internal/config/namespace_filter.go b/internal/config/namespace_filter/namespace_filter.go similarity index 54% rename from internal/config/namespace_filter.go rename to internal/config/namespace_filter/namespace_filter.go index 7011c98d..92803f78 100644 --- a/internal/config/namespace_filter.go +++ b/internal/config/namespace_filter/namespace_filter.go @@ -1,8 +1,8 @@ -package config +package namespace_filter import ( + "context" "regexp" - "strconv" "github.com/clevyr/kubedb/internal/consts" "github.com/spf13/viper" @@ -16,7 +16,7 @@ var ( type AccessLevel uint8 const ( - ReadWrite = iota + ReadWrite AccessLevel = iota ReadOnly ) @@ -31,12 +31,15 @@ func (level NamespaceRegexp) Match(namespace string) bool { return level.re.MatchString(namespace) } -func NewNamespaceRegexp(accessLevel string) NamespaceRegexp { - v, _ := strconv.ParseUint(accessLevel, 10, 8) - if AccessLevel(v) == ReadOnly { +func NewFromContext(ctx context.Context) NamespaceRegexp { + accessLevel, _ := LevelFromContext(ctx) + + switch accessLevel { + case ReadOnly: return NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadOnly)} + default: + return NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadWrite)} } - return NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadWrite)} } func init() { @@ -44,3 +47,16 @@ func init() { viper.SetDefault(consts.NamespaceFilterROKey, namespaceFilterReadOnly) viper.SetDefault(consts.NamespaceFilterRWKey, namespaceFilterReadWrite) } + +type contextKey string + +var accessLevelKey = contextKey("accessLevel") + +func NewContext(ctx context.Context, level AccessLevel) context.Context { + return context.WithValue(ctx, accessLevelKey, level) +} + +func LevelFromContext(ctx context.Context) (AccessLevel, bool) { + filter, ok := ctx.Value(accessLevelKey).(AccessLevel) + return filter, ok +} diff --git a/internal/config/namespace_filter_test.go b/internal/config/namespace_filter/namespace_filter_test.go similarity index 82% rename from internal/config/namespace_filter_test.go rename to internal/config/namespace_filter/namespace_filter_test.go index 6b5cd64c..d14f1e05 100644 --- a/internal/config/namespace_filter_test.go +++ b/internal/config/namespace_filter/namespace_filter_test.go @@ -1,8 +1,8 @@ -package config +package namespace_filter import ( + "context" "regexp" - "strconv" "testing" "github.com/clevyr/kubedb/internal/consts" @@ -52,22 +52,25 @@ func TestNamespaceRegexp_Match(t *testing.T) { } } -func TestNewNamespaceRegexp(t *testing.T) { +func TestNewFromContext(t *testing.T) { + roCtx := NewContext(context.Background(), ReadOnly) + rwCtx := NewContext(context.Background(), ReadWrite) + type args struct { - accessLevel string + ctx context.Context } tests := []struct { name string args args want NamespaceRegexp }{ - {"ro", args{strconv.Itoa(ReadOnly)}, NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadOnly)}}, - {"rw", args{strconv.Itoa(ReadWrite)}, NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadWrite)}}, + {"ro", args{roCtx}, NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadOnly)}}, + {"rw", args{rwCtx}, NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadWrite)}}, + {"unset", args{context.Background()}, NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadWrite)}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := NewNamespaceRegexp(tt.args.accessLevel) - assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want, NewFromContext(tt.args.ctx)) }) } } diff --git a/internal/util/cmd_setup.go b/internal/util/cmd_setup.go index 35e1b07a..5e083019 100644 --- a/internal/util/cmd_setup.go +++ b/internal/util/cmd_setup.go @@ -10,6 +10,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/clevyr/kubedb/internal/config" + "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/database" "github.com/clevyr/kubedb/internal/kubernetes" @@ -49,7 +50,7 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global, opts SetupOptions) (e conf.Context = conf.Client.Context conf.Namespace = conf.Client.Namespace - access := config.NewNamespaceRegexp(cmd.Annotations["access"]) + access := namespace_filter.NewFromContext(cmd.Context()) if !access.Match(conf.Client.Namespace) { return errors.New("The current action is disabled for namespace " + conf.Client.Namespace) }