Skip to content

Commit

Permalink
Renamed config-ns to ns-list
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Apr 29, 2018
1 parent 4b13de1 commit b95d48b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 60 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,12 @@ wouldn't be shown among the users for any cluster other than "minikube" (when `k

#### Access Control

If a user is not allowed to list namespaces, you can either provide a list of namespaces known to that user with config-ns:
If a user is not allowed to list namespaces, you can either provide a list of namespaces known to that user with `ns-list`

```sh
# interactive
$ kubensx config-ns
# mark default, kube-system and kube-public namespaces as known in us-west1 cluster
$ kubensx config-ns us-west1/{default,kube-system,kube-public}
$ kubensx ns-list
# make default, kube-system and kube-public namespaces known to current user in us-west1 cluster
$ kubensx ns-list us-west1/{default,kube-system,kube-public}
$ kubensx use west/def
Switched to account@possibly-gmail.com:us-west1/default
```
Expand Down
78 changes: 39 additions & 39 deletions cli/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,6 @@ func (c *Completion) Execute() (bool, error) {
"zsh": complete.Command{},
},
},
"config-ns": complete.Command{
Flags: complete.Flags{
"--delete": complete.PredictNothing,
"-d": complete.PredictNothing,
"--delete-all": complete.PredictNothing,
"--dry-run": complete.PredictNothing,
"-x": complete.PredictNothing,
"--exact": complete.PredictNothing,
"-e": complete.PredictNothing,
"--fuzzy": complete.PredictNothing,
"-z": complete.PredictNothing,
"--list": complete.PredictNothing,
"-l": complete.PredictNothing,
},
// todo:
// Args: oneOf(c.ctx().Users()),
},
"current": complete.Command{
Flags: complete.Flags{
"--cluster": complete.PredictNothing,
Expand All @@ -115,25 +98,42 @@ func (c *Completion) Execute() (bool, error) {
"-n": complete.PredictNothing,
},
},
"ns-list": complete.Command{
Flags: complete.Flags{
"--delete": complete.PredictNothing,
"-d": complete.PredictNothing,
"--delete-all": complete.PredictNothing,
"--dry-run": complete.PredictNothing,
"-x": complete.PredictNothing,
"--exact": complete.PredictNothing,
"-e": complete.PredictNothing,
"--fuzzy": complete.PredictNothing,
"-z": complete.PredictNothing,
"--list": complete.PredictNothing,
"-l": complete.PredictNothing,
},
// todo:
// Args: oneOf(c.ctx().Users()),
},
"use": complete.Command{
Flags: complete.Flags{
"--cluster": complete.PredictNothing,
"-c": complete.PredictNothing,
"--dry-run": complete.PredictNothing,
"-x": complete.PredictNothing,
"--exact": complete.PredictNothing,
"-e": complete.PredictNothing,
"--force": complete.PredictNothing,
"-f": complete.PredictNothing,
"--fuzzy": complete.PredictNothing,
"-z": complete.PredictNothing,
"--ignore-assoc": complete.PredictNothing,
"--ignore-config-ns": complete.PredictNothing,
"--namespace": complete.PredictNothing,
"--ns": complete.PredictNothing,
"-n": complete.PredictNothing,
"--user": complete.PredictNothing,
"-u": complete.PredictNothing,
"--cluster": complete.PredictNothing,
"-c": complete.PredictNothing,
"--dry-run": complete.PredictNothing,
"-x": complete.PredictNothing,
"--exact": complete.PredictNothing,
"-e": complete.PredictNothing,
"--force": complete.PredictNothing,
"-f": complete.PredictNothing,
"--fuzzy": complete.PredictNothing,
"-z": complete.PredictNothing,
"--ignore-assoc": complete.PredictNothing,
"--ignore-ns-list": complete.PredictNothing,
"--namespace": complete.PredictNothing,
"--ns": complete.PredictNothing,
"-n": complete.PredictNothing,
"--user": complete.PredictNothing,
"-u": complete.PredictNothing,
},
// todo:
// Args: oneOf(c.ctx().Users()),
Expand All @@ -147,10 +147,10 @@ func (c *Completion) Execute() (bool, error) {
"zsh": complete.Command{},
},
},
"config-ns": complete.Command{},
"current": complete.Command{},
"ls": complete.Command{},
"use": complete.Command{},
"current": complete.Command{},
"ls": complete.Command{},
"ns-list": complete.Command{},
"use": complete.Command{},
},
},
},
Expand All @@ -166,9 +166,9 @@ func (c *Completion) Execute() (bool, error) {
},
}
run.Sub["a"] = run.Sub["assoc"]
run.Sub["n"] = run.Sub["config-ns"]
run.Sub["c"] = run.Sub["current"]
run.Sub["l"] = run.Sub["ls"]
run.Sub["n"] = run.Sub["ns-list"]
run.Sub["u"] = run.Sub["use"]
completion := complete.New(filepath.Base(bin), run)
if os.Getenv("COMP_LINE") != "" {
Expand Down
32 changes: 16 additions & 16 deletions kubensx.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ func main() {
assocCmd.Flags().BoolP("list", "l", false, "List assoc[iations] (<user>:<cluster>|s)")
rootCmd.AddCommand(assocCmd)
assocNsCmd := &cobra.Command{
Use: "config-ns [pattern...]",
Use: "ns-list [pattern...]",
Aliases: []string{"n"},
Short: "Control list of namespaces",
Long: "Control list of namespaces\n\n" +
"Use config-ns when:" +
"Use ns-list when:" +
"\n - You wish to able to select a namespace from a list of options (e.g. \"kubensx use\")\nbut Access Control configuration prohibits user from listing namespaces; " +
"\n - You wish to reduce number of namespaces available for selection.",
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -403,17 +403,17 @@ func main() {
return nil
},
Example: " # (interactive)\n" +
" kubensx config-ns\n" +
" kubensx ns-list\n" +
" # make staging namespace known for qa in us-west1 cluster\n" +
" kubensx config-ns qa:us-west1/staging\n" +
" kubensx ns-list qa:us-west1/staging\n" +
" \n" +
" # list assoc[iations]\n" +
" kubensx config-ns -l\n" +
" kubensx ns-list -l\n" +
" \n" +
" # list <user>:<cluster>/<namespace> triples that would be assoc[iated] should\n" +
" # `kubensx config-ns <user>:<cluster>/<namespace>` be executed\n" +
" kubensx config-ns --dry-run minikube/staging\n" +
" kubensx config-ns --dry-run '*:minikube/staging'",
" # `kubensx ns-list <user>:<cluster>/<namespace>` be executed\n" +
" kubensx ns-list --dry-run minikube/staging\n" +
" kubensx ns-list --dry-run '*:minikube/staging'",
}
assocNsCmd.Flags().BoolP("delete", "d", false, "Delete assoc[iation](s)")
assocNsCmd.Flags().Bool("delete-all", false, "Delete all assoc[iations]")
Expand Down Expand Up @@ -477,7 +477,7 @@ func main() {
return errors.New("--cluster(-c) cannot be omitted when both --user(-u) and --namespace(--ns,-n) are present")
}
switch {
case u == c == n:
case u == c && c == n:
fmt.Println(formatContext(ctx))
case u && c && !n:
fmt.Printf("%s:%s\n", ctx.User(), ctx.Cluster())
Expand Down Expand Up @@ -506,7 +506,7 @@ func main() {
u, _ := cmd.Flags().GetBool("users")
c, _ := cmd.Flags().GetBool("clusters")
n, _ := cmd.Flags().GetBool("namespaces")
ignoreExplicitNS, _ := cmd.Flags().GetBool("ignore-config-ns")
ignoreExplicitNS, _ := cmd.Flags().GetBool("ignore-ns-list")
if !u && !c && !n {
return pflag.ErrHelp
}
Expand All @@ -531,7 +531,7 @@ func main() {
lsCmd.Flags().BoolP("clusters", "c", false, "List clusters")
lsCmd.Flags().BoolP("namespaces", "n", false, "List namespaces")
lsCmd.Flags().BoolP("users", "u", false, "List users")
lsCmd.Flags().Bool("ignore-config-ns", false, "Ignore explicit user:cluster/namespace(s) (if any)")
lsCmd.Flags().Bool("ignore-ns-list", false, "Ignore explicit user:cluster/namespace(s) (if any)")
rootCmd.AddCommand(lsCmd)
useCmd := &cobra.Command{
Use: "use [user:cluster/namespace]",
Expand All @@ -553,7 +553,7 @@ func main() {
}
dryRun, _ := cmd.Flags().GetBool("dry-run")
ignoreAssoc, _ := cmd.Flags().GetBool("ignore-assoc")
ignoreExplicitNS, _ := cmd.Flags().GetBool("ignore-config-ns")
ignoreExplicitNS, _ := cmd.Flags().GetBool("ignore-ns-list")
force, _ := cmd.Flags().GetBool("force")
if len(args) == 0 {
mustContainAtLeastOneCluster(ctx)
Expand All @@ -573,7 +573,7 @@ func main() {
nss := requireNamespaces(ctx, !ignoreExplicitNS)
if len(nss) == 0 {
fmt.Println("\nIt appears that the user you have selected is not allowed to list namespaces.\n" +
"If you wish to avoid manual entry next time you `kubensx use` - see `kubensx config-ns --help`.\n")
"If you wish to avoid manual entry next time you `kubensx use` - see `kubensx ns-list --help`.\n")
ns := promptInput("namespace:", "", "")
if err := validateNS(ns); err != nil {
log.Fatalf(err.Error())
Expand Down Expand Up @@ -647,7 +647,7 @@ func main() {
if len(r) == 0 {
log.Fatalf("It appears that \"%s\" is not allowed to list namespaces in \"%s\" cluster.\n"+
"Either use --force(-f) (in which case namespace must be specified --exact|ly) or "+
"provide an explicit list of namespaces via `kubensx config-ns`.", ctx.User(), ctx.Cluster())
"provide an explicit list of namespaces via `kubensx ns-list`.", ctx.User(), ctx.Cluster())
}
return r
}
Expand Down Expand Up @@ -720,12 +720,12 @@ func main() {
useCmd.Flags().BoolP("exact", "e", false, "Match exactly (by default wildcard matching is used)")
useCmd.Flags().BoolP("fuzzy", "z", false, "Match fuzzily (by default wildcard matching is used)")
useCmd.Flags().Bool("ignore-assoc", false, "Ignore user:cluster assoc[iations] (if any)")
useCmd.Flags().Bool("ignore-config-ns", false, "Ignore explicit user:cluster/namespace(s) (if any)")
useCmd.Flags().Bool("ignore-ns-list", false, "Ignore explicit user:cluster/namespace(s) (if any)")
useCmd.Flags().BoolP("namespace", "n", false, "Change namespace only")
useCmd.Flags().Bool("ns", false, "Alias for --namespace")
useCmd.Flags().BoolP("user", "u", false, "Change user only")
useCmd.Flags().BoolP("force", "f", false, "Skip namespace validation (NOTE: namespace must be provided --exact|ly)"+
"\n(useful when user is not allowed to list namespaces; see also \"kubensx config-ns --help\")")
"\n(useful when user is not allowed to list namespaces; see also \"kubensx ns-list --help\")")
rootCmd.AddCommand(useCmd)
walk(rootCmd, func(cmd *cobra.Command) {
cmd.Flags().BoolP("help", "h", false, "Print usage")
Expand Down

0 comments on commit b95d48b

Please sign in to comment.