Beispiel #1
0
// freeContainerMetrics returns a metric timeseries for a metric of the Container entity.
// freeContainerMetrics addresses only free containers, by using the node-name/container-name path.
func (a *Api) freeContainerMetrics(request *restful.Request, response *restful.Response) {
	a.processMetricRequest(
		core.NodeContainerKey(request.PathParameter("node-name"),
			request.PathParameter("container-name"),
		),
		request, response)
}
Beispiel #2
0
func (a *Api) podMetricsList(request *restful.Request, response *restful.Response) {
	ns := request.PathParameter("namespace-name")
	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(ns).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.PodMetrics{}
	for _, pod := range pods.Items {
		if m := a.getPodMetrics(&pod); m != nil {
			res = append(res, m)
		} else {
			glog.Infof("No metrics for pod %s/%s", pod.Namespace, pod.Name)
		}
	}
	response.WriteEntity(res)
}
Beispiel #3
0
func (s *server) serveAttach(req *restful.Request, resp *restful.Response) {
	token := req.PathParameter("token")
	cachedRequest, ok := s.cache.Consume(token)
	if !ok {
		http.NotFound(resp.ResponseWriter, req.Request)
		return
	}
	attach, ok := cachedRequest.(*runtimeapi.AttachRequest)
	if !ok {
		http.NotFound(resp.ResponseWriter, req.Request)
		return
	}

	streamOpts := &remotecommand.Options{
		Stdin:  attach.GetStdin(),
		Stdout: true,
		Stderr: !attach.GetTty(),
		TTY:    attach.GetTty(),
	}
	remotecommand.ServeAttach(
		resp.ResponseWriter,
		req.Request,
		s.runtime,
		"", // unused: podName
		"", // unusued: podUID
		attach.GetContainerId(),
		streamOpts,
		s.config.StreamIdleTimeout,
		s.config.StreamCreationTimeout,
		s.config.SupportedProtocols)
}
// 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)
	}
}
// 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)
	}
}
// parseMetricRequest returns a MetricRequest from the metric-related query and path parameters of the request.
func parseMetricRequest(request *restful.Request, response *restful.Response) model_api.MetricRequest {
	return model_api.MetricRequest{
		MetricName: request.PathParameter("metric-name"),
		Start:      parseRequestParam("start", request, response),
		End:        parseRequestParam("end", request, response),
	}
}
// nodeMetrics returns a metric timeseries for a metric of the Node entity.
func (a *HistoricalApi) nodeMetrics(request *restful.Request, response *restful.Response) {
	key := core.HistoricalKey{
		ObjectType: core.MetricSetTypeNode,
		NodeName:   request.PathParameter("node-name"),
	}
	a.processMetricRequest(key, request, response)
}
// namespaceAggregations returns a metric timeseries for a metric of the Namespace entity.
func (a *HistoricalApi) namespaceAggregations(request *restful.Request, response *restful.Response) {
	key := core.HistoricalKey{
		ObjectType:    core.MetricSetTypeNamespace,
		NamespaceName: request.PathParameter("namespace-name"),
	}
	a.processAggregationRequest(key, request, response)
}
// 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)
}
Beispiel #10
0
func (s *server) serveAttach(req *restful.Request, resp *restful.Response) {
	containerID := req.PathParameter("containerID")
	if containerID == "" {
		resp.WriteError(http.StatusBadRequest, errors.New("missing required containerID path parameter"))
		return
	}

	streamOpts, err := remotecommand.NewOptions(req.Request)
	if err != nil {
		resp.WriteError(http.StatusBadRequest, err)
		return
	}

	remotecommand.ServeAttach(
		resp.ResponseWriter,
		req.Request,
		s.runtime,
		"", // unused: podName
		"", // unusued: podUID
		containerID,
		streamOpts,
		s.config.StreamIdleTimeout,
		s.config.StreamCreationTimeout,
		s.config.SupportedProtocols)
}
// availableNamespaceMetrics returns a list of available namespace metric names.
func (a *HistoricalApi) availableNamespaceMetrics(request *restful.Request, response *restful.Response) {
	key := core.HistoricalKey{
		ObjectType:    core.MetricSetTypeNamespace,
		NamespaceName: request.PathParameter("namespace-name"),
	}
	a.processMetricNamesRequest(key, response)
}
// 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)))
}
Beispiel #13
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))
}
// freeContainerAggregations returns a metric timeseries for a metric of the Container entity.
// freeContainerAggregations addresses only free containers.
func (a *HistoricalApi) freeContainerAggregations(request *restful.Request, response *restful.Response) {
	key := core.HistoricalKey{
		ObjectType:    core.MetricSetTypeSystemContainer,
		NodeName:      request.PathParameter("node-name"),
		ContainerName: request.PathParameter("container-name"),
	}
	a.processAggregationRequest(key, request, response)
}
Beispiel #15
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))
}
Beispiel #16
0
// Handle node stats API call.
func (apiHandler *ApiHandler) handleGetNodeStats(request *restful.Request, response *restful.Response) {
	name := request.PathParameter("name")
	result, err := GetNodeStats(name)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	response.WriteHeaderAndEntity(http.StatusCreated, result)
}
Beispiel #17
0
// Handles get secrets list API call.
func (apiHandler *ApiHandler) handleGetSecrets(request *restful.Request, response *restful.Response) {
	namespace := request.PathParameter("namespace")
	result, err := GetSecrets(apiHandler.client, namespace)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	response.WriteHeaderAndEntity(http.StatusOK, result)
}
// 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)
}
// Handles get node detail API call.
func (apiHandler *ApiHandler) handleGetNodeDetail(request *restful.Request, response *restful.Response) {
	name := request.PathParameter("name")
	result, err := node.GetNodeDetail(apiHandler.client, apiHandler.heapsterClient, name)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	response.WriteHeaderAndEntity(http.StatusCreated, result)
}
Beispiel #20
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)
}
Beispiel #21
0
// Handles event API call.
func (apiHandler *ApiHandler) handleEvents(request *restful.Request, response *restful.Response) {
	namespace := request.PathParameter("namespace")
	replicationController := request.PathParameter("replicationController")
	result, err := GetEvents(apiHandler.client, namespace, replicationController)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	response.WriteHeaderAndEntity(http.StatusCreated, result)
}
// 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)))
}
func (apiHandler *ApiHandler) handleGetPodContainers(request *restful.Request, response *restful.Response) {
	namespace := request.PathParameter("namespace")
	podId := request.PathParameter("pod")

	result, err := GetPodContainers(apiHandler.client, namespace, podId)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	response.WriteHeaderAndEntity(http.StatusCreated, result)
}
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)
}
Beispiel #25
0
func (a *Api) processMetricRequest(key string, request *restful.Request, response *restful.Response) {
	start, end, err := getStartEndTime(request)
	if err != nil {
		response.WriteError(http.StatusBadRequest, err)
		return
	}
	metricName := request.PathParameter("metric-name")
	convertedMetricName := convertMetricName(metricName)
	metrics := a.metricSink.GetMetric(convertedMetricName, []string{key}, start, end)
	converted := exportTimestampedMetricValue(metrics[key])
	response.WriteEntity(converted)
}
// Handles get pet set detail API call.
func (apiHandler *ApiHandler) handleGetPetSetDetail(request *restful.Request,
	response *restful.Response) {
	namespace := request.PathParameter("namespace")
	service := request.PathParameter("petset")
	result, err := petset.GetPetSetDetail(apiHandler.client, apiHandler.heapsterClient,
		namespace, service)
	if err != nil {
		handleInternalError(response, err)
		return
	}
	response.WriteHeaderAndEntity(http.StatusCreated, result)
}
// parseNamespacePathParameter parses namespace selector for list pages in path paramater.
// The namespace selector is a comma separated list of namespaces that are trimmed.
// No namespaces means "view all user namespaces", i.e., everything except kube-system.
func parseNamespacePathParameter(request *restful.Request) *common.NamespaceQuery {
	namespace := request.PathParameter("namespace")
	namespaces := strings.Split(namespace, ",")
	var nonEmptyNamespaces []string
	for _, n := range namespaces {
		n = strings.Trim(n, " ")
		if len(n) > 0 {
			nonEmptyNamespaces = append(nonEmptyNamespaces, n)
		}
	}
	return common.NewNamespaceQuery(nonEmptyNamespaces)
}
Beispiel #28
0
// Handles get Replica Set detail API call.
func (apiHandler *ApiHandler) handleGetReplicaSetDetail(
	request *restful.Request, response *restful.Response) {

	namespace := request.PathParameter("namespace")
	replicaSet := request.PathParameter("replicaSet")
	result, err := GetReplicaSetDetail(apiHandler.client, namespace, replicaSet)
	if err != nil {
		handleInternalError(response, err)
		return
	}

	response.WriteHeaderAndEntity(http.StatusCreated, result)
}
Beispiel #29
0
// Handles delete Replication Controller API call.
func (apiHandler *ApiHandler) handleDeleteReplicationController(
	request *restful.Request, response *restful.Response) {

	namespace := request.PathParameter("namespace")
	replicationController := request.PathParameter("replicationController")

	if err := DeleteReplicationControllerWithPods(apiHandler.client, namespace, replicationController); err != nil {
		handleInternalError(response, err)
		return
	}

	response.WriteHeader(http.StatusOK)
}
// namespaceStats returns a map of StatBundles for each usage metric of a Namespace entity.
func (a *Api) namespaceStats(request *restful.Request, response *restful.Response) {
	model := a.manager.GetModel()
	if model == nil {
		response.WriteError(400, errModelNotActivated)
	}
	res, uptime, err := model.GetNamespaceStats(model_api.NamespaceRequest{
		NamespaceName: request.PathParameter("namespace-name"),
	})
	if err != nil {
		response.WriteError(400, err)
	}
	response.WriteEntity(exportStatBundle(res, uptime))
}