Skip to content

Commit

Permalink
chore: Improve namespace filter
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 25, 2023
1 parent 5f533c5 commit 2a042b6
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 27 deletions.
9 changes: 3 additions & 6 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/port_forward/port_forward.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -55,6 +57,7 @@ func New() *cobra.Command {
panic(err)
}

cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadWrite))
return cmd
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/restore/restore.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/status/status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package status

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -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"
)

Expand All @@ -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
}

Expand All @@ -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"))
Expand Down
4 changes: 2 additions & 2 deletions internal/config/flags/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package config
package namespace_filter

import (
"context"
"regexp"
"strconv"

"github.com/clevyr/kubedb/internal/consts"
"github.com/spf13/viper"
Expand All @@ -16,7 +16,7 @@ var (
type AccessLevel uint8

const (
ReadWrite = iota
ReadWrite AccessLevel = iota
ReadOnly
)

Expand All @@ -31,16 +31,32 @@ 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() {
viper.SetDefault(consts.NamespaceFilterKey, true)
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
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package config
package namespace_filter

import (
"context"
"regexp"
"strconv"
"testing"

"github.com/clevyr/kubedb/internal/consts"
Expand Down Expand Up @@ -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))
})
}
}
3 changes: 2 additions & 1 deletion internal/util/cmd_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 2a042b6

Please sign in to comment.