conn.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "errors"
  16. "io"
  17. "net"
  18. "time"
  19. "k8s.io/klog"
  20. "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
  21. )
  22. // CloseTimeout is the timeout to wait CLOSE_RSP packet after a
  23. // successful delivery of CLOSE_REQ.
  24. const CloseTimeout = 10 * time.Second
  25. // conn is an implementation of net.Conn, where the data is transported
  26. // over an established tunnel defined by a gRPC service ProxyService.
  27. type conn struct {
  28. stream client.ProxyService_ProxyClient
  29. connID int64
  30. readCh chan []byte
  31. closeCh chan string
  32. rdata []byte
  33. }
  34. var _ net.Conn = &conn{}
  35. // Write sends the data thru the connection over proxy service
  36. func (c *conn) Write(data []byte) (n int, err error) {
  37. req := &client.Packet{
  38. Type: client.PacketType_DATA,
  39. Payload: &client.Packet_Data{
  40. Data: &client.Data{
  41. ConnectID: c.connID,
  42. Data: data,
  43. },
  44. },
  45. }
  46. klog.Infof("[tracing] send req %+v", req)
  47. err = c.stream.Send(req)
  48. if err != nil {
  49. return 0, err
  50. }
  51. return len(data), err
  52. }
  53. // Read receives data from the connection over proxy service
  54. func (c *conn) Read(b []byte) (n int, err error) {
  55. var data []byte
  56. if c.rdata != nil {
  57. data = c.rdata
  58. } else {
  59. data = <-c.readCh
  60. }
  61. if data == nil {
  62. return 0, io.EOF
  63. }
  64. if len(data) > len(b) {
  65. copy(b, data[:len(b)])
  66. c.rdata = data[len(b):]
  67. return len(b), nil
  68. }
  69. c.rdata = nil
  70. copy(b, data)
  71. return len(data), nil
  72. }
  73. func (c *conn) LocalAddr() net.Addr {
  74. return nil
  75. }
  76. func (c *conn) RemoteAddr() net.Addr {
  77. return nil
  78. }
  79. func (c *conn) SetDeadline(t time.Time) error {
  80. return errors.New("not implemented")
  81. }
  82. func (c *conn) SetReadDeadline(t time.Time) error {
  83. return errors.New("not implemented")
  84. }
  85. func (c *conn) SetWriteDeadline(t time.Time) error {
  86. return errors.New("not implemented")
  87. }
  88. // Close closes the connection. It also sends CLOSE_REQ packet over
  89. // proxy service to notify remote to drop the connection.
  90. func (c *conn) Close() error {
  91. klog.Info("conn.Close()")
  92. req := &client.Packet{
  93. Type: client.PacketType_CLOSE_REQ,
  94. Payload: &client.Packet_CloseRequest{
  95. CloseRequest: &client.CloseRequest{
  96. ConnectID: c.connID,
  97. },
  98. },
  99. }
  100. klog.Infof("[tracing] send req %+v", req)
  101. if err := c.stream.Send(req); err != nil {
  102. return err
  103. }
  104. select {
  105. case errMsg := <-c.closeCh:
  106. if errMsg != "" {
  107. return errors.New(errMsg)
  108. }
  109. return nil
  110. case <-time.After(CloseTimeout):
  111. }
  112. return errors.New("close timeout")
  113. }