-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from robscott/rs/containers
Adding Container Level Output, YAML Formatting, and Sorting
- Loading branch information
Showing
17 changed files
with
1,278 additions
and
612 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
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
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,127 @@ | ||
// Copyright 2019 Kube Capacity Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package capacity | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
metrics "k8s.io/metrics/pkg/client/clientset/versioned" | ||
|
||
"github.com/robscott/kube-capacity/pkg/kube" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
v1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1" | ||
) | ||
|
||
// FetchAndPrint gathers cluster resource data and outputs it | ||
func FetchAndPrint(showContainers, showPods, showUtil bool, podLabels, nodeLabels, namespaceLabels, kubeContext, output, sortBy string) { | ||
clientset, err := kube.NewClientSet(kubeContext) | ||
if err != nil { | ||
fmt.Printf("Error connecting to Kubernetes: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
podList, nodeList := getPodsAndNodes(clientset, podLabels, nodeLabels, namespaceLabels) | ||
pmList := &v1beta1.PodMetricsList{} | ||
if showUtil { | ||
mClientset, err := kube.NewMetricsClientSet(kubeContext) | ||
if err != nil { | ||
fmt.Printf("Error connecting to Metrics API: %v\n", err) | ||
os.Exit(4) | ||
} | ||
|
||
pmList = getMetrics(mClientset) | ||
} | ||
|
||
cm := buildClusterMetric(podList, pmList, nodeList) | ||
printList(&cm, showContainers, showPods, showUtil, output, sortBy) | ||
} | ||
|
||
func getPodsAndNodes(clientset kubernetes.Interface, podLabels, nodeLabels, namespaceLabels string) (*corev1.PodList, *corev1.NodeList) { | ||
nodeList, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{ | ||
LabelSelector: nodeLabels, | ||
}) | ||
if err != nil { | ||
fmt.Printf("Error listing Nodes: %v\n", err) | ||
os.Exit(2) | ||
} | ||
|
||
podList, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{ | ||
LabelSelector: podLabels, | ||
}) | ||
if err != nil { | ||
fmt.Printf("Error listing Pods: %v\n", err) | ||
os.Exit(3) | ||
} | ||
|
||
newPodItems := []corev1.Pod{} | ||
|
||
nodes := map[string]bool{} | ||
for _, node := range nodeList.Items { | ||
nodes[node.GetName()] = true | ||
} | ||
|
||
for _, pod := range podList.Items { | ||
if !nodes[pod.Spec.NodeName] { | ||
continue | ||
} | ||
|
||
newPodItems = append(newPodItems, pod) | ||
} | ||
|
||
podList.Items = newPodItems | ||
|
||
if namespaceLabels != "" { | ||
namespaceList, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{ | ||
LabelSelector: namespaceLabels, | ||
}) | ||
if err != nil { | ||
fmt.Printf("Error listing Namespaces: %v\n", err) | ||
os.Exit(3) | ||
} | ||
|
||
namespaces := map[string]bool{} | ||
for _, ns := range namespaceList.Items { | ||
namespaces[ns.GetName()] = true | ||
} | ||
|
||
newPodItems := []corev1.Pod{} | ||
|
||
for _, pod := range podList.Items { | ||
if !namespaces[pod.GetNamespace()] { | ||
continue | ||
} | ||
|
||
newPodItems = append(newPodItems, pod) | ||
} | ||
|
||
podList.Items = newPodItems | ||
} | ||
|
||
return podList, nodeList | ||
} | ||
|
||
func getMetrics(mClientset *metrics.Clientset) *v1beta1.PodMetricsList { | ||
pmList, err := mClientset.MetricsV1beta1().PodMetricses("").List(metav1.ListOptions{}) | ||
if err != nil { | ||
fmt.Printf("Error getting Pod Metrics: %v\n", err) | ||
fmt.Println("For this to work, metrics-server needs to be running in your cluster") | ||
os.Exit(6) | ||
} | ||
|
||
return pmList | ||
} |
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,119 @@ | ||
// Copyright 2019 Kube Capacity Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package capacity | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestGetPodsAndNodes(t *testing.T) { | ||
clientset := fake.NewSimpleClientset( | ||
node("mynode", map[string]string{"hello": "world"}), | ||
node("mynode2", map[string]string{"hello": "world", "moon": "lol"}), | ||
namespace("default", map[string]string{"app": "true"}), | ||
namespace("kube-system", map[string]string{"system": "true"}), | ||
namespace("other", map[string]string{"app": "true", "system": "true"}), | ||
namespace("another", map[string]string{"hello": "world"}), | ||
pod("mynode", "default", "mypod", map[string]string{"a": "test"}), | ||
pod("mynode2", "kube-system", "mypod1", map[string]string{"b": "test"}), | ||
pod("mynode", "other", "mypod2", map[string]string{"c": "test"}), | ||
pod("mynode2", "other", "mypod3", map[string]string{"d": "test"}), | ||
pod("mynode2", "default", "mypod4", map[string]string{"e": "test"}), | ||
pod("mynode", "another", "mypod5", map[string]string{"f": "test"}), | ||
pod("mynode", "default", "mypod6", map[string]string{"g": "test"}), | ||
) | ||
|
||
podList, nodeList := getPodsAndNodes(clientset, "", "", "") | ||
assert.Equal(t, []string{"mynode", "mynode2"}, listNodes(nodeList)) | ||
assert.Equal(t, []string{ | ||
"default/mypod", "kube-system/mypod1", "other/mypod2", "other/mypod3", "default/mypod4", | ||
"another/mypod5", "default/mypod6", | ||
}, listPods(podList)) | ||
|
||
podList, nodeList = getPodsAndNodes(clientset, "", "hello=world", "") | ||
assert.Equal(t, []string{"mynode", "mynode2"}, listNodes(nodeList)) | ||
assert.Equal(t, []string{ | ||
"default/mypod", "kube-system/mypod1", "other/mypod2", "other/mypod3", "default/mypod4", | ||
"another/mypod5", "default/mypod6", | ||
}, listPods(podList)) | ||
|
||
podList, nodeList = getPodsAndNodes(clientset, "", "moon=lol", "") | ||
assert.Equal(t, []string{"mynode2"}, listNodes(nodeList)) | ||
assert.Equal(t, []string{ | ||
"kube-system/mypod1", "other/mypod3", "default/mypod4", | ||
}, listPods(podList)) | ||
|
||
podList, nodeList = getPodsAndNodes(clientset, "a=test", "", "") | ||
assert.Equal(t, []string{"mynode", "mynode2"}, listNodes(nodeList)) | ||
assert.Equal(t, []string{ | ||
"default/mypod", | ||
}, listPods(podList)) | ||
|
||
podList, nodeList = getPodsAndNodes(clientset, "a=test,b!=test", "", "app=true") | ||
assert.Equal(t, []string{"mynode", "mynode2"}, listNodes(nodeList)) | ||
assert.Equal(t, []string{ | ||
"default/mypod", | ||
}, listPods(podList)) | ||
} | ||
|
||
func node(name string, labels map[string]string) *corev1.Node { | ||
return &corev1.Node{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Node", | ||
APIVersion: "v1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Labels: labels, | ||
}, | ||
} | ||
} | ||
|
||
func namespace(name string, labels map[string]string) *corev1.Namespace { | ||
return &corev1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Namespace", | ||
APIVersion: "v1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Labels: labels, | ||
}, | ||
} | ||
} | ||
|
||
func pod(node, namespace, name string, labels map[string]string) *corev1.Pod { | ||
return &corev1.Pod{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Pod", | ||
APIVersion: "v1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
Labels: labels, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
NodeName: node, | ||
}, | ||
} | ||
} |
Oops, something went wrong.