2023-07-24 00:16:29 +00:00
package kubernetes
import (
"net/http"
2023-10-13 00:43:36 +00:00
"github.com/portainer/portainer/api/http/middlewares"
2023-09-01 22:27:02 +00:00
httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response"
2024-10-01 01:15:51 +00:00
"github.com/rs/zerolog/log"
2023-09-01 22:27:02 +00:00
2023-07-24 00:16:29 +00:00
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
2024-10-01 01:15:51 +00:00
// @id GetKubernetesMetricsForAllNodes
2023-07-24 00:16:29 +00:00
// @summary Get a list of nodes with their live metrics
2024-10-01 01:15:51 +00:00
// @description Get a list of metrics associated with all nodes of a cluster.
// @description **Access policy**: Authenticated user.
2023-07-24 00:16:29 +00:00
// @tags kubernetes
2024-10-01 01:15:51 +00:00
// @security ApiKeyAuth || jwt
2023-07-24 00:16:29 +00:00
// @produce json
2024-10-01 01:15:51 +00:00
// @param id path int true "Environment identifier"
2023-07-24 00:16:29 +00:00
// @success 200 {object} v1beta1.NodeMetricsList "Success"
2024-10-01 01:15:51 +00:00
// @failure 401 "Unauthorized access - the user is not authenticated or does not have the necessary permissions. Ensure that you have provided a valid API key or JWT token, and that you have the required permissions."
// @failure 500 "Server error occurred while attempting to retrieve the list of nodes with their live metrics."
2023-07-24 00:16:29 +00:00
// @router /kubernetes/{id}/metrics/nodes [get]
func ( handler * Handler ) getKubernetesMetricsForAllNodes ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
2023-10-13 00:43:36 +00:00
endpoint , err := middlewares . FetchEndpoint ( r )
2023-07-24 00:16:29 +00:00
if err != nil {
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( err . Error ( ) , err )
2023-07-24 00:16:29 +00:00
}
cli , err := handler . KubernetesClientFactory . CreateRemoteMetricsClient ( endpoint )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForAllNodes" ) . Msg ( "Failed to create metrics KubeClient" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( "failed to create metrics KubeClient" , nil )
2023-07-24 00:16:29 +00:00
}
metrics , err := cli . MetricsV1beta1 ( ) . NodeMetricses ( ) . List ( r . Context ( ) , v1 . ListOptions { } )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForAllNodes" ) . Msg ( "Failed to fetch metrics" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( "Failed to fetch metrics" , err )
2023-07-24 00:16:29 +00:00
}
return response . JSON ( w , metrics )
}
2024-10-01 01:15:51 +00:00
// @id GetKubernetesMetricsForNode
2023-07-24 00:16:29 +00:00
// @summary Get live metrics for a node
2024-10-01 01:15:51 +00:00
// @description Get live metrics for the specified node.
// @description **Access policy**: Authenticated user.
2023-07-24 00:16:29 +00:00
// @tags kubernetes
2024-10-01 01:15:51 +00:00
// @security ApiKeyAuth || jwt
2023-07-24 00:16:29 +00:00
// @produce json
2024-10-01 01:15:51 +00:00
// @param id path int true "Environment identifier"
2023-07-24 00:16:29 +00:00
// @param name path string true "Node identifier"
// @success 200 {object} v1beta1.NodeMetrics "Success"
2024-10-01 01:15:51 +00:00
// @failure 400 "Invalid request payload, such as missing required fields or fields not meeting validation criteria."
// @failure 401 "Unauthorized access - the user is not authenticated or does not have the necessary permissions. Ensure that you have provided a valid API key or JWT token, and that you have the required permissions."
// @failure 500 "Server error occurred while attempting to retrieve the live metrics for the specified node."
2023-07-24 00:16:29 +00:00
// @router /kubernetes/{id}/metrics/nodes/{name} [get]
func ( handler * Handler ) getKubernetesMetricsForNode ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
2023-10-13 00:43:36 +00:00
endpoint , err := middlewares . FetchEndpoint ( r )
2023-07-24 00:16:29 +00:00
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForNode" ) . Msg ( "Failed to fetch endpoint" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( err . Error ( ) , err )
2023-07-24 00:16:29 +00:00
}
cli , err := handler . KubernetesClientFactory . CreateRemoteMetricsClient ( endpoint )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForNode" ) . Msg ( "Failed to create metrics KubeClient" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( "failed to create metrics KubeClient" , nil )
2023-07-24 00:16:29 +00:00
}
nodeName , err := request . RetrieveRouteVariableValue ( r , "name" )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForNode" ) . Msg ( "Invalid node identifier route variable" )
2023-10-13 00:43:36 +00:00
return httperror . BadRequest ( "Invalid node identifier route variable" , err )
2023-07-24 00:16:29 +00:00
}
metrics , err := cli . MetricsV1beta1 ( ) . NodeMetricses ( ) . Get (
r . Context ( ) ,
nodeName ,
v1 . GetOptions { } ,
)
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForNode" ) . Msg ( "Failed to fetch metrics" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( "Failed to fetch metrics" , err )
2023-07-24 00:16:29 +00:00
}
return response . JSON ( w , metrics )
}
2024-10-01 01:15:51 +00:00
// @id GetKubernetesMetricsForAllPods
2023-07-24 00:16:29 +00:00
// @summary Get a list of pods with their live metrics
2024-10-01 01:15:51 +00:00
// @description Get a list of pods with their live metrics for the specified namespace.
// @description **Access policy**: Authenticated user.
2023-07-24 00:16:29 +00:00
// @tags kubernetes
2024-10-01 01:15:51 +00:00
// @security ApiKeyAuth || jwt
2023-07-24 00:16:29 +00:00
// @produce json
2024-10-01 01:15:51 +00:00
// @param id path int true "Environment identifier"
2023-07-24 00:16:29 +00:00
// @param namespace path string true "Namespace"
// @success 200 {object} v1beta1.PodMetricsList "Success"
2024-10-01 01:15:51 +00:00
// @failure 400 "Invalid request payload, such as missing required fields or fields not meeting validation criteria."
// @failure 401 "Unauthorized access - the user is not authenticated or does not have the necessary permissions. Ensure that you have provided a valid API key or JWT token, and that you have the required permissions."
// @failure 500 "Server error occurred while attempting to retrieve the list of pods with their live metrics."
2023-07-24 00:16:29 +00:00
// @router /kubernetes/{id}/metrics/pods/{namespace} [get]
func ( handler * Handler ) getKubernetesMetricsForAllPods ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
2023-10-13 00:43:36 +00:00
endpoint , err := middlewares . FetchEndpoint ( r )
2023-07-24 00:16:29 +00:00
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForAllPods" ) . Msg ( "Failed to fetch endpoint" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( err . Error ( ) , err )
2023-07-24 00:16:29 +00:00
}
cli , err := handler . KubernetesClientFactory . CreateRemoteMetricsClient ( endpoint )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForAllPods" ) . Msg ( "Failed to create metrics KubeClient" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( "failed to create metrics KubeClient" , nil )
2023-07-24 00:16:29 +00:00
}
namespace , err := request . RetrieveRouteVariableValue ( r , "namespace" )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForAllPods" ) . Msg ( "Invalid namespace identifier route variable" )
2023-10-13 00:43:36 +00:00
return httperror . BadRequest ( "Invalid namespace identifier route variable" , err )
2023-07-24 00:16:29 +00:00
}
metrics , err := cli . MetricsV1beta1 ( ) . PodMetricses ( namespace ) . List ( r . Context ( ) , v1 . ListOptions { } )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForAllPods" ) . Msg ( "Failed to fetch metrics" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( "Failed to fetch metrics" , err )
2023-07-24 00:16:29 +00:00
}
return response . JSON ( w , metrics )
}
2024-10-01 01:15:51 +00:00
// @id GetKubernetesMetricsForPod
2023-07-24 00:16:29 +00:00
// @summary Get live metrics for a pod
2024-10-01 01:15:51 +00:00
// @description Get live metrics for the specified pod.
// @description **Access policy**: Authenticated user.
2023-07-24 00:16:29 +00:00
// @tags kubernetes
2024-10-01 01:15:51 +00:00
// @security ApiKeyAuth || jwt
2023-07-24 00:16:29 +00:00
// @produce json
2024-10-01 01:15:51 +00:00
// @param id path int true "Environment identifier"
2023-07-24 00:16:29 +00:00
// @param namespace path string true "Namespace"
// @param name path string true "Pod identifier"
// @success 200 {object} v1beta1.PodMetrics "Success"
2024-10-01 01:15:51 +00:00
// @failure 400 "Invalid request payload, such as missing required fields or fields not meeting validation criteria."
// @failure 401 "Unauthorized access - the user is not authenticated or does not have the necessary permissions. Ensure that you have provided a valid API key or JWT token, and that you have the required permissions."
// @failure 500 "Server error occurred while attempting to retrieve the live metrics for the specified pod."
2023-07-24 00:16:29 +00:00
// @router /kubernetes/{id}/metrics/pods/{namespace}/{name} [get]
func ( handler * Handler ) getKubernetesMetricsForPod ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
2023-10-13 00:43:36 +00:00
endpoint , err := middlewares . FetchEndpoint ( r )
2023-07-24 00:16:29 +00:00
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForPod" ) . Msg ( "Failed to fetch endpoint" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( err . Error ( ) , err )
2023-07-24 00:16:29 +00:00
}
cli , err := handler . KubernetesClientFactory . CreateRemoteMetricsClient ( endpoint )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForPod" ) . Msg ( "Failed to create metrics KubeClient" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( "failed to create metrics KubeClient" , nil )
2023-07-24 00:16:29 +00:00
}
namespace , err := request . RetrieveRouteVariableValue ( r , "namespace" )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForPod" ) . Msg ( "Invalid namespace identifier route variable" )
2023-10-13 00:43:36 +00:00
return httperror . BadRequest ( "Invalid namespace identifier route variable" , err )
2023-07-24 00:16:29 +00:00
}
podName , err := request . RetrieveRouteVariableValue ( r , "name" )
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForPod" ) . Msg ( "Invalid pod identifier route variable" )
2023-10-13 00:43:36 +00:00
return httperror . BadRequest ( "Invalid pod identifier route variable" , err )
2023-07-24 00:16:29 +00:00
}
2023-10-13 00:43:36 +00:00
metrics , err := cli . MetricsV1beta1 ( ) . PodMetricses ( namespace ) . Get ( r . Context ( ) , podName , v1 . GetOptions { } )
2023-07-24 00:16:29 +00:00
if err != nil {
2024-10-01 01:15:51 +00:00
log . Error ( ) . Err ( err ) . Str ( "context" , "getKubernetesMetricsForPod" ) . Str ( "namespace" , namespace ) . Str ( "pod" , podName ) . Msg ( "Failed to fetch metrics" )
2023-10-13 00:43:36 +00:00
return httperror . InternalServerError ( "Failed to fetch metrics" , err )
2023-07-24 00:16:29 +00:00
}
return response . JSON ( w , metrics )
}