From 811442460c25ba7d5e9467773f7a031232c582b0 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Thu, 26 Aug 2021 07:28:39 -0700 Subject: [PATCH] :seedling: KubeadmControlPlane internal/proxy should use pointer structs The net.Conn interface doesn't need a plain struct, it can work well with pointers as well. To avoid copying structs around every time we initiate a connection or call a method, convert the underlying Conn struct in the proxy package to using pointer receiving methods. This change also fixes an issue with read/writeDeadline, which was being set in the local copy of the Conn struct before instead of the current object, which created an ineffective assignment. Signed-off-by: Vince Prignano --- controlplane/kubeadm/internal/proxy/addr.go | 2 +- controlplane/kubeadm/internal/proxy/conn.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/controlplane/kubeadm/internal/proxy/addr.go b/controlplane/kubeadm/internal/proxy/addr.go index d29f4dc25208..5d228e277dff 100644 --- a/controlplane/kubeadm/internal/proxy/addr.go +++ b/controlplane/kubeadm/internal/proxy/addr.go @@ -51,7 +51,7 @@ func (a Addr) String() string { } // NewAddrFromConn creates an Addr from the given connection. -func NewAddrFromConn(c Conn) Addr { +func NewAddrFromConn(c *Conn) Addr { return Addr{ port: c.stream.Headers().Get(corev1.PortHeader), identifier: c.stream.Identifier(), diff --git a/controlplane/kubeadm/internal/proxy/conn.go b/controlplane/kubeadm/internal/proxy/conn.go index 226eb2ef20ce..c612c4130fd5 100644 --- a/controlplane/kubeadm/internal/proxy/conn.go +++ b/controlplane/kubeadm/internal/proxy/conn.go @@ -33,32 +33,32 @@ type Conn struct { } // Read from the connection. -func (c Conn) Read(b []byte) (n int, err error) { +func (c *Conn) Read(b []byte) (n int, err error) { return c.stream.Read(b) } // Close the underlying proxied connection. -func (c Conn) Close() error { +func (c *Conn) Close() error { return kerrors.NewAggregate([]error{c.stream.Close(), c.connection.Close()}) } // Write to the connection. -func (c Conn) Write(b []byte) (n int, err error) { +func (c *Conn) Write(b []byte) (n int, err error) { return c.stream.Write(b) } // LocalAddr returns a fake address representing the proxied connection. -func (c Conn) LocalAddr() net.Addr { +func (c *Conn) LocalAddr() net.Addr { return NewAddrFromConn(c) } // RemoteAddr returns a fake address representing the proxied connection. -func (c Conn) RemoteAddr() net.Addr { +func (c *Conn) RemoteAddr() net.Addr { return NewAddrFromConn(c) } // SetDeadline sets the read and write deadlines to the specified interval. -func (c Conn) SetDeadline(t time.Time) error { +func (c *Conn) SetDeadline(t time.Time) error { // TODO: Handle deadlines c.readDeadline = t c.writeDeadline = t @@ -66,7 +66,7 @@ func (c Conn) SetDeadline(t time.Time) error { } // SetWriteDeadline sets the read and write deadlines to the specified interval. -func (c Conn) SetWriteDeadline(t time.Time) error { +func (c *Conn) SetWriteDeadline(t time.Time) error { c.writeDeadline = t return nil } @@ -79,8 +79,8 @@ func (c Conn) SetReadDeadline(t time.Time) error { // NewConn creates a new net/conn interface based on an underlying Kubernetes // API server proxy connection. -func NewConn(connection httpstream.Connection, stream httpstream.Stream) Conn { - return Conn{ +func NewConn(connection httpstream.Connection, stream httpstream.Stream) *Conn { + return &Conn{ connection: connection, stream: stream, }