Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more columns for tkctl get tikv #842

Merged
merged 20 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions pkg/tkctl/alias/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2019. PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package alias

import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type Tikv v1.Pod

type TikvList v1.PodList

// implement type Object interface
func (t Tikv) GetObjectKind() schema.ObjectKind {
return t.GetObjectKind()
}

func (t Tikv) DeepCopyObject() runtime.Object {
return t.DeepCopyObject()
}

// implement type Object interface
func (t TikvList) GetObjectKind() schema.ObjectKind {
return t.GetObjectKind()
}

func (t TikvList) DeepCopyObject() runtime.Object {
return t.DeepCopyObject()
}

func (t *Tikv) FromPod(pod *v1.Pod) {
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
t.Labels = pod.Labels
t.Annotations = pod.Annotations
t.TypeMeta = pod.TypeMeta
t.ObjectMeta = pod.ObjectMeta
t.Spec = pod.Spec
t.Status = pod.Status
}

func (t *Tikv) ToPod() *v1.Pod {
pod := &v1.Pod{}
pod.Labels = t.Labels
pod.Annotations = t.Annotations
pod.TypeMeta = t.TypeMeta
pod.ObjectMeta = t.ObjectMeta
pod.Spec = t.Spec
t.Status = pod.Status
return pod
}

func (t *TikvList) FromPodList(podList *v1.PodList) {
t.TypeMeta = podList.TypeMeta
t.ListMeta = podList.ListMeta
t.Items = podList.Items
}

func (t *TikvList) ToPodList() *v1.PodList {
podList := &v1.PodList{}
podList.Items = t.Items
podList.ListMeta = t.ListMeta
podList.TypeMeta = t.TypeMeta
return podList
}
10 changes: 9 additions & 1 deletion pkg/tkctl/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package get
import (
"encoding/json"
"fmt"
"github.com/pingcap/tidb-operator/pkg/tkctl/alias"
"strings"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1"
Expand Down Expand Up @@ -279,7 +280,14 @@ func (o *GetOptions) PrintOutput(tc *v1alpha1.TidbCluster, resourceType string,
if err != nil {
return err
}

switch resourceType {
case kindTiKV:
tikvList := &alias.TikvList{}
tikvList.FromPodList(podList)
return printer.PrintObj(tikvList, o.Out)
default:
break
}
return printer.PrintObj(podList, o.Out)
case kindVolume:
var objs []runtime.Object
Expand Down
41 changes: 41 additions & 0 deletions pkg/tkctl/readable/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ package readable

import (
"fmt"
"github.com/pingcap/tidb-operator/pkg/label"
"time"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/tkctl/alias"
"k8s.io/api/core/v1"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -48,6 +50,10 @@ type PodBasicColumns struct {
CPUInfo string
}

type TikvExtraInfoColumn struct {
StoreId string
}

func AddHandlers(h printers.PrintHandler) {
tidbClusterColumns := []metav1beta1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
Expand All @@ -73,6 +79,16 @@ func AddHandlers(h printers.PrintHandler) {
}
h.TableHandler(commonPodColumns, printPod)
h.TableHandler(commonPodColumns, printPodList)
//tikv columns
tikvPodColumns := commonPodColumns
storeId := metav1beta1.TableColumnDefinition{
Name: "StoreID",
Type: "string",
Description: "TiKV StoreID",
}
tikvPodColumns = append(tikvPodColumns, storeId)
h.TableHandler(tikvPodColumns, printTikvList)
h.TableHandler(tikvPodColumns, printTikv)
// TODO: add available space for volume
volumeColumns := []metav1beta1.TableColumnDefinition{
{Name: "Volume", Type: "string", Format: "name", Description: "Volume name"},
Expand Down Expand Up @@ -151,9 +167,26 @@ func printPod(pod *v1.Pod, options printers.PrintOptions) ([]metav1beta1.TableRo
if options.Wide {
row.Cells = append(row.Cells, columns.PodIP, columns.NodeName)
}
componentKind := pod.Labels[label.ComponentLabelKey]
switch componentKind {
case label.TiKVLabelVal:
extraInfoColumn := extraTikvDataColumn(pod)
row.Cells = append(row.Cells, extraInfoColumn.StoreId)
break
default:
break
}
return []metav1beta1.TableRow{row}, nil
}

func printTikvList(tikvList *alias.TikvList, options printers.PrintOptions) ([]metav1beta1.TableRow, error) {
return printPodList(tikvList.ToPodList(), options)
}

func printTikv(tikv *alias.Tikv, options printers.PrintOptions) ([]metav1beta1.TableRow, error) {
return printPod(tikv.ToPod(), options)
}

func printVolumeList(volumeList *v1.PersistentVolumeList, options printers.PrintOptions) ([]metav1beta1.TableRow, error) {
rows := make([]metav1beta1.TableRow, 0, len(volumeList.Items))
for i := range volumeList.Items {
Expand Down Expand Up @@ -350,3 +383,11 @@ func translateTimestampSince(timestamp metav1.Time) string {

return duration.HumanDuration(time.Since(timestamp.Time))
}

// extra Component Tikv Data
func extraTikvDataColumn(pod *v1.Pod) *TikvExtraInfoColumn {
storeId := pod.Labels[label.StoreIDLabelKey]
return &TikvExtraInfoColumn{
StoreId: storeId,
}
}