Skip to content

Commit

Permalink
Add port podip nodeip to tkctl upinfo (#538)
Browse files Browse the repository at this point in the history
* add port podip nodeip to tkctl upinfo

* address comment

* use port : nodeport format to show up the upinfo port
  • Loading branch information
shuijing198799 authored Jun 4, 2019
1 parent bc2298c commit 3e9a3f0
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions pkg/tkctl/cmd/upinfo/upinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,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"
Expand Down Expand Up @@ -147,15 +148,55 @@ 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
}
fmt.Fprint(o.Out, msg)
return nil
}

func renderTCUpgradeInfo(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, podList *v1.PodList) (string, error) {
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 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 "<none>"
}

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
Expand All @@ -170,8 +211,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

Expand All @@ -183,25 +224,14 @@ 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)

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)
}
} else {
w.WriteLine(readable.LEVEL_1, "no resource found")
Expand Down

0 comments on commit 3e9a3f0

Please sign in to comment.