From 1b7948e1dae5b9dadf6b69a1659687680f03075f Mon Sep 17 00:00:00 2001 From: shuijing198799 Date: Tue, 28 May 2019 16:32:16 +0800 Subject: [PATCH 1/3] add port podip nodeip to tkctl upinfo --- pkg/label/label.go | 2 + pkg/manager/member/pd_member_manager.go | 2 +- pkg/manager/member/tidb_member_manager.go | 2 +- pkg/manager/member/tikv_member_manager.go | 2 +- pkg/tkctl/cmd/upinfo/upinfo.go | 65 ++++++++++++++++------- pkg/util/util.go | 15 ++++++ 6 files changed, 67 insertions(+), 21 deletions(-) diff --git a/pkg/label/label.go b/pkg/label/label.go index 17ea6c6d95..d2b7e6680a 100644 --- a/pkg/label/label.go +++ b/pkg/label/label.go @@ -59,6 +59,8 @@ const ( TiDBLabelVal string = "tidb" // TiKVLabelVal is TiKV label value TiKVLabelVal string = "tikv" + // Container port name of TiDB components + ServerVal string = "server" ) // Label is the label field in metadata diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index d975ebec8d..1d0105141e 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -487,7 +487,7 @@ func (pmm *pdMemberManager) getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster) ImagePullPolicy: tc.Spec.PD.ImagePullPolicy, Ports: []corev1.ContainerPort{ { - Name: "server", + Name: label.ServerVal, ContainerPort: int32(2380), Protocol: corev1.ProtocolTCP, }, diff --git a/pkg/manager/member/tidb_member_manager.go b/pkg/manager/member/tidb_member_manager.go index b40756c81a..f26b70c458 100644 --- a/pkg/manager/member/tidb_member_manager.go +++ b/pkg/manager/member/tidb_member_manager.go @@ -305,7 +305,7 @@ func (tmm *tidbMemberManager) getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbClust ImagePullPolicy: tc.Spec.TiDB.ImagePullPolicy, Ports: []corev1.ContainerPort{ { - Name: "server", + Name: label.ServerVal, ContainerPort: int32(4000), Protocol: corev1.ProtocolTCP, }, diff --git a/pkg/manager/member/tikv_member_manager.go b/pkg/manager/member/tikv_member_manager.go index 3546b9bf82..3f4dc18ca9 100644 --- a/pkg/manager/member/tikv_member_manager.go +++ b/pkg/manager/member/tikv_member_manager.go @@ -346,7 +346,7 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster) ImagePullPolicy: tc.Spec.TiKV.ImagePullPolicy, Ports: []corev1.ContainerPort{ { - Name: "server", + Name: label.ServerVal, ContainerPort: int32(20160), Protocol: corev1.ProtocolTCP, }, diff --git a/pkg/tkctl/cmd/upinfo/upinfo.go b/pkg/tkctl/cmd/upinfo/upinfo.go index b49ea965a9..9ebb466e5e 100644 --- a/pkg/tkctl/cmd/upinfo/upinfo.go +++ b/pkg/tkctl/cmd/upinfo/upinfo.go @@ -14,6 +14,7 @@ package upinfo import ( + "errors" "fmt" "io" @@ -155,6 +156,38 @@ func (o *UpInfoOptions) Run() error { return nil } +func getState(updateReplicas int32, ordinal int32, tc *v1alpha1.TidbCluster, pod *v1.Pod) string { + + var state string + + if updateReplicas < ordinal { + state = UPDATED + } else if updateReplicas == ordinal { + + state = UPDATING + + if pod.Labels[apps.ControllerRevisionHashLabelKey] == tc.Status.TiDB.StatefulSet.UpdateRevision { + if member, exist := tc.Status.TiDB.Members[pod.Name]; exist && member.Health { + state = UPDATED + } + } + + } else { + state = WAITING + } + + return state +} + +func getServerPort(container *v1.Container) (int32, bool) { + for _, port := range container.Ports { + if port.Name == label.ServerVal { + return port.ContainerPort, true + } + } + return 0, false +} + func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podList *v1.PodList) (string, error) { return readable.TabbedString(func(out io.Writer) error { w := readable.NewPrefixWriter(out) @@ -170,8 +203,8 @@ func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podLis } } { - w.WriteLine(readable.LEVEL_1, "Name\tState\t") - w.WriteLine(readable.LEVEL_1, "----\t-----\t") + w.WriteLine(readable.LEVEL_1, "Name\tState\tNodeIP\tPodIP\tPort\t") + w.WriteLine(readable.LEVEL_1, "----\t-----\t------\t-----\t----\t") { updateReplicas := set.Spec.UpdateStrategy.RollingUpdate.Partition @@ -183,25 +216,21 @@ func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podLis return err } if dbPhase == v1alpha1.UpgradePhase { - if (*updateReplicas) < ordinal { - state = UPDATED - } else if (*updateReplicas) == ordinal { - - state = UPDATING - - if pod.Labels[apps.ControllerRevisionHashLabelKey] == tc.Status.TiDB.StatefulSet.UpdateRevision { - if member, exist := tc.Status.TiDB.Members[pod.Name]; exist && member.Health { - state = UPDATED - } - } - - } else { - state = WAITING - } + state = getState(*updateReplicas, ordinal, tc, &pod) } else { state = UPDATED } - w.WriteLine(readable.LEVEL_1, "%s\t%s\t", pod.Name, state) + + container, ok := util.GetContainerViaName(v1alpha1.TiDBMemberType.String(), &pod) + if !ok { + return errors.New("no tidb container") + } + port, ok := getServerPort(container) + if !ok { + return errors.New("no server port") + } + + w.WriteLine(readable.LEVEL_1, "%s\t%s\t%s\t%s\t%d\t", pod.Name, state, pod.Status.HostIP, pod.Status.PodIP, port) } } else { w.WriteLine(readable.LEVEL_1, "no resource found") diff --git a/pkg/util/util.go b/pkg/util/util.go index ba196f362c..7231c31d84 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -91,3 +91,18 @@ func GetNextOrdinalPodName(podName string, ordinal int32) string { basicStr := podName[:strings.LastIndex(podName, "-")] return fmt.Sprintf("%s-%d", basicStr, ordinal+1) } + +// Get container via container name +func GetContainerViaName(name string, pod *corev1.Pod) (*corev1.Container, bool) { + + if len(pod.Spec.Containers) == 0 { + return nil, false + } + + for _, container := range pod.Spec.Containers { + if container.Name == name { + return &container, true + } + } + return nil, false +} From b6967f28cdb8cde6c737a1f088f48e9b604d911b Mon Sep 17 00:00:00 2001 From: shuijing198799 Date: Fri, 31 May 2019 09:56:33 +0800 Subject: [PATCH 2/3] address comment --- pkg/tkctl/cmd/upinfo/upinfo.go | 27 +++++++++++++++++---------- pkg/util/util.go | 4 ---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/pkg/tkctl/cmd/upinfo/upinfo.go b/pkg/tkctl/cmd/upinfo/upinfo.go index 9ebb466e5e..b0069908f1 100644 --- a/pkg/tkctl/cmd/upinfo/upinfo.go +++ b/pkg/tkctl/cmd/upinfo/upinfo.go @@ -14,9 +14,9 @@ package upinfo import ( - "errors" "fmt" "io" + "strconv" "github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client/clientset/versioned" @@ -188,6 +188,20 @@ func getServerPort(container *v1.Container) (int32, bool) { return 0, false } +func getTiDBServerPort(pod *v1.Pod) string { + + container, ok := util.GetContainerViaName(v1alpha1.TiDBMemberType.String(), pod) + if !ok { + return "NONE" + } + port, ok := getServerPort(container) + if !ok { + return "NONE" + } + + return strconv.Itoa(int(port)) +} + func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podList *v1.PodList) (string, error) { return readable.TabbedString(func(out io.Writer) error { w := readable.NewPrefixWriter(out) @@ -221,16 +235,9 @@ func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podLis state = UPDATED } - container, ok := util.GetContainerViaName(v1alpha1.TiDBMemberType.String(), &pod) - if !ok { - return errors.New("no tidb container") - } - port, ok := getServerPort(container) - if !ok { - return errors.New("no server port") - } + port := getTiDBServerPort(&pod) - w.WriteLine(readable.LEVEL_1, "%s\t%s\t%s\t%s\t%d\t", pod.Name, state, pod.Status.HostIP, pod.Status.PodIP, port) + w.WriteLine(readable.LEVEL_1, "%s\t%s\t%s\t%s\t%s\t", pod.Name, state, pod.Status.HostIP, pod.Status.PodIP, port) } } else { w.WriteLine(readable.LEVEL_1, "no resource found") diff --git a/pkg/util/util.go b/pkg/util/util.go index 7231c31d84..f3c9453c54 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -95,10 +95,6 @@ func GetNextOrdinalPodName(podName string, ordinal int32) string { // Get container via container name func GetContainerViaName(name string, pod *corev1.Pod) (*corev1.Container, bool) { - if len(pod.Spec.Containers) == 0 { - return nil, false - } - for _, container := range pod.Spec.Containers { if container.Name == name { return &container, true From 33414027b714692ac3053c1d1473ffe99154ec9d Mon Sep 17 00:00:00 2001 From: shuijing198799 Date: Mon, 3 Jun 2019 12:52:17 +0800 Subject: [PATCH 3/3] use port : nodeport format to show up the upinfo port --- pkg/label/label.go | 2 -- pkg/manager/member/pd_member_manager.go | 2 +- pkg/manager/member/tidb_member_manager.go | 2 +- pkg/manager/member/tikv_member_manager.go | 2 +- pkg/tkctl/cmd/upinfo/upinfo.go | 40 ++++++++++------------- pkg/util/util.go | 11 ------- 6 files changed, 20 insertions(+), 39 deletions(-) diff --git a/pkg/label/label.go b/pkg/label/label.go index d2b7e6680a..17ea6c6d95 100644 --- a/pkg/label/label.go +++ b/pkg/label/label.go @@ -59,8 +59,6 @@ const ( TiDBLabelVal string = "tidb" // TiKVLabelVal is TiKV label value TiKVLabelVal string = "tikv" - // Container port name of TiDB components - ServerVal string = "server" ) // Label is the label field in metadata diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index 1d0105141e..d975ebec8d 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -487,7 +487,7 @@ func (pmm *pdMemberManager) getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster) ImagePullPolicy: tc.Spec.PD.ImagePullPolicy, Ports: []corev1.ContainerPort{ { - Name: label.ServerVal, + Name: "server", ContainerPort: int32(2380), Protocol: corev1.ProtocolTCP, }, diff --git a/pkg/manager/member/tidb_member_manager.go b/pkg/manager/member/tidb_member_manager.go index f26b70c458..b40756c81a 100644 --- a/pkg/manager/member/tidb_member_manager.go +++ b/pkg/manager/member/tidb_member_manager.go @@ -305,7 +305,7 @@ func (tmm *tidbMemberManager) getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbClust ImagePullPolicy: tc.Spec.TiDB.ImagePullPolicy, Ports: []corev1.ContainerPort{ { - Name: label.ServerVal, + Name: "server", ContainerPort: int32(4000), Protocol: corev1.ProtocolTCP, }, diff --git a/pkg/manager/member/tikv_member_manager.go b/pkg/manager/member/tikv_member_manager.go index 3f4dc18ca9..3546b9bf82 100644 --- a/pkg/manager/member/tikv_member_manager.go +++ b/pkg/manager/member/tikv_member_manager.go @@ -346,7 +346,7 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster) ImagePullPolicy: tc.Spec.TiKV.ImagePullPolicy, Ports: []corev1.ContainerPort{ { - Name: label.ServerVal, + Name: "server", ContainerPort: int32(20160), Protocol: corev1.ProtocolTCP, }, diff --git a/pkg/tkctl/cmd/upinfo/upinfo.go b/pkg/tkctl/cmd/upinfo/upinfo.go index b0069908f1..da802385bb 100644 --- a/pkg/tkctl/cmd/upinfo/upinfo.go +++ b/pkg/tkctl/cmd/upinfo/upinfo.go @@ -16,7 +16,6 @@ package upinfo import ( "fmt" "io" - "strconv" "github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client/clientset/versioned" @@ -24,6 +23,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/label" "github.com/pingcap/tidb-operator/pkg/tkctl/config" "github.com/pingcap/tidb-operator/pkg/tkctl/readable" + tkctlUtil "github.com/pingcap/tidb-operator/pkg/tkctl/util" "github.com/pingcap/tidb-operator/pkg/util" "github.com/spf13/cobra" apps "k8s.io/api/apps/v1beta1" @@ -148,7 +148,12 @@ func (o *UpInfoOptions) Run() error { if err != nil { return err } - msg, err := renderTCUpgradeInfo(tc, set, podList) + svcName := tkctlUtil.GetTidbServiceName(tc.Name) + svc, err := o.KubeCli.CoreV1().Services(o.Namespace).Get(svcName, metav1.GetOptions{}) + if err != nil { + return err + } + msg, err := renderTCUpgradeInfo(tc, set, podList, svc) if err != nil { return err } @@ -179,30 +184,19 @@ func getState(updateReplicas int32, ordinal int32, tc *v1alpha1.TidbCluster, pod return state } -func getServerPort(container *v1.Container) (int32, bool) { - for _, port := range container.Ports { - if port.Name == label.ServerVal { - return port.ContainerPort, true +func getTiDBServerPort(svc *v1.Service) string { + for _, port := range svc.Spec.Ports { + if port.Name == "mysql-client" { + if port.NodePort != 0 { + return fmt.Sprintf("%d:%d/%s", port.Port, port.NodePort, port.Protocol) + } + return fmt.Sprintf("%d/%s", port.Port, port.Protocol) } } - return 0, false -} - -func getTiDBServerPort(pod *v1.Pod) string { - - container, ok := util.GetContainerViaName(v1alpha1.TiDBMemberType.String(), pod) - if !ok { - return "NONE" - } - port, ok := getServerPort(container) - if !ok { - return "NONE" - } - - return strconv.Itoa(int(port)) + return "" } -func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podList *v1.PodList) (string, error) { +func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podList *v1.PodList, svc *v1.Service) (string, error) { return readable.TabbedString(func(out io.Writer) error { w := readable.NewPrefixWriter(out) dbPhase := tc.Status.TiDB.Phase @@ -235,7 +229,7 @@ func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podLis state = UPDATED } - port := getTiDBServerPort(&pod) + port := getTiDBServerPort(svc) w.WriteLine(readable.LEVEL_1, "%s\t%s\t%s\t%s\t%s\t", pod.Name, state, pod.Status.HostIP, pod.Status.PodIP, port) } diff --git a/pkg/util/util.go b/pkg/util/util.go index f3c9453c54..ba196f362c 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -91,14 +91,3 @@ func GetNextOrdinalPodName(podName string, ordinal int32) string { basicStr := podName[:strings.LastIndex(podName, "-")] return fmt.Sprintf("%s-%d", basicStr, ordinal+1) } - -// Get container via container name -func GetContainerViaName(name string, pod *corev1.Pod) (*corev1.Container, bool) { - - for _, container := range pod.Spec.Containers { - if container.Name == name { - return &container, true - } - } - return nil, false -}