Skip to content

Commit

Permalink
Rework kubecost extractor for in-cluster deployment (#323)
Browse files Browse the repository at this point in the history
Need to remove all kube proxy usage, also bump timeouts and change window to explicit "monthly" param
  • Loading branch information
michaeljguarino authored Dec 19, 2024
1 parent 145848b commit 9f39974
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions internal/controller/kubecostextractor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ func (r *KubecostExtractorReconciler) Reconcile(ctx context.Context, req ctrl.Re
reterr = err
}
}()
recommendations, err := r.getRecommendationAttributes(ctx, kubecostService, kubecost.Spec.GetPort(), kubecost.Spec.GetInterval(), recommendationThreshold)
if err != nil {
logger.Error(err, "Unable to fetch recommendations")
utils.MarkCondition(kubecost.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionFalse, v1alpha1.ErrorConditionReason, err.Error())
return false, nil
}
clusterCostAttr, err := r.getClusterCost(ctx, kubecostService, kubecost.Spec.GetPort(), kubecost.Spec.GetInterval())
if err != nil {
logger.Error(err, "Unable to fetch cluster cost")
Expand All @@ -153,6 +147,13 @@ func (r *KubecostExtractorReconciler) Reconcile(ctx context.Context, req ctrl.Re
return false, nil
}

recommendations, err := r.getRecommendationAttributes(ctx, kubecostService, kubecost.Spec.GetPort(), kubecost.Spec.GetInterval(), recommendationThreshold)
if err != nil {
logger.Error(err, "Unable to fetch recommendations")
// utils.MarkCondition(kubecost.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionFalse, v1alpha1.ErrorConditionReason, err.Error())
// return false, nil
}

// nothing for specified time window
if clusterCostAttr == nil && namespacesCostAtrr == nil && recommendations == nil {
utils.MarkCondition(kubecost.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionTrue, v1alpha1.ReadyConditionReason, "")
Expand All @@ -176,19 +177,24 @@ func (r *KubecostExtractorReconciler) Reconcile(ctx context.Context, req ctrl.Re
}

func (r *KubecostExtractorReconciler) fetch(host, path string, params map[string]string) ([]byte, error) {
query := ""
if len(params) > 0 {
urlParams := url.Values{}
for k, v := range params {
urlParams.Add(k, v)
}
query = "?" + urlParams.Encode()
}

tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
ResponseHeaderTimeout: 60 * time.Second,
}
client := &http.Client{Transport: tr}
urlParams := url.Values{}
for k, v := range params {
urlParams.Add(k, v)
ResponseHeaderTimeout: 120 * time.Second,
}

resp, err := client.Get(fmt.Sprintf("http://%s%s?%s", host, path, urlParams.Encode()))
client := &http.Client{Transport: tr}
resp, err := client.Get(fmt.Sprintf("http://%s%s%s", host, path, query))
if err != nil {
return nil, err
}
Expand All @@ -204,12 +210,8 @@ func (r *KubecostExtractorReconciler) fetch(host, path string, params map[string
}

func (r *KubecostExtractorReconciler) getAllocation(ctx context.Context, srv *corev1.Service, servicePort, aggregate string) (*allocationResponse, error) {
now := time.Now()
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,
"window": "month",
"aggregate": aggregate,
"accumulate": "true",
}
Expand Down Expand Up @@ -281,7 +283,7 @@ func (r *KubecostExtractorReconciler) getNamespacesCost(ctx context.Context, srv
}

func (r *KubecostExtractorReconciler) getClusterCost(ctx context.Context, srv *corev1.Service, servicePort string, interval time.Duration) (*console.CostAttributes, error) {
bytes, err := r.KubeClient.CoreV1().Services(srv.Namespace).ProxyGet("", srv.Name, servicePort, "/model/clusterInfo", nil).DoRaw(ctx)
bytes, err := r.fetch(fmt.Sprintf("%s.%s:%s", srv.Name, srv.Namespace, servicePort), "/model/clusterInfo", nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9f39974

Please sign in to comment.