forked from ohauer/PodResourceCalculator
-
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.
- Loading branch information
0 parents
commit 11d434e
Showing
7 changed files
with
452 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.xlsx | ||
*.bin |
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,2 @@ | ||
A K8s resource spreadsheet calculator tool | ||
See details on https://medium.com/@zhimin.wen/pod-resource-spreadsheet-calculator-22fc5c6173b9 |
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,29 @@ | ||
module main | ||
|
||
go 1.14 | ||
|
||
replace pkg/k8sDiscovery => ./pkg/k8sDiscovery | ||
|
||
require ( | ||
github.com/360EntSecGroup-Skylar/excelize v1.4.1 | ||
github.com/360EntSecGroup-Skylar/excelize/v2 v2.2.0 | ||
github.com/MakeNowJust/heredoc v1.0.0 // indirect | ||
github.com/Masterminds/goutils v1.1.0 // indirect | ||
github.com/Masterminds/semver v1.5.0 // indirect | ||
github.com/Masterminds/sprig v2.22.0+incompatible // indirect | ||
github.com/googleapis/gnostic v0.5.1 // indirect | ||
github.com/huandu/xstrings v1.3.2 // indirect | ||
github.com/imdario/mergo v0.3.10 // indirect | ||
github.com/mitchellh/copystructure v1.0.0 // indirect | ||
github.com/sirupsen/logrus v1.6.0 | ||
github.com/zhiminwen/quote v0.0.0-20200612004834-54f3725dbd6a | ||
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 // indirect | ||
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect | ||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect | ||
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect | ||
k8s.io/api v0.18.6 // indirect | ||
k8s.io/apimachinery v0.18.6 | ||
k8s.io/client-go v0.18.6 | ||
k8s.io/utils v0.0.0-20200724153422-f32512634ab7 // indirect | ||
pkg/k8sDiscovery v0.0.0-00010101000000-000000000000 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
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,81 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"pkg/k8sDiscovery" | ||
|
||
"github.com/360EntSecGroup-Skylar/excelize/v2" | ||
"github.com/sirupsen/logrus" | ||
"github.com/zhiminwen/quote" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func main() { | ||
namespace := os.Getenv("K8S_NAMESPACE") | ||
clientSet, _, err := k8sDiscovery.K8s() | ||
if err != nil { | ||
logrus.Fatalf("Failed to connect to K8s:%v", err) | ||
} | ||
|
||
pods, err := clientSet.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{}) | ||
if err != nil { | ||
logrus.Fatalf("Failed to connect to pods:%v", err) | ||
} | ||
|
||
f := excelize.NewFile() | ||
f.SetActiveSheet(f.NewSheet("Sheet1")) | ||
|
||
header := quote.Word(`Namespace Pod Node Container Request.Cpu Request.Cpu(Canonical) Request.Mem Request.Mem(Canonical) Limits.Cpu Limits.Cpu(Canonical) Limits.Mem Limits.Mem(Canonical) `) | ||
err = f.SetSheetRow("Sheet1", "A2", &header) | ||
if err != nil { | ||
logrus.Fatalf("Failed to save title row:%v", err) | ||
} | ||
err = f.AutoFilter("Sheet1", "A2", "L2", "") | ||
if err != nil { | ||
logrus.Fatalf("Failed to set auto filter on title row:%v", err) | ||
} | ||
|
||
row := 3 | ||
for _, p := range pods.Items { | ||
for _, c := range p.Spec.Containers { | ||
reqCpu := c.Resources.Requests.Cpu() | ||
reqMem := c.Resources.Requests.Memory() | ||
limCpu := c.Resources.Limits.Cpu() | ||
limMem := c.Resources.Limits.Memory() | ||
|
||
cellName, err := excelize.CoordinatesToCellName(1, row) | ||
if err != nil { | ||
log.Fatalf("Could not get cell name from row: %v", err) | ||
} | ||
err = f.SetSheetRow("Sheet1", cellName, | ||
&[]interface{}{ | ||
p.Namespace, | ||
p.Name, | ||
p.Status.HostIP, | ||
c.Name, | ||
reqCpu.MilliValue(), reqCpu, | ||
reqMem.Value(), reqMem, | ||
limCpu.MilliValue(), limCpu, | ||
limMem.Value(), limMem, | ||
}) | ||
if err != nil { | ||
logrus.Fatalf("Failed to save for pod:%v", p.Name) | ||
} | ||
row = row + 1 | ||
|
||
// logrus.Infof("save as %s", cellName) | ||
} | ||
} | ||
|
||
f.SetCellFormula("Sheet1", "E1", fmt.Sprintf(`subtotal(109, E3:E%d)/1000`, row)) //cpu | ||
f.SetCellFormula("Sheet1", "G1", fmt.Sprintf(`subtotal(109, G3:G%d)/1024/1024/1024`, row)) // mem | ||
f.SetCellFormula("Sheet1", "I1", fmt.Sprintf(`subtotal(109, I3:I%d)/1000`, row)) | ||
f.SetCellFormula("Sheet1", "K1", fmt.Sprintf(`subtotal(109, K3:K%d)/1024/1024/1024`, row)) | ||
|
||
if err = f.SaveAs("resource.xlsx"); err != nil { | ||
logrus.Fatalf("Failed to save as xlsx:%v", err) | ||
} | ||
} |
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,68 @@ | ||
package k8sDiscovery | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func K8s() (kubernetes.Interface, *rest.Config, error) { | ||
if _, inCluster := os.LookupEnv("KUBERNETES_SERVICE_HOST"); inCluster == true { | ||
log.Infof("inside cluster, using in-cluster configuration") | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
log.Errorf("Failed to get incluster config:%v", err) | ||
return nil, nil, err | ||
} | ||
clientSet, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
log.Errorf("Failed to construct the clientSet:%v", err) | ||
return nil, nil, err | ||
} | ||
return clientSet, config, nil | ||
} | ||
|
||
log.Infof("outside of cluster") | ||
var kubeconfig *string | ||
if home := homeDir(); home != "" { | ||
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | ||
} else { | ||
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | ||
} | ||
flag.Parse() | ||
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | ||
if err != nil { | ||
log.Errorf("Failed to build the config:%v", err) | ||
return nil, nil, err | ||
} | ||
clientSet, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
log.Errorf("Failed to construct the clientSet:%v", err) | ||
return nil, nil, err | ||
} | ||
return clientSet, config, nil | ||
} | ||
|
||
func homeDir() string { | ||
if h := os.Getenv("HOME"); h != "" { | ||
return h | ||
} | ||
return os.Getenv("USERPROFILE") | ||
} | ||
|
||
//for testing | ||
func GetServerVersion(clientSet kubernetes.Interface) (string, error) { | ||
version, err := clientSet.Discovery().ServerVersion() | ||
if err != nil { | ||
log.Errorf("Failed to get server version:%v", err) | ||
return "", err | ||
} | ||
return fmt.Sprintf("%s", version), nil | ||
} |
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,3 @@ | ||
module k8sDiscovery | ||
|
||
go 1.14 |