Skip to content

Commit

Permalink
dedup the GetTunnelServerAddr and getYurttunelServerDNSandIP functions
Browse files Browse the repository at this point in the history
  • Loading branch information
charleszheng44 committed Jul 17, 2020
1 parent c308da3 commit 5c5e1b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 91 deletions.
103 changes: 15 additions & 88 deletions pkg/yurttunnel/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"

"github.com/alibaba/openyurt/pkg/yurttunnel/constants"
"github.com/alibaba/openyurt/pkg/yurttunnel/pki/certmanager"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -35,106 +36,32 @@ func GetTunnelServerAddr(clientset kubernetes.Interface) (string, error) {
return "", err
}

switch svc.Spec.Type {
case corev1.ServiceTypeLoadBalancer:
return getServerAddrLoadBalancer(svc)
case corev1.ServiceTypeClusterIP:
return getServerAddrClusterIP(clientset, svc)
case corev1.ServiceTypeNodePort:
return getServerAddrNodePort(clientset, svc)
default:
return "", fmt.Errorf("unupported service type: %s", svc.Spec.Type)
}
}

// getServerAddrLoadBalancer gets the service address of the yurttunnel-server
// if the service type is LoadBalancer
func getServerAddrLoadBalancer(
svc *corev1.Service) (string, error) {
var tcpPort int32
for _, port := range svc.Spec.Ports {
if port.Name == constants.YurttunnelServerAgentPortName {
tcpPort = port.Port
break
}
}

for _, ingress := range svc.Status.LoadBalancer.Ingress {
if len(ingress.IP) != 0 {
return fmt.Sprintf("%s:%d", ingress.IP, tcpPort), nil
}
}
return "", errors.New("can't find qualified ingress")
}

// getServerAddrClusterIP gets the service address of the yurttunnel-server
// if the service type is ClusterIP
func getServerAddrClusterIP(
clientset kubernetes.Interface,
svc *corev1.Service) (string, error) {
if addr, ok := svc.Annotations[constants.YurttunnelServerExternalAddrKey]; ok {
return addr, nil
}

eps, err := clientset.CoreV1().Endpoints(constants.YurttunnelEndpointsNs).
Get(constants.YurttunnelEndpointsName, metav1.GetOptions{})
_, ips, err := certmanager.GetYurttunelServerDNSandIP(clientset)
if err != nil {
return "", err
}

for _, ss := range eps.Subsets {
if len(ss.Addresses) == 1 && len(ss.Ports) == 1 {
return fmt.Sprintf("%s:%d", ss.Addresses[0].IP, ss.Ports[0].Port), nil
}
if len(ips) <= 0 {
return "", errors.New("there is no available ip")
}
return "", errors.New("can't find qualified endpoint subsets")
}

// getServerAddrNodePort gets the service address of the yurttunnel-server
// if the service type is NodePort
func getServerAddrNodePort(
clientset kubernetes.Interface,
svc *corev1.Service) (string, error) {
// get node ip
labelSelector := "alibabacloud.com/is-edge-worker=false"
nodeLst, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{
LabelSelector: labelSelector,
})
if err != nil {
return "", err
}
if len(nodeLst.Items) == 0 {
return "", errors.New("there is no cloud node")
}
var (
nodeIP string
foundNodeIP bool
)
for _, addr := range nodeLst.Items[0].Status.Addresses {
if addr.Type == corev1.NodeInternalIP {
nodeIP = addr.Address
foundNodeIP = true
}
}
if !foundNodeIP {
return "", errors.New("can't find node IP")
}
// get node port
var (
tcpPort int32
foundTCPPort bool
)
var tcpPort int32
for _, port := range svc.Spec.Ports {
if port.Name == constants.YurttunnelServerAgentPortName {
tcpPort = port.NodePort
foundTCPPort = true
if svc.Spec.Type == corev1.ServiceTypeNodePort {
tcpPort = port.NodePort
} else {
tcpPort = port.Port
}
break
}
}
if !foundTCPPort {
return "", errors.New("tcp port not found")

if tcpPort == 0 {
return "", errors.New("fail to get the port number")
}
return fmt.Sprintf("%s:%d", nodeIP, tcpPort), nil

return fmt.Sprintf("%s:%d", ips[0].String(), tcpPort), nil
}

// RunAgent runs the yurttunnel-agent
Expand Down
6 changes: 3 additions & 3 deletions pkg/yurttunnel/pki/certmanager/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewYurttunnelServerCertManager(
err error
)
_ = wait.PollUntil(5*time.Second, func() (bool, error) {
dnsNames, ips, err = getYurttunelServerDNSandIP(clientset)
dnsNames, ips, err = GetYurttunelServerDNSandIP(clientset)
if err == nil {
return true, nil
}
Expand All @@ -75,9 +75,9 @@ func NewYurttunnelServerCertManager(
dnsNames, ips)
}

// getYurttunelServerDNSandIP gets DNS names and IPS that will be added into
// GetYurttunelServerDNSandIP gets DNS names and IPS that will be added into
// the yurttunnel-server certificate
func getYurttunelServerDNSandIP(
func GetYurttunelServerDNSandIP(
clientset kubernetes.Interface) ([]string, []net.IP, error) {
var (
dnsNames = make([]string, 0)
Expand Down

0 comments on commit 5c5e1b5

Please sign in to comment.