sigs.k8s.io/cluster-api@v1.7.1/controlplane/kubeadm/internal/proxy/conn.go (about)

     1  /*
     2  Copyright 2020 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 proxy
    18  
    19  import (
    20  	"net"
    21  	"time"
    22  
    23  	kerrors "k8s.io/apimachinery/pkg/util/errors"
    24  	"k8s.io/apimachinery/pkg/util/httpstream"
    25  )
    26  
    27  // Conn is a Kubernetes API server proxied type of net/conn.
    28  type Conn struct {
    29  	connection    httpstream.Connection
    30  	stream        httpstream.Stream
    31  	readDeadline  time.Time
    32  	writeDeadline time.Time
    33  }
    34  
    35  // Read from the connection.
    36  func (c *Conn) Read(b []byte) (n int, err error) {
    37  	return c.stream.Read(b)
    38  }
    39  
    40  // Close the underlying proxied connection.
    41  func (c *Conn) Close() error {
    42  	return kerrors.NewAggregate([]error{c.stream.Close(), c.connection.Close()})
    43  }
    44  
    45  // Write to the connection.
    46  func (c *Conn) Write(b []byte) (n int, err error) {
    47  	return c.stream.Write(b)
    48  }
    49  
    50  // LocalAddr returns a fake address representing the proxied connection.
    51  func (c *Conn) LocalAddr() net.Addr {
    52  	return NewAddrFromConn(c)
    53  }
    54  
    55  // RemoteAddr returns a fake address representing the proxied connection.
    56  func (c *Conn) RemoteAddr() net.Addr {
    57  	return NewAddrFromConn(c)
    58  }
    59  
    60  // SetDeadline sets the read and write deadlines to the specified interval.
    61  func (c *Conn) SetDeadline(t time.Time) error {
    62  	// TODO: Handle deadlines
    63  	c.readDeadline = t
    64  	c.writeDeadline = t
    65  	return nil
    66  }
    67  
    68  // SetWriteDeadline sets the read and write deadlines to the specified interval.
    69  func (c *Conn) SetWriteDeadline(t time.Time) error {
    70  	c.writeDeadline = t
    71  	return nil
    72  }
    73  
    74  // SetReadDeadline sets the read and write deadlines to the specified interval.
    75  func (c *Conn) SetReadDeadline(t time.Time) error {
    76  	c.readDeadline = t
    77  	return nil
    78  }
    79  
    80  // NewConn creates a new net/conn interface based on an underlying Kubernetes
    81  // API server proxy connection.
    82  func NewConn(connection httpstream.Connection, stream httpstream.Stream) *Conn {
    83  	return &Conn{
    84  		connection: connection,
    85  		stream:     stream,
    86  	}
    87  }