github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/unversioned/containerinfo.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  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  	"net"
    25  	"net/http"
    26  	"strconv"
    27  
    28  	cadvisorapi "github.com/google/cadvisor/info/v1"
    29  )
    30  
    31  type ContainerInfoGetter interface {
    32  	// GetContainerInfo returns information about a container.
    33  	GetContainerInfo(host, podID, containerID string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error)
    34  	// GetRootInfo returns information about the root container on a machine.
    35  	GetRootInfo(host string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error)
    36  	// GetMachineInfo returns the machine's information like number of cores, memory capacity.
    37  	GetMachineInfo(host string) (*cadvisorapi.MachineInfo, error)
    38  }
    39  
    40  type HTTPContainerInfoGetter struct {
    41  	Client *http.Client
    42  	Port   int
    43  }
    44  
    45  func (self *HTTPContainerInfoGetter) GetMachineInfo(host string) (*cadvisorapi.MachineInfo, error) {
    46  	request, err := http.NewRequest(
    47  		"GET",
    48  		fmt.Sprintf("http://%v/spec",
    49  			net.JoinHostPort(host, strconv.Itoa(self.Port)),
    50  		),
    51  		nil,
    52  	)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	response, err := self.Client.Do(request)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	defer response.Body.Close()
    62  	if response.StatusCode != http.StatusOK {
    63  		return nil, fmt.Errorf("trying to get machine spec from %v; received status %v",
    64  			host, response.Status)
    65  	}
    66  	var minfo cadvisorapi.MachineInfo
    67  	err = json.NewDecoder(response.Body).Decode(&minfo)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return &minfo, nil
    72  }
    73  
    74  func (self *HTTPContainerInfoGetter) getContainerInfo(host, path string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
    75  	var body io.Reader
    76  	if req != nil {
    77  		content, err := json.Marshal(req)
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  		body = bytes.NewBuffer(content)
    82  	}
    83  
    84  	request, err := http.NewRequest(
    85  		"GET",
    86  		fmt.Sprintf("http://%v/stats/%v",
    87  			net.JoinHostPort(host, strconv.Itoa(self.Port)),
    88  			path,
    89  		),
    90  		body,
    91  	)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	response, err := self.Client.Do(request)
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  	defer response.Body.Close()
   101  	if response.StatusCode != http.StatusOK {
   102  		return nil, fmt.Errorf("trying to get info for %v from %v; received status %v",
   103  			path, host, response.Status)
   104  	}
   105  	var cinfo cadvisorapi.ContainerInfo
   106  	err = json.NewDecoder(response.Body).Decode(&cinfo)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	return &cinfo, nil
   111  }
   112  
   113  func (self *HTTPContainerInfoGetter) GetContainerInfo(host, podID, containerID string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
   114  	return self.getContainerInfo(
   115  		host,
   116  		fmt.Sprintf("%v/%v", podID, containerID),
   117  		req,
   118  	)
   119  }
   120  
   121  func (self *HTTPContainerInfoGetter) GetRootInfo(host string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
   122  	return self.getContainerInfo(host, "", req)
   123  }