Skip to content

Commit

Permalink
docs: support forward service
Browse files Browse the repository at this point in the history
  • Loading branch information
anthhub committed Jul 28, 2021
1 parent f34ba78 commit 650478a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
6 changes: 3 additions & 3 deletions forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func handleOptions(ctx context.Context, options []*Option, config *restclient.Co
podName := option.Pod.ObjectMeta.Name

if podName != "" {
namespace := option.Service.ObjectMeta.Namespace
namespace := option.Pod.ObjectMeta.Namespace
if namespace == "" {
namespace = "default"
}
Expand All @@ -202,7 +202,7 @@ func handleOptions(ctx context.Context, options []*Option, config *restclient.Co
return err
}
if pod == nil {
return fmt.Errorf("no such pod: %s", podName)
return fmt.Errorf("no such pod: %v", podName)
}

newOptions[index] = option
Expand All @@ -223,7 +223,7 @@ func handleOptions(ctx context.Context, options []*Option, config *restclient.Co
return err
}
if svc == nil {
return fmt.Errorf("no such service: %s", svcName)
return fmt.Errorf("no such service: %v", svcName)
}
labels := []string{}
for key, val := range svc.Spec.Selector {
Expand Down
35 changes: 22 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ go get github.com/anthhub/forwarder
## Usage

```go
options := []*Option{

import (
"github.com/anthhub/forwarder"
)

options := []*forwarder.Option{
{
// the local port for forwarding
LocalPort: 8080,
Expand All @@ -28,33 +33,37 @@ go get github.com/anthhub/forwarder
},
},
{
LocalPort: 8081,
// if local port isn't provided, forwarder will generate a random port number
// LocalPort: 8081,
PodPort: 80,
Pod: v1.Pod{
// the k8s service metadata, it's to forward service
Service: v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx-deployment-66b6c48dd5-n86z4",
Namespace: "default",
Name: "my-nginx-svc",
// the namespace default is "default"
// Namespace: "default",
},
},
},
}

// create a forwarder
ret, err := WithForwarders(context.Background(), options, *kubecfg)
// it's to create a forwarder, and you need provide a path of kubeconfig
ret, err := forwarder.WithForwarders(context.Background(), options, "./kubecfg")
if err != nil {
panic(err)
}
// remember to close the forwarding
defer ret.Close()
// wait the ready of forwarding
ret.Ready()
// wait forwarding ready
// the remote and local ports are listed
ports, err := ret.Ready()
if err != nil {
panic(err)
}
// ...

// if you want to block the goroutine and listen IOStreams close signal, you can do as following:
//
// if err := ret.Wait(); err != nil {
// panic(err)
// }
ret.Wait()
```

> If you want to learn more about `forwarder`, you can read test cases and source code.

0 comments on commit 650478a

Please sign in to comment.