Skip to content

Commit

Permalink
fix dt case bug (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojingchen authored and weekface committed Jun 19, 2019
1 parent e533707 commit 01ca75e
Showing 1 changed file with 15 additions and 37 deletions.
52 changes: 15 additions & 37 deletions tests/dt.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,14 @@ func (oa *operatorActions) LabelNodesOrDie() {
}

func (oa *operatorActions) CheckDisasterTolerance(cluster *TidbClusterConfig) error {
nodeMap := map[string]corev1.Node{}
nodes, err := oa.kubeCli.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return err
}
for _, node := range nodes.Items {
nodeMap[node.Name] = node
}

pds, err := oa.kubeCli.CoreV1().Pods(cluster.Namespace).List(
metav1.ListOptions{LabelSelector: labels.SelectorFromSet(
label.New().Instance(cluster.ClusterName).PD().Labels(),
).String()})
if err != nil {
return err
}
err = oa.checkPodsDisasterTolerance(pds.Items, nodeMap)
err = oa.checkPodsDisasterTolerance(pds.Items)
if err != nil {
return err
}
Expand All @@ -114,7 +105,7 @@ func (oa *operatorActions) CheckDisasterTolerance(cluster *TidbClusterConfig) er
if err != nil {
return err
}
err = oa.checkPodsDisasterTolerance(tikvs.Items, nodeMap)
err = oa.checkPodsDisasterTolerance(tikvs.Items)
if err != nil {
return err
}
Expand All @@ -126,37 +117,24 @@ func (oa *operatorActions) CheckDisasterTolerance(cluster *TidbClusterConfig) er
if err != nil {
return err
}
return oa.checkPodsDisasterTolerance(tidbs.Items, nodeMap)
return oa.checkPodsDisasterTolerance(tidbs.Items)
}

func (oa *operatorActions) checkPodsDisasterTolerance(allPods []corev1.Pod, nodeMap map[string]corev1.Node) error {
rackPods := map[string][]corev1.Pod{}
func (oa *operatorActions) checkPodsDisasterTolerance(allPods []corev1.Pod) error {
for _, pod := range allPods {
if node, exist := nodeMap[pod.Spec.NodeName]; exist {
pods, exist := rackPods[node.Labels[RackLabel]]
if !exist {
pods = []corev1.Pod{}
}
pods = append(pods, pod)
rackPods[node.Labels[RackLabel]] = pods
if pod.Spec.Affinity == nil {
return fmt.Errorf("the pod:[%s/%s] has not Affinity", pod.Namespace, pod.Name)
}
}

podNum := len(allPods)
minPodsOneRack := podNum / RackNum
maxPodsOneRack := minPodsOneRack
mod := podNum % RackNum
if mod > 0 {
maxPodsOneRack = maxPodsOneRack + 1
}

for rack, pods := range rackPods {
podNumOnRack := len(pods)
if podNumOnRack > maxPodsOneRack {
return fmt.Errorf("the rack:[%s] have pods more than %d", rack, maxPodsOneRack)
if pod.Spec.Affinity.PodAntiAffinity == nil {
return fmt.Errorf("the pod:[%s/%s] has not Affinity.PodAntiAffinity", pod.Namespace, pod.Name)
}
if len(pod.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution) == 0 {
return fmt.Errorf("the pod:[%s/%s] has not PreferredDuringSchedulingIgnoredDuringExecution", pod.Namespace, pod.Name)
}
if podNumOnRack < minPodsOneRack {
return fmt.Errorf("the rack:[%s] have pods less than %d", rack, mod)
for _, prefer := range pod.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
if prefer.PodAffinityTerm.TopologyKey != RackLabel {
return fmt.Errorf("the pod:[%s/%s] topology key is not %s", pod.Namespace, pod.Name, RackLabel)
}
}
}
return nil
Expand Down

0 comments on commit 01ca75e

Please sign in to comment.