根据selector获取pod
(selector string)
| 586 | |
| 587 | // 根据selector获取pod |
| 588 | func (k *KubernetesUtil) GetPodInstanceBySelector(selector string) (*coreV1.Pod, error) { |
| 589 | command := "" |
| 590 | command = fmt.Sprintf("get pod --selector=%v -o=yaml ", selector) |
| 591 | yaml, err := k.ExecKubectlCommandCombined(command, "") |
| 592 | if err != nil { |
| 593 | return nil, err |
| 594 | } |
| 595 | |
| 596 | decode := k8sScheme.Codecs.UniversalDeserializer().Decode |
| 597 | obj, _, _ := decode([]byte(yaml), nil, nil) |
| 598 | list := obj.(*coreV1.List) |
| 599 | |
| 600 | if list == nil || len(list.Items) == 0 { |
| 601 | return nil, errors.New("查找不到对应的pod,请检查k8s运行环境是否正常!") |
| 602 | } |
| 603 | |
| 604 | item := list.Items[0] |
| 605 | bytes, _ := item.MarshalJSON() |
| 606 | objPod, _, _ := decode(bytes, nil, nil) |
| 607 | pod := objPod.(*coreV1.Pod) |
| 608 | |
| 609 | return pod, nil |
| 610 | } |
| 611 | |
| 612 | func (k *KubernetesUtil) GetPodInstanceByName(podName string) (*coreV1.Pod, error) { |
| 613 | if podName == "" { |
no test coverage detected