costmodel

package
v1.95.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2022 License: Apache-2.0 Imports: 52 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SplitTypeWeighted signals that shared costs should be shared
	// proportionally, rather than evenly
	SplitTypeWeighted = "weighted"

	// UnallocatedSubfield indicates an allocation datum that does not have the
	// chosen Aggregator; e.g. during aggregation by some label, there may be
	// cost data that do not have the given label.
	UnallocatedSubfield = "__unallocated__"
)
View Source
const (
	RFC3339Milli = "2006-01-02T15:04:05.000Z"

	CustomPricingSetting = "CustomPricing"
	DiscountSetting      = "Discount"

	LogSeparator = "+-------------------------------------------------------------------------------------"
)
View Source
const MAX_CPU_CAP = 512

This is a bit of a hack to work around garbage data from cadvisor Ideally you cap each pod to the max CPU on its node, but that involves a bit more complexity, as it it would need to be done when allocations joins with asset data.

Variables

View Source
var (
	// Static KeyTuple Errors
	NewKeyTupleErr = errors.New("NewKeyTuple() Provided key not containing exactly 3 components.")

	// Static Errors for ContainerMetric creation
	InvalidKeyErr      error = errors.New("Not a valid key")
	NoContainerErr     error = errors.New("Prometheus vector does not have container name")
	NoContainerNameErr error = errors.New("Prometheus vector does not have string container name")
	NoPodErr           error = errors.New("Prometheus vector does not have pod name")
	NoPodNameErr       error = errors.New("Prometheus vector does not have string pod name")
	NoNamespaceErr     error = errors.New("Prometheus vector does not have namespace")
	NoNamespaceNameErr error = errors.New("Prometheus vector does not have string namespace")
	NoNodeNameErr      error = errors.New("Prometheus vector does not have string node")
	NoClusterIDErr     error = errors.New("Prometheus vector does not have string cluster id")
)
View Source
var (

	// ANSIRegex matches ANSI escape and colors https://en.wikipedia.org/wiki/ANSI_escape_code
	ANSIRegex = regexp.MustCompile("\x1b\\[[0-9;]*m")
)

Functions

func AggregateCostData

func AggregateCostData(costData map[string]*CostData, field string, subfields []string, cp cloud.Provider, opts *AggregationOptions) map[string]*Aggregation

AggregateCostData aggregates raw cost data by field; e.g. namespace, cluster, service, or label. In the case of label, callers must pass a slice of subfields indicating the labels by which to group. Provider is used to define custom resource pricing. See AggregationOptions for optional parameters.

func ClusterDisks

func ClusterDisks(client prometheus.Client, provider cloud.Provider, start, end time.Time) (map[DiskIdentifier]*Disk, error)

func ClusterLoadBalancers

func ClusterLoadBalancers(client prometheus.Client, start, end time.Time) (map[LoadBalancerIdentifier]*LoadBalancer, error)

func ClusterNodes

func ClusterNodes(cp cloud.Provider, client prometheus.Client, start, end time.Time) (map[NodeIdentifier]*Node, error)

func CostDataRangeFromSQL

func CostDataRangeFromSQL(field string, value string, window string, start string, end string) (map[string]*CostData, error)

func FilterCostData

func FilterCostData(data map[string]*CostData, retains []FilterFunc, filters []FilterFunc) (map[string]*CostData, int, map[string]int)

FilterCostData allows through only CostData that matches all the given filter functions

func GenerateAggKey

func GenerateAggKey(window kubecost.Window, field string, subfields []string, opts *AggregateQueryOpts) string

GenerateAggKey generates a parameter-unique key for caching the aggregate cost model

func GetContainerMetricVector

func GetContainerMetricVector(qrs []*prom.QueryResult, normalize bool, normalizationValue float64, defaultClusterID string) (map[string][]*util.Vector, error)

func GetContainerMetricVectors

func GetContainerMetricVectors(qrs []*prom.QueryResult, defaultClusterID string) (map[string][]*util.Vector, error)

func GetDeploymentMatchLabelsMetrics

func GetDeploymentMatchLabelsMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]map[string]string, error)

func GetNamespaceAnnotationsMetrics

func GetNamespaceAnnotationsMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]map[string]string, error)

func GetNamespaceLabelsMetrics

func GetNamespaceLabelsMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]map[string]string, error)

func GetNetworkCost

func GetNetworkCost(usage *NetworkUsageData, cloud costAnalyzerCloud.Provider) ([]*util.Vector, error)

GetNetworkCost computes the actual cost for NetworkUsageData based on data provided by the Provider.

func GetNetworkUsageData

func GetNetworkUsageData(zr []*prom.QueryResult, rr []*prom.QueryResult, ir []*prom.QueryResult, defaultClusterID string) (map[string]*NetworkUsageData, error)

GetNetworkUsageData performs a join of the the results of zone, region, and internet usage queries to return a single map containing network costs for each namespace+pod

func GetNormalizedContainerMetricVectors

func GetNormalizedContainerMetricVectors(qrs []*prom.QueryResult, normalizationValues []*util.Vector, defaultClusterID string) (map[string][]*util.Vector, error)

func GetPVAllocationMetrics

func GetPVAllocationMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string][]*PersistentVolumeClaimData, error)

func GetPVCost

func GetPVCost(pv *costAnalyzerCloud.PV, kpv *v1.PersistentVolume, cp costAnalyzerCloud.Provider, defaultRegion string) error

