-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize with parallelism and .CSV file support (#1)
* Refactor * Upgrade with support to .csv * Updated Readme.md Co-authored-by: Matteo Gazzadi Poggioli <ITGAZZADIM@tetrapak.com>
- Loading branch information
1 parent
910e6fd
commit 4a3e36b
Showing
9 changed files
with
346 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package calculator | ||
|
||
import ( | ||
"sync" | ||
|
||
domain "github.com/matteogazzadi/kuberes/pkg/domain" | ||
helper "github.com/matteogazzadi/kuberes/pkg/helpers" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// Calculate Resources | ||
func CalculateResources(groupByNamespace bool, pods *[]v1.Pod, resources *[]domain.K8sStats) { | ||
|
||
// Statistics Channel | ||
k8sStatsChan := make(chan domain.K8sStats, len(*pods)) | ||
|
||
// Wait Group | ||
var wg sync.WaitGroup | ||
|
||
for _, pod := range *pods { | ||
wg.Add(1) | ||
go helper.GetPodStats(pod, k8sStatsChan, &wg) | ||
} | ||
|
||
wg.Wait() | ||
close(k8sStatsChan) | ||
|
||
if groupByNamespace { | ||
resByNs := make(map[string]*domain.K8sStats) | ||
|
||
for stats := range k8sStatsChan { | ||
|
||
curStats, ok := resByNs[stats.Namespace] | ||
|
||
if ok { | ||
// Update current stats | ||
curStats.Cpu.Limit += stats.Cpu.Limit | ||
curStats.Cpu.Request += stats.Cpu.Request | ||
curStats.Memory.Limit += stats.Memory.Limit | ||
curStats.Memory.Request += stats.Memory.Request | ||
} else { | ||
// Create new stats | ||
var newStats domain.K8sStats | ||
|
||
newStats.Namespace = stats.Namespace | ||
newStats.Cpu = stats.Cpu | ||
newStats.Memory = stats.Memory | ||
|
||
resByNs[stats.Namespace] = &newStats | ||
} | ||
} | ||
|
||
for _, stats := range resByNs { | ||
*resources = append(*resources, *stats) | ||
} | ||
|
||
} else { | ||
for stats := range k8sStatsChan { | ||
*resources = append(*resources, stats) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"sync" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// Get all Pods for a given namespace | ||
func GetPodsByNamespace(clientset *kubernetes.Clientset, ctx context.Context, namespace string, podChan chan<- []v1.Pod, wg *sync.WaitGroup) { | ||
|
||
defer wg.Done() | ||
|
||
// Get pods | ||
pods, err := clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{}) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} else { | ||
podChan <- pods.Items | ||
} | ||
} | ||
|
||
// Get All Namespaces in the cluster | ||
func GetAllNamespace(clientset *kubernetes.Clientset, ctx context.Context) ([]v1.Namespace, error) { | ||
namespaces, err := clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return namespaces.Items, nil | ||
} | ||
|
||
// Get all Pods | ||
func GetAllPods(clientset *kubernetes.Clientset, ctx context.Context) ([]v1.Pod, error) { | ||
|
||
namespaces, err := GetAllNamespace(clientset, ctx) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
|
||
// Pod Channel | ||
podChannel := make(chan []v1.Pod, len(namespaces)) | ||
|
||
// Wait Group | ||
var wg sync.WaitGroup | ||
|
||
// Loop on Namespace | ||
for _, namespace := range namespaces { | ||
wg.Add(1) | ||
go GetPodsByNamespace(clientset, ctx, namespace.Name, podChannel, &wg) | ||
} | ||
|
||
wg.Wait() | ||
close(podChannel) | ||
|
||
var pods []v1.Pod | ||
|
||
for podList := range podChannel { | ||
for _, pod := range podList { | ||
pods = append(pods, pod) | ||
} | ||
} | ||
|
||
return pods, nil | ||
} |
Oops, something went wrong.