Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Include idle node costs #329

Merged
merged 2 commits into from
Dec 19, 2024
Merged
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
20 changes: 10 additions & 10 deletions internal/controller/kubecostextractor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,15 @@ func (r *KubecostExtractorReconciler) fetch(host, path string, params map[string
return buffer.Bytes(), nil
}

func (r *KubecostExtractorReconciler) getAllocation(ctx context.Context, srv *corev1.Service, servicePort, aggregate string) (*allocationResponse, error) {
func (r *KubecostExtractorReconciler) getAllocation(ctx context.Context, srv *corev1.Service, servicePort, aggregate string, idle bool) (*allocationResponse, error) {
queryParams := map[string]string{
"window": "30d",
"aggregate": aggregate,
"accumulate": "true",
}
if idle {
queryParams["shareIdle"] = "true"
}

var response []byte
var err error
Expand All @@ -239,7 +242,7 @@ func (r *KubecostExtractorReconciler) getAllocation(ctx context.Context, srv *co
func (r *KubecostExtractorReconciler) getRecommendationAttributes(ctx context.Context, srv *corev1.Service, servicePort string, recommendationThreshold float64) ([]*console.ClusterRecommendationAttributes, error) {
var result []*console.ClusterRecommendationAttributes
for _, resourceType := range kubecostResourceTypes {
ar, err := r.getAllocation(ctx, srv, servicePort, resourceType)
ar, err := r.getAllocation(ctx, srv, servicePort, resourceType, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -267,7 +270,7 @@ func (r *KubecostExtractorReconciler) getRecommendationAttributes(ctx context.Co

func (r *KubecostExtractorReconciler) getNamespacesCost(ctx context.Context, srv *corev1.Service, servicePort string) ([]*console.CostAttributes, error) {
var result []*console.CostAttributes
ar, err := r.getAllocation(ctx, srv, servicePort, "namespace")
ar, err := r.getAllocation(ctx, srv, servicePort, "namespace", false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -305,7 +308,7 @@ func (r *KubecostExtractorReconciler) getClusterCost(ctx context.Context, srv *c
if err != nil {
return nil, err
}
ar, err := r.getAllocation(ctx, srv, servicePort, "cluster")
ar, err := r.getAllocation(ctx, srv, servicePort, "cluster", true)
if err != nil {
return nil, err
}
Expand All @@ -327,7 +330,7 @@ func (r *KubecostExtractorReconciler) getClusterCost(ctx context.Context, srv *c
}

func (r *KubecostExtractorReconciler) getControlPlaneCost(ctx context.Context, srv *corev1.Service, servicePort string) (*float64, error) {
ar, err := r.getAllocation(ctx, srv, servicePort, "controller")
ar, err := r.getAllocation(ctx, srv, servicePort, "controller", false)
if err != nil {
return nil, err
}
Expand All @@ -349,7 +352,7 @@ func (r *KubecostExtractorReconciler) getControlPlaneCost(ctx context.Context, s

func (r *KubecostExtractorReconciler) getNodeCost(ctx context.Context, srv *corev1.Service, servicePort string) (*float64, error) {
var totalNodeCost float64
ar, err := r.getAllocation(ctx, srv, servicePort, "node")
ar, err := r.getAllocation(ctx, srv, servicePort, "node", true)
if err != nil {
return nil, err
}
Expand All @@ -360,10 +363,7 @@ func (r *KubecostExtractorReconciler) getNodeCost(ctx context.Context, srv *core
if nodeCosts == nil {
continue
}
for name, allocation := range nodeCosts {
if name == opencost.IdleSuffix {
continue
}
for _, allocation := range nodeCosts {
totalNodeCost += allocation.TotalCost()
}
}
Expand Down
Loading