func GetPVCostMetrics

func GetPVCostMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]*costAnalyzerCloud.PV, error)

func GetPVInfo

func GetPVInfo(qrs []*prom.QueryResult, defaultClusterID string) (map[string]*PersistentVolumeClaimData, error)

func GetPVInfoLocal

func GetPVInfoLocal(cache clustercache.ClusterCache, defaultClusterID string) (map[string]*PersistentVolumeClaimData, error)

func GetPodAnnotationsMetrics

func GetPodAnnotationsMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]map[string]string, error)

func GetPodDaemonsetsWithMetrics

func GetPodDaemonsetsWithMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]string, error)

func GetPodJobsWithMetrics

func GetPodJobsWithMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]string, error)

func GetPodLabelsMetrics

func GetPodLabelsMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]map[string]string, error)

func GetServiceSelectorLabelsMetrics

func GetServiceSelectorLabelsMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]map[string]string, error)

func GetStatefulsetMatchLabelsMetrics

func GetStatefulsetMatchLabelsMetrics(qrs []*prom.QueryResult, defaultClusterID string) (map[string]map[string]string, error)

func GetTotalContainerCost

func GetTotalContainerCost(costData map[string]*CostData, rate string, cp cloud.Provider, discount float64, customDiscount float64, idleCoefficients map[string]float64) float64

func NewClusterInfoWriteOnRequest

func NewClusterInfoWriteOnRequest(clusterInfo clusters.ClusterInfoProvider, config *config.ConfigFile) clusters.ClusterInfoProvider

NewClusterInfoWriteOnRequest instantiates and returns a cluster info provider which writes the cluster info to a configuration before each request.

func NewConfiguredClusterInfoProvider

func NewConfiguredClusterInfoProvider(config *config.ConfigFile) clusters.ClusterInfoProvider

NewConfiguredClusterInfoProvider instantiates and returns a cluster info provider which loads cluster info from a config file.

func NewLocalClusterInfoProvider

func NewLocalClusterInfoProvider(k8s kubernetes.Interface, cloud cloudProvider.Provider) clusters.ClusterInfoProvider

NewLocalClusterInfoProvider creates a new clusters.LocalClusterInfoProvider implementation for providing local cluster information

func ParseAggregationProperties

func ParseAggregationProperties(qp httputil.QueryParams, key string) ([]string, error)

ParseAggregationProperties attempts to parse and return aggregation properties encoded under the given key. If none exist, or if parsing fails, an error is returned with empty AllocationProperties.

func ParsePercentString

func ParsePercentString(percentStr string) (float64, error)

ParsePercentString takes a string of expected format "N%" and returns a floating point 0.0N. If the "%" symbol is missing, it just returns 0.0N. Empty string is interpreted as "0%" and return 0.0.

func ScaleAggregationTimeSeries

func ScaleAggregationTimeSeries(aggregation *Aggregation, resolutionHours float64)

ScaleAggregationTimeSeries reverses the scaling done by ScaleHourlyCostData, returning the aggregation's time series to hourly data.

func ScaleHourlyCostData

func ScaleHourlyCostData(data map[string]*CostData, resolutionHours float64) map[string]*CostData

ScaleHourlyCostData converts per-hour cost data to per-resolution data. If the target resolution is higher (i.e. < 1.0h) then we can do simple multiplication by the fraction-of-an-hour and retain accuracy. If the target resolution is lower (i.e. > 1.0h) then we sum groups of hourly data by resolution to maintain fidelity. e.g. (100 hours of per-hour hourly data, resolutionHours=10) => 10 data points, grouped and summed by 10-hour window e.g. (20 minutes of per-minute hourly data, resolutionHours=1/60) => 20 data points, scaled down by a factor of 60

func WrapData

func WrapData(data interface{}, err error) []byte

func WrapDataWithMessage

func WrapDataWithMessage(data interface{}, err error, message string) []byte

func WrapDataWithMessageAndWarning

func WrapDataWithMessageAndWarning(data interface{}, err error, message, warning string) []byte

func WrapDataWithWarning

func WrapDataWithWarning(data interface{}, err error, warning string) []byte

func WriteError

func WriteError(w http.ResponseWriter, err Error)

Types

type Accesses

type Accesses struct {
	Router              *httprouter.Router
	PrometheusClient    prometheus.Client
	ThanosClient        prometheus.Client
	KubeClientSet       kubernetes.Interface
	ClusterCache        clustercache.ClusterCache
	ClusterMap          clusters.ClusterMap
	CloudProvider       cloud.Provider
	ConfigFileManager   *config.ConfigFileManager
	ClusterInfoProvider clusters.ClusterInfoProvider
	Model               *CostModel
	MetricsEmitter      *CostModelMetricsEmitter
	OutOfClusterCache   *cache.Cache
	AggregateCache      *cache.Cache
	CostDataCache       *cache.Cache
	ClusterCostsCache   *cache.Cache
	CacheExpiration     map[time.Duration]time.Duration
	AggAPI              Aggregator
	// SettingsCache stores current state of app settings
	SettingsCache *cache.Cache
	// contains filtered or unexported fields
}

Accesses defines a singleton application instance, providing access to Prometheus, Kubernetes, the cloud provider, and caches.

func Initialize

func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses

func (*Accesses) AddServiceKey

func (a *Accesses) AddServiceKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) AggregateCostModelHandler

