Skip to content

Commit

Permalink
Merge pull request #475 from openinfradev/namespace_resources
Browse files Browse the repository at this point in the history
feature. implementation calculating resources for namespace
  • Loading branch information
ktkfree authored May 7, 2024
2 parents 624efe6 + a9a70f4 commit 8c5ac83
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/delivery/http/app-serve-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (h *AppServeAppHandler) CreateAppServeApp(w http.ResponseWriter, r *http.Re
appReq := domain.CreateAppServeAppRequest{}
err := UnmarshalRequestInput(r, &appReq)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Error while unmarshalling request"), "C_INTERNAL_ERROR", ""))
ErrorJSON(w, r, err)
return
}

Expand Down
5 changes: 5 additions & 0 deletions internal/usecase/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,11 @@ func (u *DashboardUsecase) GetThanosClient(ctx context.Context, organizationId s
return nil, httpErrors.NewInternalServerError(err, "D_INVALID_PRIMARY_STACK", "")
}
address, port := helper.SplitAddress(ctx, thanosUrl)

// [TEST]
//address = "http://a93c60de70c794ef39b495976588c989-d7cd29ca75def693.elb.ap-northeast-2.amazonaws.com"
//port = 9090

client, err := thanos.New(address, port, false, "")
if err != nil {
return nil, errors.Wrap(err, "failed to create thanos client")
Expand Down
40 changes: 30 additions & 10 deletions internal/usecase/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,19 +911,39 @@ func (u *ProjectUsecase) GetResourcesUsage(ctx context.Context, thanosClient tha
if err != nil {
return out, errors.Wrap(err, fmt.Sprintf("Failed to get cluster : stackId %s", stackId))
}
/*
query := "sum(rate(container_cpu_usage_seconds_total{image!=\"\"}[10m]) ) by (taco_cluster, namespace)"
result, err := thanosClient.Get(ctx, query)
if err != nil {
return out, err

// sum(rate(container_cpu_usage_seconds_total{taco_cluster=\"$taco_cluster\",image!=\"\"}[$__rate_interval])) by (namespace)
query := fmt.Sprintf("sum(rate(container_cpu_usage_seconds_total{image!=\"\", namespace=\"%s\"}[10m]) ) by (taco_cluster, namespace)", namespace)
result, err := thanosClient.Get(ctx, query)
if err != nil {
return out, err
}
for _, val := range result.Data.Result {
if val.Metric.TacoCluster == stackId.String() {
if val.Metric.Namespace == namespace {
if s, err := strconv.ParseFloat(val.Value[1].(string), 32); err == nil {
out.Cpu = fmt.Sprintf("%0.2f %%", s*100)
}
}
}
log.Info(ctx, helper.ModelToJson(result))
}

*/
// sum(container_memory_working_set_bytes{taco_cluster=\"$taco_cluster\",image!=\"\"}) by (namespace)
query = fmt.Sprintf("sum(container_memory_working_set_bytes{image!=\"\", namespace=\"%s\"}) by (taco_cluster, namespace)", namespace)
result, err = thanosClient.Get(ctx, query)
if err != nil {
return out, err
}
for _, val := range result.Data.Result {
if val.Metric.TacoCluster == stackId.String() {
if val.Metric.Namespace == namespace {
memory, _ := strconv.Atoi(val.Value[1].(string))
out.Memory = fmt.Sprintf("%d MiB", memory/1024/1024)
}
}
}

out.Cpu = "1.0 %"
out.Memory = "2.0 %"
out.Storage = "3.0 %"
out.Storage = ""

return
}
Expand Down
5 changes: 3 additions & 2 deletions internal/validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"context"
"regexp"
"strings"
"unicode/utf8"
Expand All @@ -10,7 +11,7 @@ import (
validator "github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/opentracing/opentracing-go/log"
"github.com/openinfradev/tks-api/pkg/log"
)

const (
Expand All @@ -30,7 +31,7 @@ func NewValidator() (*validator.Validate, *ut.UniversalTranslator) {
v := validator.New()
err := en_translations.RegisterDefaultTranslations(v, trans)
if err != nil {
log.Error(err)
log.Error(context.TODO(), err)
}

// register custom validator
Expand Down
1 change: 1 addition & 0 deletions pkg/thanos-client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type MetricDataResult struct {

type MetricDataResultMetric struct {
Name string `json:"__name__"`
Namespace string `json:"namespace,omitempty"`
TacoCluster string `json:"taco_cluster"`
}

Expand Down

0 comments on commit 8c5ac83

Please sign in to comment.