Skip to content

Commit 650478a

Browse files
committed
docs: support forward service
1 parent f34ba78 commit 650478a

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

forwarder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func handleOptions(ctx context.Context, options []*Option, config *restclient.Co
193193
podName := option.Pod.ObjectMeta.Name
194194

195195
if podName != "" {
196-
namespace := option.Service.ObjectMeta.Namespace
196+
namespace := option.Pod.ObjectMeta.Namespace
197197
if namespace == "" {
198198
namespace = "default"
199199
}
@@ -202,7 +202,7 @@ func handleOptions(ctx context.Context, options []*Option, config *restclient.Co
202202
return err
203203
}
204204
if pod == nil {
205-
return fmt.Errorf("no such pod: %s", podName)
205+
return fmt.Errorf("no such pod: %v", podName)
206206
}
207207

208208
newOptions[index] = option
@@ -223,7 +223,7 @@ func handleOptions(ctx context.Context, options []*Option, config *restclient.Co
223223
return err
224224
}
225225
if svc == nil {
226-
return fmt.Errorf("no such service: %s", svcName)
226+
return fmt.Errorf("no such service: %v", svcName)
227227
}
228228
labels := []string{}
229229
for key, val := range svc.Spec.Selector {

readme.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ go get github.com/anthhub/forwarder
1313
## Usage
1414

1515
```go
16-
options := []*Option{
16+
17+
import (
18+
"github.com/anthhub/forwarder"
19+
)
20+
21+
options := []*forwarder.Option{
1722
{
1823
// the local port for forwarding
1924
LocalPort: 8080,
@@ -28,33 +33,37 @@ go get github.com/anthhub/forwarder
2833
},
2934
},
3035
{
31-
LocalPort: 8081,
36+
// if local port isn't provided, forwarder will generate a random port number
37+
// LocalPort: 8081,
3238
PodPort: 80,
33-
Pod: v1.Pod{
39+
// the k8s service metadata, it's to forward service
40+
Service: v1.Service{
3441
ObjectMeta: metav1.ObjectMeta{
35-
Name: "nginx-deployment-66b6c48dd5-n86z4",
36-
Namespace: "default",
42+
Name: "my-nginx-svc",
43+
// the namespace default is "default"
44+
// Namespace: "default",
3745
},
3846
},
3947
},
4048
}
4149

42-
// create a forwarder
43-
ret, err := WithForwarders(context.Background(), options, *kubecfg)
50+
// it's to create a forwarder, and you need provide a path of kubeconfig
51+
ret, err := forwarder.WithForwarders(context.Background(), options, "./kubecfg")
4452
if err != nil {
4553
panic(err)
4654
}
4755
// remember to close the forwarding
4856
defer ret.Close()
49-
// wait the ready of forwarding
50-
ret.Ready()
57+
// wait forwarding ready
58+
// the remote and local ports are listed
59+
ports, err := ret.Ready()
60+
if err != nil {
61+
panic(err)
62+
}
5163
// ...
5264

5365
// if you want to block the goroutine and listen IOStreams close signal, you can do as following:
54-
//
55-
// if err := ret.Wait(); err != nil {
56-
// panic(err)
57-
// }
66+
ret.Wait()
5867
```
5968

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

0 commit comments

Comments
 (0)