func (a *Accesses) AggregateCostModelHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

AggregateCostModelHandler handles requests to the aggregated cost model API. See ComputeAggregateCostModel for details.

func (*Accesses) ClusterCosts

func (a *Accesses) ClusterCosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) ClusterCostsFromCacheHandler

func (a *Accesses) ClusterCostsFromCacheHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) ClusterCostsOverTime

func (a *Accesses) ClusterCostsOverTime(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) ClusterInfo

func (a *Accesses) ClusterInfo(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) ComputeAggregateCostModel

func (a *Accesses) ComputeAggregateCostModel(promClient prometheusClient.Client, window kubecost.Window, field string, subfields []string, opts *AggregateQueryOpts) (map[string]*Aggregation, string, error)

ComputeAggregateCostModel computes cost data for the given window, then aggregates it by the given fields. Data is cached on two levels: the aggregation is cached as well as the underlying cost data.

func (*Accesses) ComputeAllocationHandler

func (a *Accesses) ComputeAllocationHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

ComputeAllocationHandler computes an AllocationSetRange from the CostModel.

func (*Accesses) ComputeAllocationHandlerSummary

func (a *Accesses) ComputeAllocationHandlerSummary(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) ComputeClusterCosts

func (a *Accesses) ComputeClusterCosts(client prometheus.Client, provider cloud.Provider, window, offset time.Duration, withBreakdown bool) (map[string]*ClusterCosts, error)

ComputeClusterCosts gives the cumulative and monthly-rate cluster costs over a window of time for all clusters.

func (*Accesses) ComputeIdleCoefficient

func (a *Accesses) ComputeIdleCoefficient(costData map[string]*CostData, cli prometheusClient.Client, cp cloud.Provider, discount float64, customDiscount float64, window, offset time.Duration) (map[string]float64, error)

func (*Accesses) CostDataModel

func (a *Accesses) CostDataModel(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) CostDataModelRange

func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllDaemonSets

