GetMasterNodeIP could find the one master node IP.
(ctx context.Context)
| 581 | |
| 582 | // GetMasterNodeIP could find the one master node IP. |
| 583 | func (c *Controller) GetMasterNodeIP(ctx context.Context) (string, error) { |
| 584 | // the label "node-role.kubernetes.io/master" be removed in k8s 1.24, and replace with |
| 585 | // "node-role.kubernetes.io/control-plane". |
| 586 | nodes := &corev1.NodeList{} |
| 587 | labels := []string{ |
| 588 | "node-role.kubernetes.io/master", |
| 589 | "node-role.kubernetes.io/control-plane", |
| 590 | } |
| 591 | |
| 592 | for _, label := range labels { |
| 593 | if err := c.List(ctx, nodes, client.MatchingLabels{label: ""}); err != nil { |
| 594 | return "", err |
| 595 | } |
| 596 | if len(nodes.Items) > 0 { |
| 597 | break |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if len(nodes.Items) == 0 { |
| 602 | return "", fmt.Errorf("no nodes found with the master node labels") |
| 603 | } |
| 604 | |
| 605 | var internalIP string |
| 606 | for _, node := range nodes.Items { |
| 607 | for _, condition := range node.Status.Conditions { |
| 608 | if condition.Type == corev1.NodeReady && condition.Status == corev1.ConditionTrue { |
| 609 | for _, addr := range node.Status.Addresses { |
| 610 | // Using External IP as first priority |
| 611 | if addr.Type == corev1.NodeExternalIP { |
| 612 | return addr.Address, nil |
| 613 | } |
| 614 | if addr.Type == corev1.NodeInternalIP { |
| 615 | internalIP = addr.Address |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | if len(internalIP) != 0 { |
| 622 | return internalIP, nil |
| 623 | } |
| 624 | return "", fmt.Errorf("no available node IP was found") |
| 625 | } |
| 626 | |
| 627 | // CreateCloudShellService Create service resource for cloudshell, the service type is either ClusterIP, NodePort, |
| 628 | // Ingress or virtualService. if the expose model is ingress or virtualService. it will create clusterIP type service. |