| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509 |
- package costmodel
- import (
- "context"
- "encoding/json"
- "fmt"
- "math"
- "net/http"
- "os"
- "sort"
- "strconv"
- "strings"
- "sync"
- "time"
- costAnalyzerCloud "github.com/kubecost/cost-model/cloud"
- prometheusClient "github.com/prometheus/client_golang/api"
- v1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/labels"
- "k8s.io/client-go/kubernetes"
- "k8s.io/klog"
- )
- const (
- statusAPIError = 422
- apiPrefix = "/api/v1"
- epAlertManagers = apiPrefix + "/alertmanagers"
- epQuery = apiPrefix + "/query"
- epQueryRange = apiPrefix + "/query_range"
- epLabelValues = apiPrefix + "/label/:name/values"
- epSeries = apiPrefix + "/series"
- epTargets = apiPrefix + "/targets"
- epSnapshot = apiPrefix + "/admin/tsdb/snapshot"
- epDeleteSeries = apiPrefix + "/admin/tsdb/delete_series"
- epCleanTombstones = apiPrefix + "/admin/tsdb/clean_tombstones"
- epConfig = apiPrefix + "/status/config"
- epFlags = apiPrefix + "/status/flags"
- clusterIDKey = "CLUSTER_ID"
- remoteEnabled = "REMOTE_WRITE_ENABLED"
- thanosEnabled = "THANOS_ENABLED"
- thanosQueryUrl = "THANOS_QUERY_URL"
- )
- type CostModel struct {
- Cache ClusterCache
- stop chan struct{}
- }
- func NewCostModel(client kubernetes.Interface) *CostModel {
- stopCh := make(chan struct{})
- cache := NewKubernetesClusterCache(client)
- cache.Run(stopCh)
- return &CostModel{
- Cache: cache,
- stop: stopCh,
- }
- }
- type CostData struct {
- Name string `json:"name,omitempty"`
- PodName string `json:"podName,omitempty"`
- NodeName string `json:"nodeName,omitempty"`
- NodeData *costAnalyzerCloud.Node `json:"node,omitempty"`
- Namespace string `json:"namespace,omitempty"`
- Deployments []string `json:"deployments,omitempty"`
- Services []string `json:"services,omitempty"`
- Daemonsets []string `json:"daemonsets,omitempty"`
- Statefulsets []string `json:"statefulsets,omitempty"`
- Jobs []string `json:"jobs,omitempty"`
- RAMReq []*Vector `json:"ramreq,omitempty"`
- RAMUsed []*Vector `json:"ramused,omitempty"`
- RAMAllocation []*Vector `json:"ramallocated,omitempty"`
- CPUReq []*Vector `json:"cpureq,omitempty"`
- CPUUsed []*Vector `json:"cpuused,omitempty"`
- CPUAllocation []*Vector `json:"cpuallocated,omitempty"`
- GPUReq []*Vector `json:"gpureq,omitempty"`
- PVCData []*PersistentVolumeClaimData `json:"pvcData,omitempty"`
- NetworkData []*Vector `json:"network,omitempty"`
- Labels map[string]string `json:"labels,omitempty"`
- NamespaceLabels map[string]string `json:"namespaceLabels,omitempty"`
- ClusterID string `json:"clusterId"`
- }
- func (cd *CostData) String() string {
- return fmt.Sprintf("\n\tName: %s; PodName: %s, NodeName: %s\n\tNamespace: %s\n\tDeployments: %s\n\tServices: %s\n\tCPU (req, used, alloc): %d, %d, %d\n\tRAM (req, used, alloc): %d, %d, %d",
- cd.Name, cd.PodName, cd.NodeName, cd.Namespace, strings.Join(cd.Deployments, ", "), strings.Join(cd.Services, ", "),
- len(cd.CPUReq), len(cd.CPUUsed), len(cd.CPUAllocation),
- len(cd.RAMReq), len(cd.RAMUsed), len(cd.RAMAllocation))
- }
- type Vector struct {
- Timestamp float64 `json:"timestamp"`
- Value float64 `json:"value"`
- }
- const (
- queryRAMRequestsStr = `avg(
- label_replace(
- label_replace(
- avg(
- count_over_time(kube_pod_container_resource_requests_memory_bytes{container!="",container!="POD", node!=""}[%s] %s)
- *
- avg_over_time(kube_pod_container_resource_requests_memory_bytes{container!="",container!="POD", node!=""}[%s] %s)
- ) by (namespace,container,pod,node,cluster_id) , "container_name","$1","container","(.+)"
- ), "pod_name","$1","pod","(.+)"
- )
- ) by (namespace,container_name,pod_name,node,cluster_id)`
- queryRAMUsageStr = `sort_desc(
- avg(
- label_replace(count_over_time(container_memory_working_set_bytes{container_name!="",container_name!="POD", instance!=""}[%s] %s), "node", "$1", "instance","(.+)")
- *
- label_replace(avg_over_time(container_memory_working_set_bytes{container_name!="",container_name!="POD", instance!=""}[%s] %s), "node", "$1", "instance","(.+)")
- ) by (namespace,container_name,pod_name,node,cluster_id)
- )`
- queryCPURequestsStr = `avg(
- label_replace(
- label_replace(
- avg(
- count_over_time(kube_pod_container_resource_requests_cpu_cores{container!="",container!="POD", node!=""}[%s] %s)
- *
- avg_over_time(kube_pod_container_resource_requests_cpu_cores{container!="",container!="POD", node!=""}[%s] %s)
- ) by (namespace,container,pod,node,cluster_id) , "container_name","$1","container","(.+)"
- ), "pod_name","$1","pod","(.+)"
- )
- ) by (namespace,container_name,pod_name,node,cluster_id)`
- queryCPUUsageStr = `avg(
- label_replace(
- rate(
- container_cpu_usage_seconds_total{container_name!="",container_name!="POD",instance!=""}[%s] %s
- ) , "node", "$1", "instance", "(.+)"
- )
- ) by (namespace,container_name,pod_name,node,cluster_id)`
- queryGPURequestsStr = `avg(
- label_replace(
- label_replace(
- avg(
- count_over_time(kube_pod_container_resource_requests{resource="nvidia_com_gpu", container!="",container!="POD", node!=""}[%s] %s)
- *
- avg_over_time(kube_pod_container_resource_requests{resource="nvidia_com_gpu", container!="",container!="POD", node!=""}[%s] %s)
- ) by (namespace,container,pod,node,cluster_id) , "container_name","$1","container","(.+)"
- ), "pod_name","$1","pod","(.+)"
- )
- ) by (namespace,container_name,pod_name,node,cluster_id)`
- queryPVRequestsStr = `avg(kube_persistentvolumeclaim_info) by (persistentvolumeclaim, storageclass, namespace, volumename, cluster_id)
- *
- on (persistentvolumeclaim, namespace, cluster_id) group_right(storageclass, volumename)
- sum(kube_persistentvolumeclaim_resource_requests_storage_bytes) by (persistentvolumeclaim, namespace, cluster_id)`
- queryPVCAllocation = `avg_over_time(pod_pvc_allocation[%s])`
- queryPVHourlyCost = `avg_over_time(pv_hourly_cost[%s])`
- queryNSLabels = `avg_over_time(kube_namespace_labels[%s])`
- queryPodLabels = `avg_over_time(kube_pod_labels[%s])`
- queryDeploymentLabels = `avg_over_time(deployment_match_labels[%s])`
- queryServiceLabels = `avg_over_time(service_selector_labels[%s])`
- queryZoneNetworkUsage = `sum(increase(kubecost_pod_network_egress_bytes_total{internet="false", sameZone="false", sameRegion="true"}[%s] %s)) by (namespace,pod_name,cluster_id) / 1024 / 1024 / 1024`
- queryRegionNetworkUsage = `sum(increase(kubecost_pod_network_egress_bytes_total{internet="false", sameZone="false", sameRegion="false"}[%s] %s)) by (namespace,pod_name,cluster_id) / 1024 / 1024 / 1024`
- queryInternetNetworkUsage = `sum(increase(kubecost_pod_network_egress_bytes_total{internet="true"}[%s] %s)) by (namespace,pod_name,cluster_id) / 1024 / 1024 / 1024`
- normalizationStr = `max(count_over_time(kube_pod_container_resource_requests_memory_bytes{}[%s] %s))`
- )
- type PrometheusMetadata struct {
- Running bool `json:"running"`
- KubecostDataExists bool `json:"kubecostDataExists"`
- }
- // ValidatePrometheus tells the model what data prometheus has on it.
- func ValidatePrometheus(cli prometheusClient.Client, isThanos bool) (*PrometheusMetadata, error) {
- q := "up"
- if isThanos {
- q += " offset 3h"
- }
- data, err := Query(cli, q)
- if err != nil {
- return &PrometheusMetadata{
- Running: false,
- KubecostDataExists: false,
- }, err
- }
- v, kcmetrics, err := getUptimeData(data)
- if err != nil {
- return &PrometheusMetadata{
- Running: false,
- KubecostDataExists: false,
- }, err
- }
- if len(v) > 0 {
- return &PrometheusMetadata{
- Running: true,
- KubecostDataExists: kcmetrics,
- }, nil
- } else {
- return &PrometheusMetadata{
- Running: false,
- KubecostDataExists: false,
- }, fmt.Errorf("No running jobs found on Prometheus at %s", cli.URL(epQuery, nil).Path)
- }
- }
- func getUptimeData(qr interface{}) ([]*Vector, bool, error) {
- data, ok := qr.(map[string]interface{})["data"]
- if !ok {
- e, err := wrapPrometheusError(qr)
- if err != nil {
- return nil, false, err
- }
- return nil, false, fmt.Errorf(e)
- }
- r, ok := data.(map[string]interface{})["result"]
- if !ok {
- return nil, false, fmt.Errorf("Improperly formatted data from prometheus, data has no result field")
- }
- results, ok := r.([]interface{})
- if !ok {
- return nil, false, fmt.Errorf("Improperly formatted results from prometheus, result field is not a slice")
- }
- jobData := []*Vector{}
- kubecostMetrics := false
- for _, val := range results {
- // For now, just do this for validation. TODO: This can be parsed to figure out the exact running jobs.
- metrics, ok := val.(map[string]interface{})["metric"].(map[string]interface{})
- if !ok {
- return nil, false, fmt.Errorf("Prometheus vector does not have metric labels")
- }
- jobname, ok := metrics["job"]
- if !ok {
- return nil, false, fmt.Errorf("up query does not have job names")
- }
- if jobname == "kubecost" {
- kubecostMetrics = true
- }
- value, ok := val.(map[string]interface{})["value"]
- if !ok {
- return nil, false, fmt.Errorf("Improperly formatted results from prometheus, value is not a field in the vector")
- }
- dataPoint, ok := value.([]interface{})
- if !ok || len(dataPoint) != 2 {
- return nil, false, fmt.Errorf("Improperly formatted datapoint from Prometheus")
- }
- strVal := dataPoint[1].(string)
- v, _ := strconv.ParseFloat(strVal, 64)
- toReturn := &Vector{
- Timestamp: dataPoint[0].(float64),
- Value: v,
- }
- jobData = append(jobData, toReturn)
- }
- return jobData, kubecostMetrics, nil
- }
- func ComputeUptimes(cli prometheusClient.Client) (map[string]float64, error) {
- res, err := Query(cli, `container_start_time_seconds{container_name != "POD",container_name != ""}`)
- if err != nil {
- return nil, err
- }
- vectors, err := GetContainerMetricVector(res, false, 0, os.Getenv(clusterIDKey))
- if err != nil {
- return nil, err
- }
- results := make(map[string]float64)
- for key, vector := range vectors {
- if err != nil {
- return nil, err
- }
- val := vector[0].Value
- uptime := time.Now().Sub(time.Unix(int64(val), 0)).Seconds()
- results[key] = uptime
- }
- return results, nil
- }
- func (cm *CostModel) ComputeCostData(cli prometheusClient.Client, clientset kubernetes.Interface, cp costAnalyzerCloud.Provider, window string, offset string, filterNamespace string) (map[string]*CostData, error) {
- queryRAMRequests := fmt.Sprintf(queryRAMRequestsStr, window, offset, window, offset)
- queryRAMUsage := fmt.Sprintf(queryRAMUsageStr, window, offset, window, offset)
- queryCPURequests := fmt.Sprintf(queryCPURequestsStr, window, offset, window, offset)
- queryCPUUsage := fmt.Sprintf(queryCPUUsageStr, window, offset)
- queryGPURequests := fmt.Sprintf(queryGPURequestsStr, window, offset, window, offset)
- queryPVRequests := fmt.Sprintf(queryPVRequestsStr)
- queryNetZoneRequests := fmt.Sprintf(queryZoneNetworkUsage, window, "")
- queryNetRegionRequests := fmt.Sprintf(queryRegionNetworkUsage, window, "")
- queryNetInternetRequests := fmt.Sprintf(queryInternetNetworkUsage, window, "")
- normalization := fmt.Sprintf(normalizationStr, window, offset)
- // Cluster ID is specific to the source cluster
- clusterID := os.Getenv(clusterIDKey)
- var wg sync.WaitGroup
- wg.Add(11)
- var promErr error
- var resultRAMRequests interface{}
- go func() {
- resultRAMRequests, promErr = Query(cli, queryRAMRequests)
- defer wg.Done()
- }()
- var resultRAMUsage interface{}
- go func() {
- resultRAMUsage, promErr = Query(cli, queryRAMUsage)
- defer wg.Done()
- }()
- var resultCPURequests interface{}
- go func() {
- resultCPURequests, promErr = Query(cli, queryCPURequests)
- defer wg.Done()
- }()
- var resultCPUUsage interface{}
- go func() {
- resultCPUUsage, promErr = Query(cli, queryCPUUsage)
- defer wg.Done()
- }()
- var resultGPURequests interface{}
- go func() {
- resultGPURequests, promErr = Query(cli, queryGPURequests)
- defer wg.Done()
- }()
- var resultPVRequests interface{}
- go func() {
- resultPVRequests, promErr = Query(cli, queryPVRequests)
- defer wg.Done()
- }()
- var resultNetZoneRequests interface{}
- go func() {
- resultNetZoneRequests, promErr = Query(cli, queryNetZoneRequests)
- defer wg.Done()
- }()
- var resultNetRegionRequests interface{}
- go func() {
- resultNetRegionRequests, promErr = Query(cli, queryNetRegionRequests)
- defer wg.Done()
- }()
- var resultNetInternetRequests interface{}
- go func() {
- resultNetInternetRequests, promErr = Query(cli, queryNetInternetRequests)
- defer wg.Done()
- }()
- var normalizationResult interface{}
- go func() {
- normalizationResult, promErr = Query(cli, normalization)
- defer wg.Done()
- }()
- podDeploymentsMapping := make(map[string]map[string][]string)
- podServicesMapping := make(map[string]map[string][]string)
- namespaceLabelsMapping := make(map[string]map[string]string)
- podlist := cm.Cache.GetAllPods()
- var k8sErr error
- go func() {
- defer wg.Done()
- podDeploymentsMapping, k8sErr = getPodDeployments(cm.Cache, podlist, clusterID)
- if k8sErr != nil {
- return
- }
- podServicesMapping, k8sErr = getPodServices(cm.Cache, podlist, clusterID)
- if k8sErr != nil {
- return
- }
- namespaceLabelsMapping, k8sErr = getNamespaceLabels(cm.Cache, clusterID)
- if k8sErr != nil {
- return
- }
- }()
- wg.Wait()
- if promErr != nil {
- return nil, fmt.Errorf("Error querying prometheus: %s", promErr.Error())
- }
- if k8sErr != nil {
- return nil, fmt.Errorf("Error querying the kubernetes api: %s", k8sErr.Error())
- }
- normalizationValue, err := getNormalization(normalizationResult)
- if err != nil {
- return nil, fmt.Errorf("Error parsing normalization values: " + err.Error())
- }
- nodes, err := getNodeCost(cm.Cache, cp)
- if err != nil {
- klog.V(1).Infof("Warning, no Node cost model available: " + err.Error())
- return nil, err
- }
- pvClaimMapping, err := getPVInfoVector(resultPVRequests, clusterID)
- if err != nil {
- klog.Infof("Unable to get PV Data: %s", err.Error())
- }
- if pvClaimMapping != nil {
- err = addPVData(cm.Cache, pvClaimMapping, cp)
- if err != nil {
- return nil, err
- }
- }
- networkUsageMap, err := GetNetworkUsageData(resultNetZoneRequests, resultNetRegionRequests, resultNetInternetRequests, clusterID)
- if err != nil {
- klog.V(1).Infof("Unable to get Network Cost Data: %s", err.Error())
- networkUsageMap = make(map[string]*NetworkUsageData)
- }
- containerNameCost := make(map[string]*CostData)
- containers := make(map[string]bool)
- RAMReqMap, err := GetContainerMetricVector(resultRAMRequests, true, normalizationValue, clusterID)
- if err != nil {
- return nil, err
- }
- for key := range RAMReqMap {
- containers[key] = true
- }
- RAMUsedMap, err := GetContainerMetricVector(resultRAMUsage, true, normalizationValue, clusterID)
- if err != nil {
- return nil, err
- }
- for key := range RAMUsedMap {
- containers[key] = true
- }
- CPUReqMap, err := GetContainerMetricVector(resultCPURequests, true, normalizationValue, clusterID)
- if err != nil {
- return nil, err
- }
- for key := range CPUReqMap {
- containers[key] = true
- }
- GPUReqMap, err := GetContainerMetricVector(resultGPURequests, true, normalizationValue, clusterID)
- if err != nil {
- return nil, err
- }
- for key := range GPUReqMap {
- containers[key] = true
- }
- CPUUsedMap, err := GetContainerMetricVector(resultCPUUsage, false, 0, clusterID) // No need to normalize here, as this comes from a counter
- if err != nil {
- return nil, err
- }
- for key := range CPUUsedMap {
- containers[key] = true
- }
- currentContainers := make(map[string]v1.Pod)
- for _, pod := range podlist {
- if pod.Status.Phase != v1.PodRunning {
- continue
- }
- cs, err := newContainerMetricsFromPod(*pod, clusterID)
- if err != nil {
- return nil, err
- }
- for _, c := range cs {
- containers[c.Key()] = true // captures any containers that existed for a time < a prometheus scrape interval. We currently charge 0 for this but should charge something.
- currentContainers[c.Key()] = *pod
- }
- }
- missingNodes := make(map[string]*costAnalyzerCloud.Node)
- missingContainers := make(map[string]*CostData)
- for key := range containers {
- if _, ok := containerNameCost[key]; ok {
- continue // because ordering is important for the allocation model (all PV's applied to the first), just dedupe if it's already been added.
- }
- if pod, ok := currentContainers[key]; ok {
- podName := pod.GetObjectMeta().GetName()
- ns := pod.GetObjectMeta().GetNamespace()
- nsLabels := namespaceLabelsMapping[ns+","+clusterID]
- podLabels := pod.GetObjectMeta().GetLabels()
- if podLabels == nil {
- podLabels = make(map[string]string)
- }
- for k, v := range nsLabels {
- podLabels[k] = v
- }
- nodeName := pod.Spec.NodeName
- var nodeData *costAnalyzerCloud.Node
- if _, ok := nodes[nodeName]; ok {
- nodeData = nodes[nodeName]
- }
- nsKey := ns + "," + clusterID
- var podDeployments []string
- if _, ok := podDeploymentsMapping[nsKey]; ok {
- if ds, ok := podDeploymentsMapping[nsKey][pod.GetObjectMeta().GetName()]; ok {
- podDeployments = ds
- } else {
- podDeployments = []string{}
- }
- }
- var podPVs []*PersistentVolumeClaimData
- podClaims := pod.Spec.Volumes
- for _, vol := range podClaims {
- if vol.PersistentVolumeClaim != nil {
- name := vol.PersistentVolumeClaim.ClaimName
- if pvClaim, ok := pvClaimMapping[ns+","+name+","+clusterID]; ok {
- podPVs = append(podPVs, pvClaim)
- }
- }
- }
- var podNetCosts []*Vector
- if usage, ok := networkUsageMap[ns+","+podName+","+clusterID]; ok {
- netCosts, err := GetNetworkCost(usage, cp)
- if err != nil {
- klog.V(3).Infof("Error pulling network costs: %s", err.Error())
- } else {
- podNetCosts = netCosts
- }
- }
- var podServices []string
- if _, ok := podServicesMapping[nsKey]; ok {
- if svcs, ok := podServicesMapping[nsKey][pod.GetObjectMeta().GetName()]; ok {
- podServices = svcs
- } else {
- podServices = []string{}
- }
- }
- for i, container := range pod.Spec.Containers {
- containerName := container.Name
- // recreate the key and look up data for this container
- newKey := newContainerMetricFromValues(ns, podName, containerName, pod.Spec.NodeName, clusterID).Key()
- RAMReqV, ok := RAMReqMap[newKey]
- if !ok {
- klog.V(4).Info("no RAM requests for " + newKey)
- RAMReqV = []*Vector{&Vector{}}
- }
- RAMUsedV, ok := RAMUsedMap[newKey]
- if !ok {
- klog.V(4).Info("no RAM usage for " + newKey)
- RAMUsedV = []*Vector{&Vector{}}
- }
- CPUReqV, ok := CPUReqMap[newKey]
- if !ok {
- klog.V(4).Info("no CPU requests for " + newKey)
- CPUReqV = []*Vector{&Vector{}}
- }
- GPUReqV, ok := GPUReqMap[newKey]
- if !ok {
- klog.V(4).Info("no GPU requests for " + newKey)
- GPUReqV = []*Vector{&Vector{}}
- }
- CPUUsedV, ok := CPUUsedMap[newKey]
- if !ok {
- klog.V(4).Info("no CPU usage for " + newKey)
- CPUUsedV = []*Vector{&Vector{}}
- }
- var pvReq []*PersistentVolumeClaimData
- var netReq []*Vector
- if i == 0 { // avoid duplicating by just assigning all claims to the first container.
- pvReq = podPVs
- netReq = podNetCosts
- }
- costs := &CostData{
- Name: containerName,
- PodName: podName,
- NodeName: nodeName,
- Namespace: ns,
- Deployments: podDeployments,
- Services: podServices,
- Daemonsets: getDaemonsetsOfPod(pod),
- Jobs: getJobsOfPod(pod),
- Statefulsets: getStatefulSetsOfPod(pod),
- NodeData: nodeData,
- RAMReq: RAMReqV,
- RAMUsed: RAMUsedV,
- CPUReq: CPUReqV,
- CPUUsed: CPUUsedV,
- GPUReq: GPUReqV,
- PVCData: pvReq,
- NetworkData: netReq,
- Labels: podLabels,
- NamespaceLabels: nsLabels,
- ClusterID: clusterID,
- }
- costs.CPUAllocation = getContainerAllocation(costs.CPUReq, costs.CPUUsed)
- costs.RAMAllocation = getContainerAllocation(costs.RAMReq, costs.RAMUsed)
- if filterNamespace == "" {
- containerNameCost[newKey] = costs
- } else if costs.Namespace == filterNamespace {
- containerNameCost[newKey] = costs
- }
- }
- } else {
- // The container has been deleted. Not all information is sent to prometheus via ksm, so fill out what we can without k8s api
- klog.V(4).Info("The container " + key + " has been deleted. Calculating allocation but resulting object will be missing data.")
- c, err := NewContainerMetricFromKey(key)
- if err != nil {
- return nil, err
- }
- RAMReqV, ok := RAMReqMap[key]
- if !ok {
- klog.V(4).Info("no RAM requests for " + key)
- RAMReqV = []*Vector{&Vector{}}
- }
- RAMUsedV, ok := RAMUsedMap[key]
- if !ok {
- klog.V(4).Info("no RAM usage for " + key)
- RAMUsedV = []*Vector{&Vector{}}
- }
- CPUReqV, ok := CPUReqMap[key]
- if !ok {
- klog.V(4).Info("no CPU requests for " + key)
- CPUReqV = []*Vector{&Vector{}}
- }
- GPUReqV, ok := GPUReqMap[key]
- if !ok {
- klog.V(4).Info("no GPU requests for " + key)
- GPUReqV = []*Vector{&Vector{}}
- }
- CPUUsedV, ok := CPUUsedMap[key]
- if !ok {
- klog.V(4).Info("no CPU usage for " + key)
- CPUUsedV = []*Vector{&Vector{}}
- }
- node, ok := nodes[c.NodeName]
- if !ok {
- klog.V(2).Infof("Node \"%s\" has been deleted from Kubernetes. Query historical data to get it.", c.NodeName)
- if n, ok := missingNodes[c.NodeName]; ok {
- node = n
- } else {
- node = &costAnalyzerCloud.Node{}
- missingNodes[c.NodeName] = node
- }
- }
- namespacelabels, ok := namespaceLabelsMapping[c.Namespace+","+c.ClusterID]
- if !ok {
- klog.V(3).Infof("Missing data for namespace %s", c.Namespace)
- }
- costs := &CostData{
- Name: c.ContainerName,
- PodName: c.PodName,
- NodeName: c.NodeName,
- NodeData: node,
- Namespace: c.Namespace,
- RAMReq: RAMReqV,
- RAMUsed: RAMUsedV,
- CPUReq: CPUReqV,
- CPUUsed: CPUUsedV,
- GPUReq: GPUReqV,
- NamespaceLabels: namespacelabels,
- ClusterID: c.ClusterID,
- }
- costs.CPUAllocation = getContainerAllocation(costs.CPUReq, costs.CPUUsed)
- costs.RAMAllocation = getContainerAllocation(costs.RAMReq, costs.RAMUsed)
- if filterNamespace == "" {
- containerNameCost[key] = costs
- missingContainers[key] = costs
- } else if costs.Namespace == filterNamespace {
- containerNameCost[key] = costs
- missingContainers[key] = costs
- }
- }
- }
- err = findDeletedNodeInfo(cli, missingNodes, window)
- if err != nil {
- klog.V(1).Infof("Error fetching historical node data: %s", err.Error())
- }
- err = findDeletedPodInfo(cli, missingContainers, window)
- if err != nil {
- klog.V(1).Infof("Error fetching historical pod data: %s", err.Error())
- }
- return containerNameCost, err
- }
- func findDeletedPodInfo(cli prometheusClient.Client, missingContainers map[string]*CostData, window string) error {
- if len(missingContainers) > 0 {
- queryHistoricalPodLabels := fmt.Sprintf(`kube_pod_labels{}[%s]`, window)
- podLabelsResult, err := Query(cli, queryHistoricalPodLabels)
- if err != nil {
- klog.V(1).Infof("Error parsing historical labels: %s", err.Error())
- }
- podLabels := make(map[string]map[string]string)
- if podLabelsResult != nil {
- podLabels, err = labelsFromPrometheusQuery(podLabelsResult)
- if err != nil {
- klog.V(1).Infof("Error parsing historical labels: %s", err.Error())
- }
- }
- for key, costData := range missingContainers {
- cm, _ := NewContainerMetricFromKey(key)
- labels, ok := podLabels[cm.PodName]
- if !ok {
- klog.V(1).Infof("Unable to find historical data for pod '%s'", cm.PodName)
- labels = make(map[string]string)
- }
- for k, v := range costData.NamespaceLabels {
- labels[k] = v
- }
- costData.Labels = labels
- }
- }
- return nil
- }
- func labelsFromPrometheusQuery(qr interface{}) (map[string]map[string]string, error) {
- toReturn := make(map[string]map[string]string)
- data, ok := qr.(map[string]interface{})["data"]
- if !ok {
- e, err := wrapPrometheusError(qr)
- if err != nil {
- return toReturn, err
- }
- return toReturn, fmt.Errorf(e)
- }
- for _, val := range data.(map[string]interface{})["result"].([]interface{}) {
- metricInterface, ok := val.(map[string]interface{})["metric"]
- if !ok {
- return toReturn, fmt.Errorf("Metric field does not exist in data result vector")
- }
- metricMap, ok := metricInterface.(map[string]interface{})
- if !ok {
- return toReturn, fmt.Errorf("Metric field is improperly formatted")
- }
- pod, ok := metricMap["pod"]
- if !ok {
- return toReturn, fmt.Errorf("pod field does not exist in data result vector")
- }
- podName, ok := pod.(string)
- if !ok {
- return toReturn, fmt.Errorf("pod field is improperly formatted")
- }
- for labelName, labelValue := range metricMap {
- parsedLabelName := labelName
- parsedLv, ok := labelValue.(string)
- if !ok {
- return toReturn, fmt.Errorf("label value is improperly formatted")
- }
- if strings.HasPrefix(parsedLabelName, "label_") {
- l := strings.Replace(parsedLabelName, "label_", "", 1)
- if podLabels, ok := toReturn[podName]; ok {
- podLabels[l] = parsedLv
- } else {
- toReturn[podName] = make(map[string]string)
- toReturn[podName][l] = parsedLv
- }
- }
- }
- }
- return toReturn, nil
- }
- func findDeletedNodeInfo(cli prometheusClient.Client, missingNodes map[string]*costAnalyzerCloud.Node, window string) error {
- if len(missingNodes) > 0 {
- q := make([]string, 0, len(missingNodes))
- for nodename := range missingNodes {
- klog.V(3).Infof("Finding data for deleted node %v", nodename)
- q = append(q, nodename)
- }
- l := strings.Join(q, "|")
- queryHistoricalCPUCost := fmt.Sprintf(`avg_over_time(node_cpu_hourly_cost{instance=~"%s"}[%s])`, l, window)
- queryHistoricalRAMCost := fmt.Sprintf(`avg_over_time(node_ram_hourly_cost{instance=~"%s"}[%s])`, l, window)
- queryHistoricalGPUCost := fmt.Sprintf(`avg_over_time(node_gpu_hourly_cost{instance=~"%s"}[%s])`, l, window)
- cpuCostResult, err := Query(cli, queryHistoricalCPUCost)
- if err != nil {
- return fmt.Errorf("Error fetching cpu cost data: " + err.Error())
- }
- ramCostResult, err := Query(cli, queryHistoricalRAMCost)
- if err != nil {
- return fmt.Errorf("Error fetching ram cost data: " + err.Error())
- }
- gpuCostResult, err := Query(cli, queryHistoricalGPUCost)
- if err != nil {
- return fmt.Errorf("Error fetching gpu cost data: " + err.Error())
- }
- cpuCosts, err := getCost(cpuCostResult)
- if err != nil {
- return err
- }
- ramCosts, err := getCost(ramCostResult)
- if err != nil {
- return err
- }
- gpuCosts, err := getCost(gpuCostResult)
- if err != nil {
- return err
- }
- if len(cpuCosts) == 0 {
- klog.V(1).Infof("Historical data for node prices not available. Ingest this server's /metrics endpoint to get that data.")
- }
- for node, costv := range cpuCosts {
- if _, ok := missingNodes[node]; ok {
- missingNodes[node].VCPUCost = fmt.Sprintf("%f", costv[0].Value)
- }
- }
- for node, costv := range ramCosts {
- if _, ok := missingNodes[node]; ok {
- missingNodes[node].RAMCost = fmt.Sprintf("%f", costv[0].Value)
- }
- }
- for node, costv := range gpuCosts {
- if _, ok := missingNodes[node]; ok {
- missingNodes[node].GPUCost = fmt.Sprintf("%f", costv[0].Value)
- }
- }
- }
- return nil
- }
- func getContainerAllocation(req []*Vector, used []*Vector) []*Vector {
- if req == nil || len(req) == 0 {
- for _, usedV := range used {
- if usedV.Timestamp == 0 {
- continue
- }
- usedV.Timestamp = math.Round(usedV.Timestamp/10) * 10
- }
- return used
- }
- if used == nil || len(used) == 0 {
- for _, reqV := range req {
- if reqV.Timestamp == 0 {
- continue
- }
- reqV.Timestamp = math.Round(reqV.Timestamp/10) * 10
- }
- return req
- }
- var allocation []*Vector
- var timestamps []float64
- reqMap := make(map[float64]float64)
- for _, reqV := range req {
- if reqV.Timestamp == 0 {
- continue
- }
- reqV.Timestamp = math.Round(reqV.Timestamp/10) * 10
- reqMap[reqV.Timestamp] = reqV.Value
- timestamps = append(timestamps, reqV.Timestamp)
- }
- usedMap := make(map[float64]float64)
- for _, usedV := range used {
- if usedV.Timestamp == 0 {
- continue
- }
- usedV.Timestamp = math.Round(usedV.Timestamp/10) * 10
- usedMap[usedV.Timestamp] = usedV.Value
- if _, ok := reqMap[usedV.Timestamp]; !ok { // no need to double add, since we'll range over sorted timestamps and check.
- timestamps = append(timestamps, usedV.Timestamp)
- }
- }
- sort.Float64s(timestamps)
- for _, t := range timestamps {
- rv, okR := reqMap[t]
- uv, okU := usedMap[t]
- allocationVector := &Vector{
- Timestamp: t,
- }
- if okR && okU {
- allocationVector.Value = math.Max(rv, uv)
- } else if okR {
- allocationVector.Value = rv
- } else if okU {
- allocationVector.Value = uv
- }
- allocation = append(allocation, allocationVector)
- }
- return allocation
- }
- func addPVData(cache ClusterCache, pvClaimMapping map[string]*PersistentVolumeClaimData, cloud costAnalyzerCloud.Provider) error {
- cfg, err := cloud.GetConfig()
- if err != nil {
- return err
- }
- storageClasses := cache.GetAllStorageClasses()
- storageClassMap := make(map[string]map[string]string)
- for _, storageClass := range storageClasses {
- params := storageClass.Parameters
- storageClassMap[storageClass.ObjectMeta.Name] = params
- if storageClass.GetAnnotations()["storageclass.kubernetes.io/is-default-class"] == "true" || storageClass.GetAnnotations()["storageclass.beta.kubernetes.io/is-default-class"] == "true" {
- storageClassMap["default"] = params
- storageClassMap[""] = params
- }
- }
- pvs := cache.GetAllPersistentVolumes()
- pvMap := make(map[string]*costAnalyzerCloud.PV)
- for _, pv := range pvs {
- parameters, ok := storageClassMap[pv.Spec.StorageClassName]
- if !ok {
- klog.V(4).Infof("Unable to find parameters for storage class \"%s\". Does pv \"%s\" have a storageClassName?", pv.Spec.StorageClassName, pv.Name)
- }
- cacPv := &costAnalyzerCloud.PV{
- Class: pv.Spec.StorageClassName,
- Region: pv.Labels[v1.LabelZoneRegion],
- Parameters: parameters,
- }
- err := GetPVCost(cacPv, pv, cloud)
- if err != nil {
- return err
- }
- pvMap[pv.Name] = cacPv
- }
- for _, pvc := range pvClaimMapping {
- if vol, ok := pvMap[pvc.VolumeName]; ok {
- pvc.Volume = vol
- } else {
- klog.V(1).Infof("PV not found, using default")
- pvc.Volume = &costAnalyzerCloud.PV{
- Cost: cfg.Storage,
- }
- }
- }
- return nil
- }
- func GetPVCost(pv *costAnalyzerCloud.PV, kpv *v1.PersistentVolume, cp costAnalyzerCloud.Provider) error {
- cfg, err := cp.GetConfig()
- if err != nil {
- return err
- }
- key := cp.GetPVKey(kpv, pv.Parameters)
- pvWithCost, err := cp.PVPricing(key)
- if err != nil {
- pv.Cost = cfg.Storage
- return err
- }
- if pvWithCost == nil || pvWithCost.Cost == "" {
- pv.Cost = cfg.Storage
- return nil // set default cost
- }
- pv.Cost = pvWithCost.Cost
- return nil
- }
- func getNodeCost(cache ClusterCache, cp costAnalyzerCloud.Provider) (map[string]*costAnalyzerCloud.Node, error) {
- cfg, err := cp.GetConfig()
- if err != nil {
- return nil, err
- }
- nodeList := cache.GetAllNodes()
- nodes := make(map[string]*costAnalyzerCloud.Node)
- for _, n := range nodeList {
- name := n.GetObjectMeta().GetName()
- nodeLabels := n.GetObjectMeta().GetLabels()
- nodeLabels["providerID"] = n.Spec.ProviderID
- cnode, err := cp.NodePricing(cp.GetKey(nodeLabels))
- if err != nil {
- klog.V(1).Infof("Error getting node. Error: " + err.Error())
- nodes[name] = cnode
- continue
- }
- newCnode := *cnode
- var cpu float64
- if newCnode.VCPU == "" {
- cpu = float64(n.Status.Capacity.Cpu().Value())
- newCnode.VCPU = n.Status.Capacity.Cpu().String()
- } else {
- cpu, _ = strconv.ParseFloat(newCnode.VCPU, 64)
- }
- var ram float64
- if newCnode.RAM == "" {
- newCnode.RAM = n.Status.Capacity.Memory().String()
- }
- ram = float64(n.Status.Capacity.Memory().Value())
- newCnode.RAMBytes = fmt.Sprintf("%f", ram)
- if newCnode.GPU != "" && newCnode.GPUCost == "" {
- // We couldn't find a gpu cost, so fix cpu and ram, then accordingly
- klog.V(4).Infof("GPU without cost found for %s, calculating...", cp.GetKey(nodeLabels).Features())
- defaultCPU, err := strconv.ParseFloat(cfg.CPU, 64)
- if err != nil {
- klog.V(3).Infof("Could not parse default cpu price")
- return nil, err
- }
- defaultRAM, err := strconv.ParseFloat(cfg.RAM, 64)
- if err != nil {
- klog.V(3).Infof("Could not parse default ram price")
- return nil, err
- }
- defaultGPU, err := strconv.ParseFloat(cfg.RAM, 64)
- if err != nil {
- klog.V(3).Infof("Could not parse default gpu price")
- return nil, err
- }
- cpuToRAMRatio := defaultCPU / defaultRAM
- gpuToRAMRatio := defaultGPU / defaultRAM
- ramGB := ram / 1024 / 1024 / 1024
- ramMultiple := gpuToRAMRatio + cpu*cpuToRAMRatio + ramGB
- var nodePrice float64
- if newCnode.Cost != "" {
- nodePrice, err = strconv.ParseFloat(newCnode.Cost, 64)
- if err != nil {
- klog.V(3).Infof("Could not parse total node price")
- return nil, err
- }
- } else {
- nodePrice, err = strconv.ParseFloat(newCnode.VCPUCost, 64) // all the price was allocated the the CPU
- if err != nil {
- klog.V(3).Infof("Could not parse node vcpu price")
- return nil, err
- }
- }
- ramPrice := (nodePrice / ramMultiple)
- cpuPrice := ramPrice * cpuToRAMRatio
- gpuPrice := ramPrice * gpuToRAMRatio
- newCnode.VCPUCost = fmt.Sprintf("%f", cpuPrice)
- newCnode.RAMCost = fmt.Sprintf("%f", ramPrice)
- newCnode.RAMBytes = fmt.Sprintf("%f", ram)
- newCnode.GPUCost = fmt.Sprintf("%f", gpuPrice)
- } else if newCnode.RAMCost == "" {
- // We couldn't find a ramcost, so fix cpu and allocate ram accordingly
- klog.V(4).Infof("No RAM cost found for %s, calculating...", cp.GetKey(nodeLabels).Features())
- defaultCPU, err := strconv.ParseFloat(cfg.CPU, 64)
- if err != nil {
- klog.V(3).Infof("Could not parse default cpu price")
- return nil, err
- }
- defaultRAM, err := strconv.ParseFloat(cfg.RAM, 64)
- if err != nil {
- klog.V(3).Infof("Could not parse default ram price")
- return nil, err
- }
- cpuToRAMRatio := defaultCPU / defaultRAM
- ramGB := ram / 1024 / 1024 / 1024
- ramMultiple := cpu*cpuToRAMRatio + ramGB
- var nodePrice float64
- if newCnode.Cost != "" {
- nodePrice, err = strconv.ParseFloat(newCnode.Cost, 64)
- if err != nil {
- klog.V(3).Infof("Could not parse total node price")
- return nil, err
- }
- } else {
- nodePrice, err = strconv.ParseFloat(newCnode.VCPUCost, 64) // all the price was allocated the the CPU
- if err != nil {
- klog.V(3).Infof("Could not parse node vcpu price")
- return nil, err
- }
- }
- ramPrice := (nodePrice / ramMultiple)
- cpuPrice := ramPrice * cpuToRAMRatio
- newCnode.VCPUCost = fmt.Sprintf("%f", cpuPrice)
- newCnode.RAMCost = fmt.Sprintf("%f", ramPrice)
- newCnode.RAMBytes = fmt.Sprintf("%f", ram)
- klog.V(4).Infof("Computed \"%s\" RAM Cost := %v", name, newCnode.RAMCost)
- }
- nodes[name] = &newCnode
- }
- return nodes, nil
- }
- func getPodServices(cache ClusterCache, podList []*v1.Pod, clusterID string) (map[string]map[string][]string, error) {
- servicesList := cache.GetAllServices()
- podServicesMapping := make(map[string]map[string][]string)
- for _, service := range servicesList {
- namespace := service.GetObjectMeta().GetNamespace()
- name := service.GetObjectMeta().GetName()
- key := namespace + "," + clusterID
- if _, ok := podServicesMapping[key]; !ok {
- podServicesMapping[key] = make(map[string][]string)
- }
- s := labels.Set(service.Spec.Selector).AsSelectorPreValidated()
- for _, pod := range podList {
- labelSet := labels.Set(pod.GetObjectMeta().GetLabels())
- if s.Matches(labelSet) && pod.GetObjectMeta().GetNamespace() == namespace {
- services, ok := podServicesMapping[key][pod.GetObjectMeta().GetName()]
- if ok {
- podServicesMapping[key][pod.GetObjectMeta().GetName()] = append(services, name)
- } else {
- podServicesMapping[key][pod.GetObjectMeta().GetName()] = []string{name}
- }
- }
- }
- }
- return podServicesMapping, nil
- }
- func getPodDeployments(cache ClusterCache, podList []*v1.Pod, clusterID string) (map[string]map[string][]string, error) {
- deploymentsList := cache.GetAllDeployments()
- podDeploymentsMapping := make(map[string]map[string][]string) // namespace: podName: [deploymentNames]
- for _, deployment := range deploymentsList {
- namespace := deployment.GetObjectMeta().GetNamespace()
- name := deployment.GetObjectMeta().GetName()
- key := namespace + "," + clusterID
- if _, ok := podDeploymentsMapping[key]; !ok {
- podDeploymentsMapping[key] = make(map[string][]string)
- }
- s, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
- if err != nil {
- klog.V(2).Infof("Error doing deployment label conversion: " + err.Error())
- }
- for _, pod := range podList {
- labelSet := labels.Set(pod.GetObjectMeta().GetLabels())
- if s.Matches(labelSet) && pod.GetObjectMeta().GetNamespace() == namespace {
- deployments, ok := podDeploymentsMapping[key][pod.GetObjectMeta().GetName()]
- if ok {
- podDeploymentsMapping[key][pod.GetObjectMeta().GetName()] = append(deployments, name)
- } else {
- podDeploymentsMapping[key][pod.GetObjectMeta().GetName()] = []string{name}
- }
- }
- }
- }
- return podDeploymentsMapping, nil
- }
- func getPodDeploymentsWithMetrics(deploymentLabels map[string]map[string]string, podLabels map[string]map[string]string) (map[string]map[string][]string, error) {
- podDeploymentsMapping := make(map[string]map[string][]string)
- for depKey, depLabels := range deploymentLabels {
- kt, err := NewKeyTuple(depKey)
- if err != nil {
- continue
- }
- namespace := kt.Namespace
- name := kt.Key
- clusterID := kt.ClusterID
- key := namespace + "," + clusterID
- if _, ok := podDeploymentsMapping[key]; !ok {
- podDeploymentsMapping[key] = make(map[string][]string)
- }
- s := labels.Set(depLabels).AsSelectorPreValidated()
- for podKey, pLabels := range podLabels {
- pkey, err := NewKeyTuple(podKey)
- if err != nil {
- continue
- }
- podNamespace := pkey.Namespace
- podName := pkey.Key
- podClusterID := pkey.ClusterID
- labelSet := labels.Set(pLabels)
- if s.Matches(labelSet) && podNamespace == namespace && podClusterID == clusterID {
- deployments, ok := podDeploymentsMapping[key][podName]
- if ok {
- podDeploymentsMapping[key][podName] = append(deployments, name)
- } else {
- podDeploymentsMapping[key][podName] = []string{name}
- }
- }
- }
- }
- return podDeploymentsMapping, nil
- }
- func getPodServicesWithMetrics(serviceLabels map[string]map[string]string, podLabels map[string]map[string]string) (map[string]map[string][]string, error) {
- podServicesMapping := make(map[string]map[string][]string)
- for servKey, servLabels := range serviceLabels {
- kt, err := NewKeyTuple(servKey)
- if err != nil {
- continue
- }
- namespace := kt.Namespace
- name := kt.Key
- clusterID := kt.ClusterID
- key := namespace + "," + clusterID
- if _, ok := podServicesMapping[key]; !ok {
- podServicesMapping[key] = make(map[string][]string)
- }
- s := labels.Set(servLabels).AsSelectorPreValidated()
- for podKey, pLabels := range podLabels {
- pkey, err := NewKeyTuple(podKey)
- if err != nil {
- continue
- }
- podNamespace := pkey.Namespace
- podName := pkey.Key
- podClusterID := pkey.ClusterID
- labelSet := labels.Set(pLabels)
- if s.Matches(labelSet) && podNamespace == namespace && podClusterID == clusterID {
- services, ok := podServicesMapping[key][podName]
- if ok {
- podServicesMapping[key][podName] = append(services, name)
- } else {
- podServicesMapping[key][podName] = []string{name}
- }
- }
- }
- }
- return podServicesMapping, nil
- }
- func costDataPassesFilters(costs *CostData, namespace string, cluster string) bool {
- passesNamespace := namespace == "" || costs.Namespace == namespace
- passesCluster := cluster == "" || costs.ClusterID == cluster
- return passesNamespace && passesCluster
- }
- func (cm *CostModel) ComputeCostDataRange(cli prometheusClient.Client, clientset kubernetes.Interface, cp costAnalyzerCloud.Provider,
- startString, endString, windowString string, filterNamespace string, filterCluster string, remoteEnabled bool) (map[string]*CostData, error) {
- queryRAMRequests := fmt.Sprintf(queryRAMRequestsStr, windowString, "", windowString, "")
- queryRAMUsage := fmt.Sprintf(queryRAMUsageStr, windowString, "", windowString, "")
- queryCPURequests := fmt.Sprintf(queryCPURequestsStr, windowString, "", windowString, "")
- queryCPUUsage := fmt.Sprintf(queryCPUUsageStr, windowString, "")
- queryGPURequests := fmt.Sprintf(queryGPURequestsStr, windowString, "", windowString, "")
- queryPVRequests := fmt.Sprintf(queryPVRequestsStr)
- queryNetZoneRequests := fmt.Sprintf(queryZoneNetworkUsage, windowString, "")
- queryNetRegionRequests := fmt.Sprintf(queryRegionNetworkUsage, windowString, "")
- queryNetInternetRequests := fmt.Sprintf(queryInternetNetworkUsage, windowString, "")
- normalization := fmt.Sprintf(normalizationStr, windowString, "")
- layout := "2006-01-02T15:04:05.000Z"
- start, err := time.Parse(layout, startString)
- if err != nil {
- klog.V(1).Infof("Error parsing time " + startString + ". Error: " + err.Error())
- return nil, err
- }
- end, err := time.Parse(layout, endString)
- if err != nil {
- klog.V(1).Infof("Error parsing time " + endString + ". Error: " + err.Error())
- return nil, err
- }
- window, err := time.ParseDuration(windowString)
- if err != nil {
- klog.V(1).Infof("Error parsing time " + windowString + ". Error: " + err.Error())
- return nil, err
- }
- clusterID := os.Getenv(clusterIDKey)
- if remoteEnabled == true {
- remoteLayout := "2006-01-02T15:04:05Z"
- remoteStartStr := start.Format(remoteLayout)
- remoteEndStr := end.Format(remoteLayout)
- klog.V(1).Infof("Using remote database for query from %s to %s with window %s", startString, endString, windowString)
- return CostDataRangeFromSQL("", "", windowString, remoteStartStr, remoteEndStr)
- }
- var wg sync.WaitGroup
- wg.Add(17)
- var promErr error
- var resultRAMRequests interface{}
- go func() {
- resultRAMRequests, promErr = QueryRange(cli, queryRAMRequests, start, end, window)
- defer wg.Done()
- }()
- var resultRAMUsage interface{}
- go func() {
- resultRAMUsage, promErr = QueryRange(cli, queryRAMUsage, start, end, window)
- defer wg.Done()
- }()
- var resultCPURequests interface{}
- go func() {
- resultCPURequests, promErr = QueryRange(cli, queryCPURequests, start, end, window)
- defer wg.Done()
- }()
- var resultCPUUsage interface{}
- go func() {
- resultCPUUsage, promErr = QueryRange(cli, queryCPUUsage, start, end, window)
- defer wg.Done()
- }()
- var resultGPURequests interface{}
- go func() {
- resultGPURequests, promErr = QueryRange(cli, queryGPURequests, start, end, window)
- defer wg.Done()
- }()
- var resultPVRequests interface{}
- go func() {
- resultPVRequests, promErr = QueryRange(cli, queryPVRequests, start, end, window)
- defer wg.Done()
- }()
- var resultNetZoneRequests interface{}
- go func() {
- resultNetZoneRequests, promErr = QueryRange(cli, queryNetZoneRequests, start, end, window)
- defer wg.Done()
- }()
- var resultNetRegionRequests interface{}
- go func() {
- resultNetRegionRequests, promErr = QueryRange(cli, queryNetRegionRequests, start, end, window)
- defer wg.Done()
- }()
- var resultNetInternetRequests interface{}
- go func() {
- resultNetInternetRequests, promErr = QueryRange(cli, queryNetInternetRequests, start, end, window)
- defer wg.Done()
- }()
- var pvPodAllocationResults interface{}
- go func() {
- pvPodAllocationResults, promErr = QueryRange(cli, fmt.Sprintf(queryPVCAllocation, windowString), start, end, window)
- defer wg.Done()
- }()
- var pvCostResults interface{}
- go func() {
- pvCostResults, promErr = QueryRange(cli, fmt.Sprintf(queryPVHourlyCost, windowString), start, end, window)
- defer wg.Done()
- }()
- var nsLabelsResults interface{}
- go func() {
- nsLabelsResults, promErr = QueryRange(cli, fmt.Sprintf(queryNSLabels, windowString), start, end, window)
- defer wg.Done()
- }()
- var podLabelsResults interface{}
- go func() {
- podLabelsResults, promErr = QueryRange(cli, fmt.Sprintf(queryPodLabels, windowString), start, end, window)
- defer wg.Done()
- }()
- var serviceLabelsResults interface{}
- go func() {
- serviceLabelsResults, promErr = QueryRange(cli, fmt.Sprintf(queryServiceLabels, windowString), start, end, window)
- defer wg.Done()
- }()
- var deploymentLabelsResults interface{}
- go func() {
- deploymentLabelsResults, promErr = QueryRange(cli, fmt.Sprintf(queryDeploymentLabels, windowString), start, end, window)
- defer wg.Done()
- }()
- var normalizationResults interface{}
- go func() {
- normalizationResults, promErr = QueryRange(cli, normalization, start, end, window)
- defer wg.Done()
- }()
- podDeploymentsMapping := make(map[string]map[string][]string)
- podServicesMapping := make(map[string]map[string][]string)
- namespaceLabelsMapping := make(map[string]map[string]string)
- podlist := cm.Cache.GetAllPods()
- var k8sErr error
- go func() {
- defer wg.Done()
- podDeploymentsMapping, k8sErr = getPodDeployments(cm.Cache, podlist, clusterID)
- if k8sErr != nil {
- return
- }
- podServicesMapping, k8sErr = getPodServices(cm.Cache, podlist, clusterID)
- if k8sErr != nil {
- return
- }
- namespaceLabelsMapping, k8sErr = getNamespaceLabels(cm.Cache, clusterID)
- if k8sErr != nil {
- return
- }
- }()
- wg.Wait()
- if promErr != nil {
- return nil, fmt.Errorf("Error querying prometheus: %s", promErr.Error())
- }
- if k8sErr != nil {
- return nil, fmt.Errorf("Error querying the kubernetes api: %s", k8sErr.Error())
- }
- normalizationValue, err := getNormalizations(normalizationResults)
- if err != nil {
- return nil, fmt.Errorf("Error parsing normalization values: " + err.Error())
- }
- nodes, err := getNodeCost(cm.Cache, cp)
- if err != nil {
- klog.V(1).Infof("Warning, no cost model available: " + err.Error())
- return nil, err
- }
- pvClaimMapping, err := getPVInfoVectors(resultPVRequests, clusterID)
- if err != nil {
- // Just log for compatibility with KSM less than 1.6
- klog.Infof("Unable to get PV Data: %s", err.Error())
- }
- if pvClaimMapping != nil {
- err = addPVData(cm.Cache, pvClaimMapping, cp)
- if err != nil {
- return nil, err
- }
- }
- pvCostMapping, err := GetPVCostMetrics(pvCostResults, clusterID)
- if err != nil {
- klog.V(1).Infof("Unable to get PV Hourly Cost Data: %s", err.Error())
- }
- pvAllocationMapping, err := GetPVAllocationMetrics(pvPodAllocationResults, clusterID)
- if err != nil {
- klog.V(1).Infof("Unable to get PV Allocation Cost Data: %s", err.Error())
- }
- if pvAllocationMapping != nil {
- addMetricPVData(pvAllocationMapping, pvCostMapping, cp)
- }
- nsLabels, err := GetNamespaceLabelsMetrics(nsLabelsResults, clusterID)
- if err != nil {
- klog.V(1).Infof("Unable to get Namespace Labels for Metrics: %s", err.Error())
- }
- if nsLabels != nil {
- appendNamespaceLabels(namespaceLabelsMapping, nsLabels)
- }
- podLabels, err := GetPodLabelsMetrics(podLabelsResults, clusterID)
- if err != nil {
- klog.V(1).Infof("Unable to get Pod Labels for Metrics: %s", err.Error())
- }
- serviceLabels, err := GetServiceSelectorLabelsMetrics(serviceLabelsResults, clusterID)
- if err != nil {
- klog.V(1).Infof("Unable to get Service Selector Labels for Metrics: %s", err.Error())
- }
- deploymentLabels, err := GetDeploymentMatchLabelsMetrics(deploymentLabelsResults, clusterID)
- if err != nil {
- klog.V(1).Infof("Unable to get Deployment Match Labels for Metrics: %s", err.Error())
- }
- podDeploymentsMetricsMapping, err := getPodDeploymentsWithMetrics(deploymentLabels, podLabels)
- if err != nil {
- klog.V(1).Infof("Unable to get match Deployment Labels Metrics to Pods: %s", err.Error())
- }
- appendLabelsList(podDeploymentsMapping, podDeploymentsMetricsMapping)
- podServicesMetricsMapping, err := getPodServicesWithMetrics(serviceLabels, podLabels)
- if err != nil {
- klog.V(1).Infof("Unable to get match Service Labels Metrics to Pods: %s", err.Error())
- }
- appendLabelsList(podServicesMapping, podServicesMetricsMapping)
- networkUsageMap, err := GetNetworkUsageData(resultNetZoneRequests, resultNetRegionRequests, resultNetInternetRequests, clusterID)
- if err != nil {
- klog.V(1).Infof("Unable to get Network Cost Data: %s", err.Error())
- networkUsageMap = make(map[string]*NetworkUsageData)
- }
- containerNameCost := make(map[string]*CostData)
- containers := make(map[string]bool)
- otherClusterPVRecorded := make(map[string]bool)
- RAMReqMap, err := GetContainerMetricVectors(resultRAMRequests, true, normalizationValue, clusterID)
- if err != nil {
- return nil, err
- }
- for key := range RAMReqMap {
- containers[key] = true
- }
- RAMUsedMap, err := GetContainerMetricVectors(resultRAMUsage, true, normalizationValue, clusterID)
- if err != nil {
- return nil, err
- }
- for key := range RAMUsedMap {
- containers[key] = true
- }
- CPUReqMap, err := GetContainerMetricVectors(resultCPURequests, true, normalizationValue, clusterID)
- if err != nil {
- return nil, err
- }
- for key := range CPUReqMap {
- containers[key] = true
- }
- GPUReqMap, err := GetContainerMetricVectors(resultGPURequests, true, normalizationValue, clusterID)
- if err != nil {
- return nil, err
- }
- for key := range GPUReqMap {
- containers[key] = true
- }
- CPUUsedMap, err := GetContainerMetricVectors(resultCPUUsage, false, normalizationValue, clusterID) // No need to normalize here, as this comes from a counter
- if err != nil {
- return nil, err
- }
- for key := range CPUUsedMap {
- containers[key] = true
- }
- currentContainers := make(map[string]v1.Pod)
- for _, pod := range podlist {
- if pod.Status.Phase != v1.PodRunning {
- continue
- }
- cs, err := newContainerMetricsFromPod(*pod, clusterID)
- if err != nil {
- return nil, err
- }
- for _, c := range cs {
- containers[c.Key()] = true // captures any containers that existed for a time < a prometheus scrape interval. We currently charge 0 for this but should charge something.
- currentContainers[c.Key()] = *pod
- }
- }
- missingNodes := make(map[string]*costAnalyzerCloud.Node)
- missingContainers := make(map[string]*CostData)
- for key := range containers {
- if _, ok := containerNameCost[key]; ok {
- continue // because ordering is important for the allocation model (all PV's applied to the first), just dedupe if it's already been added.
- }
- if pod, ok := currentContainers[key]; ok {
- podName := pod.GetObjectMeta().GetName()
- ns := pod.GetObjectMeta().GetNamespace()
- nodeName := pod.Spec.NodeName
- var nodeData *costAnalyzerCloud.Node
- if _, ok := nodes[nodeName]; ok {
- nodeData = nodes[nodeName]
- }
- nsKey := ns + "," + clusterID
- var podDeployments []string
- if _, ok := podDeploymentsMapping[nsKey]; ok {
- if ds, ok := podDeploymentsMapping[nsKey][pod.GetObjectMeta().GetName()]; ok {
- podDeployments = ds
- } else {
- podDeployments = []string{}
- }
- }
- var podPVs []*PersistentVolumeClaimData
- podClaims := pod.Spec.Volumes
- for _, vol := range podClaims {
- if vol.PersistentVolumeClaim != nil {
- name := vol.PersistentVolumeClaim.ClaimName
- if pvClaim, ok := pvClaimMapping[ns+","+name+","+clusterID]; ok {
- podPVs = append(podPVs, pvClaim)
- }
- }
- }
- var podNetCosts []*Vector
- if usage, ok := networkUsageMap[ns+","+podName+","+clusterID]; ok {
- netCosts, err := GetNetworkCost(usage, cp)
- if err != nil {
- klog.V(3).Infof("Error pulling network costs: %s", err.Error())
- } else {
- podNetCosts = netCosts
- }
- }
- var podServices []string
- if _, ok := podServicesMapping[nsKey]; ok {
- if svcs, ok := podServicesMapping[nsKey][pod.GetObjectMeta().GetName()]; ok {
- podServices = svcs
- } else {
- podServices = []string{}
- }
- }
- nsLabels := namespaceLabelsMapping[nsKey]
- podLabels := pod.GetObjectMeta().GetLabels()
- if podLabels == nil {
- podLabels = make(map[string]string)
- }
- for k, v := range nsLabels {
- podLabels[k] = v
- }
- for i, container := range pod.Spec.Containers {
- containerName := container.Name
- newKey := newContainerMetricFromValues(ns, podName, containerName, pod.Spec.NodeName, clusterID).Key()
- RAMReqV, ok := RAMReqMap[newKey]
- if !ok {
- klog.V(4).Info("no RAM requests for " + newKey)
- RAMReqV = []*Vector{}
- }
- RAMUsedV, ok := RAMUsedMap[newKey]
- if !ok {
- klog.V(4).Info("no RAM usage for " + newKey)
- RAMUsedV = []*Vector{}
- }
- CPUReqV, ok := CPUReqMap[newKey]
- if !ok {
- klog.V(4).Info("no CPU requests for " + newKey)
- CPUReqV = []*Vector{}
- }
- GPUReqV, ok := GPUReqMap[newKey]
- if !ok {
- klog.V(4).Info("no GPU requests for " + newKey)
- GPUReqV = []*Vector{}
- }
- CPUUsedV, ok := CPUUsedMap[newKey]
- if !ok {
- klog.V(4).Info("no CPU usage for " + newKey)
- CPUUsedV = []*Vector{}
- }
- var pvReq []*PersistentVolumeClaimData
- var netReq []*Vector
- if i == 0 { // avoid duplicating by just assigning all claims to the first container.
- pvReq = podPVs
- netReq = podNetCosts
- }
- costs := &CostData{
- Name: containerName,
- PodName: podName,
- NodeName: nodeName,
- Namespace: ns,
- Deployments: podDeployments,
- Services: podServices,
- Daemonsets: getDaemonsetsOfPod(pod),
- Jobs: getJobsOfPod(pod),
- Statefulsets: getStatefulSetsOfPod(pod),
- NodeData: nodeData,
- RAMReq: RAMReqV,
- RAMUsed: RAMUsedV,
- CPUReq: CPUReqV,
- CPUUsed: CPUUsedV,
- GPUReq: GPUReqV,
- PVCData: pvReq,
- Labels: podLabels,
- NetworkData: netReq,
- NamespaceLabels: nsLabels,
- ClusterID: clusterID,
- }
- costs.CPUAllocation = getContainerAllocation(costs.CPUReq, costs.CPUUsed)
- costs.RAMAllocation = getContainerAllocation(costs.RAMReq, costs.RAMUsed)
- if costDataPassesFilters(costs, filterNamespace, filterCluster) {
- containerNameCost[newKey] = costs
- }
- }
- } else {
- // The container has been deleted, or is from a different clusterID
- // Not all information is sent to prometheus via ksm, so fill out what we can without k8s api
- klog.V(4).Info("The container " + key + " has been deleted. Calculating allocation but resulting object will be missing data.")
- c, _ := NewContainerMetricFromKey(key)
- RAMReqV, ok := RAMReqMap[key]
- if !ok {
- klog.V(4).Info("no RAM requests for " + key)
- RAMReqV = []*Vector{}
- }
- RAMUsedV, ok := RAMUsedMap[key]
- if !ok {
- klog.V(4).Info("no RAM usage for " + key)
- RAMUsedV = []*Vector{}
- }
- CPUReqV, ok := CPUReqMap[key]
- if !ok {
- klog.V(4).Info("no CPU requests for " + key)
- CPUReqV = []*Vector{}
- }
- GPUReqV, ok := GPUReqMap[key]
- if !ok {
- klog.V(4).Info("no GPU requests for " + key)
- GPUReqV = []*Vector{}
- }
- CPUUsedV, ok := CPUUsedMap[key]
- if !ok {
- klog.V(4).Info("no CPU usage for " + key)
- CPUUsedV = []*Vector{}
- }
- node, ok := nodes[c.NodeName]
- if !ok {
- klog.V(4).Infof("Node \"%s\" has been deleted from Kubernetes. Query historical data to get it.", c.NodeName)
- if n, ok := missingNodes[c.NodeName]; ok {
- node = n
- } else {
- node = &costAnalyzerCloud.Node{}
- missingNodes[c.NodeName] = node
- }
- }
- nsKey := c.Namespace + "," + c.ClusterID
- podKey := c.Namespace + "," + c.PodName + "," + c.ClusterID
- namespaceLabels, ok := namespaceLabelsMapping[nsKey]
- if !ok {
- klog.V(3).Infof("Missing data for namespace %s", c.Namespace)
- }
- pLabels := podLabels[podKey]
- if pLabels == nil {
- pLabels = make(map[string]string)
- }
- for k, v := range namespaceLabels {
- pLabels[k] = v
- }
- var podDeployments []string
- if _, ok := podDeploymentsMapping[nsKey]; ok {
- if ds, ok := podDeploymentsMapping[nsKey][c.PodName]; ok {
- podDeployments = ds
- } else {
- podDeployments = []string{}
- }
- }
- var podServices []string
- if _, ok := podServicesMapping[nsKey]; ok {
- if svcs, ok := podServicesMapping[nsKey][c.PodName]; ok {
- podServices = svcs
- } else {
- podServices = []string{}
- }
- }
- var podPVs []*PersistentVolumeClaimData
- var podNetCosts []*Vector
- // For PVC data, we'll need to find the claim mapping and cost data. Will need to append
- // cost data since that was populated by cluster data previously. We do this with
- // the pod_pvc_allocation metric
- podPVData, ok := pvAllocationMapping[podKey]
- if !ok {
- klog.V(4).Infof("Failed to locate pv allocation mapping for missing pod.")
- }
- // For network costs, we'll use existing map since it should still contain the
- // correct data.
- var podNetworkCosts []*Vector
- if usage, ok := networkUsageMap[podKey]; ok {
- netCosts, err := GetNetworkCost(usage, cp)
- if err != nil {
- klog.V(3).Infof("Error pulling network costs: %s", err.Error())
- } else {
- podNetworkCosts = netCosts
- }
- }
- // Check to see if any other data has been recorded for this namespace, pod, clusterId
- // Follow the pattern of only allowing claims data per pod
- if !otherClusterPVRecorded[podKey] {
- otherClusterPVRecorded[podKey] = true
- podPVs = podPVData
- podNetCosts = podNetworkCosts
- }
- costs := &CostData{
- Name: c.ContainerName,
- PodName: c.PodName,
- NodeName: c.NodeName,
- NodeData: node,
- Namespace: c.Namespace,
- Services: podServices,
- Deployments: podDeployments,
- RAMReq: RAMReqV,
- RAMUsed: RAMUsedV,
- CPUReq: CPUReqV,
- CPUUsed: CPUUsedV,
- GPUReq: GPUReqV,
- Labels: pLabels,
- NamespaceLabels: namespaceLabels,
- PVCData: podPVs,
- NetworkData: podNetCosts,
- ClusterID: c.ClusterID,
- }
- costs.CPUAllocation = getContainerAllocation(costs.CPUReq, costs.CPUUsed)
- costs.RAMAllocation = getContainerAllocation(costs.RAMReq, costs.RAMUsed)
- if costDataPassesFilters(costs, filterNamespace, filterCluster) {
- containerNameCost[key] = costs
- missingContainers[key] = costs
- }
- }
- }
- w := end.Sub(start)
- w += window
- if w.Minutes() > 0 {
- wStr := fmt.Sprintf("%dm", int(w.Minutes()))
- err = findDeletedNodeInfo(cli, missingNodes, wStr)
- if err != nil {
- klog.V(1).Infof("Error fetching historical node data: %s", err.Error())
- }
- err = findDeletedPodInfo(cli, missingContainers, wStr)
- if err != nil {
- klog.V(1).Infof("Error fetching historical pod data: %s", err.Error())
- }
- }
- return containerNameCost, err
- }
- func addMetricPVData(pvAllocationMap map[string][]*PersistentVolumeClaimData, pvCostMap map[string]*costAnalyzerCloud.PV, cp costAnalyzerCloud.Provider) {
- cfg, err := cp.GetConfig()
- if err != nil {
- klog.V(1).Infof("Failed to get provider config while adding pv metrics data.")
- return
- }
- for _, pvcDataArray := range pvAllocationMap {
- for _, pvcData := range pvcDataArray {
- costKey := fmt.Sprintf("%s,%s", pvcData.VolumeName, pvcData.ClusterID)
- pvCost, ok := pvCostMap[costKey]
- if !ok {
- pvcData.Volume = &costAnalyzerCloud.PV{
- Cost: cfg.Storage,
- }
- continue
- }
- pvcData.Volume = pvCost
- }
- }
- }
- // Append labels into nsLabels iff the ns key doesn't already exist
- func appendNamespaceLabels(nsLabels map[string]map[string]string, labels map[string]map[string]string) {
- for k, v := range labels {
- if _, ok := nsLabels[k]; !ok {
- nsLabels[k] = v
- }
- }
- }
- func appendLabelsList(mainLabels map[string]map[string][]string, labels map[string]map[string][]string) {
- for k, v := range labels {
- mainLabels[k] = v
- }
- }
- func getNamespaceLabels(cache ClusterCache, clusterID string) (map[string]map[string]string, error) {
- nsToLabels := make(map[string]map[string]string)
- nss := cache.GetAllNamespaces()
- for _, ns := range nss {
- nsToLabels[ns.Name+","+clusterID] = ns.Labels
- }
- return nsToLabels, nil
- }
- func getDaemonsetsOfPod(pod v1.Pod) []string {
- for _, ownerReference := range pod.ObjectMeta.OwnerReferences {
- if ownerReference.Kind == "DaemonSet" {
- return []string{ownerReference.Name}
- }
- }
- return []string{}
- }
- func getJobsOfPod(pod v1.Pod) []string {
- for _, ownerReference := range pod.ObjectMeta.OwnerReferences {
- if ownerReference.Kind == "Job" {
- return []string{ownerReference.Name}
- }
- }
- return []string{}
- }
- func getStatefulSetsOfPod(pod v1.Pod) []string {
- for _, ownerReference := range pod.ObjectMeta.OwnerReferences {
- if ownerReference.Kind == "StatefulSet" {
- return []string{ownerReference.Name}
- }
- }
- return []string{}
- }
- type PersistentVolumeClaimData struct {
- Class string `json:"class"`
- Claim string `json:"claim"`
- Namespace string `json:"namespace"`
- ClusterID string `json:"clusterId"`
- VolumeName string `json:"volumeName"`
- Volume *costAnalyzerCloud.PV `json:"persistentVolume"`
- Values []*Vector `json:"values"`
- }
- func getCost(qr interface{}) (map[string][]*Vector, error) {
- toReturn := make(map[string][]*Vector)
- for _, val := range qr.(map[string]interface{})["data"].(map[string]interface{})["result"].([]interface{}) {
- metricInterface, ok := val.(map[string]interface{})["metric"]
- if !ok {
- return nil, fmt.Errorf("Metric field does not exist in data result vector")
- }
- metricMap, ok := metricInterface.(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("Metric field is improperly formatted")
- }
- instance, ok := metricMap["instance"]
- if !ok {
- return nil, fmt.Errorf("Instance field does not exist in data result vector")
- }
- instanceStr, ok := instance.(string)
- if !ok {
- return nil, fmt.Errorf("Instance is improperly formatted")
- }
- dataPoint, ok := val.(map[string]interface{})["value"]
- if !ok {
- return nil, fmt.Errorf("Value field does not exist in data result vector")
- }
- value, ok := dataPoint.([]interface{})
- if !ok || len(value) != 2 {
- return nil, fmt.Errorf("Improperly formatted datapoint from Prometheus")
- }
- var vectors []*Vector
- strVal := value[1].(string)
- v, _ := strconv.ParseFloat(strVal, 64)
- vectors = append(vectors, &Vector{
- Timestamp: value[0].(float64),
- Value: v,
- })
- toReturn[instanceStr] = vectors
- }
- return toReturn, nil
- }
- func getPVInfoVectors(qr interface{}, defaultClusterID string) (map[string]*PersistentVolumeClaimData, error) {
- pvmap := make(map[string]*PersistentVolumeClaimData)
- data, ok := qr.(map[string]interface{})["data"]
- if !ok {
- e, err := wrapPrometheusError(qr)
- if err != nil {
- return nil, err
- }
- return nil, fmt.Errorf(e)
- }
- d, ok := data.(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("Data field improperly formatted in prometheus repsonse")
- }
- result, ok := d["result"]
- if !ok {
- return nil, fmt.Errorf("Result field not present in prometheus response")
- }
- results, ok := result.([]interface{})
- if !ok {
- return nil, fmt.Errorf("Result field improperly formatted in prometheus response")
- }
- for _, val := range results {
- metricInterface, ok := val.(map[string]interface{})["metric"]
- if !ok {
- return nil, fmt.Errorf("Metric field does not exist in data result vector")
- }
- metricMap, ok := metricInterface.(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("Metric field is improperly formatted")
- }
- pvclaim, ok := metricMap["persistentvolumeclaim"]
- if !ok {
- return nil, fmt.Errorf("Claim field does not exist in data result vector")
- }
- pvclaimStr, ok := pvclaim.(string)
- if !ok {
- return nil, fmt.Errorf("Claim field improperly formatted")
- }
- pvnamespace, ok := metricMap["namespace"]
- if !ok {
- return nil, fmt.Errorf("Namespace field does not exist in data result vector")
- }
- pvnamespaceStr, ok := pvnamespace.(string)
- if !ok {
- return nil, fmt.Errorf("Namespace field improperly formatted")
- }
- pv, ok := metricMap["volumename"]
- if !ok {
- klog.V(3).Infof("Warning: Unfulfilled claim %s: volumename field does not exist in data result vector", pvclaimStr)
- pv = ""
- }
- pvStr, ok := pv.(string)
- if !ok {
- return nil, fmt.Errorf("Volumename field improperly formatted")
- }
- pvclass, ok := metricMap["storageclass"]
- if !ok { // TODO: We need to look up the actual PV and PV capacity. For now just proceed with "".
- klog.V(2).Infof("Storage Class not found for claim \"%s/%s\".", pvnamespaceStr, pvclaimStr)
- pvclass = ""
- }
- pvclassStr, ok := pvclass.(string)
- if !ok {
- return nil, fmt.Errorf("StorageClass field improperly formatted")
- }
- cid, ok := metricMap["cluster_id"]
- if !ok {
- klog.V(4).Info("Prometheus vector does not have cluster id")
- cid = defaultClusterID
- }
- clusterID, ok := cid.(string)
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have string cluster_id")
- }
- values, ok := val.(map[string]interface{})["values"].([]interface{})
- if !ok {
- return nil, fmt.Errorf("Values field is improperly formatted")
- }
- var vectors []*Vector
- for _, value := range values {
- dataPoint, ok := value.([]interface{})
- if !ok || len(dataPoint) != 2 {
- return nil, fmt.Errorf("Improperly formatted datapoint from Prometheus")
- }
- strVal := dataPoint[1].(string)
- v, _ := strconv.ParseFloat(strVal, 64)
- vectors = append(vectors, &Vector{
- Timestamp: math.Round(dataPoint[0].(float64)/10) * 10,
- Value: v,
- })
- }
- key := pvnamespaceStr + "," + pvclaimStr + "," + clusterID
- pvmap[key] = &PersistentVolumeClaimData{
- Class: pvclassStr,
- Claim: pvclaimStr,
- Namespace: pvnamespaceStr,
- ClusterID: clusterID,
- VolumeName: pvStr,
- Values: vectors,
- }
- }
- return pvmap, nil
- }
- func getPVInfoVector(qr interface{}, defaultClusterID string) (map[string]*PersistentVolumeClaimData, error) {
- pvmap := make(map[string]*PersistentVolumeClaimData)
- data, ok := qr.(map[string]interface{})["data"]
- if !ok {
- e, err := wrapPrometheusError(qr)
- if err != nil {
- return nil, err
- }
- return nil, fmt.Errorf(e)
- }
- d, ok := data.(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("Data field improperly formatted in prometheus repsonse")
- }
- result, ok := d["result"]
- if !ok {
- return nil, fmt.Errorf("Result field not present in prometheus response")
- }
- results, ok := result.([]interface{})
- if !ok {
- return nil, fmt.Errorf("Result field improperly formatted in prometheus response")
- }
- for _, val := range results {
- metricInterface, ok := val.(map[string]interface{})["metric"]
- if !ok {
- return nil, fmt.Errorf("Metric field does not exist in data result vector")
- }
- metricMap, ok := metricInterface.(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("Metric field is improperly formatted")
- }
- pvclaim, ok := metricMap["persistentvolumeclaim"]
- if !ok {
- return nil, fmt.Errorf("Claim field does not exist in data result vector")
- }
- pvclaimStr, ok := pvclaim.(string)
- if !ok {
- return nil, fmt.Errorf("Claim field improperly formatted")
- }
- pvnamespace, ok := metricMap["namespace"]
- if !ok {
- return nil, fmt.Errorf("Namespace field does not exist in data result vector")
- }
- pvnamespaceStr, ok := pvnamespace.(string)
- if !ok {
- return nil, fmt.Errorf("Namespace field improperly formatted")
- }
- pv, ok := metricMap["volumename"]
- if !ok {
- klog.V(3).Infof("Warning: Unfulfilled claim %s: volumename field does not exist in data result vector", pvclaimStr)
- pv = ""
- }
- pvStr, ok := pv.(string)
- if !ok {
- return nil, fmt.Errorf("Volumename field improperly formatted")
- }
- pvclass, ok := metricMap["storageclass"]
- if !ok { // TODO: We need to look up the actual PV and PV capacity. For now just proceed with "".
- klog.V(2).Infof("Storage Class not found for claim \"%s/%s\".", pvnamespaceStr, pvclaimStr)
- pvclass = ""
- }
- pvclassStr, ok := pvclass.(string)
- if !ok {
- return nil, fmt.Errorf("StorageClass field improperly formatted")
- }
- cid, ok := metricMap["cluster_id"]
- if !ok {
- klog.V(4).Info("Prometheus vector does not have cluster id")
- cid = defaultClusterID
- }
- clusterID, ok := cid.(string)
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have string cluster_id")
- }
- dataPoint, ok := val.(map[string]interface{})["value"]
- if !ok {
- return nil, fmt.Errorf("Value field does not exist in data result vector")
- }
- value, ok := dataPoint.([]interface{})
- if !ok || len(value) != 2 {
- return nil, fmt.Errorf("Improperly formatted datapoint from Prometheus")
- }
- var vectors []*Vector
- strVal := value[1].(string)
- v, _ := strconv.ParseFloat(strVal, 64)
- vectors = append(vectors, &Vector{
- Timestamp: value[0].(float64),
- Value: v,
- })
- key := pvnamespaceStr + "," + pvclaimStr + "," + clusterID
- pvmap[key] = &PersistentVolumeClaimData{
- Class: pvclassStr,
- Claim: pvclaimStr,
- Namespace: pvnamespaceStr,
- ClusterID: clusterID,
- VolumeName: pvStr,
- Values: vectors,
- }
- }
- return pvmap, nil
- }
- func QueryRange(cli prometheusClient.Client, query string, start, end time.Time, step time.Duration) (interface{}, error) {
- u := cli.URL(epQueryRange, nil)
- q := u.Query()
- q.Set("query", query)
- q.Set("start", start.Format(time.RFC3339Nano))
- q.Set("end", end.Format(time.RFC3339Nano))
- q.Set("step", strconv.FormatFloat(step.Seconds(), 'f', 3, 64))
- u.RawQuery = q.Encode()
- req, err := http.NewRequest(http.MethodPost, u.String(), nil)
- if err != nil {
- return nil, err
- }
- _, body, warnings, err := cli.Do(context.Background(), req)
- for _, w := range warnings {
- klog.V(3).Infof("%s", w)
- }
- if err != nil {
- return nil, fmt.Errorf("Error %s fetching query %s", err.Error(), query)
- }
- var toReturn interface{}
- err = json.Unmarshal(body, &toReturn)
- if err != nil {
- return nil, fmt.Errorf("Error %s fetching query %s", err.Error(), query)
- }
- return toReturn, err
- }
- func Query(cli prometheusClient.Client, query string) (interface{}, error) {
- u := cli.URL(epQuery, nil)
- q := u.Query()
- q.Set("query", query)
- u.RawQuery = q.Encode()
- req, err := http.NewRequest(http.MethodPost, u.String(), nil)
- if err != nil {
- return nil, err
- }
- resp, body, warnings, err := cli.Do(context.Background(), req)
- for _, w := range warnings {
- klog.V(3).Infof("%s", w)
- }
- if err != nil {
- if resp == nil {
- return nil, fmt.Errorf("Error %s fetching query %s", err.Error(), query)
- }
- return nil, fmt.Errorf("%d Error %s fetching query %s", resp.StatusCode, err.Error(), query)
- }
- var toReturn interface{}
- err = json.Unmarshal(body, &toReturn)
- if err != nil {
- return nil, fmt.Errorf("Error %s fetching query %s", err.Error(), query)
- }
- return toReturn, nil
- }
- //todo: don't cast, implement unmarshaler interface
- func getNormalizations(qr interface{}) ([]*Vector, error) {
- data, ok := qr.(map[string]interface{})["data"]
- if !ok {
- e, err := wrapPrometheusError(qr)
- if err != nil {
- return nil, err
- }
- return nil, fmt.Errorf(e)
- }
- results, ok := data.(map[string]interface{})["result"].([]interface{})
- if !ok {
- return nil, fmt.Errorf("Result field not found in normalization response, aborting")
- }
- if len(results) > 0 {
- vectors := []*Vector{}
- for i := range results {
- klog.Infof("%+v", results[i])
- values, ok := results[i].(map[string]interface{})["values"].([]interface{})
- for _, d := range values {
- dataPoint := d.([]interface{})
- if !ok || len(dataPoint) != 2 {
- return nil, fmt.Errorf("Improperly formatted datapoint from Prometheus")
- }
- strVal := dataPoint[1].(string)
- v, _ := strconv.ParseFloat(strVal, 64)
- vectors = append(vectors, &Vector{
- Timestamp: math.Round(dataPoint[0].(float64)/10) * 10,
- Value: v,
- })
- }
- }
- return vectors, nil
- }
- return nil, fmt.Errorf("Normalization data is empty, kube-state-metrics or node-exporter may not be running")
- }
- //todo: don't cast, implement unmarshaler interface
- func getNormalization(qr interface{}) (float64, error) {
- data, ok := qr.(map[string]interface{})["data"]
- if !ok {
- e, err := wrapPrometheusError(qr)
- if err != nil {
- return 0, err
- }
- return 0, fmt.Errorf(e)
- }
- results, ok := data.(map[string]interface{})["result"].([]interface{})
- if !ok {
- return 0, fmt.Errorf("Result field not found in normalization response, aborting")
- }
- if len(results) > 0 {
- dataPoint := results[0].(map[string]interface{})["value"].([]interface{})
- if len(dataPoint) == 2 {
- strNorm := dataPoint[1].(string)
- val, _ := strconv.ParseFloat(strNorm, 64)
- return val, nil
- }
- return 0, fmt.Errorf("Improperly formatted datapoint from Prometheus")
- }
- return 0, fmt.Errorf("Normalization data is empty, kube-state-metrics or node-exporter may not be running")
- }
- type ContainerMetric struct {
- Namespace string
- PodName string
- ContainerName string
- NodeName string
- ClusterID string
- }
- func (c *ContainerMetric) Key() string {
- return c.Namespace + "," + c.PodName + "," + c.ContainerName + "," + c.NodeName + "," + c.ClusterID
- }
- func NewContainerMetricFromKey(key string) (*ContainerMetric, error) {
- s := strings.Split(key, ",")
- if len(s) == 5 {
- return &ContainerMetric{
- Namespace: s[0],
- PodName: s[1],
- ContainerName: s[2],
- NodeName: s[3],
- ClusterID: s[4],
- }, nil
- }
- return nil, fmt.Errorf("Not a valid key")
- }
- func newContainerMetricFromValues(ns string, podName string, containerName string, nodeName string, clusterId string) *ContainerMetric {
- return &ContainerMetric{
- Namespace: ns,
- PodName: podName,
- ContainerName: containerName,
- NodeName: nodeName,
- ClusterID: clusterId,
- }
- }
- func newContainerMetricsFromPod(pod v1.Pod, clusterID string) ([]*ContainerMetric, error) {
- podName := pod.GetObjectMeta().GetName()
- ns := pod.GetObjectMeta().GetNamespace()
- node := pod.Spec.NodeName
- var cs []*ContainerMetric
- for _, container := range pod.Spec.Containers {
- containerName := container.Name
- cs = append(cs, &ContainerMetric{
- Namespace: ns,
- PodName: podName,
- ContainerName: containerName,
- NodeName: node,
- ClusterID: clusterID,
- })
- }
- return cs, nil
- }
- func newContainerMetricFromPrometheus(metrics map[string]interface{}, defaultClusterID string) (*ContainerMetric, error) {
- cName, ok := metrics["container_name"]
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have container name")
- }
- containerName, ok := cName.(string)
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have string container name")
- }
- pName, ok := metrics["pod_name"]
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have pod name")
- }
- podName, ok := pName.(string)
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have string pod name")
- }
- ns, ok := metrics["namespace"]
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have namespace")
- }
- namespace, ok := ns.(string)
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have string namespace")
- }
- node, ok := metrics["node"]
- if !ok {
- klog.V(4).Info("Prometheus vector does not have node name")
- node = ""
- }
- nodeName, ok := node.(string)
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have string node")
- }
- cid, ok := metrics["cluster_id"]
- if !ok {
- klog.V(4).Info("Prometheus vector does not have cluster id")
- cid = defaultClusterID
- }
- clusterID, ok := cid.(string)
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have string cluster_id")
- }
- return &ContainerMetric{
- ContainerName: containerName,
- PodName: podName,
- Namespace: namespace,
- NodeName: nodeName,
- ClusterID: clusterID,
- }, nil
- }
- type KeyTuple struct {
- Namespace string
- Key string
- ClusterID string
- }
- func NewKeyTuple(key string) (*KeyTuple, error) {
- r := strings.Split(key, ",")
- if len(r) != 3 {
- return nil, fmt.Errorf("NewKeyTuple() Provided key not containing exactly 3 components.")
- }
- return &KeyTuple{
- Namespace: r[0],
- Key: r[1],
- ClusterID: r[2],
- }, nil
- }
- func GetContainerMetricVector(qr interface{}, normalize bool, normalizationValue float64, defaultClusterID string) (map[string][]*Vector, error) {
- data, ok := qr.(map[string]interface{})["data"]
- if !ok {
- e, err := wrapPrometheusError(qr)
- if err != nil {
- return nil, err
- }
- return nil, fmt.Errorf(e)
- }
- r, ok := data.(map[string]interface{})["result"]
- if !ok {
- return nil, fmt.Errorf("Improperly formatted data from prometheus, data has no result field")
- }
- results, ok := r.([]interface{})
- if !ok {
- return nil, fmt.Errorf("Improperly formatted results from prometheus, result field is not a slice")
- }
- containerData := make(map[string][]*Vector)
- for _, val := range results {
- metric, ok := val.(map[string]interface{})["metric"].(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have metric labels")
- }
- containerMetric, err := newContainerMetricFromPrometheus(metric, defaultClusterID)
- if err != nil {
- return nil, err
- }
- value, ok := val.(map[string]interface{})["value"]
- if !ok {
- return nil, fmt.Errorf("Improperly formatted results from prometheus, value is not a field in the vector")
- }
- dataPoint, ok := value.([]interface{})
- if !ok || len(dataPoint) != 2 {
- return nil, fmt.Errorf("Improperly formatted datapoint from Prometheus")
- }
- strVal := dataPoint[1].(string)
- v, _ := strconv.ParseFloat(strVal, 64)
- if normalize && normalizationValue != 0 {
- v = v / normalizationValue
- }
- toReturn := &Vector{
- Timestamp: dataPoint[0].(float64),
- Value: v,
- }
- klog.V(4).Info("key: " + containerMetric.Key())
- containerData[containerMetric.Key()] = []*Vector{toReturn}
- }
- return containerData, nil
- }
- func GetContainerMetricVectors(qr interface{}, normalize bool, normalizationValues []*Vector, defaultClusterID string) (map[string][]*Vector, error) {
- data, ok := qr.(map[string]interface{})["data"]
- if !ok {
- e, err := wrapPrometheusError(qr)
- if err != nil {
- return nil, err
- }
- return nil, fmt.Errorf(e)
- }
- r, ok := data.(map[string]interface{})["result"]
- if !ok {
- return nil, fmt.Errorf("Improperly formatted data from prometheus, data has no result field")
- }
- results, ok := r.([]interface{})
- if !ok {
- return nil, fmt.Errorf("Improperly formatted results from prometheus, result field is not a slice")
- }
- containerData := make(map[string][]*Vector)
- for _, val := range results {
- metric, ok := val.(map[string]interface{})["metric"].(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("Prometheus vector does not have metric labels")
- }
- containerMetric, err := newContainerMetricFromPrometheus(metric, defaultClusterID)
- if err != nil {
- return nil, err
- }
- vs, ok := val.(map[string]interface{})["values"]
- if !ok {
- return nil, fmt.Errorf("Improperly formatted results from prometheus, values is not a field in the vector")
- }
- values, ok := vs.([]interface{})
- if !ok {
- return nil, fmt.Errorf("Improperly formatted results from prometheus, values is not a slice")
- }
- var vectors []*Vector
- for _, value := range values {
- dataPoint, ok := value.([]interface{})
- if !ok || len(dataPoint) != 2 {
- return nil, fmt.Errorf("Improperly formatted datapoint from Prometheus")
- }
- strVal := dataPoint[1].(string)
- v, _ := strconv.ParseFloat(strVal, 64)
- vectors = append(vectors, &Vector{
- Timestamp: math.Round(dataPoint[0].(float64)/10) * 10,
- Value: v,
- })
- }
- normalizedVectors := NormalizeVectorByVector(vectors, normalizationValues)
- containerData[containerMetric.Key()] = normalizedVectors
- }
- return containerData, nil
- }
- func wrapPrometheusError(qr interface{}) (string, error) {
- e, ok := qr.(map[string]interface{})["error"]
- if !ok {
- return "", fmt.Errorf("Unexpected response from Prometheus")
- }
- eStr, ok := e.(string)
- return eStr, nil
- }
|