github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/unversioned/kubelet.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     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 unversioned
    18  
    19  import (
    20  	"errors"
    21  	"net/http"
    22  
    23  	"k8s.io/kubernetes/pkg/client/transport"
    24  	"k8s.io/kubernetes/pkg/util"
    25  )
    26  
    27  // KubeletClient is an interface for all kubelet functionality
    28  type KubeletClient interface {
    29  	ConnectionInfoGetter
    30  }
    31  
    32  type ConnectionInfoGetter interface {
    33  	GetConnectionInfo(host string) (scheme string, port uint, transport http.RoundTripper, err error)
    34  }
    35  
    36  // HTTPKubeletClient is the default implementation of KubeletHealthchecker, accesses the kubelet over HTTP.
    37  type HTTPKubeletClient struct {
    38  	Client *http.Client
    39  	Config *KubeletConfig
    40  }
    41  
    42  func MakeTransport(config *KubeletConfig) (http.RoundTripper, error) {
    43  	tlsConfig, err := transport.TLSConfigFor(config.transportConfig())
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	rt := http.DefaultTransport
    49  	if config.Dial != nil || tlsConfig != nil {
    50  		rt = util.SetTransportDefaults(&http.Transport{
    51  			Dial:            config.Dial,
    52  			TLSClientConfig: tlsConfig,
    53  		})
    54  	}
    55  
    56  	return transport.HTTPWrappersForConfig(config.transportConfig(), rt)
    57  }
    58  
    59  // TODO: this structure is questionable, it should be using client.Config and overriding defaults.
    60  func NewKubeletClient(config *KubeletConfig) (KubeletClient, error) {
    61  	transport, err := MakeTransport(config)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	c := &http.Client{
    66  		Transport: transport,
    67  		Timeout:   config.HTTPTimeout,
    68  	}
    69  	return &HTTPKubeletClient{
    70  		Client: c,
    71  		Config: config,
    72  	}, nil
    73  }
    74  
    75  func (c *HTTPKubeletClient) GetConnectionInfo(host string) (string, uint, http.RoundTripper, error) {
    76  	scheme := "http"
    77  	if c.Config.EnableHttps {
    78  		scheme = "https"
    79  	}
    80  	return scheme, c.Config.Port, c.Client.Transport, nil
    81  }
    82  
    83  // FakeKubeletClient is a fake implementation of KubeletClient which returns an error
    84  // when called.  It is useful to pass to the master in a test configuration with
    85  // no kubelets.
    86  type FakeKubeletClient struct{}
    87  
    88  func (c FakeKubeletClient) GetConnectionInfo(host string) (string, uint, http.RoundTripper, error) {
    89  	return "", 0, nil, errors.New("Not Implemented")
    90  }