Skip to content

Commit

Permalink
Add namespace pattern support
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Tymchyshyn committed Jun 14, 2020
1 parent 0f3310e commit 6ce7c61
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 388 deletions.
121 changes: 73 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,91 @@
# Kubectl Aggregate

![](https://github.com/daftping/kubeagg/workflows/Go/badge.svg) ![Release](https://github.com/daftping/kubeagg/workflows/Release/badge.svg)

Kubectl wrapper to run against multiple contexts.
Kubectl wrapper to run against multiple contexts and namespaces.

kubeagg features which are not available in kubectl:
- provide multiple context (literally or as pattern)

- get objects from multiple context (as list or as pattern)
- get objects from multiple namespaces (as list or as pattern)

## Usage

# Usage
```bash
Kubectl wrapper to run against multiple contexts
Kubectl wrapper can get any objects in any cluster in any namespace.

You can provide list of contexts or context-pattern (regexp)
If object you are trying to get namespaced you can provide list of
namespaces or namespace-pattern (regexp)

Usage:
kubeagg get [flags]
kubeagg [command]

Examples:
// Get namespaces from all contexts in (kubectl config view)
kubeagg get ns

// Get pods from contexts matched 'dev$|test$' regexp and in
// namespaces matched 'default|test|dev' regexp
kubeagg \
--context-pattern='dev$|test$' \
--namespace-pattern='default|test|dev' \
get pod

// Get all nodes in "docker-desktop" and "test" contexts
kubeagg --contexts=docker-desktop,test get no

Available Commands:
get Kubectl wrapper to run against multiple contexts and namespaces
help Help about any command

Flags:
-p, --context-pattern string Send request to contexts matched provided regexp. Ignored if --contexts is provided. (default ".*")
-c, --contexts strings Send request to provided contexts. Has precedence over --context-pattern.(default: '', --context-pattern is used)
-h, --help help for get
-n, --namespace string Namespace to operate in. (default "default")
-o, --output string Output format. Supported values: table, wide, json. (default "table")

Global Flags:
-l, --loglevel string Debug, Info, Warn, Error, Fatal (default "Error")
--context-pattern string Send request to contexts matched provided regexp.
Ignored if --contexts is provided. (default ".*")
--contexts strings Send request to provided contexts. Has precedence over --context-pattern.
(default: '', --context-pattern is used)
-h, --help help for kubeagg
-l, --loglevel string Debug, Info, Warn, Error, Fatal (default "Error")
--namespace-pattern string Get objects from namespaces matched provided regexp.
Ignored if --namespaces is provided. (default ".*")
-n, --namespaces strings List namespaces to get objects from.(default: '', --namespace-pattern is used)
--no-headers Skip headers in output
-o, --output string Output format. Supported values: table, json. (default "table")

Use "kubeagg [command] --help" for more information about a command.
```
## All contexts in ~/.kube/config
## Get namespaces in all contexts in ~/.kube/config
```bash
kubeagg get ns
CONTEXT NAME
prod-a default
prod-a kube-public
prod-a kube-system
test-a default
test-a kube-public
test-a kube-system
test-b default
test-b kube-public
test-b kube-system
test-c default
test-c kube-public
test-c kube-system
```
## List of selected namespaces
```bash
kubeagg get ns --contexts=test-a,test-b -o wide
CONTEXT TYPE NAME
test-a ns default
test-a ns kube-public
test-a ns kube-system
test-b ns default
test-b ns kube-public
test-b ns kube-system
CONTEXT NAMESPACE TYPE NAME
docker-desktop n\a Namespace default
docker-desktop n\a Namespace docker
docker-desktop n\a Namespace kube-node-lease
docker-desktop n\a Namespace kube-public
docker-desktop n\a Namespace kube-system
prod-a n\a Namespace default
prod-a n\a Namespace kube-public
prod-a n\a Namespace kube-system
test-a n\a Namespace default
test-a n\a Namespace kube-public
test-a n\a Namespace kube-system
test-b n\a Namespace default
test-b n\a Namespace kube-public
test-b n\a Namespace kube-system
```
## Regexp matched deployments
```bash
kubeagg get deploy --context-pattern='test-[b|c]$' --namespace=kube-system -o wide
CONTEXT TYPE NAME
test-b deploy cluster-autoscaler
test-b deploy kube-dns
test-b deploy metrics-server
test-c deploy cluster-autoscaler
test-c deploy kube-dns
test-c deploy metrics-server
## Get pod from contexts matched pattern and namespaces matched pattern
```bash
kubeagg \
--context-pattern='^docker-desktop$|^test' \
--namespace-pattern='default|-public$' \
get pod
CONTEXT NAMESPACE TYPE NAME
docker-desktop default Pod kubeagg
docker-desktop default Pod kubeagg-68898df575-hcvs5
test-a default Pod kubeagg2
test-a kube-public Pod kubeagg3
```
17 changes: 0 additions & 17 deletions TODO.md

This file was deleted.

42 changes: 7 additions & 35 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ var getConfig kubeagg.Config
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get",
Short: "Kubectl wrapper to run against multiple contexts",
// Long: `A longer description that spans multiple lines and likely contains examples
// and usage of using your command. For example:

// Cobra is a CLI library for Go that empowers applications.
// This application is a tool to generate the needed files
// to quickly create a Cobra application.`,
Short: "Kubectl wrapper to run against multiple contexts and namespaces",
Run: func(cmd *cobra.Command, args []string) {
// TODO Check for null
getConfig.ObjectType = args[0]
if len(args) > 0 {
getConfig.ObjectType = args[0]
} else {
getConfig.ObjectType = "all"
}

kubeagg.SetGlobalConfig(GlobalConfig)
kubeagg.SetConfig(getConfig)
kubeagg.Run()
Expand All @@ -34,30 +32,4 @@ var getCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(getCmd)

getCmd.PersistentFlags().StringVarP(
&getConfig.Output,
"output", "o",
"table",
"Output format. Supported values: table, wide, json.",
)
getCmd.PersistentFlags().StringVarP(
&getConfig.Namespace,
"namespace", "n",
"default",
"Namespace to operate in.",
)
getCmd.PersistentFlags().StringSliceVarP(
&getConfig.Contexts,
"contexts", "c",
[]string{},
"Send request to provided contexts. Has precedence over --context-pattern."+
"(default: '', --context-pattern is used)",
)
getCmd.PersistentFlags().StringVarP(
&getConfig.ContextPattern,
"context-pattern", "p",
".*",
"Send request to contexts matched provided regexp. Ignored if --contexts is provided.",
)
}
69 changes: 63 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ var GlobalConfig kubeagg.GlobalConfig
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "kubeagg",
Short: "Kubectl wrapper to run against multiple contexts",
// Long: `A longer description that spans multiple lines and likely contains
// examples and usage of using your application. For example:
Short: "Kubectl wrapper to run against multiple contexts and namespaces",
Long: `Kubectl wrapper can get any objects in any cluster in any namespace.
You can provide list of contexts or context-pattern (regexp)
If object you are trying to get namespaced you can provide list of
namespaces or namespace-pattern (regexp)
`,
Example: `
// Get namespaces from all contexts in (kubectl config view)
kubeagg get ns
// Cobra is a CLI library for Go that empowers applications.
// This application is a tool to generate the needed files
// to quickly create a Cobra application.`,
// Get pods from contexts matched 'dev$|test$' regexp and in
// namespaces matched 'default|test|dev' regexp
kubeagg \
--context-pattern='dev$|test$' \
--namespace-pattern='default|test|dev' \
get pod
// Get all nodes in "docker-desktop" and "test" contexts
kubeagg --contexts=docker-desktop,test get no
`,

// Uncomment the following line if your bare application
// has an action associated with it:
Expand Down Expand Up @@ -51,6 +65,49 @@ func init() {
"Debug, Info, Warn, Error, Fatal",
)

rootCmd.PersistentFlags().StringVarP(
&GlobalConfig.Output,
"output", "o",
"table",
"Output format. Supported values: table, wide, json.",
)

rootCmd.PersistentFlags().StringSliceVar(
&GlobalConfig.Contexts,
"contexts",
[]string{},
"Send request to provided contexts. Has precedence over --context-pattern."+
"(default: '', --context-pattern is used)",
)
rootCmd.PersistentFlags().StringVar(
&GlobalConfig.ContextPattern,
"context-pattern",
".*",
"Send request to contexts matched provided regexp. Ignored if --contexts is provided.",
)

rootCmd.PersistentFlags().BoolVar(
&GlobalConfig.NoHeaders,
"no-headers",
false,
"Skip headers in output",
)

rootCmd.PersistentFlags().StringSliceVarP(
&GlobalConfig.Namespaces,
"namespaces", "n",
[]string{},
"List namespaces to get objects from."+
"(default: '', --namespace-pattern is used)",
)

rootCmd.PersistentFlags().StringVar(
&GlobalConfig.NamespacePattern,
"namespace-pattern",
".*",
"Get objects from namespaces matched provided regexp. Ignored if --namespaces is provided.",
)

}

// initConfig reads in config file and ENV variables if set.
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module github.com/daftping/kubeagg
go 1.14

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.0
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/zap v1.15.0
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
)
Loading

0 comments on commit 6ce7c61

Please sign in to comment.