sigs.k8s.io/cluster-api@v1.7.1/controlplane/kubeadm/internal/proxy/addr.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 implements kubeadm proxy functionality.
    18  package proxy
    19  
    20  import (
    21  	"fmt"
    22  	"net"
    23  
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/client-go/tools/portforward"
    26  )
    27  
    28  const scheme string = "proxy"
    29  
    30  // Addr defines a proxy net/addr format.
    31  type Addr struct {
    32  	net.Addr
    33  	port       string
    34  	identifier uint32
    35  }
    36  
    37  // Network returns a fake network.
    38  func (a Addr) Network() string {
    39  	return portforward.PortForwardProtocolV1Name
    40  }
    41  
    42  // String returns encoded information about the connection.
    43  func (a Addr) String() string {
    44  	return fmt.Sprintf(
    45  		"%s://%d.%s.local:%s",
    46  		scheme,
    47  		a.identifier,
    48  		portforward.PortForwardProtocolV1Name,
    49  		a.port,
    50  	)
    51  }
    52  
    53  // NewAddrFromConn creates an Addr from the given connection.
    54  func NewAddrFromConn(c *Conn) Addr {
    55  	return Addr{
    56  		port:       c.stream.Headers().Get(corev1.PortHeader),
    57  		identifier: c.stream.Identifier(),
    58  	}
    59  }