mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Adding a kubectl resource printer for deployments
This commit is contained in:
parent
044e4854e1
commit
ac0b060e62
@ -273,6 +273,7 @@ _kubectl_get()
|
||||
must_have_one_flag=()
|
||||
must_have_one_noun=()
|
||||
must_have_one_noun+=("componentstatus")
|
||||
must_have_one_noun+=("deployment")
|
||||
must_have_one_noun+=("endpoints")
|
||||
must_have_one_noun+=("event")
|
||||
must_have_one_noun+=("limitrange")
|
||||
@ -447,6 +448,7 @@ _kubectl_delete()
|
||||
must_have_one_flag=()
|
||||
must_have_one_noun=()
|
||||
must_have_one_noun+=("componentstatus")
|
||||
must_have_one_noun+=("deployment")
|
||||
must_have_one_noun+=("endpoints")
|
||||
must_have_one_noun+=("event")
|
||||
must_have_one_noun+=("limitrange")
|
||||
|
@ -341,6 +341,7 @@ var persistentVolumeClaimColumns = []string{"NAME", "LABELS", "STATUS", "VOLUME"
|
||||
var componentStatusColumns = []string{"NAME", "STATUS", "MESSAGE", "ERROR"}
|
||||
var thirdPartyResourceColumns = []string{"NAME", "DESCRIPTION", "VERSION(S)"}
|
||||
var withNamespacePrefixColumns = []string{"NAMESPACE"} // TODO(erictune): print cluster name too.
|
||||
var deploymentColumns = []string{"NAME", "UPDATEDREPLICAS", "AGE"}
|
||||
|
||||
// addDefaultHandlers adds print handlers for default Kubernetes types.
|
||||
func (h *HumanReadablePrinter) addDefaultHandlers() {
|
||||
@ -376,6 +377,8 @@ func (h *HumanReadablePrinter) addDefaultHandlers() {
|
||||
h.Handler(componentStatusColumns, printComponentStatusList)
|
||||
h.Handler(thirdPartyResourceColumns, printThirdPartyResource)
|
||||
h.Handler(thirdPartyResourceColumns, printThirdPartyResourceList)
|
||||
h.Handler(deploymentColumns, printDeployment)
|
||||
h.Handler(deploymentColumns, printDeploymentList)
|
||||
}
|
||||
|
||||
func (h *HumanReadablePrinter) unknown(data []byte, w io.Writer) error {
|
||||
@ -1096,6 +1099,31 @@ func printThirdPartyResourceList(list *expapi.ThirdPartyResourceList, w io.Write
|
||||
return nil
|
||||
}
|
||||
|
||||
func printDeployment(deployment *expapi.Deployment, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
|
||||
if withNamespace {
|
||||
if _, err := fmt.Fprintf(w, "%s\t", deployment.Namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
updatedReplicas := fmt.Sprintf("%d/%d", deployment.Status.UpdatedReplicas, deployment.Spec.Replicas)
|
||||
age := translateTimestamp(deployment.CreationTimestamp)
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s", deployment.Name, updatedReplicas, age); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := fmt.Fprint(w, appendLabels(deployment.Labels, columnLabels))
|
||||
return err
|
||||
}
|
||||
|
||||
func printDeploymentList(list *expapi.DeploymentList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
|
||||
for _, item := range list.Items {
|
||||
if err := printDeployment(&item, w, withNamespace, wide, showAll, columnLabels); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func appendLabels(itemLabels map[string]string, columnLabels []string) string {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
|
@ -29,6 +29,7 @@ import (
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
"k8s.io/kubernetes/pkg/expapi"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
|
||||
@ -1221,3 +1222,39 @@ func TestTranslateTimestamp(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintDeployment(t *testing.T) {
|
||||
tests := []struct {
|
||||
deployment expapi.Deployment
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
expapi.Deployment{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "test1",
|
||||
CreationTimestamp: util.Time{Time: time.Now().Add(1.9e9)},
|
||||
},
|
||||
Spec: expapi.DeploymentSpec{
|
||||
Replicas: 5,
|
||||
Template: &api.PodTemplateSpec{
|
||||
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
|
||||
},
|
||||
},
|
||||
Status: expapi.DeploymentStatus{
|
||||
Replicas: 10,
|
||||
UpdatedReplicas: 2,
|
||||
},
|
||||
},
|
||||
"test1\t2/5\t0s\n",
|
||||
},
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
for _, test := range tests {
|
||||
printDeployment(&test.deployment, buf, false, false, true, []string{})
|
||||
if buf.String() != test.expect {
|
||||
t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
|
||||
}
|
||||
buf.Reset()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user