Documentation
¶
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) GetCostForPersistentVolume(ctx context.Context, cluster string) (Cost, error)
- func (c *Client) GetCostPerCPU(ctx context.Context, cluster string) (Cost, error)
- func (c *Client) GetMemoryCost(ctx context.Context, cluster string) (Cost, error)
- func (c *Client) GetNodeCount(ctx context.Context, cluster string) (int, error)
- func (c *Client) GetObservedReplicas(ctx context.Context, cluster, namespace, kind, name string) (float64, error)
- func (c *Client) HPATargeting(ctx context.Context, cluster, namespace, kind, name string) (string, error)
- type ClientConfig
- type Clients
- func (c *Clients) GetClusterCosts(ctx context.Context, cluster string) (*CostModel, error)
- func (c *Clients) GetObservedReplicas(ctx context.Context, cluster, namespace, kind, name string) (float64, error)
- func (c *Clients) HPATargeting(ctx context.Context, cluster, namespace, kind, name string) (string, error)
- type Cluster
- type Cost
- func (c Cost) DollarsForPeriod(p Period, r int64) float64
- func (c Cost) DollarsYearly(memReq int64) float64
- func (c Cost) NonSpotCPUForPeriod(p Period, r int64) float64
- func (c Cost) NonSpotMemoryForPeriod(p Period, r int64) float64
- func (c Cost) NonSpotYearly(cpuReq int64) float64
- func (c Cost) SpotCPUForPeriod(p Period, r int64) float64
- func (c Cost) SpotMemoryForPeriod(p Period, r int64) float64
- func (c Cost) SpotYearly(cpuReq int64) float64
- type CostModel
- type HPAResolver
- type Period
- type PeriodKeys
- type ReplicaSource
- type ReportType
- type Reporter
- func (r *Reporter) AddError(msg string)
- func (r *Reporter) AddReport(costModel *CostModel, from, to Requirements)
- func (r *Reporter) AddReportWithResolvedReplicas(ctx context.Context, resolver HPAResolver, cm *CostModel, ...)
- func (r *Reporter) AddWarning(msg string)
- func (r *Reporter) Write() error
- type Requirements
Constants ¶
const ( Hourly Period = 1 Daily = 24 Weekly = 24 * 7 Monthly = 24 * 30 Yearly = 24 * 365 )
const CommentPrefix = "<!-- kost -->"
CommentPrefix is used to identify and hide old messages on GitHub.
Variables ¶
var ( ErrNoResults = errors.New("no cost results") ErrBadQuery = errors.New("bad query") ErrNilConfig = errors.New("client config is nil") ErrEmptyAddress = errors.New("client address can't be empty") ErrProdConfigMissing = errors.New("prod config is missing") ErrHPADetectionFailed = errors.New("hpa detection failed") ErrUnsupportedKind = errors.New("unsupported workload kind") )
ErrNoResults is the error returned when querying for costs returns no results.
var ErrNoReports = errors.New("nothing to report")
var ErrUnknownKind = errors.New("unknown kind")
ErrUnknownKind is the error throw when the kind of the resource in the manifest is unknown to the parser.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a client for the cost model.
func NewClient ¶
func NewClient(config *ClientConfig) (*Client, error)
NewClient creates a new cost model client with the given configuration.
func (*Client) GetCostForPersistentVolume ¶
GetCostForPersistentVolume returns the average cost per persistent volume for a given cluster
func (*Client) GetCostPerCPU ¶
GetCostPerCPU returns the average cost per CPU for a given cluster.
func (*Client) GetMemoryCost ¶
GetMemoryCost returns the cost per memory for a given cluster
func (*Client) GetNodeCount ¶
GetNodeCount returns the average number of nodes over 30 days for a given cluster
func (*Client) GetObservedReplicas ¶ added in v0.2.0
func (c *Client) GetObservedReplicas(ctx context.Context, cluster, namespace, kind, name string) (float64, error)
GetObservedReplicas returns the 7-day average replica count for the given workload from kube-state-metrics, regardless of who scales it (HPA, KEDA, manual). Used to substitute manifest replicas with reality for HPA-managed workloads. Returns ErrUnsupportedKind for kinds without a replica-count metric (DaemonSet, Pod, Job). Returns ErrNoResults if the query succeeds but has no samples (e.g., brand-new workload with no history) — callers should surface this rather than silently fall back.
func (*Client) HPATargeting ¶ added in v0.2.0
func (c *Client) HPATargeting(ctx context.Context, cluster, namespace, kind, name string) (string, error)
HPATargeting returns the name of the HorizontalPodAutoscaler targeting the given (cluster, namespace, kind, name) workload, or an empty string if no HPA targets it. Works for vanilla HPAs and KEDA-managed HPAs (KEDA creates a regular HPA underneath). Transport or unexpected response shape errors are wrapped with ErrHPADetectionFailed so callers can distinguish "definitely not HPA-managed" from "we couldn't tell."
type ClientConfig ¶
ClientConfig is the configuration for the cost model client.
type Clients ¶
Clients bundles the dev and prod client in one struct.
func NewClients ¶
func NewClients(prodConfig, devConfig *ClientConfig) (*Clients, error)
NewClients creates a new cost model clients with the given configuration.
func (*Clients) GetClusterCosts ¶
GetClusterCosts returns the cost for a cluster and differentiate for dev and prod clusters
type Cost ¶
Cost represents the _hourly_ cost of a resource in USD. If the cluster does not have pricing data for spot nodes, then Dollars will be set.
func (Cost) DollarsForPeriod ¶
DollarsForPeriod returns the cost of a resource in USD for a given period. Primarily used by PersistentVolumeClaims which do not have spot/non spot pricing.
func (Cost) DollarsYearly ¶
func (Cost) NonSpotMemoryForPeriod ¶
func (Cost) NonSpotYearly ¶
func (Cost) SpotYearly ¶
type CostModel ¶
CostModel represents the cost of each resource for a specific cluster
func GetCostModelForCluster ¶
func (*CostModel) TotalCostForPeriod ¶
func (c *CostModel) TotalCostForPeriod(p Period, r Requirements) float64
TotalCostForPeriod calculates the costs of each resource on the CostModel and returns the sum of the costs
type HPAResolver ¶ added in v0.2.0
type HPAResolver interface {
HPATargeting(ctx context.Context, cluster, namespace, kind, name string) (string, error)
GetObservedReplicas(ctx context.Context, cluster, namespace, kind, name string) (float64, error)
}
HPAResolver is the subset of *Client behavior ResolveReplicas needs. Lets policy be tested without httptest.
type PeriodKeys ¶
type ReplicaSource ¶ added in v0.2.0
type ReplicaSource int
ReplicaSource describes which signal ResolveReplicas used.
const ( // SourceManifest indicates the manifest's spec.replicas was authoritative // (no HPA targets the workload). SourceManifest ReplicaSource = iota // SourceObservedHPA indicates an HPA targets the workload and the manifest // replicas were replaced with the observed average from kube-state-metrics. SourceObservedHPA )
func ResolveReplicas ¶ added in v0.2.0
func ResolveReplicas(ctx context.Context, r HPAResolver, cluster, namespace, kind, name string, manifestReplicas int) (int, ReplicaSource, error)
ResolveReplicas decides the right replica count for a workload at PR-cost-estimation time. If no HPA targets the workload, the manifest count is authoritative. If an HPA is detected, the observed 7d-average from kube-state-metrics is substituted (rounded to nearest int) — manifest replicas are a lie under HPA control. Errors from either query are returned so the caller can surface them rather than silently fall back to a wrong number.
type ReportType ¶
type ReportType string
const ( Table ReportType = "table" Summary ReportType = "summary" Markdown ReportType = "markdown" )
type Reporter ¶
func (*Reporter) AddError ¶ added in v0.2.0
AddError records a message about an unexpected event that may have led to inaccurate cost numbers. Surfaced under the Errors section in the markdown report.
func (*Reporter) AddReport ¶
func (r *Reporter) AddReport(costModel *CostModel, from, to Requirements)
AddReport adds a costmodel and associated from, to resources to the reporter.
func (*Reporter) AddReportWithResolvedReplicas ¶ added in v0.2.0
func (r *Reporter) AddReportWithResolvedReplicas( ctx context.Context, resolver HPAResolver, cm *CostModel, from, to Requirements, )
AddReportWithResolvedReplicas adds a cost report after substituting the manifest replica count with the observed 7d-average for HPA-managed Deployment/StatefulSet workloads. For unsupported kinds (DaemonSet, Pod, Job, CronJob) or when the CostModel cannot be used to identify a cluster, falls back to AddReport with the manifest replica count.
On substitution, an audit-trail Warning is added to the reporter so reviewers can see that observed replicas were used. On resolver errors, the manifest count is preserved and an Error is added so misleading numbers are flagged rather than silently presented.
func (*Reporter) AddWarning ¶ added in v0.2.0
AddWarning records a non-fatal message that will be surfaced in the markdown report under the Warnings section. Use for expected events or known limitations (e.g., observed-replicas substitution applied for an HPA-managed workload).
type Requirements ¶
type Requirements struct {
CPUPerPod int64
MemoryPerPod int64
PersistentVolumePerPod int64
Replicas int
Kind string
Namespace string
Name string
}
Requirements holds the per-pod resource requirements parsed from a manifest, plus the replica count needed to compute aggregate cost. CPUPerPod is in millicores; MemoryPerPod and PersistentVolumePerPod are in bytes. Each PerPod field is the sum across all containers (or PVC templates) in a single pod. Use TotalCPU / TotalMemory / TotalPersistentVolume to get aggregate values across replicas.
func Delta ¶
func Delta(from, to Requirements) Requirements
Delta returns the field-wise difference between two resources. A positive value signals that the resource has increased. A negative value signals that the resource has decreased.
func ParseManifest ¶
func ParseManifest(src []byte, costModel *CostModel) (Requirements, error)
ParseManifest will parse a manifest file and return the aggregated amount of resources requested. The manifest can be a Deployment, StatefulSet, DaemonSet, Cronjob, Job, or Pod. If the manifest has the number of Replicas, the total resources will be multiplied by the number of replicas.
func (*Requirements) AddRequirements ¶
func (r *Requirements) AddRequirements(reqs corev1.ResourceRequirements)
AddRequirements increments the per-pod resources by the amount specified.
func (Requirements) TotalCPU ¶ added in v0.2.0
func (r Requirements) TotalCPU() int64
TotalCPU returns aggregate CPU (millicores) across all replicas.
func (Requirements) TotalMemory ¶ added in v0.2.0
func (r Requirements) TotalMemory() int64
TotalMemory returns aggregate memory (bytes) across all replicas.
func (Requirements) TotalPersistentVolume ¶ added in v0.2.0
func (r Requirements) TotalPersistentVolume() int64
TotalPersistentVolume returns aggregate persistent volume (bytes) across all replicas.