Skip to content

Commit

Permalink
Merge remote-tracking branch 'txn2/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	README_CN.md
#	cmd/kubefwd/kubefwd.go
#	cmd/kubefwd/services/services.go
#	pkg/fwdservice/fwdservice.go
  • Loading branch information
ndj888 committed Sep 12, 2021
2 parents 6dae886 + 848a248 commit 8acd76c
Show file tree
Hide file tree
Showing 10 changed files with 643 additions and 206 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,4 @@ brews:
- name: kubectl
type: optional
test: |-
kubefwd version
kubefwd version
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.13.x
- 1.16.x

# Only clone the most recent commit.
git:
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ Forward all svc for the namespace `the-project` where labeled `system: wx`:
sudo kubefwd svc -l system=wx -n the-project
```

Forward a single service named `my-service` in the namespace `the-project`:

```
sudo kubefwd svc -n the-project -f metadata.name=my-service
```

Forward more than one service using the `in` clause:
```bash
sudo kubefwd svc -l "app in (app1, app2)"
Expand Down Expand Up @@ -128,11 +134,11 @@ Aliases:
Examples:
kubefwd svc -n the-project
kubefwd svc -n the-project -l app=wx,component=api
kubefwd svc -n the-project -f metadata.name=service-name
kubefwd svc -n default -n the-project
kubefwd svc -n default -d internal.example.com
kubefwd svc -n the-project -x prod-cluster
kubefwd svc -n the-project -p "8080:80,3309:3306"
kubefwd svc -n the-project -p "8080:80"
kubefwd svc -n the-project -m 80:8080 -m 443:1443
Flags:
Expand All @@ -143,6 +149,7 @@ Flags:
-c, --kubeconfig string absolute path to a kubectl config file
-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.
-v, --verbose Verbose output.
```
Expand Down Expand Up @@ -171,12 +178,6 @@ GITHUB_TOKEN=$GITHUB_TOKEN goreleaser --rm-dist
Apache License 2.0
## Similar projects, products, alternatives and complementary utilities
- [inlets](https://inlets.dev) - inlets provides an L7 HTTP tunnel for applications through the use of an exit node, it is used by the inlets operator
- [inlets-operator](https://github.com/inlets/inlets-operator) - L4 TCP tunnel, which can tunnel any TCP traffic and is on the roadmap for the inlets-operator
- [telepresence.io](https://www.telepresence.io/) - Telepresence substitutes a two-way network proxy for your normal pod running in the Kubernetes cluster.
# Sponsor
Open source utility by [Craig Johnston](https://twitter.com/cjimti), [imti blog](http://imti.co/) and sponsored by [Deasil Works, Inc.]
Expand Down
6 changes: 3 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ Examples:
kubefwd svc -n default -n the-project
kubefwd svc -n default -d internal.example.com
kubefwd svc -n the-project -x prod-cluster
kubefwd svc -n the-project -p "8080:80,3309:3306"
kubefwd svc -n the-project -p "8080:80"
kubefwd svc -n the-project -m 80:8080 -m 443:1443
Flags:
-x, --context strings specify a context to override the current context
Expand All @@ -140,6 +139,7 @@ Flags:
-c, --kubeconfig string absolute path to a kubectl config fil (default "/Users/cjimti/.kube/config")
-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.
-v, --verbose Verbose output.
```
Expand Down
5 changes: 3 additions & 2 deletions cmd/kubefwd/kubefwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ func newRootCmd() *cobra.Command {
Example: " kubefwd services --help\n" +
" kubefwd svc -n the-project\n" +
" kubefwd svc -n the-project -l env=dev,component=api\n" +
" 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 -p \"8080:80,3309:3306\"\n" +
" kubefwd svc -n the-project -p \"8080:80\"\n",
" kubefwd svc -n the-project -m 80:8080 -m 443:1443\n",

Long: globalUsage,
}

Expand Down
37 changes: 19 additions & 18 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package services

