github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/metrics.go (about)

     1  package kube
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jenkins-x/jx-logging/pkg/log"
     7  	"github.com/jenkins-x/jx/v2/pkg/util"
     8  	"k8s.io/client-go/kubernetes"
     9  
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
    12  	metricsclient "k8s.io/metrics/pkg/client/clientset/versioned"
    13  )
    14  
    15  type HeapterConfig struct {
    16  	KubeClient        kubernetes.Interface
    17  	HeapsterNamespace string
    18  	HeapsterScheme    string
    19  	HeapsterPort      string
    20  	HeapsterService   string
    21  	checkedForService bool
    22  }
    23  
    24  func (q *HeapterConfig) GetPodMetrics(ns string, pod string, selector string, metric string, start string, end string) ([]byte, error) {
    25  	kubeClient := q.KubeClient
    26  	heapsterNamespace := util.FirstNotEmptyString(q.HeapsterNamespace, "kube-system")
    27  	heapsterScheme := util.FirstNotEmptyString(q.HeapsterScheme, "http")
    28  	heapsterService := util.FirstNotEmptyString(q.HeapsterService, "heapster")
    29  	heapsterPort := util.FirstNotEmptyString(q.HeapsterPort, "80")
    30  
    31  	if !q.checkedForService {
    32  		svc, err := kubeClient.CoreV1().Services(heapsterNamespace).Get(heapsterService, metav1.GetOptions{})
    33  		if err != nil {
    34  			return nil, fmt.Errorf("Could not find heapster service %s in namespace %s: %s", heapsterService, heapsterNamespace, err)
    35  		}
    36  		q.checkedForService = true
    37  		// lets check the port is OK?
    38  		found := false
    39  		ports := []string{}
    40  		if svc.Spec.Ports != nil {
    41  			for _, p := range svc.Spec.Ports {
    42  				i := p.Port
    43  				if i > 0 {
    44  					t := util.Int32ToA(i)
    45  					ports = append(ports, t)
    46  					if !found {
    47  						if q.HeapsterPort == "" {
    48  							q.HeapsterPort = t
    49  							found = true
    50  						}
    51  						if q.HeapsterPort == t {
    52  							found = true
    53  						}
    54  					}
    55  				}
    56  			}
    57  		}
    58  		if !found {
    59  			if q.HeapsterPort == "" {
    60  				return nil, fmt.Errorf("Heapster service %s in namespace %s is not listing on a port", heapsterService, heapsterNamespace)
    61  			} else {
    62  				return nil, util.InvalidOption("heapster-port", q.HeapsterPort, ports)
    63  			}
    64  		}
    65  	}
    66  
    67  	path := util.UrlJoin("/apis/metrics/v1alpha1/namespaces/", ns, "/pods")
    68  	if pod != "" {
    69  		path = util.UrlJoin(path, pod)
    70  		if metric != "" {
    71  			path = util.UrlJoin(path, "metrics", metric)
    72  		}
    73  	}
    74  	params := map[string]string{}
    75  	if selector != "" {
    76  		params["labelSelector"] = selector
    77  	}
    78  	if start != "" {
    79  		params["start"] = start
    80  	}
    81  	if end != "" {
    82  		params["end"] = end
    83  	}
    84  	log.Logger().Infof("Querying %s using query parameters: %#v", path, params)
    85  	resp := kubeClient.CoreV1().Services(heapsterNamespace).ProxyGet(heapsterScheme, heapsterService, heapsterPort, path, params)
    86  	return resp.DoRaw()
    87  }
    88  
    89  func GetPodMetrics(client *metricsclient.Clientset, ns string) (*metricsv1beta1.PodMetricsList, error) {
    90  	return client.MetricsV1beta1().PodMetricses(ns).List(metav1.ListOptions{})
    91  }