Skip to content

Commit

Permalink
fix get external address for the authentication service
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli committed Apr 1, 2021
1 parent 0a65961 commit 099893c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
Expand Up @@ -494,8 +494,18 @@ func (r *ForeignClusterReconciler) getAddress() (string, error) {
klog.Error(err)
return "", err
}
lbIngress := svc.Status.LoadBalancer.Ingress[0]
// return the external service IP
return svc.Status.LoadBalancer.Ingress[0].IP, nil
if hostname := lbIngress.Hostname; hostname != "" {
return hostname, nil
} else if ip := lbIngress.IP; ip != "" {
return ip, nil
} else {
// the service has no external IPs
err := goerrors.New("no valid external IP for LoadBalancer Service")
klog.Error(err)
return "", err
}
}

// get the IP from the Nodes, to be used with NodePort services
Expand All @@ -515,23 +525,12 @@ func (r *ForeignClusterReconciler) getAddress() (string, error) {
}

node := nodes.Items[0]
for _, addr := range node.Status.Addresses {
// get the accresses that are IPs, other addresses (like the hostname) can not be reachable and valid for a remote host
if addr.Type == apiv1.NodeExternalIP || addr.Type == apiv1.NodeInternalIP {
return addr.Address, nil
}
}
return discoveryPkg.GetAddress(&node)

// we was not able to get an address in any of the previous cases:
// when an error occurs, it means that we was not able to get an address in any of the previous cases:
// 1. no overwrite variable is set
// 2. the service is not of type LoadBalancer
// 3. there are no nodes in the cluster to get the IP for a NodePort service
err = errors.NewNotFound(schema.GroupResource{
Group: apiv1.GroupName,
Resource: "nodes",
}, "no valid ip")
klog.Error(err)
return "", err
}

// get the external port where the Authentication Service is reachable from the external world
Expand Down
48 changes: 48 additions & 0 deletions pkg/discovery/nodeUtils.go
@@ -0,0 +1,48 @@
package discovery

import (
"fmt"

v1 "k8s.io/api/core/v1"
"k8s.io/klog"
)

var preferOrder = []v1.NodeAddressType{
v1.NodeExternalDNS,
v1.NodeExternalIP,
v1.NodeInternalDNS,
v1.NodeInternalIP,
v1.NodeHostName,
}

// return an address from a Node pool
func GetAddressFromNodeList(nodes []v1.Node) (string, error) {
for _, addrType := range preferOrder {
for _, node := range nodes {
if addr, err := getAddressByType(&node, addrType); err != nil {
klog.V(4).Info(err.Error())
continue
} else {
klog.V(4).Infof("found address %v with type %v", addr, addrType)
return addr, nil
}
}
}
return "", fmt.Errorf("no address found")
}

// return an address for a Node
func GetAddress(node *v1.Node) (string, error) {
return GetAddressFromNodeList([]v1.Node{
*node,
})
}

func getAddressByType(node *v1.Node, addrType v1.NodeAddressType) (string, error) {
for _, addr := range node.Status.Addresses {
if addr.Type == addrType {
return addr.Address, nil
}
}
return "", fmt.Errorf("no address with type %v found in node %v", addrType, node.Name)
}
14 changes: 11 additions & 3 deletions pkg/kubeconfig/create.go
Expand Up @@ -3,7 +3,9 @@ package kubeconfig
import (
"context"
"errors"

"github.com/liqotech/liqo/pkg/clusterConfig"
"github.com/liqotech/liqo/pkg/discovery"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -26,14 +28,19 @@ func CreateKubeConfigFromServiceAccount(apiServerConfigProvider clusterConfig.Ap
LabelSelector: "node-role.kubernetes.io/master",
})
if err != nil {
klog.Error(err)
return "", err
}
if len(nodes.Items) == 0 || len(nodes.Items[0].Status.Addresses) == 0 {
if len(nodes.Items) == 0 {
err = errors.New("no APISERVER env variable found and no master node found, one of the two values must be present")
klog.Error(err, err.Error())
klog.Error(err)
return "", err
}
address, err = discovery.GetAddressFromNodeList(nodes.Items)
if err != nil {
klog.Error(err)
return "", err
}
address = nodes.Items[0].Status.Addresses[0].Address
}

port := apiServerConfigProvider.GetApiServerConfig().Port
Expand All @@ -54,6 +61,7 @@ func CreateKubeConfigFromServiceAccount(apiServerConfigProvider clusterConfig.Ap
cnf := kubeconfigutil.CreateWithToken(server, "service-cluster", serviceAccount.Name, caCrt, token)
r, err := runtime.Encode(clientcmdlatest.Codec, cnf)
if err != nil {
klog.Error(err)
return "", err
}
return string(r), nil
Expand Down

0 comments on commit 099893c

Please sign in to comment.