import (
"context"
"fmt"
"os"
"os/signal"
Expand All @@ -38,7 +39,6 @@ import (
authorizationv1 "k8s.io/api/authorization/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilRuntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -54,7 +54,7 @@ var namespaces []string
var contexts []string
var verbose bool
var domain string
var port string
var mappings []string

func init() {
// override error output from k8s.io/apimachinery/pkg/util/runtime
Expand All @@ -67,10 +67,10 @@ func init() {
Cmd.Flags().StringSliceVarP(&contexts, "context", "x", []string{}, "specify a context to override the current context")
Cmd.Flags().StringSliceVarP(&namespaces, "namespace", "n", []string{}, "Specify a namespace. Specify multiple namespaces by duplicating this argument.")
Cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on; supports '=', '==', and '!=' (e.g. -l key1=value1,key2=value2).")
Cmd.Flags().StringP("field-selector", "f", "", "Field selector to filter on; supports '=', '==', and '!=' (e.g. -f metadata.name=service-name).")
Cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output.")
Cmd.Flags().StringVarP(&domain, "domain", "d", "", "Append a pseudo domain name to generated host names.")
// "The Issues https://github.com/txn2/kubefwd/issues/121"
Cmd.Flags().StringVarP(&port, "port", "p", "", "Map the ports you need.(e.g. host port:container port -p 8080:80")
Cmd.Flags().StringSliceVarP(&mappings, "mapping", "m", []string{}, "Specify a port mapping. Specify multiple mapping by duplicating this argument.")

}

Expand All @@ -84,7 +84,8 @@ var Cmd = &cobra.Command{
" kubefwd svc -n default -l \"app in (ws, api)\"\n" +
" 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 -x prod-cluster\n" +
" kubefwd svc -n the-project -m 80:8080 -m 443:1443\n",
Run: runCmd,
}

Expand All @@ -110,7 +111,7 @@ func checkConnection(clientSet *kubernetes.Clientset, namespaces []string) error
ResourceAttributes: &perm,
},
}
accessReview, err = clientSet.AuthorizationV1().SelfSubjectAccessReviews().Create(accessReview)
accessReview, err = clientSet.AuthorizationV1().SelfSubjectAccessReviews().Create(context.TODO(), accessReview, metav1.CreateOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -196,11 +197,9 @@ Try:

// labels selector to filter services
// see: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
selector := cmd.Flag("selector").Value.String()
listOptions := metav1.ListOptions{}
if selector != "" {
listOptions.LabelSelector = selector
}
listOptions.LabelSelector = cmd.Flag("selector").Value.String()
listOptions.FieldSelector = cmd.Flag("field-selector").Value.String()

// if no namespaces were specified via the flags, check config from the k8s context
// then explicitly set one to "default"
Expand Down Expand Up @@ -292,6 +291,7 @@ Try:
NamespaceN: ii,
Domain: domain,
ManualStopChannel: stopListenCh,
PortMapping: mappings,
}

go func(npo NamespaceOpts) {
Expand Down Expand Up @@ -340,6 +340,8 @@ type NamespaceOpts struct {

// Domain is specified by the user and used in place of .local
Domain string
// meaning any source port maps to target port.
PortMapping []string

ManualStopChannel chan struct{}
}
Expand All @@ -348,7 +350,7 @@ type NamespaceOpts struct {
func (opts *NamespaceOpts) watchServiceEvents(stopListenCh <-chan struct{}) {
// Apply filtering
optionsModifier := func(options *metav1.ListOptions) {
options.FieldSelector = fields.Everything().String()
options.FieldSelector = opts.ListOptions.FieldSelector
options.LabelSelector = opts.ListOptions.LabelSelector
}

Expand All @@ -359,12 +361,12 @@ func (opts *NamespaceOpts) watchServiceEvents(stopListenCh <-chan struct{}) {
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
optionsModifier(&options)
return opts.ClientSet.CoreV1().Services(opts.Namespace).List(options)
return opts.ClientSet.CoreV1().Services(opts.Namespace).List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.Watch = true
optionsModifier(&options)
return opts.ClientSet.CoreV1().Services(opts.Namespace).Watch(options)
return opts.ClientSet.CoreV1().Services(opts.Namespace).Watch(context.TODO(), options)
},
},
&v1.Service{},
Expand Down Expand Up @@ -414,7 +416,7 @@ func (opts *NamespaceOpts) AddServiceHandler(obj interface{}) {
PortForwards: make(map[string]*fwdport.PortForwardOpts),
SyncDebouncer: debounce.New(5 * time.Second),
DoneChannel: make(chan struct{}),
PortMap: opts.ParsePortMap(port),
PortMap: opts.ParsePortMap(mappings),
}

// Add the service to the catalog of services being forwarded
Expand Down Expand Up @@ -442,13 +444,12 @@ func (opts *NamespaceOpts) UpdateServiceHandler(_ interface{}, new interface{})
}

// parse string port to PortMap
func (opts *NamespaceOpts) ParsePortMap(port string) *[]fwdservice.PortMap {
func (opts *NamespaceOpts) ParsePortMap(mappings []string) *[]fwdservice.PortMap {
var portList []fwdservice.PortMap
if port == "" {
if mappings == nil {
return nil
}
strArr := strings.Split(port, ",")
for _, s := range strArr {
for _, s := range mappings {
portInfo := strings.Split(s, ":")
portList = append(portList, fwdservice.PortMap{SourcePort: portInfo[0], TargetPort: portInfo[1]})
}
Expand Down
23 changes: 12 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module github.com/txn2/kubefwd

go 1.13
go 1.16

require (
github.com/bep/debounce v1.2.0
github.com/elazarl/goproxy v0.0.0-20181111060418-2ce16c963a8a // indirect
github.com/pkg/errors v0.8.1
github.com/securego/gosec v0.0.0-20200401082031-e946c8c39989 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.1.1
github.com/txn2/txeh v1.2.1
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e
k8s.io/api v0.0.0-20191108065827-59e77acf588f
k8s.io/apimachinery v0.0.0-20191108065633-c18f71bf2947
k8s.io/cli-runtime v0.0.0-20191108072024-9fe36560f3af
k8s.io/client-go v0.0.0-20191108070106-f8f007fd456c
k8s.io/kubectl v0.0.0-20191108073526-feacf2b3d48e
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd
k8s.io/api v0.20.4
k8s.io/apimachinery v0.20.4
k8s.io/cli-runtime v0.20.4
k8s.io/client-go v0.20.4
k8s.io/kubectl v0.20.4
)
Loading

0 comments on commit 8acd76c

Please sign in to comment.