Skip to content

Commit

Permalink
Merge pull request tailwarden#1057 from asamirr/feat/support-k8s-cont…
Browse files Browse the repository at this point in the history
…rollers

feat: support k8s controllers
  • Loading branch information
mlabouardy authored Oct 9, 2023
2 parents 9c54d99 + b0bb059 commit 6120697
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
77 changes: 77 additions & 0 deletions providers/k8s/core/daemonsets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package core

import (
"context"
"time"

log "github.com/sirupsen/logrus"

"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"
)

func DaemonSets(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)

var config metav1.ListOptions

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

for {
res, err := client.K8sClient.Client.AppsV1().DaemonSets("").List(ctx, config)
if err != nil {
return nil, err
}

for _, daemonset := range res.Items {
tags := make([]models.Tag, 0)

for key, value := range daemonset.Labels {
tags = append(tags, models.Tag{
Key: key,
Value: value,
})
}

cost := 0.0
if opencostEnabled {
cost = daemonsetsCost[daemonset.Name].TotalCost
}

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

if res.GetContinue() == "" {
break
}

config.Continue = res.GetContinue()
}

log.WithFields(log.Fields{
"provider": "Kubernetes",
"account": client.Name,
"service": "DaemonSet",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}
77 changes: 77 additions & 0 deletions providers/k8s/core/jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package core

import (
"context"
"time"

log "github.com/sirupsen/logrus"

"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"
)

func Jobs(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)

var config metav1.ListOptions

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

for {
res, err := client.K8sClient.Client.BatchV1().Jobs("").List(ctx, config)
if err != nil {
return nil, err
}

for _, job := range res.Items {
tags := make([]models.Tag, 0)

for key, value := range job.Labels {
tags = append(tags, models.Tag{
Key: key,
Value: value,
})
}

cost := 0.0
if opencostEnabled {
cost = jobsCost[job.Name].TotalCost
}

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

if res.GetContinue() == "" {
break
}

config.Continue = res.GetContinue()
}

log.WithFields(log.Fields{
"provider": "Kubernetes",
"account": client.Name,
"service": "DaemonSet",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}
77 changes: 77 additions & 0 deletions providers/k8s/core/statefulsets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package core

import (
"context"
"time"

log "github.com/sirupsen/logrus"

"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"
)

func StatefulSets(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)

var config metav1.ListOptions

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

for {
res, err := client.K8sClient.Client.AppsV1().StatefulSets("").List(ctx, config)
if err != nil {
return nil, err
}

for _, statefulset := range res.Items {
tags := make([]models.Tag, 0)

for key, value := range statefulset.Labels {
tags = append(tags, models.Tag{
Key: key,
Value: value,
})
}

cost := 0.0
if opencostEnabled {
cost = statefulsetsCost[statefulset.Name].TotalCost
}

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

if res.GetContinue() == "" {
break
}

config.Continue = res.GetContinue()
}

log.WithFields(log.Fields{
"provider": "Kubernetes",
"account": client.Name,
"service": "StatefulSet",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}
3 changes: 3 additions & 0 deletions providers/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func listOfSupportedServices() []providers.FetchDataFunction {
core.ServiceAccounts,
core.Nodes,
core.Namespaces,
core.DaemonSets,
core.StatefulSets,
core.Jobs,
}
}

Expand Down

0 comments on commit 6120697

Please sign in to comment.