Skip to content

Commit

Permalink
Merge pull request #1045 from shubhindia/feature/add-cost-for-services
Browse files Browse the repository at this point in the history
add cost for k8s services
  • Loading branch information
Azanul authored Oct 6, 2023
2 parents 5b4c2e4 + be8fe3d commit 9a556a0
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion providers/k8s/core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

. "github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
oc "github.com/tailwarden/komiser/providers/k8s/opencost"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -16,6 +17,14 @@ func Services(ctx context.Context, client providers.ProviderClient) ([]Resource,

var config metav1.ListOptions

opencostEnabled := true
serviceCost, err := oc.GetOpencostInfo(client.K8sClient.OpencostBaseUrl, "service")
if err != nil {
log.Errorf("ERROR: Couldn't get service info from OpenCost: %v", err)
log.Warn("Opencost disabled")
opencostEnabled = false
}

for {
res, err := client.K8sClient.Client.CoreV1().Services("").List(ctx, config)
if err != nil {
Expand All @@ -32,14 +41,19 @@ func Services(ctx context.Context, client providers.ProviderClient) ([]Resource,
})
}

cost := 0.0
if opencostEnabled {
cost = serviceCost[service.Name].TotalCost
}

resources = append(resources, Resource{
Provider: "Kubernetes",
Account: client.Name,
Service: "Service",
ResourceId: string(service.UID),
Name: service.Name,
Region: service.Namespace,
Cost: 0,
Cost: cost,
CreatedAt: service.CreationTimestamp.Time,
FetchedAt: time.Now(),
Tags: tags,
Expand Down

0 comments on commit 9a556a0

Please sign in to comment.