Skip to content

Commit

Permalink
Merge pull request #205 from ndj888/feature/add-all-ns
Browse files Browse the repository at this point in the history
Feature/add all ns
  • Loading branch information
cjimti authored Sep 15, 2021
2 parents 848a248 + 38fa53a commit 6e7bc29
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Examples:
kubefwd svc -n default -d internal.example.com
kubefwd svc -n the-project -x prod-cluster
kubefwd svc -n the-project -m 80:8080 -m 443:1443
kubefwd svc -n the-project --all-namespaces
Flags:
Expand All @@ -150,6 +151,7 @@ Flags:
-n, --namespace strings Specify a namespace. Specify multiple namespaces by duplicating this argument.
-l, --selector string Selector (label query) to filter on; supports '=', '==', '!=' (e.g. -l key1=value1,key2=value2) and 'in' (e.g. -l "app in (value1, value2)").
-m, --mapping strings Specify a port mapping. Specify multiple mapping by duplicating this argument.
--all-namespaces Enable --all-namespaces or -A option like kubectl.
-v, --verbose Verbose output.
```
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Examples:
kubefwd svc -n default -d internal.example.com
kubefwd svc -n the-project -x prod-cluster
kubefwd svc -n the-project -m 80:8080 -m 443:1443
kubefwd svc -n the-project --all-namespaces
Flags:
Expand All @@ -140,6 +141,7 @@ Flags:
-n, --namespace strings Specify a namespace. Specify multiple namespaces by duplicating this argument.
-l, --selector string Selector (label query) to filter on; supports '=', '==', and '!=' (e.g. -l key1=value1,key2=value2).
-m, --mapping strings Specify a port mapping. Specify multiple mapping by duplicating this argument.
--all-namespaces Enable --all-namespaces or -A option like kubectl.
-v, --verbose Verbose output.
```
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubefwd/kubefwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func newRootCmd() *cobra.Command {
" kubefwd svc -n the-project -f metadata.name=service-name\n" +
" kubefwd svc -n default -l \"app in (ws, api)\"\n" +
" kubefwd svc -n default -n the-project\n" +
" kubefwd svc -n the-project -m 80:8080 -m 443:1443\n",
" kubefwd svc -n the-project -m 80:8080 -m 443:1443\n" +
" kubefwd svc --all-namespaces",

Long: globalUsage,
}
Expand Down
26 changes: 24 additions & 2 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var contexts []string
var verbose bool
var domain string
var mappings []string
var isAllNs bool

func init() {
// override error output from k8s.io/apimachinery/pkg/util/runtime
Expand All @@ -71,6 +72,7 @@ func init() {
Cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output.")
Cmd.Flags().StringVarP(&domain, "domain", "d", "", "Append a pseudo domain name to generated host names.")
Cmd.Flags().StringSliceVarP(&mappings, "mapping", "m", []string{}, "Specify a port mapping. Specify multiple mapping by duplicating this argument.")
Cmd.Flags().BoolVarP(&isAllNs, "all-namespaces", "A", false, "Enable --all-namespaces option like kubectl.")

}

Expand All @@ -85,10 +87,22 @@ var Cmd = &cobra.Command{
" kubefwd svc -n default -n the-project\n" +
" kubefwd svc -n default -d internal.example.com\n" +
" kubefwd svc -n the-project -x prod-cluster\n" +
" kubefwd svc -n the-project -m 80:8080 -m 443:1443\n",
" kubefwd svc -n the-project -m 80:8080 -m 443:1443\n" +
" kubefwd svc --all-namespaces",
Run: runCmd,
}

// setAllNamespace Form V1Core get all namespace
func setAllNamespace(clientSet *kubernetes.Clientset, options metav1.ListOptions, namespaces *[]string) {
nsList, err := clientSet.CoreV1().Namespaces().List(context.TODO(), options)
if err != nil {
log.Fatalf("Error get all namespaces by CoreV1: %s\n", err.Error())
}
for _, ns := range nsList.Items {
*namespaces = append(*namespaces, ns.Name)
}
}

// checkConnection tests if you can connect to the cluster in your config,
// and if you have the necessary permissions to use kubefwd.
func checkConnection(clientSet *kubernetes.Clientset, namespaces []string) error {
Expand Down Expand Up @@ -258,6 +272,14 @@ Try:
log.Fatalf("Error creating k8s clientSet: %s\n", err.Error())
}

// if use --all-namespace ,from v1 api get all ns.
if isAllNs {
if len(namespaces) > 1 {
log.Fatalf("Error: cannot combine options --all-namespaces and -n.")
}
setAllNamespace(clientSet, listOptions, &namespaces)
}

// check connectivity
err = checkConnection(clientSet, namespaces)
if err != nil {
Expand Down Expand Up @@ -443,7 +465,7 @@ func (opts *NamespaceOpts) UpdateServiceHandler(_ interface{}, new interface{})
}
}

// parse string port to PortMap
// ParsePortMap parse string port to PortMap
func (opts *NamespaceOpts) ParsePortMap(mappings []string) *[]fwdservice.PortMap {
var portList []fwdservice.PortMap
if mappings == nil {
Expand Down

0 comments on commit 6e7bc29

Please sign in to comment.