Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion pkg/costmodel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,31 @@ avg by (spot) (node_ram_hourly_cost{cluster="%s"}
cloudcost_gcp_gke_instance_memory_usd_per_gib_hour{cluster_name="%s"}
)
`
queryPersistentVolumeCost = "avg_over_time(avg(pv_hourly_cost{cluster=\"%s\"})[24h:1m])"
queryPersistentVolumeCost = `
avg_over_time(
avg(
pv_hourly_cost{cluster="%s"}
)[24h:1m]
)`
cloudcostQueryPersistentVolumeCost = `
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason to use 24h for opencost vs instant query for cloudcost-exporter metrics?

Copy link
Contributor Author

@Pokom Pokom Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question! During the hackathon, I had arbitrarily picked a 24h lookup window. When testing out the new queries, there was a negligible difference between using a lookback vs instant query. The resulting values were ~$.00001 different. I don't think that difference is worth the computational increase of issuing the queries with a 24h look back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't really link to the explore's as they would surface internal metrics. If you want, I can share the explores in slack

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the details, no need to hard proof them :) 👍🏼

avg(
cloudcost_aws_ec2_persistent_volume_usd_per_hour{persistentvolume!="", state="in-use"}
/ on (persistentvolume) group_left() (
kube_persistentvolume_capacity_bytes{cluster="%s"} / 1e9
)
)
or
avg(
cloudcost_gcp_gke_persistent_volume_usd_per_hour{persistentvolume!="", use_status="in-use", cluster_name="%s"}
/ on (persistentvolume) group_left() (
kube_persistentvolume_capacity_bytes{cluster="%s"} / 1e9
)
)
or
avg(
pv_hourly_cost{cluster="%s"}
)
`

queryAverageNodeCount = `
avg_over_time(
Expand Down Expand Up @@ -212,6 +236,10 @@ func (c *Client) GetNodeCount(ctx context.Context, cluster string) (int, error)
// GetCostForPersistentVolume returns the average cost per persistent volume for a given cluster
func (c *Client) GetCostForPersistentVolume(ctx context.Context, cluster string) (Cost, error) {
query := fmt.Sprintf(queryPersistentVolumeCost, cluster)
if c.useCloudCostExporterMetrics {
slog.Info("GetCostForPersistentVolume", "cluster", cluster, "message", "using cloudcost exporter metrics")
query = fmt.Sprintf(cloudcostQueryPersistentVolumeCost, cluster, cluster, cluster, cluster)
}
results, err := c.query(ctx, query)
if err != nil {
return Cost{}, err
Expand Down
147 changes: 147 additions & 0 deletions pkg/costmodel/testdata/resource/StatefulSet-more-storage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"apiVersion": "apps/v1",
"kind": "StatefulSet",
"metadata": {
"creationTimestamp": "2022-10-05T20:38:31Z",
"generation": 17,
"labels": {
"kustomize.toolkit.fluxcd.io/name": "kube-manifests-opencost",
"kustomize.toolkit.fluxcd.io/namespace": "opencost",
"tanka.dev/environment": "85ead74422d749cb54711e74c81bc5d6ed6da54e92b5fa69"
},
"name": "opencost",
"namespace": "opencost",
"resourceVersion": "2386985939",
"uid": "56495ef8-2650-46e8-9528-28759cf47151"
},
"spec": {
"podManagementPolicy": "OrderedReady",
"replicas": 1,
"revisionHistoryLimit": 10,
"selector": {
"matchLabels": {
"name": "opencost"
}
},
"serviceName": "opencost",
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"name": "opencost"
}
},
"spec": {
"affinity": {
"nodeAffinity": {
"preferredDuringSchedulingIgnoredDuringExecution": [
{
"preference": {
"matchExpressions": [
{
"key": "cloud.google.com/gke-spot",
"operator": "In",
"values": [
"true"
]
}
]
},
"weight": 100
}
]
}
},
"containers": [
{
"env": [
],
"image": "quay.io/kubecost1/kubecost-cost-model:prod-1.100.0",
"imagePullPolicy": "IfNotPresent",
"name": "opencost",
"ports": [
{
"containerPort": 9003,
"name": "http-metrics",
"protocol": "TCP"
}
],
"resources": {
"limits": {
"cpu": "4",
"memory": "8Gi"
},
"requests": {
"cpu": "1",
"memory": "4Gi"
}
},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"volumeMounts": [
{
"mountPath": "/var/configs",
"name": "opencost-data"
}
]
}
],
"dnsPolicy": "ClusterFirst",
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {
"fsGroup": 10001
},
"serviceAccount": "opencost",
"serviceAccountName": "opencost",
"terminationGracePeriodSeconds": 30,
"tolerations": [
{
"effect": "NoSchedule",
"key": "type",
"operator": "Equal",
"value": "spot-node"
}
]
}
},
"updateStrategy": {
"type": "RollingUpdate"
},
"volumeClaimTemplates": [
{
"apiVersion": "v1",
"kind": "PersistentVolumeClaim",
"metadata": {
"creationTimestamp": null,
"name": "opencost-data"
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "320Gi"
}
},
"volumeMode": "Filesystem"
},
"status": {
"phase": "Pending"
}
}
]
},
"status": {
"availableReplicas": 1,
"collisionCount": 0,
"currentReplicas": 1,
"currentRevision": "opencost-6666f8bdb7",
"observedGeneration": 17,
"readyReplicas": 1,
"replicas": 1,
"updateRevision": "opencost-6666f8bdb7",
"updatedReplicas": 1
}
}