func (a *Accesses) GetAllDaemonSets(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllDeployments

func (a *Accesses) GetAllDeployments(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllNamespaces

func (a *Accesses) GetAllNamespaces(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllNodePricing

func (a *Accesses) GetAllNodePricing(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllNodes

func (a *Accesses) GetAllNodes(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllPersistentVolumes

func (a *Accesses) GetAllPersistentVolumes(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllPods

func (a *Accesses) GetAllPods(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllStatefulSets

func (a *Accesses) GetAllStatefulSets(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetAllStorageClasses

func (a *Accesses) GetAllStorageClasses(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetCacheExpiration

func (a *Accesses) GetCacheExpiration(dur time.Duration) time.Duration

GetCacheExpiration looks up and returns custom cache expiration for the given duration. If one does not exists, it returns the default cache expiration, which is defined by the particular cache.

func (*Accesses) GetCacheRefresh

func (a *Accesses) GetCacheRefresh(dur time.Duration) time.Duration

GetCacheRefresh determines how long to wait before refreshing the cache for the given duration, which is done 1 minute before we expect the cache to expire, or 1 minute if expiration is not found or is less than 2 minutes.

func (*Accesses) GetClusterInfoMap

func (a *Accesses) GetClusterInfoMap(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetConfigs

func (a *Accesses) GetConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetHelmValues

func (a *Accesses) GetHelmValues(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetInstallInfo added in v1.95.0

func (a *Accesses) GetInstallInfo(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) GetInstallNamespace

func (a *Accesses) GetInstallNamespace(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) GetOrphanedPods

func (a *Accesses) GetOrphanedPods(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetPod

func (a *Accesses) GetPod(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetPodLogs

func (a *Accesses) GetPodLogs(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) GetPricingSourceCounts

func (a *Accesses) GetPricingSourceCounts(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)

func (*Accesses) GetPricingSourceStatus

func (a *Accesses) GetPricingSourceStatus(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)

func (*Accesses) GetPrometheusClient

func (a *Accesses) GetPrometheusClient(remote bool) prometheus.Client

GetPrometheusClient decides whether the default Prometheus client or the Thanos client should be used.

func (*Accesses) GetPrometheusMetadata

func (a *Accesses) GetPrometheusMetadata(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)

func (*Accesses) GetPrometheusMetrics

func (a *Accesses) GetPrometheusMetrics(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)

GetPrometheusMetrics retrieves availability of Prometheus and Thanos metrics

func (*Accesses) GetPrometheusQueueState

func (a *Accesses) GetPrometheusQueueState(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)

func (*Accesses) GetServiceAccountStatus

func (a *Accesses) GetServiceAccountStatus(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)

func (*Accesses) InitializeSettingsPubSub

func (a *Accesses) InitializeSettingsPubSub()

InitializeSettingsPubSub sets up the pub/sub mechanisms and kicks of routines to detect and publish changes, as well as some routines that subscribe and take actions.

func (*Accesses) ManagementPlatform

func (a *Accesses) ManagementPlatform(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) PrometheusConfig

func (a *Accesses) PrometheusConfig(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) PrometheusQuery

func (a *Accesses) PrometheusQuery(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) PrometheusQueryRange

func (a *Accesses) PrometheusQueryRange(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) PrometheusRecordingRules

func (a *Accesses) PrometheusRecordingRules(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) PrometheusTargets

func (a *Accesses) PrometheusTargets(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) RefreshPricingData

func (a *Accesses) RefreshPricingData(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

RefreshPricingData needs to be called when a new node joins the fleet, since we cache the relevant subsets of pricing data to avoid storing the whole thing.

func (*Accesses) Status

func (*Accesses) SubscribeToCustomPricingChanges

func (a *Accesses) SubscribeToCustomPricingChanges(ch chan string)

SubscribeToCustomPricingChanges subscribes the given channel to receive custom pricing changes.

func (*Accesses) SubscribeToDiscountChanges

func (a *Accesses) SubscribeToDiscountChanges(ch chan string)

SubscribeToDiscountChanges subscribes the given channel to receive discount changes.

func (*Accesses) ThanosQuery

func (a *Accesses) ThanosQuery(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) ThanosQueryRange

func (a *Accesses) ThanosQueryRange(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

func (*Accesses) UpdateAthenaInfoConfigs

func (a *Accesses) UpdateAthenaInfoConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) UpdateBigQueryInfoConfigs

func (a *Accesses) UpdateBigQueryInfoConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) UpdateConfigByKey

func (a *Accesses) UpdateConfigByKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

func (*Accesses) UpdateSpotInfoConfigs

func (a *Accesses) UpdateSpotInfoConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

type AggregateQueryOpts

type AggregateQueryOpts struct {
	Rate                  string
	Filters               map[string]string
	SharedResources       *SharedResourceInfo
	ShareSplit            string
	AllocateIdle          bool
	IncludeTimeSeries     bool
	IncludeEfficiency     bool
	DisableCache          bool
	ClearCache            bool
	NoCache               bool
	NoExpireCache         bool
	RemoteEnabled         bool
	DisableSharedOverhead bool
	UseETLAdapter         bool
}

func DefaultAggregateQueryOpts

func DefaultAggregateQueryOpts() *AggregateQueryOpts

type Aggregation

type Aggregation struct {
	Aggregator                 string                         `json:"aggregation"`
	Subfields                  []string                       `json:"subfields,omitempty"`
	Environment                string                         `json:"environment"`
	Cluster                    string                         `json:"cluster,omitempty"`
	Properties                 *kubecost.AllocationProperties `json:"-"`
	Start                      time.Time                      `json:"-"`
	End                        time.Time                      `json:"-"`
	CPUAllocationHourlyAverage float64                        `json:"cpuAllocationAverage"`
	CPUAllocationVectors       []*util.Vector                 `json:"-"`
	CPUAllocationTotal         float64                        `json:"-"`
	CPUCost                    float64                        `json:"cpuCost"`
	CPUCostVector              []*util.Vector                 `json:"cpuCostVector,omitempty"`
	CPUEfficiency              float64                        `json:"cpuEfficiency"`
	CPURequestedVectors        []*util.Vector                 `json:"-"`
	CPUUsedVectors             []*util.Vector                 `json:"-"`
	Efficiency                 float64                        `json:"efficiency"`
	GPUAllocationHourlyAverage float64                        `json:"gpuAllocationAverage"`
	GPUAllocationVectors       []*util.Vector                 `json:"-"`
	GPUCost                    float64                        `json:"gpuCost"`
	GPUCostVector              []*util.Vector                 `json:"gpuCostVector,omitempty"`
	GPUAllocationTotal         float64                        `json:"-"`
	RAMAllocationHourlyAverage float64                        `json:"ramAllocationAverage"`
	RAMAllocationVectors       []*util.Vector                 `json:"-"`
	RAMAllocationTotal         float64                        `json:"-"`
	RAMCost                    float64                        `json:"ramCost"`
	RAMCostVector              []*util.Vector                 `json:"ramCostVector,omitempty"`
	RAMEfficiency              float64                        `json:"ramEfficiency"`
	RAMRequestedVectors        []*util.Vector                 `json:"-"`
	RAMUsedVectors             []*util.Vector                 `json:"-"`
	PVAllocationHourlyAverage  float64                        `json:"pvAllocationAverage"`
	PVAllocationVectors        []*util.Vector                 `json:"-"`
	PVAllocationTotal          float64                        `json:"-"`
	PVCost                     float64                        `json:"pvCost"`
	PVCostVector               []*util.Vector                 `json:"pvCostVector,omitempty"`
	NetworkCost                float64                        `json:"networkCost"`
	NetworkCostVector          []*util.Vector                 `json:"networkCostVector,omitempty"`
	SharedCost                 float64                        `json:"sharedCost"`
	TotalCost                  float64                        `json:"totalCost"`
	TotalCostVector            []*util.Vector                 `json:"totalCostVector,omitempty"`
}

Aggregation describes aggregated cost data, containing cumulative cost and allocation data per resource, vectors of rate data per resource, efficiency data, and metadata describing the type of aggregation operation.

func (*Aggregation) RateCoefficient

func (a *Aggregation) RateCoefficient(rateStr string, resolutionHours float64) float64

RateCoefficient computes the coefficient by which the total cost needs to be multiplied in order to convert totals costs into per-rate costs.

func (*Aggregation) TotalHours

func (a *Aggregation) TotalHours(resolutionHours float64) float64

TotalHours determines the amount of hours the Aggregation covers, as a function of the cost vectors and the resolution of those vectors' data

type AggregationOptions

type AggregationOptions struct {
	Discount               float64            // percent by which to discount CPU, RAM, and GPU cost
	CustomDiscount         float64            // additional custom discount applied to all prices
	IdleCoefficients       map[string]float64 // scales costs by amount of idle resources on a per-cluster basis
	IncludeEfficiency      bool               // set to true to receive efficiency/usage data
	IncludeTimeSeries      bool               // set to true to receive time series data
	Rate                   string             // set to "hourly", "daily", or "monthly" to receive cost rate, rather than cumulative cost
	ResolutionHours        float64
	SharedResourceInfo     *SharedResourceInfo
	SharedCosts            map[string]*SharedCostInfo
	FilteredContainerCount int
	FilteredEnvironments   map[string]int
	SharedSplit            string
	TotalContainerCost     float64
}

AggregationOptions provides optional parameters to AggregateCostData, allowing callers to perform more complex operations

type Aggregator

type Aggregator interface {
	ComputeAggregateCostModel(promClient prometheusClient.Client, window kubecost.Window, field string, subfields []string, opts *AggregateQueryOpts) (map[string]*Aggregation, string, error)
}

Aggregator is capable of computing the aggregated cost model. This is a brutal interface, which should be cleaned up, but it's necessary for being able to swap in an ETL-backed implementation.

type ClusterCosts

type ClusterCosts struct {
	Start             *time.Time             `json:"startTime"`
	End               *time.Time             `json:"endTime"`
	CPUCumulative     float64                `json:"cpuCumulativeCost"`
	CPUMonthly        float64                `json:"cpuMonthlyCost"`
	CPUBreakdown      *ClusterCostsBreakdown `json:"cpuBreakdown"`
	GPUCumulative     float64                `json:"gpuCumulativeCost"`
	GPUMonthly        float64                `json:"gpuMonthlyCost"`
	RAMCumulative     float64                `json:"ramCumulativeCost"`
	RAMMonthly        float64                `json:"ramMonthlyCost"`
	RAMBreakdown      *ClusterCostsBreakdown `json:"ramBreakdown"`
	StorageCumulative float64                `json:"storageCumulativeCost"`
	StorageMonthly    float64                `json:"storageMonthlyCost"`
	StorageBreakdown  *ClusterCostsBreakdown `json:"storageBreakdown"`
	TotalCumulative   float64                `json:"totalCumulativeCost"`
	TotalMonthly      float64                `json:"totalMonthlyCost"`
	DataMinutes       float64
}

Costs represents cumulative and monthly cluster costs over a given duration. Costs are broken down by cores, memory, and storage.

func NewClusterCostsFromCumulative

func NewClusterCostsFromCumulative(cpu, gpu, ram, storage float64, window, offset time.Duration, dataHours float64) (*ClusterCosts, error)

NewClusterCostsFromCumulative takes cumulative cost data over a given time range, computes the associated monthly rate data, and returns the Costs.

type ClusterCostsBreakdown

type ClusterCostsBreakdown struct {
	Idle   float64 `json:"idle"`
	Other  float64 `json:"other"`
	System float64 `json:"system"`
	User   float64 `json:"user"`
}

ClusterCostsBreakdown provides percentage-based breakdown of a resource by categories: user for user-space (i.e. non-system) usage, system, and idle.

type ClusterInfoCollector

type ClusterInfoCollector struct {
	ClusterInfo clusters.ClusterInfoProvider
	// contains filtered or unexported fields
}

ClusterInfoCollector is a prometheus collector that generates ClusterInfoMetrics

func (ClusterInfoCollector) Collect

func (cic ClusterInfoCollector) Collect(ch chan<- prometheus.Metric)

Collect is called by the Prometheus registry when collecting metrics.

func (ClusterInfoCollector) Describe

func (cic ClusterInfoCollector) Describe(ch chan<- *prometheus.Desc)

Describe sends the super-set of all possible descriptors of metrics collected by this Collector.

type ClusterInfoMetric

type ClusterInfoMetric struct {
	// contains filtered or unexported fields
}

ClusterInfoMetric is a prometheus.Metric used to encode the local cluster info

func (ClusterInfoMetric) Desc

func (cim ClusterInfoMetric) Desc() *prometheus.Desc

Desc returns the descriptor for the Metric. This method idempotently returns the same descriptor throughout the lifetime of the Metric.

func (ClusterInfoMetric) Write

func (cim ClusterInfoMetric) Write(m *dto.Metric) error

Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.

type CoefficientComponent

type CoefficientComponent struct {
	Proportion float64
	Time       float64
}

CoefficientComponent is a representitive struct holding two fields which describe an interval as part of a single number cost coefficient calculation: 1. Proportion: The division of cost based on how many pods were running between those points 2. Time: The ratio of the time between those points to the total time that pod was running

type ContainerInfo added in v1.95.0

type ContainerInfo struct {
	ContainerName string `json:"containerName"`
	Image         string `json:"image"`
	ImageID       string `json:"imageID"`
	StartTime     string `json:"startTime"`
	Restarts      int32  `json:"restarts"`
}

type ContainerMetric

type ContainerMetric struct {
	Namespace     string
	PodName       string
	ContainerName string
	NodeName      string
	ClusterID     string
	// contains filtered or unexported fields
}

ContainerMetric contains a set of identifiers specific to a kubernetes container including a unique string key

func NewContainerMetricFromKey

func NewContainerMetricFromKey(key string) (*ContainerMetric, error)

NewContainerMetricFromKey creates a new ContainerMetric instance using a provided comma delimitted string key.

func NewContainerMetricFromPrometheus

func NewContainerMetricFromPrometheus(metrics map[string]interface{}, defaultClusterID string) (*ContainerMetric, error)

NewContainerMetricFromPrometheus accepts the metrics map from a QueryResult and returns a new ContainerMetric instance

func NewContainerMetricFromValues

func NewContainerMetricFromValues(ns, podName, containerName, nodeName, clusterId string) *ContainerMetric

NewContainerMetricFromValues creates a new ContainerMetric instance using the provided string parameters.

func NewContainerMetricsFromPod

func NewContainerMetricsFromPod(pod *v1.Pod, clusterID string) ([]*ContainerMetric, error)

NewContainerMetricsFromPod creates a slice of ContainerMetric instances for each container in the provided Pod.

func (*ContainerMetric) Key

func (c *ContainerMetric) Key() string

Key returns a unique string key that can be used in map[string]interface{}

type CostData

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          []*util.Vector               `json:"ramreq,omitempty"`
	RAMUsed         []*util.Vector               `json:"ramused,omitempty"`
	RAMAllocation   []*util.Vector               `json:"ramallocated,omitempty"`
	CPUReq          []*util.Vector               `json:"cpureq,omitempty"`
	CPUUsed         []*util.Vector               `json:"cpuused,omitempty"`
	CPUAllocation   []*util.Vector               `json:"cpuallocated,omitempty"`
	GPUReq          []*util.Vector               `json:"gpureq,omitempty"`
	PVCData         []*PersistentVolumeClaimData `json:"pvcData,omitempty"`
	NetworkData     []*util.Vector               `json:"network,omitempty"`
	Annotations     map[string]string            `json:"annotations,omitempty"`
	Labels          map[string]string            `json:"labels,omitempty"`
	NamespaceLabels map[string]string            `json:"namespaceLabels,omitempty"`
	ClusterID       string                       `json:"clusterId"`
	ClusterName     string                       `json:"clusterName"`
}

func (*CostData) GetController

func (cd *CostData) GetController() (name string, kind string, hasController bool)

func (*CostData) String

func (cd *CostData) String() string

type CostModel

type CostModel struct {
	Cache                      clustercache.ClusterCache
	ClusterMap                 clusters.ClusterMap
	MaxPrometheusQueryDuration time.Duration
	RequestGroup               *singleflight.Group
	ScrapeInterval             time.Duration
	PrometheusClient           prometheus.Client
	Provider                   costAnalyzerCloud.Provider
	// contains filtered or unexported fields
}

func NewCostModel

func NewCostModel(client prometheus.Client, provider costAnalyzerCloud.Provider, cache clustercache.ClusterCache, clusterMap clusters.ClusterMap, scrapeInterval time.Duration) *CostModel

func (*CostModel) CanCompute

func (cm *CostModel) CanCompute(start, end time.Time) bool

CanCompute should return true if CostModel can act as a valid source for the given time range. In the case of CostModel we want to attempt to compute as long as the range starts in the past. If the CostModel ends up not having data to match, that's okay, and should be communicated with an error response from ComputeAllocation.

func (*CostModel) ComputeAllocation

func (cm *CostModel) ComputeAllocation(start, end time.Time, resolution time.Duration) (*kubecost.AllocationSet, error)

ComputeAllocation uses the CostModel instance to compute an AllocationSet for the window defined by the given start and end times. The Allocations returned are unaggregated (i.e. down to the container level).

func (*CostModel) ComputeCostData

func (cm *CostModel) ComputeCostData(cli prometheusClient.Client, cp costAnalyzerCloud.Provider, window string, offset string, filterNamespace string) (map[string]*CostData, error)

func (*CostModel) ComputeCostDataRange

func (cm *CostModel) ComputeCostDataRange(cli prometheusClient.Client, cp costAnalyzerCloud.Provider, window kubecost.Window, resolution time.Duration, filterNamespace string, filterCluster string, remoteEnabled bool) (map[string]*CostData, error)

ComputeCostDataRange executes a range query for cost data. Note that "offset" represents the time between the function call and "endString", and is also passed for convenience

func (*CostModel) GetLBCost

TODO: drop some logs

func (*CostModel) GetNodeCost

func (*CostModel) GetPricingSourceCounts

func (cm *CostModel) GetPricingSourceCounts() (*costAnalyzerCloud.PricingMatchMetadata, error)

func (*CostModel) Name

func (cm *CostModel) Name() string

Name returns the name of the Source

type CostModelMetricsEmitter

type CostModelMetricsEmitter struct {
	PrometheusClient promclient.Client
	KubeClusterCache clustercache.ClusterCache
	CloudProvider    cloud.Provider
	Model            *CostModel

	// Metrics
	CPUPriceRecorder              *prometheus.GaugeVec
	RAMPriceRecorder              *prometheus.GaugeVec
	PersistentVolumePriceRecorder *prometheus.GaugeVec
	GPUPriceRecorder              *prometheus.GaugeVec
	GPUCountRecorder              *prometheus.GaugeVec
	PVAllocationRecorder          *prometheus.GaugeVec
	NodeSpotRecorder              *prometheus.GaugeVec
	NodeTotalPriceRecorder        *prometheus.GaugeVec
	RAMAllocationRecorder         *prometheus.GaugeVec
	CPUAllocationRecorder         *prometheus.GaugeVec
	GPUAllocationRecorder         *prometheus.GaugeVec
	ClusterManagementCostRecorder *prometheus.GaugeVec
	LBCostRecorder                *prometheus.GaugeVec
	NetworkZoneEgressRecorder     prometheus.Gauge
	NetworkRegionEgressRecorder   prometheus.Gauge
	NetworkInternetEgressRecorder prometheus.Gauge
	// contains filtered or unexported fields
}

CostModelMetricsEmitter emits all cost-model specific metrics calculated by the CostModel.ComputeCostData() method.

func NewCostModelMetricsEmitter

func NewCostModelMetricsEmitter(promClient promclient.Client, clusterCache clustercache.ClusterCache, provider cloud.Provider, clusterInfo clusters.ClusterInfoProvider, model *CostModel) *CostModelMetricsEmitter

NewCostModelMetricsEmitter creates a new cost-model metrics emitter. Use Start() to begin metric emission.

func (*CostModelMetricsEmitter) IsRunning

func (cmme *CostModelMetricsEmitter) IsRunning() bool

IsRunning returns true if metric recording is running.

func (*CostModelMetricsEmitter) Start

func (cmme *CostModelMetricsEmitter) Start() bool

StartCostModelMetricRecording starts the go routine that emits metrics used to determine cluster costs.

func (*CostModelMetricsEmitter) Stop

func (cmme *CostModelMetricsEmitter) Stop()

Stop halts the metrics emission loop after the current emission is completed or if the emission is paused.

type Disk

type Disk struct {
	Cluster    string
	Name       string
	ProviderID string
	Cost       float64
	Bytes      float64
	Local      bool
	Start      time.Time
	End        time.Time
	Minutes    float64
	Breakdown  *ClusterCostsBreakdown
}

type DiskIdentifier

type DiskIdentifier struct {
	Cluster string
	Name    string
}

type EmptyDataError

type EmptyDataError struct {
	// contains filtered or unexported fields
}

EmptyDataError describes an error caused by empty cost data for some defined interval

func (*EmptyDataError) Error

func (ede *EmptyDataError) Error() string

Error implements the error interface

type Error

type Error struct {
	StatusCode int
	Body       string
}

func BadRequest

func BadRequest(message string) Error

func InternalServerError

func InternalServerError(message string) Error

func NotFound

func NotFound() Error

type FilterFunc

type FilterFunc func(*CostData) (bool, string)

FilterFunc is a filter that returns true iff the given CostData should be filtered out, and the environment that was used as the filter criteria, if it was an aggregate

type InstallInfo added in v1.95.0

type InstallInfo struct {
	Containers  []ContainerInfo   `json:"containers"`
	ClusterInfo map[string]string `json:"clusterInfo"`
	Version     string            `json:"version"`
}

type IntervalPoint

type IntervalPoint struct {
	Time      time.Time
	PointType string
	Key       podKey
}

IntervalPoint describes a start or end of a window of time Currently, this used in PVC-pod relations to detect/calculate coefficients for PV cost when a PVC is shared between pods.

func NewIntervalPoint

func NewIntervalPoint(time time.Time, pointType string, key podKey) IntervalPoint

NewIntervalPoint creates and returns a new IntervalPoint instance with given parameters.

type IntervalPoints

type IntervalPoints []IntervalPoint

IntervalPoints describes a slice of IntervalPoint structs

func (IntervalPoints) Len

func (ips IntervalPoints) Len() int

Requisite functions for implementing sort.Sort for IntervalPointList

func (IntervalPoints) Less

func (ips IntervalPoints) Less(i, j int) bool

func (IntervalPoints) Swap

func (ips IntervalPoints) Swap(i, j int)

type KeyTuple

type KeyTuple struct {
	// contains filtered or unexported fields
}

KeyTuple contains is a utility which parses Namespace, Key, and ClusterID from a comma delimitted string.

func NewKeyTuple

func NewKeyTuple(key string) (*KeyTuple, error)

NewKeyTuple creates a new KeyTuple instance by determining the exact indices of each tuple entry. When each component is requested, a string slice is returned using the boundaries.

func (*KeyTuple) ClusterID

func (kt *KeyTuple) ClusterID() string

ClusterID returns the cluster identifier from the string key.

func (*KeyTuple) Key

func (kt *KeyTuple) Key() string

Key returns the identifier from the string key.

func (*KeyTuple) Namespace

func (kt *KeyTuple) Namespace() string

Namespace returns the the namespace from the string key.

type LB

type LB struct {
	TotalCost float64
	Start     time.Time
	End       time.Time
}

LB describes the start and end time of a Load Balancer along with cost

type LoadBalancer

type LoadBalancer struct {
	Cluster    string
	Namespace  string
	Name       string
	ProviderID string
	Cost       float64
	Start      time.Time
	End        time.Time
	Minutes    float64
}

type LoadBalancerIdentifier

type LoadBalancerIdentifier struct {
	Cluster   string
	Namespace string
	Name      string
}

type NetworkUsageData

type NetworkUsageData struct {
	ClusterID             string
	PodName               string
	Namespace             string
	NetworkZoneEgress     []*util.Vector
	NetworkRegionEgress   []*util.Vector
	NetworkInternetEgress []*util.Vector
}

NetworkUsageVNetworkUsageDataector contains the network usage values for egress network traffic

type NetworkUsageVector

type NetworkUsageVector struct {
	ClusterID string
	PodName   string
	Namespace string
	Values    []*util.Vector
}

NetworkUsageVector contains a network usage vector for egress network traffic

type Node

type Node struct {
	Cluster         string
	Name            string
	ProviderID      string
	NodeType        string
	CPUCost         float64
	CPUCores        float64
	GPUCost         float64
	GPUCount        float64
	RAMCost         float64
	RAMBytes        float64
	Discount        float64
	Preemptible     bool
	CPUBreakdown    *ClusterCostsBreakdown
	RAMBreakdown    *ClusterCostsBreakdown
	Start           time.Time
	End             time.Time
	Minutes         float64
	Labels          map[string]string
	CostPerCPUHr    float64
	CostPerRAMGiBHr float64
	CostPerGPUHr    float64
}

type NodeCostAverages

type NodeCostAverages struct {
	CpuCostAverage   float64
	RamCostAverage   float64
	NumCpuDataPoints float64
	NumRamDataPoints float64
}

NodeCostAverages tracks a running average of a node's cost attributes. The averages are used to detect and discard spurrious outliers.

type NodeIdentifier

type NodeIdentifier struct {
	Cluster    string
	Name       string
	ProviderID string
}

type NodePricing

type NodePricing struct {
	Name            string
	NodeType        string
	ProviderID      string
	Preemptible     bool
	CostPerCPUHr    float64
	CostPerRAMGiBHr float64
	CostPerGPUHr    float64
	Discount        float64
	Source          string
}

NodePricing describes the resource costs associated with a given node, as well as the source of the information (e.g. prometheus, custom)

type PV

type PV struct {
	Bytes          float64 `json:"bytes"`
	CostPerGiBHour float64 `json:"costPerGiBHour"`
	Cluster        string  `json:"cluster"`
	Name           string  `json:"name"`
	StorageClass   string  `json:"storageClass"`
}

PV describes a PersistentVolume TODO:CLEANUP move to pkg/kubecost?

func (*PV) String

func (pv *PV) String() string

String returns a string representation of the PV

type PVC

type PVC struct {
	Bytes     float64   `json:"bytes"`
	Count     int       `json:"count"`
	Name      string    `json:"name"`
	Cluster   string    `json:"cluster"`
	Namespace string    `json:"namespace"`
	Volume    *PV       `json:"persistentVolume"`
	Mounted   bool      `json:"mounted"`
	Start     time.Time `json:"start"`
	End       time.Time `json:"end"`
}

PVC describes a PersistentVolumeClaim TODO:CLEANUP move to pkg/kubecost? TODO:CLEANUP add PersistentVolumeClaims field to type Allocation?

func (*PVC) Cost

func (pvc *PVC) Cost() float64

Cost computes the cumulative cost of the PVC

func (*PVC) Minutes

func (pvc *PVC) Minutes() float64

Minutes computes the number of minutes over which the PVC is defined

func (*PVC) String

func (pvc *PVC) String() string

String returns a string representation of the PVC

type PersistentVolumeClaimData

type PersistentVolumeClaimData struct {
	Class        string                `json:"class"`
	Claim        string                `json:"claim"`
	Namespace    string                `json:"namespace"`
	ClusterID    string                `json:"clusterId"`
	TimesClaimed int                   `json:"timesClaimed"`
	VolumeName   string                `json:"volumeName"`
	Volume       *costAnalyzerCloud.PV `json:"persistentVolume"`
	Values       []*util.Vector        `json:"values"`
}

type Pod

type Pod struct {
	Window      kubecost.Window
	Start       time.Time
	End         time.Time
	Key         podKey
	Allocations map[string]*kubecost.Allocation
}

Pod describes a running pod's start and end time within a Window and all the Allocations (i.e. containers) contained within it.

func (Pod) AppendContainer

func (p Pod) AppendContainer(container string)

AppendContainer adds an entry for the given container name to the Pod.

type Response

type Response struct {
	Code    int         `json:"code"`
	Status  string      `json:"status"`
	Data    interface{} `json:"data"`
	Message string      `json:"message,omitempty"`
	Warning string      `json:"warning,omitempty"`
}

type SharedCostInfo

type SharedCostInfo struct {
	Name      string
	Cost      float64
	ShareType string
}

type SharedResourceInfo

type SharedResourceInfo struct {
	ShareResources  bool
	SharedNamespace map[string]bool
	LabelSelectors  map[string]map[string]bool
}

func NewSharedResourceInfo

func NewSharedResourceInfo(shareResources bool, sharedNamespaces []string, labelNames []string, labelValues []string) *SharedResourceInfo

func (*SharedResourceInfo) IsSharedResource

func (s *SharedResourceInfo) IsSharedResource(costDatum *CostData) bool

func (*SharedResourceInfo) String

func (s *SharedResourceInfo) String() string

String returns a string representation of the encapsulated shared resources, which can be used to uniquely identify a set of shared resources. Sorting sets of shared resources ensures that strings representing permutations of the same combination match.

type Totals

type Totals struct {
	TotalCost   [][]string `json:"totalcost"`
	CPUCost     [][]string `json:"cpucost"`
	MemCost     [][]string `json:"memcost"`
	StorageCost [][]string `json:"storageCost"`
}

func ClusterCostsOverTime

func ClusterCostsOverTime(cli prometheus.Client, provider cloud.Provider, startString, endString string, window, offset time.Duration) (*Totals, error)

ClusterCostsOverTime gives the full cluster costs over time

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL