Skip to content

Commit 328aa99

Browse files
authored
Merge pull request #10 from anfernee/master
Cleanup and unit test
2 parents 5791bad + 890e179 commit 328aa99

File tree

14 files changed

+144
-479
lines changed

14 files changed

+144
-479
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin

cmd/agent/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ import (
2020
"crypto/tls"
2121
"crypto/x509"
2222
"fmt"
23-
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/agentclient"
23+
"io/ioutil"
24+
"net/http"
25+
"os"
26+
2427
"github.com/golang/glog"
2528
"github.com/prometheus/client_golang/prometheus"
2629
"github.com/spf13/cobra"
2730
"github.com/spf13/pflag"
2831
"google.golang.org/grpc"
2932
"google.golang.org/grpc/credentials"
30-
"io/ioutil"
31-
"net/http"
32-
"os"
33+
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/agentclient"
3334
)
3435

3536
func main() {

cmd/client/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ import (
2020
"crypto/tls"
2121
"crypto/x509"
2222
"fmt"
23-
"github.com/spf13/pflag"
24-
"google.golang.org/grpc"
25-
"google.golang.org/grpc/credentials"
2623
"io"
2724
"io/ioutil"
2825
"os"
2926

30-
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/client"
3127
"github.com/golang/glog"
3228
"github.com/spf13/cobra"
29+
"github.com/spf13/pflag"
30+
"google.golang.org/grpc"
31+
"google.golang.org/grpc/credentials"
32+
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/client"
3333
)
3434

3535
func main() {

cmd/proxy/main.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"crypto/tls"
21+
"crypto/x509"
2022
"fmt"
21-
"github.com/prometheus/client_golang/prometheus"
22-
"google.golang.org/grpc/credentials"
23+
"io/ioutil"
2324
"net"
2425
"net/http"
2526
"os"
2627

27-
"google.golang.org/grpc"
28-
29-
"crypto/tls"
30-
"crypto/x509"
31-
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/agentserver"
32-
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
3328
"github.com/golang/glog"
29+
"github.com/prometheus/client_golang/prometheus"
3430
"github.com/spf13/cobra"
3531
"github.com/spf13/pflag"
36-
"io/ioutil"
32+
"google.golang.org/grpc"
33+
"google.golang.org/grpc/credentials"
34+
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/agentserver"
35+
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
3736
)
3837

3938
func main() {

examples/legacy/client/main.go

Lines changed: 0 additions & 89 deletions
This file was deleted.

examples/legacy/server/main.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

pkg/agent/agentclient/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
"sync"
2424
"sync/atomic"
2525

26-
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
2726
"github.com/golang/glog"
2827
"google.golang.org/grpc"
28+
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
2929
)
3030

3131
type AgentClient struct {

pkg/agent/agentclient/client_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package agentclient
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"strings"
8+
"testing"
9+
10+
"google.golang.org/grpc"
11+
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
12+
)
13+
14+
func TestServeData_HTTP(t *testing.T) {
15+
client := NewAgentClient("")
16+
client.stream = newStream()
17+
stopCh := make(chan struct{})
18+
19+
// Start agnet
20+
go client.Serve(stopCh)
21+
defer close(stopCh)
22+
23+
// Start test http server as remote service
24+
expectedBody := "Hello, client"
25+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26+
fmt.Fprint(w, expectedBody)
27+
}))
28+
defer ts.Close()
29+
30+
// Stimulate sending KAS DIAL_REQ to AgentClient
31+
dialPacket := &agent.Packet{
32+
Type: agent.PacketType_DIAL_REQ,
33+
Payload: &agent.Packet_DialRequest{
34+
DialRequest: &agent.DialRequest{
35+
Protocol: "tcp",
36+
Address: ts.URL[len("http://"):],
37+
Random: 111,
38+
},
39+
},
40+
}
41+
client.stream.Send(dialPacket)
42+
43+
// Expect receiving DIAL_RSP packet from AgentClient
44+
pkg, _ := client.stream.Recv()
45+
if pkg == nil {
46+
t.Error("unexpected nil packet")
47+
}
48+
if pkg.Type != agent.PacketType_DIAL_RSP {
49+
t.Errorf("expect PacketType_DIAL_RSP; got %v", pkg.Type)
50+
}
51+
dialRsp := pkg.Payload.(*agent.Packet_DialResponse)
52+
connID := dialRsp.DialResponse.ConnectID
53+
if dialRsp.DialResponse.Random != 111 {
54+
t.Errorf("expect random=111; got %v", dialRsp.DialResponse.Random)
55+
}
56+
57+
// Send Data (HTTP Request) via AgentClient to the test http server
58+
dataPacket := &agent.Packet{
59+
Type: agent.PacketType_DATA,
60+
Payload: &agent.Packet_Data{
61+
Data: &agent.Data{
62+
ConnectID: connID,
63+
Data: []byte("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"), // HTTP request
64+
},
65+
},
66+
}
67+
client.stream.Send(dataPacket)
68+
69+
// Expect receiving http response via AgentClient
70+
pkg, _ = client.stream.Recv()
71+
if pkg == nil {
72+
t.Error("unexpected nil packet")
73+
}
74+
if pkg.Type != agent.PacketType_DATA {
75+
t.Errorf("expect PacketType_DIAL_RSP; got %v", pkg.Type)
76+
}
77+
data := pkg.Payload.(*agent.Packet_Data).Data.Data
78+
79+
// Verify response data
80+
//
81+
// HTTP/1.1 200 OK\r\n
82+
// Date: Tue, 07 May 2019 06:44:57 GMT\r\n
83+
// Content-Length: 14\r\n
84+
// Content-Type: text/plain; charset=utf-8\r\n
85+
// \r\n
86+
// Hello, client
87+
headAndBody := strings.Split(string(data), "\r\n")
88+
if body := headAndBody[len(headAndBody)-1]; body != expectedBody {
89+
t.Errorf("expect body %v; got %v", expectedBody, body)
90+
}
91+
}
92+
93+
// fakeStream implements AgentService_ConnectClient
94+
type fakeStream struct {
95+
grpc.ClientStream
96+
ch chan *agent.Packet
97+
}
98+
99+
func newStream() agent.AgentService_ConnectClient {
100+
s := &fakeStream{}
101+
s.ch = make(chan *agent.Packet)
102+
return s
103+
}
104+
105+
func (s *fakeStream) Send(packet *agent.Packet) error {
106+
s.ch <- packet
107+
return nil
108+
}
109+
110+
func (s *fakeStream) Recv() (*agent.Packet, error) {
111+
return <-s.ch, nil
112+
}

pkg/agent/agentserver/server.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ limitations under the License.
1717
package agentserver
1818

1919
import (
20+
"fmt"
2021
"io"
22+
"net"
2123

22-
"fmt"
23-
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
2424
"github.com/golang/glog"
25-
"net"
25+
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
2626
)
2727

2828
// ProxyClientConnection...
2929
type ProxyClientConnection struct {
30-
Mode string
31-
Grpc agent.ProxyService_ProxyServer
32-
// Http http.ResponseWriter
30+
Mode string
31+
Grpc agent.ProxyService_ProxyServer
3332
Http net.Conn
3433
connected chan struct{}
3534
connectID int64
@@ -74,14 +73,14 @@ func NewProxyServer() *ProxyServer {
7473
}
7574
}
7675

77-
// Agent ...
76+
// Proxy handles incoming streams from gRPC frontend.
7877
func (s *ProxyServer) Proxy(stream agent.ProxyService_ProxyServer) error {
7978
glog.Info("proxy request from client")
8079

8180
recvCh := make(chan *agent.Packet, 10)
8281
stopCh := make(chan error)
8382

84-
go s.serveRecv(stream, recvCh)
83+
go s.serveRecvFrontend(stream, recvCh)
8584

8685
defer func() {
8786
close(recvCh)
@@ -106,7 +105,7 @@ func (s *ProxyServer) Proxy(stream agent.ProxyService_ProxyServer) error {
106105
return <-stopCh
107106
}
108107

109-
func (s *ProxyServer) serveRecv(stream agent.ProxyService_ProxyServer, recvCh <-chan *agent.Packet) {
108+
func (s *ProxyServer) serveRecvFrontend(stream agent.ProxyService_ProxyServer, recvCh <-chan *agent.Packet) {
110109
glog.Info("start serve recv ...")
111110
for pkt := range recvCh {
112111
switch pkt.Type {

0 commit comments

Comments
 (0)