diff --git a/custom-metrics-stackdriver-adapter/pkg/adapter/coreprovider/provider.go b/custom-metrics-stackdriver-adapter/pkg/adapter/coreprovider/provider.go index 5def1c23f..7602af88d 100644 --- a/custom-metrics-stackdriver-adapter/pkg/adapter/coreprovider/provider.go +++ b/custom-metrics-stackdriver-adapter/pkg/adapter/coreprovider/provider.go @@ -23,6 +23,7 @@ import ( corev1 "k8s.io/api/core/v1" apierr "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" apitypes "k8s.io/apimachinery/pkg/types" "k8s.io/klog" "k8s.io/metrics/pkg/apis/metrics" @@ -49,9 +50,83 @@ type coreClientInterface interface { getNodeRAM(nodesNames []string) (map[string]resource.Quantity, map[string]api.TimeInfo, error) } +func (p *CoreProvider) GetPodMetrics(pods ...*metav1.PartialObjectMetadata) ([]metrics.PodMetrics, error) { + resMetrics := make([]metrics.PodMetrics, 0, len(pods)) + + if len(pods) == 0 { + return resMetrics, nil + } + + podNames := []apitypes.NamespacedName{} + for _, pod := range pods { + podNames = append(podNames, apitypes.NamespacedName{ + Namespace: pod.Namespace, + Name: pod.Name, + }) + } + + timeInfos, containerMetrics, err := p.getPodMetrics(podNames...) + if err != nil { + return []metrics.PodMetrics{}, err + } + + for i, pod := range pods { + podMetric := metrics.PodMetrics{ + ObjectMeta: metav1.ObjectMeta{ + Name: pod.Name, + Namespace: pod.Namespace, + Labels: pod.Labels, + CreationTimestamp: metav1.Now(), + }, + Timestamp: metav1.NewTime(timeInfos[i].Timestamp), + Window: metav1.Duration{Duration: timeInfos[i].Window}, + } + + podMetric.Containers = containerMetrics[i] + + resMetrics = append(resMetrics, podMetric) + } + + return resMetrics, nil +} + +func (p *CoreProvider) GetNodeMetrics(nodes ...*corev1.Node) ([]metrics.NodeMetrics, error) { + resMetrics := make([]metrics.NodeMetrics, 0, len(nodes)) + if len(nodes) == 0 { + return resMetrics, nil + } + + nodeNames := []string{} + for _, node := range nodes { + nodeNames = append(nodeNames, node.Name) + } + + timeInfos, resourceList, err := p.getNodeMetrics(nodeNames...) + if err != nil { + return []metrics.NodeMetrics{}, err + } + + for i, node := range nodes { + resMetrics = append(resMetrics, metrics.NodeMetrics{ + ObjectMeta: metav1.ObjectMeta{ + Name: node.Name, + Labels: node.Labels, + CreationTimestamp: metav1.Now(), + }, + Usage: corev1.ResourceList{ + corev1.ResourceCPU: resourceList[i][corev1.ResourceCPU], + corev1.ResourceMemory: resourceList[i][corev1.ResourceMemory], + }, + Timestamp: metav1.NewTime(timeInfos[i].Timestamp), + Window: metav1.Duration{Duration: timeInfos[i].Window}, + }) + } + return resMetrics, nil +} + // GetPodMetrics implements the api.MetricsProvider interface. // If metrics from i-th pod are not present, ContainerMetrics[i] will be nil and TimeInfo[i] will be default TimeInfo value. -func (p *CoreProvider) GetPodMetrics(pods ...apitypes.NamespacedName) ([]api.TimeInfo, [][]metrics.ContainerMetrics, error) { +func (p *CoreProvider) getPodMetrics(pods ...apitypes.NamespacedName) ([]api.TimeInfo, [][]metrics.ContainerMetrics, error) { timeInfo := make([]api.TimeInfo, len(pods)) coreMetrics := make([][]metrics.ContainerMetrics, len(pods)) resourceNames := make([]string, len(pods)) @@ -109,7 +184,7 @@ func (p *CoreProvider) GetPodMetrics(pods ...apitypes.NamespacedName) ([]api.Tim // GetNodeMetrics implements the api.MetricsProvider interface. // If metrics from i-th node are not present, ResourceList[i] will be nil and TimeInfo[i] will be default TimeInfo value. -func (p *CoreProvider) GetNodeMetrics(nodes ...string) ([]api.TimeInfo, []corev1.ResourceList, error) { +func (p *CoreProvider) getNodeMetrics(nodes ...string) ([]api.TimeInfo, []corev1.ResourceList, error) { timeInfo := make([]api.TimeInfo, len(nodes)) coreMetrics := make([]corev1.ResourceList, len(nodes)) resourceNames := make([]string, len(nodes))