| 1 |
/* |
| 2 |
Copyright 2019 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package client |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"io" |
| 22 |
"net" |
| 23 |
"sync/atomic" |
| 24 |
"time" |
| 25 |
|
| 26 |
"k8s.io/klog/v2" |
| 27 |
|
| 28 |
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client" |
| 29 |
) |
| 30 |
|
| 31 |
// CloseTimeout is the timeout to wait CLOSE_RSP packet after a |
| 32 |
// successful delivery of CLOSE_REQ. |
| 33 |
const CloseTimeout = 10 * time.Second |
| 34 |
|
| 35 |
var errConnTunnelClosed = errors.New("tunnel closed") |
| 36 |
var errConnCloseTimeout = errors.New("close timeout") |
| 37 |
|
| 38 |
// conn is an implementation of net.Conn, where the data is transported |
| 39 |
// over an established tunnel defined by a gRPC service ProxyService. |
| 40 |
type conn struct { |
| 41 |
tunnel *grpcTunnel |
| 42 |
// connID is set when a successful DIAL_RSP is received |
| 43 |
connID int64 |
| 44 |
// random (dialID) is always initialized |
| 45 |
random int64 |
| 46 |
readCh chan []byte |
| 47 |
// On receiving CLOSE_RSP, closeCh will be sent any error message and closed. |
| 48 |
closeCh chan string |
| 49 |
rdata []byte |
| 50 |
|
| 51 |
// closing is an atomic bool represented as a 0 or 1, and set to true when the connection is being closed. |
| 52 |
// closing should only be accessed through atomic methods. |
| 53 |
// TODO: switch this to an atomic.Bool once the client is exclusively buit with go1.19+ |
| 54 |
closing uint32 |
| 55 |
} |
| 56 |
|
| 57 |
var _ net.Conn = &conn{} |
| 58 |
|
| 59 |
// Write sends the data through the connection over proxy service |
| 60 |
func (c *conn) Write(data []byte) (n int, err error) { |
| 61 |
req := &client.Packet{ |
| 62 |
Type: client.PacketType_DATA, |
| 63 |
Payload: &client.Packet_Data{ |
| 64 |
Data: &client.Data{ |
| 65 |
ConnectID: c.connID, |
| 66 |
Data: data, |
| 67 |
}, |
| 68 |
}, |
| 69 |
} |
| 70 |
|
| 71 |
klog.V(5).InfoS("[tracing] send req", "type", req.Type) |
| 72 |
|
| 73 |
err = c.tunnel.Send(req) |
| 74 |
if err != nil { |
| 75 |
return 0, err |
| 76 |
} |
| 77 |
return len(data), err |
| 78 |
} |
| 79 |
|
| 80 |
// Read receives data from the connection over proxy service |
| 81 |
func (c *conn) Read(b []byte) (n int, err error) { |
| 82 |
var data []byte |
| 83 |
|
| 84 |
if c.rdata != nil { |
| 85 |
data = c.rdata |
| 86 |
} else { |
| 87 |
data = <-c.readCh |
| 88 |
} |
| 89 |
|
| 90 |
if data == nil { |
| 91 |
return 0, io.EOF |
| 92 |
} |
| 93 |
|
| 94 |
if len(data) > len(b) { |
| 95 |
copy(b, data[:len(b)]) |
| 96 |
c.rdata = data[len(b):] |
| 97 |
return len(b), nil |
| 98 |
} |
| 99 |
|
| 100 |
c.rdata = nil |
| 101 |
copy(b, data) |
| 102 |
|
| 103 |
return len(data), nil |
| 104 |
} |
| 105 |
|
| 106 |
func (c *conn) LocalAddr() net.Addr { |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
func (c *conn) RemoteAddr() net.Addr { |
| 111 |
return nil |
| 112 |
} |
| 113 |
|
| 114 |
func (c *conn) SetDeadline(t time.Time) error { |
| 115 |
return errors.New("not implemented") |
| 116 |
} |
| 117 |
|
| 118 |
func (c *conn) SetReadDeadline(t time.Time) error { |
| 119 |
return errors.New("not implemented") |
| 120 |
} |
| 121 |
|
| 122 |
func (c *conn) SetWriteDeadline(t time.Time) error { |
| 123 |
return errors.New("not implemented") |
| 124 |
} |
| 125 |
|
| 126 |
// Close closes the connection, sends best-effort close signal to proxy |
| 127 |
// service, and frees resources. |
| 128 |
func (c *conn) Close() error { |
| 129 |
old := atomic.SwapUint32(&c.closing, 1) |
| 130 |
if old != 0 { |
| 131 |
// prevent duplicate messages |
| 132 |
return nil |
| 133 |
} |
| 134 |
klog.V(4).Infoln("closing connection", "dialID", c.random, "connectionID", c.connID) |
| 135 |
|
| 136 |
defer c.tunnel.closeTunnel() |
| 137 |
|
| 138 |
if c.connID != 0 { |
| 139 |
c.tunnel.sendCloseRequest(c.connID) |
| 140 |
} else { |
| 141 |
// Never received a DIAL response so no connection ID. |
| 142 |
c.tunnel.sendDialClose(c.random) |
| 143 |
} |
| 144 |
|
| 145 |
select { |
| 146 |
case errMsg := <-c.closeCh: |
| 147 |
if errMsg != "" { |
| 148 |
return errors.New(errMsg) |
| 149 |
} |
| 150 |
return nil |
| 151 |
case <-c.tunnel.Done(): |
| 152 |
return errConnTunnelClosed |
| 153 |
case <-time.After(CloseTimeout): |
| 154 |
} |
| 155 |
|
| 156 |
return errConnCloseTimeout |
| 157 |
} |