From 6069f9126e8a8dfa7d143452d46b26c4a92373bc Mon Sep 17 00:00:00 2001 From: Ahmed Samir Date: Wed, 4 Oct 2023 23:47:03 +0300 Subject: [PATCH 1/2] feat: added k8s nodes support Signed-off-by: Ahmed Samir --- providers/k8s/core/nodes.go | 77 +++++++++++++++++++++++++++++++++++++ providers/k8s/k8s.go | 1 + 2 files changed, 78 insertions(+) create mode 100644 providers/k8s/core/nodes.go diff --git a/providers/k8s/core/nodes.go b/providers/k8s/core/nodes.go new file mode 100644 index 000000000..f872e295c --- /dev/null +++ b/providers/k8s/core/nodes.go @@ -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 Nodes(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { + resources := make([]models.Resource, 0) + + var config metav1.ListOptions + + opencostEnabled := true + nodesCost, err := oc.GetOpencostInfo(client.K8sClient.OpencostBaseUrl, "node") + if err != nil { + log.Errorf("ERROR: Couldn't get nodes info from OpenCost: %v", err) + log.Warn("Opencost disabled") + opencostEnabled = false + } + + for { + res, err := client.K8sClient.Client.CoreV1().Nodes().List(ctx, config) + if err != nil { + return nil, err + } + + for _, node := range res.Items { + tags := make([]models.Tag, 0) + + for key, value := range node.Labels { + tags = append(tags, models.Tag{ + Key: key, + Value: value, + }) + } + + cost := 0.0 + if opencostEnabled { + cost = nodesCost[node.Name].TotalCost + } + + resources = append(resources, models.Resource{ + Provider: "Kubernetes", + Account: client.Name, + Service: "Node", + ResourceId: string(node.UID), + Name: node.Name, + Region: node.Namespace, + Cost: cost, + CreatedAt: node.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": "Node", + "resources": len(resources), + }).Info("Fetched resources") + return resources, nil +} diff --git a/providers/k8s/k8s.go b/providers/k8s/k8s.go index b9cd5abd1..55638274c 100644 --- a/providers/k8s/k8s.go +++ b/providers/k8s/k8s.go @@ -21,6 +21,7 @@ func listOfSupportedServices() []providers.FetchDataFunction { core.PersistentVolumeClaims, core.ServiceAccounts, core.Deployments, + core.Nodes, } } From 77fe1ec7378cf39e1ec19e2bea6e8359c27512f8 Mon Sep 17 00:00:00 2001 From: Ahmed Samir Date: Wed, 4 Oct 2023 23:48:33 +0300 Subject: [PATCH 2/2] chore: Add Nodes to list and remove Deployments dup Signed-off-by: Ahmed Samir --- providers/k8s/k8s.go | 1 - 1 file changed, 1 deletion(-) diff --git a/providers/k8s/k8s.go b/providers/k8s/k8s.go index 55638274c..6d675361d 100644 --- a/providers/k8s/k8s.go +++ b/providers/k8s/k8s.go @@ -20,7 +20,6 @@ func listOfSupportedServices() []providers.FetchDataFunction { core.PersistentVolumes, core.PersistentVolumeClaims, core.ServiceAccounts, - core.Deployments, core.Nodes, } }