github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sources/kubelet/util/kubelet_client.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client
    16  
    17  import (
    18  	"net/http"
    19  	"time"
    20  
    21  	utilnet "k8s.io/apimachinery/pkg/util/net"
    22  	restclient "k8s.io/client-go/rest"
    23  	"k8s.io/client-go/transport"
    24  )
    25  
    26  type KubeletClientConfig struct {
    27  	// Default port - used if no information about Kubelet port can be found in Node.NodeStatus.DaemonEndpoints.
    28  	Port         uint
    29  	ReadOnlyPort uint
    30  	EnableHttps  bool
    31  
    32  	// PreferredAddressTypes - used to select an address from Node.NodeStatus.Addresses
    33  	PreferredAddressTypes []string
    34  
    35  	// TLSClientConfig contains settings to enable transport layer security
    36  	restclient.TLSClientConfig
    37  
    38  	// Server requires Bearer authentication
    39  	BearerToken string
    40  
    41  	// HTTPTimeout is used by the client to timeout http requests to Kubelet.
    42  	HTTPTimeout time.Duration
    43  
    44  	// Dial is a custom dialer used for the client
    45  	Dial utilnet.DialFunc
    46  }
    47  
    48  func MakeTransport(config *KubeletClientConfig) (http.RoundTripper, error) {
    49  	tlsConfig, err := transport.TLSConfigFor(config.transportConfig())
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	rt := http.DefaultTransport
    55  	if config.Dial != nil || tlsConfig != nil {
    56  		rt = utilnet.SetOldTransportDefaults(&http.Transport{
    57  			Dial:            config.Dial,
    58  			TLSClientConfig: tlsConfig,
    59  		})
    60  	}
    61  
    62  	return transport.HTTPWrappersForConfig(config.transportConfig(), rt)
    63  }
    64  
    65  // transportConfig converts a client config to an appropriate transport config.
    66  func (c *KubeletClientConfig) transportConfig() *transport.Config {
    67  	cfg := &transport.Config{
    68  		TLS: transport.TLSConfig{
    69  			CAFile:   c.CAFile,
    70  			CAData:   c.CAData,
    71  			CertFile: c.CertFile,
    72  			CertData: c.CertData,
    73  			KeyFile:  c.KeyFile,
    74  			KeyData:  c.KeyData,
    75  		},
    76  	}
    77  	if c.EnableHttps {
    78  		cfg.BearerToken = c.BearerToken
    79  	}
    80  	if c.EnableHttps && !cfg.HasCA() {
    81  		cfg.TLS.Insecure = true
    82  	}
    83  	return cfg
    84  }