-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (59 loc) · 1.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/ykoer/kube-allocated-resources/resources"
"gopkg.in/yaml.v2"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
// Define application options
options := &resources.Options{}
// TODO: Add long options
flag.StringVar(&options.Labels, "l", "node-role.kubernetes.io/worker=", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
flag.BoolVar(&options.GetTotalsByInstanceType, "g", false, "Return totals grouped by the instance type")
flag.BoolVar(&options.GetNodeDetails, "d", false, "Return node details")
outputFormat := flag.String("o", "json", "Output format. One of: json or yaml")
flag.Parse()
client := resources.NewAllocatedResourcesClient(createKubeClient(), options)
clusterMetrics, err := client.GetAllocatedResources()
if err != nil {
panic(err)
}
var clusterMetricsOutput []byte
if *outputFormat == "json" {
clusterMetricsOutput, err = json.Marshal(clusterMetrics)
} else if *outputFormat == "yaml" {
clusterMetricsOutput, err = yaml.Marshal(clusterMetrics)
}
if err != nil {
panic(err)
}
fmt.Println(string(clusterMetricsOutput))
}
func createKubeClient() *kubernetes.Clientset {
config, err := getConfig()
if err != nil {
return nil
}
return kubernetes.NewForConfigOrDie(config)
}
func getConfig() (*rest.Config, error) {
var config *rest.Config
var err error
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
if kubeconfig == "" {
config, err = rest.InClusterConfig()
} else {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
}
if err != nil {
return nil, err
}
return config, nil
}