diff --git a/README.md b/README.md index f584424c..e43aa293 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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. ``` diff --git a/README_CN.md b/README_CN.md index 8aa04f7c..f01b42f8 100644 --- a/README_CN.md +++ b/README_CN.md @@ -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: @@ -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. ``` diff --git a/cmd/kubefwd/kubefwd.go b/cmd/kubefwd/kubefwd.go index 5cc8fec2..842bed60 100644 --- a/cmd/kubefwd/kubefwd.go +++ b/cmd/kubefwd/kubefwd.go @@ -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, } diff --git a/cmd/kubefwd/services/services.go b/cmd/kubefwd/services/services.go index 8a775cf2..4fcba77b 100644 --- a/cmd/kubefwd/services/services.go +++ b/cmd/kubefwd/services/services.go @@ -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 @@ -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.") } @@ -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 { @@ -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 { @@ -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 {