Skip to content

Commit

Permalink
support for wide out, beginning to experiment with metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
robscott committed Feb 2, 2019
1 parent 3669d19 commit 2ee6276
Show file tree
Hide file tree
Showing 105 changed files with 13,554 additions and 16 deletions.
17 changes: 17 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 105 additions & 16 deletions pkg/capacity/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
package capacity

import (
"encoding/json"
"fmt"
"os"
"sort"
"text/tabwriter"
"time"

"github.com/robscott/kube-capacity/pkg/kube"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

func List(args []string, outputFormat string) {
Expand All @@ -32,6 +35,12 @@ func List(args []string, outputFormat string) {
panic(err.Error())
}

mClientset, err := kube.NewMetricsClientSet()
if err != nil {
fmt.Println("Error connecting to Metrics Server")
panic(err.Error())
}

nodeList, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
fmt.Println("Error listing Nodes")
Expand Down Expand Up @@ -75,10 +84,34 @@ func List(args []string, outputFormat string) {
cr.addNodeCapacity(cr.capacityByNode[node.Name])
}

printList(cr)
nmList, err := mClientset.MetricsV1beta1().NodeMetricses().List(metav1.ListOptions{})
if err != nil {
fmt.Println("Error getting metrics")
panic(err.Error())
}

fmt.Printf("========> %#v\n", nmList)

for _, nm := range nmList.Items {
fmt.Printf("nm =============> %#v\n", nm)
}

pmList, err := mClientset.MetricsV1beta1().PodMetricses("").List(metav1.ListOptions{})
if err != nil {
fmt.Println("Error getting metrics")
panic(err.Error())
}

fmt.Printf("pmList ========> %#v\n", pmList)

for _, pm := range pmList.Items {
fmt.Printf("nm =============> %#v\n", pm)
}

printList(cr, outputFormat)
}

func printList(cr clusterResource) {
func printList(cr clusterResource, outputFormat string) {
names := make([]string, len(cr.capacityByNode))

i := 0
Expand All @@ -90,26 +123,82 @@ func printList(cr clusterResource) {

w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintln(w, "NODE\t NAMESPACE\t POD\t CPU REQUESTS \t CPU LIMITS \t MEMORY REQUESTS \t MEMORY LIMITS")

fmt.Fprintf(w, "* \t *\t *\t %s \t %s \t %s \t %s \n",
cr.cpuRequestString(), cr.cpuLimitString(),
cr.memRequestString(), cr.memLimitString())
if outputFormat == "wide" {
fmt.Fprintln(w, "NODE\t NAMESPACE\t POD\t CPU REQUESTS \t CPU LIMITS \t MEMORY REQUESTS \t MEMORY LIMITS")

if len(names) > 1 {
fmt.Fprintf(w, "* \t *\t *\t %s \t %s \t %s \t %s \n",
cr.cpuRequestString(), cr.cpuLimitString(),
cr.memRequestString(), cr.memLimitString())
}
} else {
fmt.Fprintln(w, "NODE\t CPU REQUESTS \t CPU LIMITS \t MEMORY REQUESTS \t MEMORY LIMITS")

if len(names) > 1 {
fmt.Fprintf(w, "* \t %s \t %s \t %s \t %s \n",
cr.cpuRequestString(), cr.cpuLimitString(),
cr.memRequestString(), cr.memLimitString())
}
}

for _, name := range names {
cap := cr.capacityByNode[name]
fmt.Fprintf(w, "%s \t *\t *\t %s \t %s \t %s \t %s \n", name,
cap.cpuRequestString(), cap.cpuLimitString(),
cap.memRequestString(), cap.memLimitString())

for _, pod := range cap.podResources {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \t %s \t %s \n", name,
pod.namespace, pod.name,
pod.cpuRequestString(cap), pod.cpuLimitString(cap),
pod.memRequestString(cap), pod.memLimitString(cap))

if outputFormat == "wide" {
fmt.Fprintf(w, "%s \t *\t *\t %s \t %s \t %s \t %s \n", name,
cap.cpuRequestString(), cap.cpuLimitString(),
cap.memRequestString(), cap.memLimitString())

for _, pod := range cap.podResources {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \t %s \t %s \n", name,
pod.namespace, pod.name,
pod.cpuRequestString(cap), pod.cpuLimitString(cap),
pod.memRequestString(cap), pod.memLimitString(cap))
}
fmt.Fprintln(w)
} else {
fmt.Fprintf(w, "%s \t %s \t %s \t %s \t %s \n", name,
cap.cpuRequestString(), cap.cpuLimitString(),
cap.memRequestString(), cap.memLimitString())
}
fmt.Fprintln(w)
}

w.Flush()
}

// PodMetricsList : PodMetricsList
type PodMetricsList struct {
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Metadata struct {
SelfLink string `json:"selfLink"`
} `json:"metadata"`
Items []struct {
Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
SelfLink string `json:"selfLink"`
CreationTimestamp time.Time `json:"creationTimestamp"`
} `json:"metadata"`
Timestamp time.Time `json:"timestamp"`
Window string `json:"window"`
Containers []struct {
Name string `json:"name"`
Usage struct {
CPU string `json:"cpu"`
Memory string `json:"memory"`
} `json:"usage"`
} `json:"containers"`
} `json:"items"`
}

func getMetrics(clientset *kubernetes.Clientset) (*PodMetricsList, error) {
data, err := clientset.RESTClient().Get().AbsPath("apis/metrics.k8s.io/v1beta1/pods").DoRaw()
if err != nil {
return nil, err
}
pods := &PodMetricsList{}
err = json.Unmarshal(data, pods)
return pods, err
}
10 changes: 10 additions & 0 deletions pkg/kube/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
metrics "k8s.io/metrics/pkg/client/clientset/versioned"

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
Expand All @@ -36,6 +37,15 @@ func NewClientSet() (*kubernetes.Clientset, error) {
return kubernetes.NewForConfig(config)
}

func NewMetricsClientSet() (*metrics.Clientset, error) {
config, err := getKubeConfig()
if err != nil {
return nil, err
}

return metrics.NewForConfig(config)
}

func getKubeConfig() (*rest.Config, error) {
var kubeconfig string
if os.Getenv("KUBECONFIG") != "" {
Expand Down
2 changes: 2 additions & 0 deletions vendor/k8s.io/metrics/.github/PULL_REQUEST_TEMPLATE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/k8s.io/metrics/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ee6276

Please sign in to comment.