Exemplo n.º 1
0
// allNamespaces returns a list of all the available namespaces in the cluster.
func (a *Api) allNamespaces(request *restful.Request, response *restful.Response) {
	cluster := a.manager.GetCluster()
	if cluster == nil {
		response.WriteError(400, errModelNotActivated)
	}
	response.WriteEntity(cluster.GetNamespaces())
}
Exemplo n.º 2
0
func (a *Api) exportMetricsSchema(request *restful.Request, response *restful.Response) {
	result := TimeseriesSchema{}
	for _, label := range sinksApi.CommonLabels() {
		result.CommonLabels = append(result.CommonLabels, LabelDescriptor{
			Key:         label.Key,
			Description: label.Description,
		})
	}
	for _, label := range sinksApi.PodLabels() {
		result.PodLabels = append(result.PodLabels, LabelDescriptor{
			Key:         label.Key,
			Description: label.Description,
		})
	}

	for _, metric := range sinksApi.SupportedStatMetrics() {
		md := MetricDescriptor{
			Name:        metric.Name,
			Description: metric.Description,
			Type:        metric.Type.String(),
			ValueType:   metric.ValueType.String(),
			Units:       metric.Units.String(),
		}
		for _, label := range metric.Labels {
			md.Labels = append(md.Labels, LabelDescriptor{
				Key:         label.Key,
				Description: label.Description,
			})
		}
		result.Metrics = append(result.Metrics, md)
	}
	response.WriteEntity(result)
}
Exemplo n.º 3
0
func (a *Api) nodeMetricsList(request *restful.Request, response *restful.Response) {
	selector := request.QueryParameter("labelSelector")

	labelSelector, err := labels.Parse(selector)
	if err != nil {
		errMsg := fmt.Errorf("Error while parsing selector %v: %v", selector, err)
		glog.Error(errMsg)
		response.WriteError(http.StatusBadRequest, errMsg)
		return
	}

	nodes, err := a.nodeLister.NodeCondition(func(node *kube_api.Node) bool {
		if labelSelector.Empty() {
			return true
		}
		return labelSelector.Matches(labels.Set(node.Labels))
	}).List()
	if err != nil {
		errMsg := fmt.Errorf("Error while listing nodes: %v", err)
		glog.Error(errMsg)
		response.WriteError(http.StatusInternalServerError, errMsg)
		return
	}

	res := v1alpha1.NodeMetricsList{}
	for _, node := range nodes {
		if m := a.getNodeMetrics(node.Name); m != nil {
			res.Items = append(res.Items, *m)
		}
	}
	response.WriteEntity(&res)
}
Exemplo n.º 4
0
// namespacePodList lists all pods for which we have metrics in a particular namespace
func (a *HistoricalApi) namespacePodList(request *restful.Request, response *restful.Response) {
	if resp, err := a.historicalSource.GetPodsFromNamespace(request.PathParameter("namespace-name")); err != nil {
		response.WriteError(http.StatusInternalServerError, err)
	} else {
		response.WriteEntity(resp)
	}
}
Exemplo n.º 5
0
// podContainerMetrics returns a metric timeseries for a metric of the Container entity.
// freeContainerMetrics addresses only pod containers, by using the namespace-name/pod-name/container-name path.
func (a *Api) podContainerMetrics(request *restful.Request, response *restful.Response) {
	cluster := a.manager.GetCluster()

	// Get namespace name
	namespace := request.PathParameter("namespace-name")

	// Get pod name
	pod := request.PathParameter("pod-name")

	// Get container name
	container := request.PathParameter("container-name")

	// Get metric name
	metric_name := request.PathParameter("metric-name")

	// Get start time, parse as time.Time
	req_stamp := parseRequestStartParam(request, response)

	timeseries, new_stamp, err := cluster.GetPodContainerMetric(namespace, pod, container, metric_name, req_stamp)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		glog.Errorf("unable to get cluster metric: %s", err)
		return
	}
	response.WriteEntity(exportTimeseries(timeseries, new_stamp))
}
Exemplo n.º 6
0
func podMetricsInNamespaceList(a *Api, request *restful.Request, response *restful.Response, namespace string) {
	selector := request.QueryParameter("labelSelector")

	labelSelector, err := labels.Parse(selector)
	if err != nil {
		errMsg := fmt.Errorf("Error while parsing selector %v: %v", selector, err)
		glog.Error(errMsg)
		response.WriteError(http.StatusBadRequest, errMsg)
		return
	}

	pods, err := a.podLister.Pods(namespace).List(labelSelector)
	if err != nil {
		errMsg := fmt.Errorf("Error while listing pods for selector %v: %v", selector, err)
		glog.Error(errMsg)
		response.WriteError(http.StatusInternalServerError, errMsg)
		return
	}

	res := v1alpha1.PodMetricsList{}
	for _, pod := range pods.Items {
		if m := a.getPodMetrics(&pod); m != nil {
			res.Items = append(res.Items, *m)
		} else {
			glog.Infof("No metrics for pod %s/%s", pod.Namespace, pod.Name)
		}
	}
	response.WriteEntity(&res)
}
Exemplo n.º 7
0
func (a *Api) podListMetrics(request *restful.Request, response *restful.Response) {
	model := a.manager.GetModel()
	if model == nil {
		response.WriteError(400, errModelNotActivated)
		return
	}
	batchResult, new_stamp, err := model.GetBatchPodMetric(model_api.BatchPodRequest{
		NamespaceName: request.PathParameter("namespace-name"),
		PodNames:      strings.Split(request.PathParameter("pod-list"), ","),
		MetricName:    request.PathParameter("metric-name"),
		Start:         parseRequestParam("start", request, response),
		End:           parseRequestParam("end", request, response),
	})
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		glog.Errorf("unable to get pod list metric: %s", err)
		return
	}
	metricResultList := types.MetricResultList{
		Items: make([]types.MetricResult, len(batchResult)),
	}
	for i, metrics := range batchResult {
		metricResultList.Items[i] = exportTimeseries(metrics, new_stamp)
	}
	response.WriteEntity(metricResultList)
}
Exemplo n.º 8
0
// nodeSystemContainerList lists all system containers on a node for which we have metrics
func (a *HistoricalApi) nodeSystemContainerList(request *restful.Request, response *restful.Response) {
	if resp, err := a.historicalSource.GetSystemContainersFromNode(request.PathParameter("node-name")); err != nil {
		response.WriteError(http.StatusInternalServerError, err)
	} else {
		response.WriteEntity(resp)
	}
}
Exemplo n.º 9
0
func (a *Api) getSinks(req *restful.Request, resp *restful.Response) {
	sinkUris := a.manager.SinkUris()
	if sinkUris == nil {
		sinkUris = make(manager.Uris, 0)
	}
	resp.WriteEntity(sinkUris)
}
Exemplo n.º 10
0
// processMetricRequest retrieves a metric for the object at the requested key.
func (a *HistoricalApi) processMetricRequest(key core.HistoricalKey, request *restful.Request, response *restful.Response) {
	start, end, err := getStartEndTimeHistorical(request)
	if err != nil {
		response.WriteError(http.StatusBadRequest, err)
		return
	}
	labels, err := getLabels(request)
	if err != nil {
		response.WriteError(http.StatusBadRequest, err)
		return
	}
	metricName := request.PathParameter("metric-name")
	convertedMetricName := convertMetricName(metricName)

	var metrics map[core.HistoricalKey][]core.TimestampedMetricValue
	if labels != nil {
		metrics, err = a.historicalSource.GetLabeledMetric(convertedMetricName, labels, []core.HistoricalKey{key}, start, end)
	} else {
		metrics, err = a.historicalSource.GetMetric(convertedMetricName, []core.HistoricalKey{key}, start, end)
	}
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}

	converted := exportTimestampedMetricValue(metrics[key])
	response.WriteEntity(converted)
}
Exemplo n.º 11
0
// containerPaths returns a list of all the available API paths that are available for a container.
func (a *Api) containerPaths(request *restful.Request, response *restful.Response) {
	entities := []string{
		"metrics/",
		"stats/",
	}
	response.WriteEntity(entities)
}
Exemplo n.º 12
0
// namespaceList lists all namespaces for which we have metrics
func (a *HistoricalApi) namespaceList(request *restful.Request, response *restful.Response) {
	if resp, err := a.historicalSource.GetNamespaces(); err != nil {
		response.WriteError(http.StatusInternalServerError, err)
	} else {
		response.WriteEntity(resp)
	}
}
Exemplo n.º 13
0
// nodePaths returns a list of all the available API paths that are available for a node.
func (a *Api) nodePaths(request *restful.Request, response *restful.Response) {
	entities := []string{
		"freecontainers/",
		"metrics/",
	}
	response.WriteEntity(entities)
}
Exemplo n.º 14
0
// namespacePaths returns a list of all the available API paths that are available for a namespace.
func (a *Api) namespacePaths(request *restful.Request, response *restful.Response) {
	entities := []string{
		"pods/",
		"metrics/",
	}
	response.WriteEntity(entities)
}
Exemplo n.º 15
0
// processMetricNamesRequest retrieves the available metrics for the object at the specified key.
func (a *HistoricalApi) processMetricNamesRequest(key core.HistoricalKey, response *restful.Response) {
	if resp, err := a.historicalSource.GetMetricNames(key); err != nil {
		response.WriteError(http.StatusInternalServerError, err)
	} else {
		response.WriteEntity(resp)
	}
}
Exemplo n.º 16
0
// allNamespaces returns a list of all the available namespaces in the model.
func (a *Api) allNamespaces(request *restful.Request, response *restful.Response) {
	model := a.manager.GetModel()
	if model == nil {
		response.WriteError(400, errModelNotActivated)
		return
	}
	response.WriteEntity(makeExternalEntityList(model.GetNamespaces()))
}
Exemplo n.º 17
0
// allFreeContainers returns a list of all the available free containers in the cluster.
func (a *Api) allFreeContainers(request *restful.Request, response *restful.Response) {
	cluster := a.manager.GetCluster()
	if cluster == nil {
		response.WriteError(400, errModelNotActivated)
	}
	node := request.PathParameter("node-name")
	response.WriteEntity(cluster.GetFreeContainers(node))
}
Exemplo n.º 18
0
// nodePods returns a list of all the available API paths that are available for a node.
func (a *Api) nodePods(request *restful.Request, response *restful.Response) {
	model := a.manager.GetModel()
	if model == nil {
		response.WriteError(400, errModelNotActivated)
	}
	node := request.PathParameter("node-name")
	response.WriteEntity(makeExternalEntityList(model.GetNodePods(node)))
}
Exemplo n.º 19
0
// allEntities returns a list of all the top-level paths that are available in the API.
func (a *Api) allEntities(request *restful.Request, response *restful.Response) {
	entities := []string{
		"metrics/",
		"namespaces/",
		"nodes/",
	}
	response.WriteEntity(entities)
}
Exemplo n.º 20
0
// availableMetrics returns a list of available metric names.
// These metric names can be used to extract metrics from the various model entities.
func (a *Api) availableMetrics(request *restful.Request, response *restful.Response) {
	cluster := a.manager.GetCluster()
	if cluster == nil {
		response.WriteError(400, errModelNotActivated)
	}
	result := cluster.GetAvailableMetrics()
	response.WriteEntity(result)
}
Exemplo n.º 21
0
// allPods returns a list of all the available pods in the cluster.
func (a *Api) allPods(request *restful.Request, response *restful.Response) {
	cluster := a.manager.GetCluster()
	if cluster == nil {
		response.WriteError(400, errModelNotActivated)
	}
	namespace := request.PathParameter("namespace-name")
	response.WriteEntity(cluster.GetPods(namespace))
}
Exemplo n.º 22
0
// getSpec handles spec requests against the Kubelet.
func (s *Server) getSpec(request *restful.Request, response *restful.Response) {
	info, err := s.host.GetCachedMachineInfo()
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}
	response.WriteEntity(info)
}
Exemplo n.º 23
0
// availableMetrics returns a list of available metric names.
// These metric names can be used to extract metrics from the various model entities.
func (a *Api) availableMetrics(request *restful.Request, response *restful.Response) {
	model := a.manager.GetModel()
	if model == nil {
		response.WriteError(400, errModelNotActivated)
		return
	}
	result := model.GetAvailableMetrics()
	response.WriteEntity(result)
}
Exemplo n.º 24
0
// podListMetrics returns a list of metric timeseries for each for the listed nodes
func (a *HistoricalApi) podListMetrics(request *restful.Request, response *restful.Response) {
	start, end, err := getStartEndTimeHistorical(request)
	if err != nil {
		response.WriteError(http.StatusBadRequest, err)
		return
	}

	keys := []core.HistoricalKey{}
	if request.PathParameter("pod-id-list") != "" {
		for _, podId := range strings.Split(request.PathParameter("pod-id-list"), ",") {
			key := core.HistoricalKey{
				ObjectType: core.MetricSetTypePod,
				PodId:      podId,
			}
			keys = append(keys, key)
		}
	} else {
		for _, podName := range strings.Split(request.PathParameter("pod-list"), ",") {
			key := core.HistoricalKey{
				ObjectType:    core.MetricSetTypePod,
				NamespaceName: request.PathParameter("namespace-name"),
				PodName:       podName,
			}
			keys = append(keys, key)
		}
	}

	labels, err := getLabels(request)
	if err != nil {
		response.WriteError(http.StatusBadRequest, err)
		return
	}

	metricName := request.PathParameter("metric-name")
	convertedMetricName := convertMetricName(metricName)

	var metrics map[core.HistoricalKey][]core.TimestampedMetricValue
	if labels != nil {
		metrics, err = a.historicalSource.GetLabeledMetric(convertedMetricName, labels, keys, start, end)
	} else {
		metrics, err = a.historicalSource.GetMetric(convertedMetricName, keys, start, end)
	}

	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}

	result := types.MetricResultList{
		Items: make([]types.MetricResult, 0, len(keys)),
	}
	for _, key := range keys {
		result.Items = append(result.Items, exportTimestampedMetricValue(metrics[key]))
	}
	response.PrettyPrint(false)
	response.WriteEntity(result)
}
Exemplo n.º 25
0
func (a *Api) nodeMetrics(request *restful.Request, response *restful.Response) {
	node := request.PathParameter("node-name")
	m := a.getNodeMetrics(node)
	if m == nil {
		response.WriteError(http.StatusNotFound, fmt.Errorf("No metrics for ode %v", node))
		return
	}
	response.WriteEntity(m)
}
Exemplo n.º 26
0
func (a *Api) nodeMetricsList(request *restful.Request, response *restful.Response) {
	res := v1alpha1.NodeMetricsList{}
	for _, node := range a.metricSink.GetNodes() {
		if m := a.getNodeMetrics(node); m != nil {
			res.Items = append(res.Items, *m)
		}
	}
	response.WriteEntity(&res)
}
Exemplo n.º 27
0
func getFoo(request *restful.Request, response *restful.Response) {
	f := Foo{
		Crew: []Crew{
			Crew{"Kirk", "Captain"},
			Crew{"McCoy", "Doctor"},
		},
	}
	response.WriteEntity(&f)

}
Exemplo n.º 28
0
// allPodContainers returns a list of all the available pod containers in the model.
func (a *Api) allPodContainers(request *restful.Request, response *restful.Response) {
	model := a.manager.GetModel()
	if model == nil {
		response.WriteError(400, errModelNotActivated)
		return
	}
	namespace := request.PathParameter("namespace-name")
	pod := request.PathParameter("pod-name")
	response.WriteEntity(makeExternalEntityList(model.GetPodContainers(namespace, pod)))
}
Exemplo n.º 29
0
func (a *Api) derivedNodeMetrics(request *restful.Request, response *restful.Response) {
	model := a.manager.GetModel()
	if model == nil {
		response.WriteError(400, errModelNotActivated)
	}
	metrics, err := getNodeMetrics(model, request.PathParameter("node-name"))
	if err != nil {
		response.WriteError(400, err)
	}
	response.WriteEntity(metrics)
}
Exemplo n.º 30
0
// clusterStats returns a map of StatBundles for each usage metric of the Cluster entity.
func (a *Api) clusterStats(request *restful.Request, response *restful.Response) {
	model := a.manager.GetModel()
	if model == nil {
		response.WriteError(400, errModelNotActivated)
	}
	res, uptime, err := model.GetClusterStats()
	if err != nil {
		response.WriteError(400, err)
	}
	response.WriteEntity(exportStatBundle(res, uptime))
}