Skip to content

Commit

Permalink
set one mont window
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Dec 18, 2024
1 parent b8ab9e9 commit 79e9496
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/samples/kubecostExtractor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
app.kubernetes.io/created-by: deployment-operator
name: default
spec:
interval: "1h1m"
interval: "10h"
recommendationThreshold: "0.00002"
kubecostServiceRef:
name: kubecost-cost-analyzer
Expand Down
13 changes: 6 additions & 7 deletions internal/controller/kubecostextractor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,10 @@ func (r *KubecostExtractorReconciler) Reconcile(ctx context.Context, req ctrl.Re
return ctrl.Result{}, nil
}

func (r *KubecostExtractorReconciler) getAllocation(ctx context.Context, srv *corev1.Service, servicePort, aggregate string, interval time.Duration) (*allocationResponse, error) {
// calculate window from interval
func (r *KubecostExtractorReconciler) getAllocation(ctx context.Context, srv *corev1.Service, servicePort, aggregate string) (*allocationResponse, error) {
now := time.Now()
subtractedTime := now.Add(-interval)
window := fmt.Sprintf("%d,%d", subtractedTime.Unix(), now.Unix())
oneMonthBefore := now.AddDate(0, -1, 0) // 0 years, -1 month, 0 days
window := fmt.Sprintf("%d,%d", oneMonthBefore.Unix(), now.Unix())

queryParams := map[string]string{
"window": window,
Expand All @@ -198,7 +197,7 @@ func (r *KubecostExtractorReconciler) getAllocation(ctx context.Context, srv *co
func (r *KubecostExtractorReconciler) getRecommendationAttributes(ctx context.Context, srv *corev1.Service, servicePort string, interval time.Duration, recommendationThreshold float64) ([]*console.ClusterRecommendationAttributes, error) {
var result []*console.ClusterRecommendationAttributes
for _, resourceType := range kubecostResourceTypes {
ar, err := r.getAllocation(ctx, srv, servicePort, resourceType, interval)
ar, err := r.getAllocation(ctx, srv, servicePort, resourceType)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -226,7 +225,7 @@ func (r *KubecostExtractorReconciler) getRecommendationAttributes(ctx context.Co

func (r *KubecostExtractorReconciler) getNamespacesCost(ctx context.Context, srv *corev1.Service, servicePort string, interval time.Duration) ([]*console.CostAttributes, error) {
var result []*console.CostAttributes
ar, err := r.getAllocation(ctx, srv, servicePort, "namespace", interval)
ar, err := r.getAllocation(ctx, srv, servicePort, "namespace")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -261,7 +260,7 @@ func (r *KubecostExtractorReconciler) getClusterCost(ctx context.Context, srv *c
return nil, err
}

ar, err := r.getAllocation(ctx, srv, servicePort, "cluster", interval)
ar, err := r.getAllocation(ctx, srv, servicePort, "cluster")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 79e9496

Please sign in to comment.