From 3f21071064b43432c50cd0787e98d780ce26560e Mon Sep 17 00:00:00 2001 From: Marcin Wielgus Date: Wed, 2 Sep 2015 12:20:11 +0200 Subject: [PATCH] HorizontalPodAutoscaler in kubectl get --- contrib/completions/bash/kubectl | 3 ++ pkg/expapi/types.go | 1 + pkg/kubectl/kubectl.go | 1 + pkg/kubectl/resource_printer.go | 49 ++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index c096937e22c..365496f8aed 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -285,6 +285,7 @@ _kubectl_get() must_have_one_noun+=("deployment") must_have_one_noun+=("endpoints") must_have_one_noun+=("event") + must_have_one_noun+=("horizontalpodautoscaler") must_have_one_noun+=("limitrange") must_have_one_noun+=("namespace") must_have_one_noun+=("node") @@ -460,6 +461,7 @@ _kubectl_delete() must_have_one_noun+=("deployment") must_have_one_noun+=("endpoints") must_have_one_noun+=("event") + must_have_one_noun+=("horizontalpodautoscaler") must_have_one_noun+=("limitrange") must_have_one_noun+=("namespace") must_have_one_noun+=("node") @@ -848,6 +850,7 @@ _kubectl_label() must_have_one_noun+=("deployment") must_have_one_noun+=("endpoints") must_have_one_noun+=("event") + must_have_one_noun+=("horizontalpodautoscaler") must_have_one_noun+=("limitrange") must_have_one_noun+=("namespace") must_have_one_noun+=("node") diff --git a/pkg/expapi/types.go b/pkg/expapi/types.go index d424b4ce8c8..1612f2e2768 100644 --- a/pkg/expapi/types.go +++ b/pkg/expapi/types.go @@ -103,6 +103,7 @@ type HorizontalPodAutoscalerSpec struct { // HorizontalPodAutoscalerStatus contains the current status of a horizontal pod autoscaler type HorizontalPodAutoscalerStatus struct { + // TODO: Consider if it is needed. // CurrentReplicas is the number of replicas of pods managed by this autoscaler. CurrentReplicas int `json:"currentReplicas"` diff --git a/pkg/kubectl/kubectl.go b/pkg/kubectl/kubectl.go index 8e2d16718e3..15c9c0d6cb4 100644 --- a/pkg/kubectl/kubectl.go +++ b/pkg/kubectl/kubectl.go @@ -96,6 +96,7 @@ func expandResourceShortcut(resource string) string { "cs": "componentstatuses", "ev": "events", "ep": "endpoints", + "hpa": "horizontalpodautoscalers", "limits": "limitranges", "no": "nodes", "ns": "namespaces", diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 37f6a517252..0918b52f26c 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -367,6 +367,7 @@ var persistentVolumeColumns = []string{"NAME", "LABELS", "CAPACITY", "ACCESSMODE var persistentVolumeClaimColumns = []string{"NAME", "LABELS", "STATUS", "VOLUME", "CAPACITY", "ACCESSMODES", "AGE"} var componentStatusColumns = []string{"NAME", "STATUS", "MESSAGE", "ERROR"} var thirdPartyResourceColumns = []string{"NAME", "DESCRIPTION", "VERSION(S)"} +var horizontalPodAutoscalerColumns = []string{"NAME", "REFERENCE", "TARGET", "CURRENT", "MINPODS", "MAXPODS", "AGE"} var withNamespacePrefixColumns = []string{"NAMESPACE"} // TODO(erictune): print cluster name too. var deploymentColumns = []string{"NAME", "UPDATEDREPLICAS", "AGE"} @@ -406,6 +407,8 @@ func (h *HumanReadablePrinter) addDefaultHandlers() { h.Handler(thirdPartyResourceColumns, printThirdPartyResourceList) h.Handler(deploymentColumns, printDeployment) h.Handler(deploymentColumns, printDeploymentList) + h.Handler(horizontalPodAutoscalerColumns, printHorizontalPodAutoscaler) + h.Handler(horizontalPodAutoscalerColumns, printHorizontalPodAutoscalerList) } func (h *HumanReadablePrinter) unknown(data []byte, w io.Writer) error { @@ -1151,6 +1154,52 @@ func printDeploymentList(list *expapi.DeploymentList, w io.Writer, withNamespace return nil } +func printHorizontalPodAutoscaler(hpa *expapi.HorizontalPodAutoscaler, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error { + namespace := hpa.Namespace + name := hpa.Name + reference := fmt.Sprintf("%s/%s/%s/%s", + hpa.Spec.ScaleRef.Kind, + hpa.Spec.ScaleRef.Namespace, + hpa.Spec.ScaleRef.Name, + hpa.Spec.ScaleRef.Subresource) + target := fmt.Sprintf("%s %v", hpa.Spec.Target.Quantity.String(), hpa.Spec.Target.Resource) + + current := "" + if hpa.Status != nil && hpa.Status.CurrentConsumption != nil { + current = fmt.Sprintf("%s %v", hpa.Status.CurrentConsumption.Quantity.String(), hpa.Status.CurrentConsumption.Resource) + } + minPods := hpa.Spec.MinCount + maxPods := hpa.Spec.MaxCount + if withNamespace { + if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil { + return err + } + } + + if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\t%d\t%s", + name, + reference, + target, + current, + minPods, + maxPods, + translateTimestamp(hpa.CreationTimestamp), + ); err != nil { + return err + } + _, err := fmt.Fprint(w, appendLabels(hpa.Labels, columnLabels)) + return err +} + +func printHorizontalPodAutoscalerList(list *expapi.HorizontalPodAutoscalerList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error { + for i := range list.Items { + if err := printHorizontalPodAutoscaler(&list.Items[i], w, withNamespace, wide, showAll, columnLabels); err != nil { + return err + } + } + return nil +} + func appendLabels(itemLabels map[string]string, columnLabels []string) string { var buffer bytes.Buffer